📄 xianxingbiao.c
字号:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
#define FALSE 0
#define TRUE 1
typedef struct LNODE
{
int data;/*数据*/
struct LNODE* next;/*指针*/
}LNODE; /*定义结构体类型*/
void menu();
void choose(int a);
LNODE* InitList();
LNODE* DestroyList(LNODE* L);
LNODE* ClearList(LNODE* L);
int ListEmpty(LNODE* L);
int ListLength(LNODE* L);
int GetElem(LNODE* L,int i,int* e);
int PriorElem(LNODE* L,int cur_e,int* pre_e);
int NextElem(LNODE* L,int cur_e,int* next_e);
LNODE* ListInsert(LNODE* L,int i,int e);
LNODE* ListDelete(LNODE* L,int i,int* e);
int ListPrint(LNODE* L);/*函数声明*/
main()
{
int x=0;
char a=0;
do
{clrscr();menu();/*清屏并显示菜单*/
scanf("%d",&x);getchar();/*输入选项吸收回车符*/
if(x<1||x>12) printf("\nThe number you entry in is wrong!\n");/*若输入的不是选项 提示出错*/
else choose(x);
printf("\nContinue?......(Y/N) ");
a=getchar();getchar();/*输入字符判断是否继续*/
}while(a=='Y'||a=='y');/*若输入为Y 则循环以继续*/
}
void menu()
/*菜单函数*/
{
printf("\n");
printf("\n ===================================================================");
printf("\n | |");
printf("\n | Welcome!Here is the menu.. |");
printf("\n | 1. Create a list; |");
printf("\n | 2. Destroy a list; |");
printf("\n | 3. Clear a list; |");
printf("\n | 4. Insert a node; |");
printf("\n | 5. Delete a node; |");
printf("\n | 6. Diagnose empty; |");
printf("\n | 7. Get listlength; |");
printf("\n | 8. Get data; |");
printf("\n | 9. Get prior data; |");
printf("\n | 10. Get next data; |");
printf("\n | 11. Print all data; |");
printf("\n | 12. exit. |");
printf("\n | |");
printf("\n ===================================================================");
printf("\n\nPlease choose which to do: ");
}
void choose(int a)
{
int x=a,i=0;
LNODE* hp=NULL;/*定义结构体指针 并赋为空*/
switch(x)
{
case 1: hp=InitList(); break;/*初始化线性表 若成功显示成功*/
case 2: hp=DestroyList(hp);break;/*销毁线性表*/
case 3: hp=ClearList(hp);break; /*清空线性表*/
case 4: printf("Please input the node-place and number you want to insert:");
scanf("%d %d",&i,&x);getchar();/*输入要插入的结点位置和数据*/
hp=ListInsert(hp,i,x);/*插入数据*/
break;
case 5: printf("Please input the node-place you want to delete:");
scanf("%d",&i);getchar();/*输入要删除的结点位置*/
hp=ListDelete(hp,i,&x);/*删除结点*/
if(hp->data) printf("The number in the node you delete is %d.\n",x);/*若线性表不为空 则输出所删结点的数据*/
break;
case 6: if(ListEmpty(hp)) printf("The list is empty.\n");/*输出线性表为空*/
else printf("The list is not empty.\n");
break;
case 7: printf("The length of the list is %d.\n",ListLength(hp));break;/*输出表长*/
case 8: printf("Please input the node-place:");
scanf("%d",&i);getchar();/*输入要获取内容的结点位置*/
if(GetElem(hp,i,&x)) printf("The number in the node is %d.\n",x);/*若结点位置存在 则输出数据*/
break;
case 9: printf("Please input the data:");
scanf("%d",&i);getchar();/*输入要查找的数据内容*/
if(PriorElem(hp,i,&x)) printf("The prior data is %d.\n",x);/*若前驱存在 则输出*/
break;
case 10:printf("Please input the data:");
scanf("%d",&i);getchar();/*输入要查找的数据内容*/
if(NextElem(hp,i,&x)) printf("The prior data is %d.\n",x);/*若后继存在 则输出*/
break;
case 11:ListPrint(hp);break;/*输出线性表中所有的数据*/
case 12:hp=DestroyList(hp);exit(1); /*销毁线性表 退出*/
}
}
LNODE* InitList()
/*构造一个空的线性表*/
{
LNODE* L;
L=(LNODE*)malloc(sizeof(LNODE));/*分配存储空间*/
if(L) {L->data=0;L->next=NULL;printf("Initialize success!!\n");}/*若分配成功 空表长度为0 显示成功*/
return L;
}
LNODE* DestroyList(LNODE* L)
/*销毁线性表*/
{
LNODE *p1=L,*p2;
if(!p1) printf("The list is not exist!\n");/*若表不存在 则提示*/
else
{while(p1)
{
p2=p1;
p1=p1->next;
free(p2);
}/*从头结点开始释放存储空间*/
printf("Destroy success!!\n");/*销毁成功 显示*/
}return NULL;
}
LNODE* ClearList(LNODE* L)
/*清空线性表*/
{
LNODE *p1=L->next,*p2;
if(!p1) printf("The list is not exist!\n");/*若表不存在 则提示*/
else
{while(p1)
{p2=p1;
p1=p1->next;
free(p2);
}/*从第一个结点开始释放存储空间*/
L->next=NULL;
L->data=0;/*表长为0*/
printf("Clear list success!!\n");/*清空成功 显示*/
}return L;
}
int ListEmpty(LNODE* L)
/*判断线性表是否为空*/
{
if(!L) {printf("The list is not exist!!\n"); return FALSE;}/*若表不存在 则提示*/
if(!(L->data)) return TRUE;/*若表为空 则返回真*/
return FALSE;/*若表不为空 则返回假*/
}
int ListLength(LNODE* L)
/*获取表长*/
{
if(!L) {printf("The list is not exist!!\n"); return FALSE;}/*若表不存在 则提示*/
return L->data;/*返回表长*/
}
int GetElem(LNODE* L,int i,int* e)
/*用e返回表中第i个结点的数据值*/
{
LNODE* p1=L;
if(!L) {printf("The list is not exist!!\n"); return FALSE;}/*若表不存在 则提示*/
for(;i>0&&p1;i--) p1=p1->next;/*寻找结点*/
if(!p1) {printf("There is not so much data!!\n");return FALSE;}/*p1为空则表中没有第i个结点*/
else {*e=p1->data;return TRUE;}/*返回*/
}
int PriorElem(LNODE* L,int cur_e,int* pre_e)
/*用pre_e返回表中cur_e之前的结点的数据值*/
{
int i=0;
LNODE *p1,*p2;
if(!L) {printf("The list is not exist!!\n"); return FALSE;}/*若表不存在 则提示*/
p1=L->next;
if(!p1) {printf("The list is empty!!\n"); return FALSE;}/*若表为空 则返回错误*/
while(p1&&(p1->data!=cur_e)) {p2=p1;p1=p1->next;i++;}/*寻找数据所在结点*/
if(!p1) {printf("Current-element no found!\n");return FALSE;}/*未找到 返回错误*/
else
{
if(!i) {printf("Current-element is the first!\n");return FALSE;}/*若为第一个结点 显示没有前驱*/
else {*pre_e=p2->data;return TRUE;}/*返回*/
}
}
int NextElem(LNODE* L,int cur_e,int* next_e)
/*用next_e返回表中cur_e之后的结点的数据值*/
{
LNODE *p1;
if(!L) {printf("The list is not exist!!\n"); return FALSE;}/*若表不存在 则提示*/
p1=L->next;
if(!p1) {printf("The list is empty!!\n"); return FALSE;}/*若表为空 则返回错误*/
while(p1&&(p1->data!=cur_e)) p1=p1->next;/*寻找数据所在结点*/
if(!p1) {printf("Current-element no found!\n");return FALSE;}/*未找到 返回错误*/
else
{
if(!(p1->next)) {printf("Current-element is the last!\n");return FALSE;}/*若为最后一个结点 显示没有后继*/
else {*next_e=p1->next->data;return TRUE;}/*返回*/
}
}
LNODE* ListInsert(LNODE* L,int i,int e)
/*在带头结点的线性表L中 第i个结点前插入元素e*/
{
LNODE *p=L,*q,*hp=L;
int j=0;
if(!hp) printf("The list is not exist!!\n");/*若表不存在 则提示*/
else{
while(p&&j<i-1) {p=p->next;j++;}/*寻找第i-1个结点*/
if(!p||j>i-1) printf("Node-place no found!\n");/*i小于1或大于表长加1*/
else
{
q=(LNODE*)malloc(sizeof(LNODE));/*生成新结点*/
q->data=e;q->next=p->next;p->next=q;/*插入线性表中*/
hp->data++;/*表长加1*/
}}
return hp;
}
LNODE* ListDelete(LNODE* L,int i,int* e)
/*在带头结点的线性表L中 删除第i个结点前 并用e返回其值*/
{
LNODE *p=L,*q,*hp=L;
int j=0;
if(!hp) printf("The list is not exist!!\n");/*若表不存在 则提示*/
else{
if(!(hp->next)) printf("The list is empty!!\n");/*若表为空 则返回*/
else {while(p&&j<i-1) {p=p->next;j++;}/*寻找第i-1个结点*/
if(!p||j>i-1) printf("Node-place no found!\n");/*删除位置不正确*/
else
{
q=p->next;*e=q->data;p->next=q->next;
free(q);/*删除结点*/
hp->data--;/*表长减一*/
printf("Delete success!!\n");/*显示删除成功*/
}}}
return hp;
}
int ListPrint(LNODE* L)
/*输出线性表中所有结点的数据*/
{
LNODE* p;
int i=1;
if(!L) {printf("The list is not exist!!\n"); return FALSE;}/*若表不存在 则提示*/
p=L->next;
if(!p) {printf("The list is empty!!\n");return FALSE;}/*若表为空 则返回错误*/
while(p) {printf("The data in node %d is %d.\n",i++,p->data);p=p->next;}/*输出第i个结点的数据*/
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -