- 论坛徽章:
- 0
|
- #include <stdio.h>
- typedef char elemtype;
- typedef struct node
- {
- elemtype data;
- struct node *next,*prior;
- }lnode,*linklist;
- lnode *creat() /*建立双链表*/
- {
- elemtype x;
- lnode *h,*p,*t;
- h=(lnode *)malloc(sizeof(lnode));
- h->next=h;
- t=h;
- while((x=getchar())!='\n')
- {
- p=(lnode *)malloc(sizeof(lnode));
- p->data=x;
- p->next=h;
- p->prior=h;
- t->next=p;
- t=p;
- }
- return(h);
- }
- int insert(lnode *h,int i,elemtype x) /*向第i个元素前插入S结点*/
- { int j=1;
- lnode *p,*s;
- p=h->next;
- while(p!=h&&j<i)
- { ++j;
- p=p->next;
- }
- if(i>0&&i==j)
- {
- s=(lnode *)malloc(sizeof(lnode));
- s->data=x;
- s->prior=p->prior;
- p->prior->next=s;
- s->next=p;
- p->prior=s;
- return(1);
- }
- else
- return(0);
- }
- int /*主函数*/
- main(void)
- { int j;
- elemtype c='a',d='b';
- lnode *h,*p;
- h=creat();
- p=h->next;
- while(p!=h)
- {
- printf("%c",p->data);
- p=p->next;
- }
- printf("\n");
- if(insert(h,3,c))
- {
- while(p!=h)
- {
- printf("%c",p->data);
- p=p->next;
- }
- }
- getch();
- return(0);
- }
复制代码 感觉没什么问题,但是不能输出插入后的新表???大家帮忙看看,谢谢!!! |
|