结点数据结构实验3_99XXX

99xxx  时间:2021-02-22  阅读:()

《数据结构》实验报告

实验序号 3 实验项目名称链式表的操作

附源程序清单

1、

#include"stdio.h"

#inclu de"string.h"

#include"stdlib.h"

#in clu de"ctype.h"typedef struct node //定义结点

{char data[10] ; //结点的数据域为字符串struct node *next; //结点的指针域char*L;

}Lis tNode;type de f ListNode *Link List; // 自定义Link Lis t单链表类型

LinkList CreatListR1(); //函数用尾插入法建立带头结点的单链表LinkList CreatListR(); //函数用头插入法建立带头结点的单链表

LinkList LocateNode(LinkList head,char*key); //函数按值查找结点void DeleteList(LinkList head,char*key); //函数删除指定值的结点LinkList LocateBreforeNode(LinkList head, char *key); //函数查找指定数值的前驱结点void DeleteBreforeNode(LinkList head,char *key); //函数删除指定数值的前驱结点void printlist(LinkList head); //函数打印链表中的所有值void DeleteAll(LinkList head); //函数删除所有结点释放内存//==========主函数==============void main()

{char*ch,*num;num=new char;ch=new char[10] ;

LinkList he ad;

LinkList pm;

head=CreatListR1(); //用尾插入法建立单链表返回头指针printlist(he ad); //遍历链表输出其值printf("Delete node(y/n):"); //输入"y"或"n"去选择是否删除结点scanf("%s",num);if(strcmp(num,"y")==0| | strcmp(num,"Y")==0)

{printf("Please input Delete_data:");scanf("%s",ch); //输入要删除的字符串

DeleteList(head,ch);printlist(he ad);

}printf("输入要查找的数值");s canf("%s",ch);pm=LocateNode(head, ch);printf("%s\n",pm->data);printf("输入要删除的数值的前驱结点");s canf("%s",ch);

DeleteBreforeNode(head,ch);printlist(he ad);printf("对单循环链表进行逆序输出\n");head=CreatListR(); //用头插入法建立单链表返回头指针printlist(he ad);

//DeleteAll(he ad); //删除所有结点释放内存

}

//==========查找的指定数值的前驱结点=======

LinkList LocateBreforeNode(LinkList head, char*key)

