注册 登录
编程论坛 数据结构与算法

求解 链表题 使用C语言

软工学生 发布于 2013-05-11 19:28, 723 次点击
Description

编写程序。 设n个元素的线性表顺序存储在一维数组r[0..maxlen-1]的前n个位置上,试将新元素e插入表中第i-1个和第i个元素之间,写出算法。顺序表的结构为:
Input
输入包含多行, 每行含一个字符e 和字符插入的位置 n, 输入以EOF结束。
Output
打印一系列插入操作后线性表的遍历结果,并换行。
Sample Input
1 a
1 b
2 c
3 d
Sample Output
bcda
3 回复
#2
笑傲2013-05-11 21:31
为什么看题目感觉是顺序表呢,
这个题目应该不难,lz应该尝试写一下,或者给出你的想法也行啊

[ 本帖最后由 笑傲 于 2013-5-11 21:33 编辑 ]
#3
邓士林2013-05-11 23:18
线性表的基本操作哦!你自己尝试写下
#4
软工学生2013-05-12 13:51
我描述错了,题目是正确的。
程序代码:
# include <stdio.h>
# include<malloc.h>
# include <string.h>
typedef struct st
{
    int pos;
    char ch;
    struct st* next;
}st1,*pst;
pst creat()
{
    pst phead = (pst)malloc(sizeof(st1));
    phead->next=NULL;
    return phead;
}
void inser(pst p)
{
    int i=1;
    int pos;
    char ch;
    while(scanf("%d %c",&pos,&ch)!=EOF)
    {
        while(i<pos)
        {
            i++;
            p=p->next;
        }
       pst pnew = (pst)malloc(sizeof(st1));
       pnew->next = p->next;
       p->next=pnew;
    }
}
void traver(pst p)
{
    while(p->next!=NULL)
    {
        printf("%c",&p->next->ch);
        p=p->next;
    }
}
void main()
{
    pst p = creat();
    inser(p);
    traver(p);
    system("pause");
}
这是我做的,不知道怎样结束输入。
1