c语言双向链表重组写法,一个C语言做的双向链表程序,请高手帮忙改错

#include

#include

#include

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(" 制作:王祥\n");

printf(" 班级:计科1101班\n");

printf(" 学号: 1109050128\n");

printf(" 指导老师: 孙夫雄\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;

intindex = 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 &&current->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;ilength;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 k,r;

EType data[100];

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

scanf("%d",&data[i].num);

printf("\n请输入要插入的姓名:");

for(i=0;i

scanf("%s",&data[i].name);

for(i=0;i

{

if(Insert(L,k,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;

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;

if(GetElem(L,k,data[0]))

printf("\n查找成功!");

else

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 <

system("pause");

system("cls");

break;

}//end switch

}

void Menu() //菜单函数

{

//cout <

cout <

cout <

cout <

cout <

cout <

cout <

cout <

cout <

cout <

}

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 <

system("pause");

system("cls");

continue;

}

else

{

if(a[0]=='0')

break;

main_switch(a[0]);

}

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值