求解 链表题 使用C语言
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
程序代码:# 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");
}
这是我做的,不知道怎样结束输入。