{

ListNode *p=head->next; //从开始结点比较

Lis tNode *t ; //p的上一个节点指针while(p&&strcmp(p->data,key)!=0) //直到p为NULL或p->data为key止

{t=p; //记录上一个节点的指针p=p->next; //扫描下一个结点

}return t; //若p=NULL则查找失败否则p指向找到的值为key的结点}

//==========删除的指定数值的前驱结点=======void DeleteBreforeNode(LinkList head,char*key)

{

ListNode *pp,*r,*q=he ad;pp=LocateBre foreNode(he ad,k ey); //按key值查找结点的if(pp==NULL)

{ //若没有找到结点退出printf("position error");

exit(0);

}while(q->ne xt!=pp) //p为要删除的结点 q为p的前结点q=q->next;r=q->next;q->next=r->next;free(r); //释放结点

}

//==========用尾插入法建立带头结点的单链表===========LinkList CreatListR1(void)

{char*ch;ch=new char[10] ;

LinkList he ad=(LinkList)malloc(size of(ListNode)); //生成头结点ListNode *s,*r;r=he ad;r->ne xt=NULL;printf("Input#to end "); //输入"#"代表输入结束printf("Please input Node_data:");scanf("%s",ch); //输入各结点的字符串while(strcmp(ch,"#")!=0)

{s=(ListNode *)malloc(sizeof(ListNode));strcpy(s->data,ch);r->next=s;r=s;r->next=NULL;

//printf("Input#to end ");

//printf("Please input Node_data:");scanf("%s",ch);

}return head; //返回头指针

}

//==========用头插入法建立带头结点的单链表===========LinkList CreatListR(void)

{

LinkList he ad=(LinkList)malloc(size of(ListNode)); //生成头结点ListNode *s,*r;r=he ad;r->ne xt=NULL;printf("Input#to end "); //输入"#"代表输入结束printf("Please input cricle Node_data:");//输入各结点的字符串do//while(strcmp(s->data,"#")!=0)

{s=(LinkList)malloc(size of(ListNode));s canf("%s",s->data);s->ne xt=r->ne xt;r->ne xt=s;

}while(strcmp(s->data,"#")!=0);r->ne xt=r->ne xt->ne xt;return head; //返回头指针

}

//==========按值查找结点找到则返回该结点的位置否则返回NULL==========LinkList LocateNode(LinkList head,char*key)

{

ListNode *p=head->next; //从开始结点比较while(p&&strcmp(p->data,key)!=0) //直到p为NULL或p->data为key止p=p->next; //扫描下一个结点return p; //若p=NULL则查找失败否则p指向找到的值为key的结点}

//==========删除带头结点的单链表中的指定结点=======void DeleteList(LinkList head,char*key)

{

ListNode *p,*r,*q=head;p=LocateNode(head,key); //按key值查找结点的if(p==NULL)

{ //若没有找到结点退出printf("position error");exit(0);

}while(q->ne xt!=p) //p为要删除的结点 q为p的前结点q=q->next;r=q->next;q->next=r->next;free(r); //释放结点

}

//===========打印链表=======void printlist(LinkList head)

{

ListNode *p=head->next; //从开始结点打印while(p)

{printf("%s, ",p->data);p=p->next;

}

printf("\n");

}

//==========删除所有结点释放空间===========void DeleteAll(LinkList head)

{

ListNode *p=head,*r;while(p->ne xt)

{r=p->next;free(p);p=r;

}free(p);

}

1 .

#include"stdio.h"

#inclu de"string.h"

#include"stdlib.h"

#in clu de"ctype.h"typedef struct node //定义结点

{char data[10] ; //结点的数据域为字符串struct node *next; //结点的指针域char*L;

}Lis tNode;type de f ListNode *Link List; // 自定义Link Lis t单链表类型

LinkList CreatListR1(); //函数用尾插入法建立带头结点的单链表LinkList CreatListR(); //函数用头插入法建立带头结点的单链表

LinkList LocateNode(LinkList head,char*key); //函数按值查找结点void DeleteList(LinkList head,char*key); //函数删除指定值的结点LinkList LocateBreforeNode(LinkList head, char *key); //函数查找指定数值的前驱结点void DeleteBreforeNode(LinkList head,char *key); //函数删除指定数值的前驱结点void printlist(LinkList head); //函数打印链表中的所有值void DeleteAll(LinkList head); //函数删除所有结点释放内存//==========主函数==============void main()

{char*ch,*num;num=new char;ch=new char[10] ;

LinkList he ad;

LinkList pm;

搬瓦工:香港PCCW机房即将关闭;可免费升级至香港CN2 GIA;2核2G/1Gbps大带宽高端线路,89美元/年

搬瓦工怎么样?这几天收到搬瓦工发来的邮件,告知香港pccw机房(HKHK_1)即将关闭,这也不算是什么出乎意料的事情,反而他不关闭我倒觉得奇怪。因为目前搬瓦工香港cn2 GIA 机房和香港pccw机房价格、配置都一样,可以互相迁移,但是不管是速度还是延迟还是丢包率,搬瓦工香港PCCW机房都比不上香港cn2 gia 机房,所以不知道香港 PCCW 机房存在还有什么意义?关闭也是理所当然的事情。点击进...

炭云188元/年,上海CN2 VPS/2核/384MB内存/8GB空间/800GB流量/77Mbps端口/共享IP

炭云怎么样?炭云(之前的碳云),国人商家,正规公司(哈尔滨桓林信息技术有限公司),主机之家测评介绍过多次。现在上海CN2共享IP的VPS有一款特价,上海cn2 vps,2核/384MB内存/8GB空间/800GB流量/77Mbps端口/共享IP/Hyper-v,188元/年,特别适合电信网络。有需要的可以关注一下。点击进入:炭云官方网站地址炭云vps套餐:套餐cpu内存硬盘流量/带宽ip价格购买上...

pia云低至20/月,七折美国服务器

Pia云是一家2018的开办的国人商家,原名叫哔哔云,目前整合到了魔方云平台上,商家主要销售VPS服务,采用KVM虚拟架构 ,机房有美国洛杉矶、中国香港和深圳地区,洛杉矶为crea机房,三网回程CN2 GIA,带20G防御,常看我测评的朋友应该知道,一般带防御去程都是骨干线路,香港的线路也是CN2直连大陆,目前商家重新开业,价格非常美丽,性价比较非常高,有需要的朋友可以关注一下。活动方案...

99xxx为你推荐
fontfamilyCSS的font-family中family-name和generic-family区别什么是电子邮件 什么是电子邮件1433端口路由器1433端口怎么开启网店推广网站怎么免费推广淘宝店铺?qq空间打扮QQ空间打扮网站优化方案网站优化方案如何写?网站优化方案网站优化方案应该从哪些方面去分析?网站优化方案网站建设及优化的方案微信电话本怎么用微信电话本短信管理功能怎么用?微信怎么看聊天记录怎样查找一个人的微信聊天记录
韩国vps俄罗斯美女 浙江vps 网站域名备案 香港vps99idc 全球付 私服服务器 dd444 165邮箱 双11秒杀 免费高速空间 免费网页申请 卡巴斯基是免费的吗 卡巴斯基免费试用版 空间租赁 789 cxz 服务器论坛 群英网络 阿里dns 时间服务器 更多