|
楼主 |
发表于 2018-4-3 21:12:41
|
显示全部楼层
我之前用单链表可以删除 现在我用双向链表又不可以删除了...
而且我只有最后一个元素不能删除,其他都可以正常删除
这是为什么??? 是因为是双向链表吗?所以最后一个元素当执行q->next->prior=p语句会出问题吗?
我执行的结果是这样的:见图
- # include <stdio.h>
- # include <stdlib.h>
- typedef struct Dulnode{
- int data;
- struct Dulnode *prior;
- struct Dulnode *next;
- }Dulnode,*Dulinklist;
- void initlist(Dulinklist *L) //初始化双向链表
- {
- (*L)=(Dulinklist)malloc(sizeof(Dulnode));
- if(!(*L))
- printf("无法正常分配空间,对不起哦!\n");
- else
- {
- (*L)->prior=(*L)->next=NULL;
- printf("初始化成功!");
- }
- }
- void creatlist(Dulinklist *L) //创建双向链表
- {
- int n,i;
- Dulinklist r,p;//指向尾节点
- r=*L;
- printf("请问您想输入几个元素:");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- p=(Dulinklist)malloc(sizeof(Dulnode));
- if(!p)
- printf("无法正常分配空间,对不起哦~\n");
- else
- {
- printf("请输入第%d个元素",i+1);
- scanf("%d",&p->data);
- p->next=NULL;
- p->prior=r;
- r->next=p;
- r=p;
- }
- }
- }
- void showlist(Dulinklist *L) //输出双向链表
- {
- Dulinklist p;
- p=(*L)->next;
- if((*L)->next==NULL)
- printf("双向链表中没有元素,不可以输出哦~\n");
- else if((*L)->next!=NULL)
- { printf("该双向链表中的元素为:");
- while(p!=NULL)
- {
- printf("%d ",p->data);
- p=p->next;
- }
- }
- else
- printf("您的线性表不存在哦,不可以显示元素呢~");
- printf("\n");
- }
- int lengthlist(Dulinklist *L) //求表长
- {
- Dulinklist p;
- int length=0;
- p=(*L)->next;
- while(p!=NULL)
- {
- length++;
- p=p->next;
- }
- printf("您的表长为%d\n",length);
- return length;
- }
- void isemptylist(Dulinklist *L) //判断该双向链表是否为空
- {
- if((*L)->next==NULL)
- printf("您的链表为空。\n");
- else
- printf("您的链表不为空\n");
- }
- void destroylist(Dulinklist *L)//销毁链表
- {
- Dulinklist p,q;
- p=*L;
- while(p)
- {
- q=p->next;
- free(p);
- p=q;
- }
- printf("已经销毁线性表了哦~\n");
- }
- void clearlist(Dulinklist *L) //清空线性表
- {
- Dulinklist p,q;
- p=(*L)->next;
- while(p!=NULL)
- {
- q=p->next;
- free(p);
- p=q;
- }
- (*L)->next=NULL;
- printf("已经清空双向链表了哦~\n");
- }
- /*void getelem(Dulinklist *L) //根据查询位置给出元素内容
- {
- int i,j,e;
- Dulinklist p;
- printf("请问您想要查询哪一个位置的元素内容:");
- scanf("%d",&i);
- p=(*L)->next;
- j=0;
- while(p)
- {
- ++j;
- if(i==j)
- {
- e=p->data;
- printf("您查询的位置的元素内容为:%d\n",e);
- break;
- }
- p=p->next;
- }
- if(i!=j||!p)
- printf("您的输入位置有误,无法查询。\n");
- }*/
- void getelem(Dulinklist *L)
- {
- int i,j,e;
- Dulinklist p;
- printf("请问您想要查询哪一个位置的元素内容:");
- scanf("%d",&i);
- p=(*L),j=0;
- while(p&&j<i)
- {
- p=p->next;
- j++;
- }
- if(!p||j>i)
- printf("查询位置有误,无法查询\n");
- else
- {
- e=p->data;
- printf("您查询的位置的元素是:%d\n",e);
- }
- }
- void locateelem(Dulinklist *L) //根据元素内容查询元素位置
- {
- int e,j=0;
- Dulinklist p;
- p=(*L)->next;
- printf("请输入您想要查询的元素内容:");
- scanf("%d",&e);
- while(p)
- {
- j++;
- if(e==p->data)
- {
- printf("您所要查询的元素内容位于%d号!\n",j);
- break;
- }
- p=p->next;
- }
- if(!p)
- printf("无法查询到元素位置。\n");
- }
- void insertlist(Dulinklist *L) //在指定元素位置前插入某元素
- {
- int i,e,j=0;
- Dulinklist p,s;
- p=(*L);
- printf("请问您想要在哪一号元素前插入什么内容:");
- scanf("%d,%d",&i,&e);
- while(p&&j<i-1)
- {
- p=p->next;
- ++j;
- }
- if(!p||j>i-1)
- printf("插入位置有误。\n");
- else
- {
- s=(Dulinklist)malloc(sizeof(Dulnode));
- if(!s)
- printf("分配空间失败,无法进行插入~\n");
- else
- {
- s->data=e;
- s->prior=p;
- s->next=p->next;
- p->next->prior=s;
- p->next=s;
- }
- }
- }
- void deletelist(Dulinklist *L)//删除某指定元素并用返回其值
- {
- int i,j,e,len;
- Dulinklist p,q;
- p=(*L);
- j=0;
- printf("请问您想删除哪一号的元素内容:");
- scanf("%d",&i);
- while(p&&j<i-1)
- {
- p=p->next;
- ++j;
- }
- if(!p||j>i-1)
- printf("删除位置有误。\n");
- else
- {
- q=p->next;
- e=q->data;
-
- p->next=q->next;
- q->next->prior=p;
- free(q);
- }
- }
- int main()
- {
- Dulinklist L;
- initlist(&L);
- creatlist(&L);
- showlist(&L);
- //isemptylist(&L);
- //clearlist(&L);
- //isemptylist(&L);
- //destroylist(&L);
- //showlist(&L);
- //getelem(&L);
- //locateelem(&L);
- lengthlist(&L);
- //insertlist(&L);
- //showlist(&L);
- deletelist(&L);
- showlist(&L);
- lengthlist(&L);
- return 0;
- }
复制代码 |
-
|