用C语言做的一个双向链表的程序,运行的时候有问题,请高手指教。
#include <stdlib.h>#include <stdio.h>
#include <iostream.h>
#include<string.h>
typedef bool Status;
typedef int HeadEType;
#define OK 1
#define ERROR 0
typedef struct
{
int num; //存放学号
char name[10]; //存放姓名
}EType;
typedef struct DoubleNode
{
EType data;
DoubleNode *plink;
DoubleNode *nlink;
}DoubleChainNode;
//链表结点
typedef struct DoubleChainList
{
HeadEType length;
DoubleChainNode *first;
}DoubleChainList;
//链表的头
DoubleChainList *L;
char re_choose[]={"\n选择非法,请输入正确的编号...\n"};
void Menu_name()
//作者信息
{
printf("\n\n\n\n\n\n\n");
printf(" *************************************************\n");
printf(" 线性表的链式存储\n\n");
printf(" 制作:xx\n");
printf(" 班级:计科1101班\n");
printf(" 学号: xxxxxxxxxxxx\n");
printf(" 指导老师: xxxxxx\n");
printf(" **************************************************\n");
printf("\n\n\n\t\t");
}
DoubleChainList *Creat(DoubleChainList *L)
{// 构造一个空链表
L=new DoubleChainList;//产生一个仅有表头结点的链表
L->first=NULL;//first的值设为空(NULL)值
L->length=0;
return L;
}
int Insert(DoubleChainList *L, int k, EType x)
{
if (k < 0)
return ERROR;
int index = 1;
DoubleChainNode *current= L->first;
while (index < k && current)
{//找第k个结点
index++;
current = current ->nlink;
}
if (k > 0 && ! current)
return ERROR;
DoubleChainNode *q = new DoubleChainNode;
q->data = x;
if (k)
{// 插入在current 之后, 两个方向的链域的修改
q->nlink = current ->nlink;
q->plink = current;
DoubleChainNode *p = current ->nlink ;
current ->nlink = q;
if(p)
p->plink = q;
}
else
{// 作为第一个元素结点插入
q->nlink = L->first;
q->plink = NULL;
DoubleChainNode *p = L->first;
if(p)
p->plink = q;
L->first = q;
}
L->length++;
return 1;
}
Status Delete(DoubleChainList *L, int k )
{// 在双向链表L中删除第k个数据元素,如果不存在第k个元素返回出错状态码
if (k < 0 || ! L->first) return ERROR;
DoubleChainNode * current = L->first;
DoubleChainNode * p;
if (k == 0) // 删除的是链表中第一个结点
{
p = current ->nlink;
p ->plink = NULL;
L->first = p;
}
else
{
DoubleChainNode *q = L->first;
for (int index = 1; index < k && q ; index++)
q = q->nlink;
if(!q)
return ERROR;
current = q; // current 指向第k个结点
q = current ->plink;
p = current ->nlink;
q->nlink = p;
p->plink = q;
}
delete current; // 释放被删除结点current的空间
L->length--;
return OK;
}
bool FindBeforeAfter(DoubleChainList *L, EType &x,DoubleChainNode *Result)
{// 查找x,如果找到返回x的直接前驱和直接后驱的数据元素的地址,如果未找到返回NULL
DoubleNode *current = L->first;
while (current &¤t->data.name!=x.name)
current=current->nlink;
if (current)
{
Result->nlink=current->nlink;
Result->plink=current->plink;
return 1;
}
return 0;
}
bool GetElem(DoubleChainList *L,int k,EType &x)
{ //L中第k个元素取至x中,如不存在返回false,找到返回true,x带回
if (k<0) return false;
DoubleChainNode *current = L->first;
int index = 1;
while (index < k && current)
{
current = current->nlink;
index++;
}
if (current){
x = current->data;
return true;
}
return false; // k值太大,不存在第k个结点
}
void Destroy(DoubleChainList *L)
{// 删除链表L中所有数据结点,并释放结点空间
DoubleChainNode * current=L->first;
while (L->first)
{
current = current ->nlink;
delete L->first;
L->first = current;
L->length--;
}
}
void output(DoubleChainList *L)
{
if(L->length==0)
printf("当前的线性表为一个空表\n");
printf("显示当前线性表:\n");
DoubleChainNode *current=L->first;
printf("\n");
for(int i=0;i<L->length;i++)
{
printf("学号为:%d\n",current->data.num);
printf("学号为:%s\n",current->data.name);
current=current->nlink;
}
printf("\n\n");
}
void main_switch(char j)
//操作选择函数
{
int r;
EType data[100];
int temp =0;
int m=0;
switch(r)
{
case '1' ://显示线性表中的数据元素
system("cls");
output(L);
system("pause");
system("cls");
break;
case '2' ://插入数据元素
int i,k;
system("cls");
printf("\n当前数据元素的下标范围:0~%d ---\n",L->length);
printf("\n请输入要插入点的个数:");
scanf("%d",&k);
printf("\n请输入要插入的学号:");
for(i=0;i<k;i++);
scanf("%d",&data[i].num);
printf("\n请输入要插入的姓名:");
for(i=0;i<k;i++);
scanf("%s",&data[i].name);
for(i=0;i<k;i++);
{
if(Insert(L,i,data[i]))
{
printf("插入成功!\n\n");
output(L);
}
else
printf("插入失败!\n\n");
}
system("pause");
system("cls");
break;
case '3'://删除数据元素
system("cls");
printf("\n当前数据元素的下标范围:0~%d ---\n",L->length-1);
printf("\n请输入要删除点的下标:");
cin>>k;
if(Delete(L,k))
{
printf("删除成功!\n\n");
output(L);
}
else
printf("删除失败!\n\n");
system("pause");
system("cls");
break;
case '4'://查找数据元素
system("cls");
int f;
char n[10];
EType x;
printf("\n请输入要查找的数据内容:");
printf("\n请输入学号:");
scanf("%d",&f);
printf("\n请输入姓名:");
scanf("%s",&n);
x.num=f;
strcpy(x.name,n); //()x.name=n;
DoubleChainNode *Result;
if(FindBeforeAfter(L,x,Result))
printf("\n数据元素查找成功!");
else
printf("\n查找失败!\n\n");
system("pause");
system("cls");
break;
case '5'://查找第k个数据元素
system("cls");
printf("\n当前数据元素的下标范围:0~%d ---\n",L->length);
printf("\n请输入要查找数据元素的下标:");
cin>>k;
for(temp=0;temp<100;temp++)
{
if(GetElem(L,k,data[temp]))
{
printf("\n查找成功!");
break;
}
}
printf("\n查找失败!\n\n");
system("pause");
system("cls");
break;
case '6'://清空链表
system("cls");
Destroy(L);
system("pause");
system("cls");
break;
case '0':
exit(0);
break;
default :
cout <<re_choose<<endl;
system("pause");
system("cls");
break;
}//end switch
}
void Menu() //菜单函数
{
// cout <<"\n\n\t\t"<<"=============线性表的链式存储=============="<<endl;
cout <<"\n\t\t"<<"请选择以下一个功能:"<<endl;
cout <<"\n\t\t"<<"1.显示线性表中的数据元素."<<endl;
cout <<"\t\t2.插入数据元素." << endl;
cout <<"\t\t3.删除数据元素." << endl;
cout <<"\t\t4.查找数据元素."<<endl;
cout <<"\t\t5.查找第k个数据元素."<<endl;
cout <<"\t\t6.清空链表."<<endl;
cout <<"\t\t0.退出.\n"<<endl;
cout <<"\t\t===============================\n"<<endl;
}
int main()
{
system("cls");
Menu_name(); //输出作者信息
system("pause");
system("cls");
L=Creat(L); //创建一个空链表
L->length=1;
while(1)
{
char *a;
system("cls");
Menu();
printf("\n\t请输入功能编号:");
gets(a);
if(a[1]!='\0')
{
cout <<"\n"<<re_choose<<endl;
system("pause");
system("cls"); //系统清屏
continue;
}
else
{
if(a[0]=='0')
break;
main_switch(a[0]);
}
}
return 0;
}