问题出在AddDode和DelNode函数里 代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef COMMON_H
#define COMMON_H
#define ERROR 0
#define OK 1
#define FALSE -1
#define TRUE 1
#endif // COMMON_H
#ifndef Datastructures_H
#define Datastructures_H
typedef struct DNode
{
int data;
struct DNode* next;
struct DNode* prev;
}DNode, * DoulbeList;
#endif //DataStructures_H
#ifndef Functions_H
#define Functions_H
int menu_select(); //菜单驱动程序
int CreatDNode(DNode *DoubleList);
int FindNode(DNode *DoubleList, int n);
int AddNode(DNode *DoubleList, int n);
int DelNode(DNode DoubleList, int n);
int PrintNode(DNode DoubleList, int n);
void freelink(DNode *DoubleList);
DoulbeList ListLocate(DNode *ptr, int i);
#endif //Functions_H
int menu_select()
{
int sn;
printf(" 双向链表基本操作\n"); //显示菜单
printf(" 1、建立双向链表(-999结束)\n");
printf(" 2、查找第i个位置数据\n");
printf(" 3、在第i个节点插入值x\n");
printf(" 4、删除第i个节点\n");
printf(" 5、显示双向链表数据\n");
printf(" 0、退出通讯录管理系统\n");
printf(" 请选择0--5: ");
for (;;) //菜单功能选择
{
scanf("%d", &sn);
getchar();
if (sn < 0 || sn>5)
printf("\n\t 输入选择错误,请重新选择 0--5: ");
else
break;
}
return sn;
}
int CreatDNode(DNode *DoubleList)
{
int n=0; //链表节点数
int x=0;
DNode *a,*b; //a为新建节点,b为上一个节点
DoubleList->next = NULL;
DoubleList->prev = NULL;
//DoubleList->data = NULL;
if (DoubleList==NULL)
{
printf("error");
return FALSE;
}
a=(DNode *)malloc(sizeof(DNode));
DoubleList->next=a;
printf("请输入第1个节点数值");
scanf("%d",&x);
a->data=x;
a->prev=NULL;
a->next=NULL;
b=a;
n++;
while(1)
{
printf("请输入第%d个节点数值", n+1);
scanf("%d",&x);
if(x ==-999)
{
break;
}
a=(DNode *)malloc(sizeof(DNode));
if (a)
{
n++;
a->data = x;
b->next = a;
a->prev = b;
a->next = NULL;
b = a;
}
else
return FALSE;
}
return n; //返回创建的节点数
}
int FindNode(DNode *DoubleList,int n)
{
int pos=0;
int start=1;
DNode *p1=DoubleList->next;
while(1)
{
printf("请输入需要查找的节点序号");
scanf("%d",&pos);
if (pos>n||pos<1)
{
printf("输入的序号错误,请重新输入");
}
else
{
while (p1!=NULL&&start<pos)
{
p1=p1->next;
start++;
}
break;
}
}
if (start==pos)
{
printf("第%d个节点数据为%d\n",pos,p1->data);
return TRUE;
}
else
{
return FALSE;
}
}
//按序号查找
DoulbeList ListLocate(DNode ptr,int i)
{
int j=1;
while(ptr!=NULL&&j<i)
{
ptr=ptr->next;
j++;
}
if(j==i)
return ptr;
else
return NULL;
}
int AddNode(DNode DoubleList,int n)
{
int i=0,x=0;
while(1)
{
printf("请输入插入的节点序号");
scanf("%d",&i);
if(i>n||i<1)
printf("输入的序号错误,请重新输入");
else
{
printf("请输入数据:");
scanf("%d",&x);
DNode *p1=ListLocate(DoubleList,i+1);
if(p1!=NULL)
{
DNode *p2=(DNode *)malloc(sizeof(DNode));
if(p2)
{
p2->data=x;
p2->prev=p1->prev;
if(p1->prev!=NULL)
p1->prev->next=p2;
else
{
DoubleList=p2;
p1->prev=p2;
}
p2->next=p1;
p1->prev=p2;
}
}break;
}
}
return TRUE;
}
int DelNode(DNode DoubleList,int n)
{
int i;
while(1)
{
printf("请输入删除的节点序号");
scanf("%d",&i);
if (i>n||i<1)
printf("输入的序号错误,请重新输入");
else
{
DNode *p1=ListLocate(DoubleList,i+1);
if(p1->prev==NULL)//删除头结点
{
DoubleList=DoubleList->next;
DoubleList->prev=NULL;
}
else
{
if(p1->next==NULL)//删除尾结点
{
p1->prev->next=NULL;
}
else
{
p1->prev->next=p1->next;
p1->next->prev=p1->prev;
free(p1);
}
}
return TRUE;
}
}
}
int PrintNode(DNode DoubleList,int n)
{
DNode p1=DoubleList->next;
int i=1;
while(p1!=NULL)
{
printf("第%d个节点数据为%d\n",i,p1->data);
p1=p1->next;
i++;
}
return TRUE;
}
/
输入参数 : DoubleList 表示头节点
输出参数 : 无
释放链表
/
void freelink(DoulbeList DoubleList)
{
DNode p;
while (DoubleList)
{
p = DoubleList;
DoubleList = DoubleList->next;
free(p);
}
}
int main()
{
int n = 0;
DoulbeList DoubleList;
DoubleList = (DoulbeList)malloc(sizeof(DNode));
for (;;) // 无限循环,选择0 退出
{
switch (menu_select()) // 调用菜单函数,按返回值选择功能函数
{
case 1:
printf(" 建立双向链表(-999结束)\n");
n=CreatDNode(DoubleList);
printf("长度为: %d\n",n);
if (n == -1)
printf("error1");
break;
case 2:
printf(" 查找第i个位置数据\n");
FindNode(DoubleList, n);
break;
case 3:
printf(" 在第i个节点插入值x\n");
AddNode(DoubleList,n);
n++;
break;
case 4:
printf(" 删除第i个节点\n");
DelNode(DoubleList, n);
n--;
break;
case 5:
printf("显示链表\n");
PrintNode(DoubleList, n);
printf("长度为: %d\n",n);
break;
case 0:
printf(" 再见!\n");
freelink(DoubleList); //退出系统
return 0;
} // switch语句结束
} // for循环结束
return 0;
} // main()函数结束