《数据结构》实验报告
实验序号 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;
PacificRack在本月发布了几款特价产品,其中最低款支持月付仅1.5美元,基于KVM架构,洛杉矶机房,PR-M系列。PacificRack简称PR,QN机房旗下站点,主要提供低价VPS主机产品,基于KVM架构,数据中心为自营洛杉矶机房,现在只有PR-M一个系列,分为了2个类别:常规(Elastic Compute Service)和多IP产品(Multi IP Server)。下面列出几款秒...
之前几个月由于CHIA挖矿导致全球固态硬盘的价格疯涨,如今硬盘挖矿基本上已死,硬盘的价格基本上恢复到常规价位,所以,pacificrack决定对全系Cloud server进行价格调整,降幅较大,“如果您是老用户,请通过续费管理或升级套餐,获取同步到最新的定价”。官方网站:https://pacificrack.com支持PayPal、支付宝等方式付款VPS特征:基于KVM虚拟,纯SSD raid...
Hostodo是一家成立于2014年的国外VPS主机商,现在主要提供基于KVM架构的VPS主机,美国三个地区机房:拉斯维加斯、迈阿密和斯波坎,采用NVMe或者SSD磁盘,支持支付宝、PayPal、加密货币等付款方式。商家最近对于上架不久的斯波坎机房SSD硬盘VPS主机提供66折优惠码,适用于1GB或者以上内存套餐年付,最低每年12美元起。下面列出几款套餐配置信息。CPU:1core内存:256MB...