问问双链表的问题
这是严蔚敏书上抄下来,改动了下. 程序代码:
#include<stdio.h> #include<stdlib.h> typedef struct DuLNode { char data; struct DuLNode *prior; struct DuLNode *next; }DuLNode,*DuLinkList; DuLinkList GetElem(DuLinkList *L,int i) { DuLinkList p; int j; p = (*L)->next; j = 0; while(p && j<i-1) { p = p->next; ++j; } if(!p || j>i-1) return 0; else return p; } void ListInsert(DuLinkList *L,int i,char e)//插入 { //在带头结点的双链循环线性表L中第i位置之前插入元素e //i的合法值为1<=i<=表长+1 DuLinkList p,s; if((p=GetElem(L,i))<=0) // exit(1); printf("%d\n",p); if(!(s=(DuLinkList)malloc(sizeof(DuLNode)))) exit(1); s->data = e; s->prior = p->prior; p->prior->next = s; s->next = p; p->prior = s; } void ListDelete(DuLinkList *L,int i) //删除 { //删除带头结点的双链循环线性表L的第i个元素, DuLinkList p; if(!(p=GetElem(L,i))) //确定删除位置 exit(1); p->prior->next = p->next; p->next->prior = p->prior; free(p); } void CreateList(DuLinkList *L,int n) { int i; DuLinkList p; (*L) = (DuLinkList)malloc(sizeof(DuLNode)); (*L)->next = NULL; for(i=n;i>0;--i) { p = (DuLinkList)malloc(sizeof(DuLNode)); scanf("%c",&p->data); fflush(stdin); p->next = (*L)->next; (*L)->prior = p; (*L)->next = p; } } void display(DuLinkList *L) { DuLinkList di; di = (*L)->next; while(di!=NULL) { printf("%c\t",di->data); di = di->next; } printf("\n"); } void main() { DuLinkList s1; CreateList(&s1,4); display(&s1); ListInsert(&s1,2,'e'); -->加上这句就会出错了,为什么呢? }
请教下应该怎么改呢?