《数据结构》实验报告
实验序号 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;
Bluehost怎么样,Bluehost好不好,Bluehost成立十八周年全场虚拟主机优惠促销活动开始,购买12个月赠送主流域名和SSL证书,Bluehost是老牌虚拟主机商家了,有需要虚拟主机的朋友赶紧入手吧,活动时间:美国MST时间7月6日中午12:00到8月13日晚上11:59。Bluehost成立于2003年,主营WordPress托管、虚拟主机、VPS主机、专用服务器业务。Blueho...
享有云怎么样?享有云是一家新的国内云服务器商家,目前提供国内、香港及海外地区的云服务器,拥有多线路如:BGP线路、CN2线路、高防等云服务器,并且提供稳定、安全、弹性、高性能的云端计算服务,实时满足您的多样性业务需求。目前,美国bgp云服务器,5M带宽,低至20元/月起,270元/年起,首月打折;香港2核2G2M仅50元/月起,450元/年起!点击进入:享有云官方网站地址享有云优惠活动:一、美国B...
桔子数据(徐州铭联信息科技有限公司)成立于2020年,是国内领先的互联网业务平台服务提供商。公司专注为用户提供低价高性能云计算产品,致力于云计算应用的易用性开发,并引导云计算在国内普及。目前公司研发以及运营云服务基础设施服务平台(IaaS),面向全球客户提供基于云计算的IT解决方案与客户服务,拥有丰富的国内BGP、双线高防、香港等优质的IDC资源。 公司一直秉承”以人为本、客户为尊、永...