教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 专业资料 >

实验一 链表操作

来源:网络收集 时间:2026-08-25
导读: 执行对链表的创建,插入,删除,显示等操作 #include stdlib.h #include stdio.h struct LNode //链表节点定义 { char c; struct LNode *next; }; struct LNode *head; struct LNode *tail; struct LNode *createlist() { printf(输入数据,中间以回车间隔,以#

执行对链表的创建,插入,删除,显示等操作

#include <stdlib.h>
#include <stdio.h>
struct LNode //链表节点定义
{
char c;
struct LNode *next;
};
struct LNode *head;
struct LNode *tail;
struct LNode *createlist()
{
printf("输入数据,中间以回车间隔,以#号结尾 \n");
struct LNode *node;
char c;
node = (struct LNode *)malloc(sizeof(struct LNode));
node->c ='#';
node->next = NULL;
tail=node;
head=tail;
scanf("%c",&c);
fflush(stdin);
while(c!='#')
{
struct LNode *node1;
node1=(struct LNode *)malloc(sizeof(struct LNode));
node1->c = c;
node1->next = NULL;
tail->next = node1;
tail = node1;
scanf("%c",&c);
fflush(stdin);
}
return node;
}
void showlist(struct LNode *head) //显示链表
{
printf("显示链表中所有元素 \n");
if(!tail)
{
printf("链表为空! \n");
exit(0);
}
struct LNode *p;
p=head->next;
while(p->c!='#')
{
printf("%c\n",p->c);
p=p->next;
if(!p)
break;
}
}
void insertnode()
{
printf("输入一个节点!\n");
struct LNode *p;
struct LNode *node;
node=(struct LNode *)malloc(sizeof(struct LNode));
p=tail;
char c;
scanf("%c",&c);
fflush(stdin);
p->next=node;
node->c=c;
node->next=NULL;
tail=node;
}
void delnode()
{
printf("删除一个节点!\n");
printf("请输入删除节点所处在的位置,如1,2,3...!\n");
int count;
int i=1;
scanf("%d",&count);
fflush(stdin);
struct LNode *p,*q;
q=head;
p=q->next;
while(i<=count)
{
if(i==count)
{
q->next=p->next;
p->next=NULL;
free(p);
break;
}
else
{
q=p;
p=p->next;
i++;
}
}
}
int Length_of_list()
{
int i=0;
struct LNode *position=head;
while (position != NULL)
{
position = position->next;
i++;
}
return i;
}
void menu()
{
printf ("****************************************************\n");
printf ("****************1.创建链表**************************\n");
printf ("****************2.插入结点**************************\n");
printf ("****************3.删除结点**************************\n");
printf ("****************4.显示所有节点**********************\n");
printf ("****************5.显示链表的长度**********************\n");
printf ("****************6.退出******************************\n");
printf ("*************************
***************************\n");
}
int main()
{
int x;
char rturn;
while(1)
{ system("cls");
menu();
printf("请选择功

执行对链表的创建,插入,删除,显示等操作

能:");
scanf("%d",&x);
fflush(stdin);
switch(x)
{
case 1: //创建链表
head = createlist();
printf("链表创建完成,是否继续(N退出,任意键继续)\n");
scanf("%c",&rturn);
if(rturn=='N')
exit(0);
break;
case 2://插入节点
insertnode();
printf("结点插入完成,是否继续(N退出,任意键继续)\n");
scanf("%c",&rturn);
if(rturn=='N')
exit(0);
break;
case 3:
delnode();
printf("结点删除完成,是否继续(N退出,任意键继续)\n");
scanf("%c",&rturn);
if(rturn=='N')
exit(0);
break;
case 4://显示所有节点
showlist(head);
printf("显示结点完成,是否继续(N退出,任意键继续)\n");
scanf("%c",&rturn);
if(rturn=='N')
exit(0);
break;
case 5:
printf("当前
链表的长度为:%d\n",Length_of_list());
scanf("%c",&rturn);
if(rturn=='N')
exit(0);
break;
case 6: //退出
return 0;
}
}
}

…… 此处隐藏:672字,全部文档内容请下载后查看。喜欢就下载吧 ……
实验一 链表操作.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/1761491.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)