数据结构实验报告-答案.doc
数据结构实验报告(C语言)(强力推荐)

数据结构实验实验内容和目的:掌握几种基本的数据结构:集合、线性结构、树形结构等在求解实际问题中的应用,以及培养书写规范文档的技巧。
学习基本的查找和排序技术。
让我们在实际上机中具有编制相当规模的程序的能力。
养成一种良好的程序设计风格。
实验教材:数据结构题集(C语言版)清华大学出版社2007年实验项目:实验一、栈和循环队列㈠、实验内容:①栈掌握栈的特点(先进后出FILO)及基本操作,如入栈、出栈等,栈的顺序存储结构和链式存储结构,以便在实际问题背景下灵活应用。
本程序采用的是链栈结构,具有初始化一个栈、PUSH、POP、显示所有栈里的元素四个功能。
②循环队列掌握队列的特点(先进先出FIFO)及基本操作,如入队、出队等,学会循环队列的实现,以便在实际问题背景下灵活运用。
本程序具有初始化一个队列、入队、出队、显示队列的所有元素、队列长度五个功能。
㈡、实验代码①栈程序代码:#include <stdio.h>#include <malloc.h>#define Stack_Size 6#define ERROR 0#define OK 1typedef int SElemType;typedef struct SNode{SElemType data;struct SNode *next;}SNode,*LinkStack;int CreatTwo(LinkStack &head,int n){int i;SNode *p;head=(LinkStack)malloc(sizeof(SNode));head->next=NULL;printf("请输入数据(数字):\n");for(i=n;i>0;--i){p=(SNode *)malloc(sizeof(SNode));scanf("%d",&p->data);p->next=head->next;head->next=p;}return 1;}int menu_select(){int sn;for(;;){scanf("%d",&sn);if(sn<1||sn>6)printf("\n\t输入错误,请重新输入\n");elsebreak;}return sn;}int Push(LinkStack &top,SElemType e){SNode *q;q=(LinkStack)malloc(sizeof(SNode));if(!q){printf("溢出!\n");return(ERROR);}q->data=e;q->next=top->next;top->next=q;return(OK);}int Pop(LinkStack &top,SElemType &e){SNode *q;if(!top->next){printf("error!\n");return(ERROR);}e=top->next->data;q=top->next;top->next=q->next;free(q);return(OK);}void main(){ int e;LinkStack top;printf("1.初始化一个栈;\n2.PUSH;\n3.POP;\n4.显示所有栈里的元素;\n5.结束;\n");while(1){switch(menu_select()){case 1:if(CreatTwo(top,Stack_Size))printf("Success!\n");break; case 2:printf("Push:\n");scanf("%d",&e);if(Push(top,e))printf("Success!\n");break;case 3:if(Pop(top,e))printf("Success!\n");printf("%d\n",e);break;case 4:LinkStack p;printf("所有栈里的元素:\n");p=top;while(p->next){p=p->next;printf("%7d",p->data);}printf("\n");break;case 5:return;}}}运行结果:②循环队列程序代码:#include<stdlib.h>#include<stdio.h>#define OVERFLOW -1#define OK 1#define ERROR 0#define MAXSIZE 100typedef struct{int *elem;//队列存储空间int front;int rear;}SqQueue;//判断选择是否正确int menu_select(){int sn;for(;;){scanf("%d",&sn);if(sn<1||sn>6)printf("\n\t输入错误,请重新输入\n");elsebreak;}return sn;}//参数(传出)SqQueue &Q,循环队列(空)int InitQueue(SqQueue &Q){Q.elem=(int *)malloc(MAXSIZE*sizeof(int));if(!Q.elem)exit(OVERFLOW);Q.front=Q.rear=-1;for(int i=0;i<MAXSIZE;i++)Q.elem[i]=-1;return OK;}//返回Q的元素个数int QueueLength(SqQueue Q){return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;}//显示队列的元素void Display(SqQueue Q){for(int i=0;i<=QueueLength(Q);i++)if(Q.elem[i]!=-1)printf("%d ",Q.elem[i]);printf("\n");}//入队int EnQueue(SqQueue &Q,int e){Q.rear=(Q.rear+1)%MAXSIZE;if(Q.rear==Q.front)return ERROR;Q.elem[Q.rear]=e;return OK;}//出队int DeQueue(SqQueue &Q,int &e){if(Q.front==Q.rear)return ERROR;e=Q.elem[Q.front+1];Q.elem[Q.front+1]=-1;Q.front=(Q.front+1)%MAXSIZE;return OK;}void main(){SqQueue Q;InitQueue(Q);int elem,e;printf("请输入队列元素(以0结束):\n");scanf("%d",&elem);while(elem!=0){EnQueue(Q,elem);scanf("%d",&elem);}printf("队列为:\n");Display(Q);printf("1.初始化一个队列;\n2.入队;\n3.出队;\n4.显示队列的所有元素;\n5.队列长度:\n6.结束;\n");while(1){switch(menu_select()){case 1:printf("请输入队列元素(以0结束):\n");scanf("%d",&elem);while(elem!=0){EnQueue(Q,elem);scanf("%d",&elem);}printf("队列为:\n");Display(Q);fflush(stdin);break;case 2:scanf("%d",&elem);EnQueue(Q,elem);printf("队列为:\n");Display(Q);fflush(stdin);break;case 3:DeQueue(Q,elem);printf("队列为:\n");Display(Q);break;case 4:printf("\n队列的所有元素:\n");Display(Q);break;case 5:printf("%d\n",QueueLength(Q));break;case 6:return;}}}运行结果:实验二、数组㈠、实验内容:数组一般不做插入或删除操作,也就是说,一旦建立了数组,则结构中的数据元素个数和元素之间的关系就不再发生变动。
数据结构上机实验报告

else insert(lc,i/2,la.list[i]); //偶数位次元素插入lc
}
printf("\n您输入的线性表元素为:\n\n");
print(la);
printf("线性表的奇数位次的元素为:\n\n");
print(lb);
printf("线性表的偶数位次的元素为:\n\n");
print(lc);
}
void initial(sqlist &v)
{printf(" ****本程序可以实现线性表奇偶位序的元素分别输出****\n\n\n");
int i,a;
printf("请输入一个偶数作为线性表的长度:\n\n");
void initlist(sqlist &);
void print(sqlist); //输出线性表
void main()
{printf(" *****************本程序可以实现线性表的复制******************\n");
sqlist la,lb; //声明线性表
initial(la);
【程序设计细想】:
建立三个线性表la,lb,lc,对线性表la进行输入,然后利用循环结构对la中奇数位序的元素对lb进行插入操作,对偶数位序的元素对lc进行插入操作,从而得到线性表lb和lc。
【程序代码】:
#include<stdio.h>
#include<malloc.h>
数据结构实验报告

HUBEI UNIVERSITY OF AUTOMOTIVE TECHNOLOGY
数据结构
实验报告
实验项目实验一实验类别基础篇
学生姓名宋大超学生学号201501149 完成日期2016-10-9
指导教师袁科
实验成绩评阅日期
评阅教师
实验一线性表基本操作的编程实现
【实验目的】
线性表基本操作的编程实现
要求:
线性表基本操作的编程实现(2学时,验证型),掌握线性表的建立、遍历、插入、删除等基本操作的编程实现,也可以进一步编程实现查找、逆序、排序等操作,存储结构可以在顺序结构或链表结构中任选,可以完成部分主要功能,也可以用菜单进行管理完成大部分功能。
还鼓励学生利用基本操作进行一些更实际的应用型程序设计。
【实验性质】
验证性实验(学时数:2H)
【实验内容】
把线性表的顺序存储和链表存储的数据插入、删除运算其中某项进行程序实现。
建议实现键盘输入数据以实现程序的通用性。
为了体现功能的正常性,至少要编制遍历数据的函数。
【注意事项】
1.开发语言:使用C。
2.可以自己增加其他功能。
《数据结构》线性结构实验报告

《数据结构》线性结构实验报告2、源程序:#include <stdio.h>#include<stdlib.h>#define MAXSIZE 1024typedef int elemtype;typedef struct SequenStack{elemtype data[MAXSIZE];int top;}SequenStack;SequenStack * Init_SequenStack(){SequenStack * S;S = (SequenStack *)malloc(sizeof(SequenStack));if (S == NULL)return S;S->top = -1;return S;}int SequenStack_Empty(SequenStack * S)//判栈空{if (S->top == -1){return 1;}{int a;printf("请以十进制输入一个数:\n");scanf_s("%d", &a);printf("转化为二进制为:");Conversion(a);printf("\n");}运行结果:3、源程序:#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct node{char data;struct node* next;}LinkStack;//初始化LinkStack* Init_LinkStack(){LinkStack* top;top = (LinkStack*)malloc(sizeof(LinkStack));top->next = NULL;return top;}//入栈void Push_LinkStack(LinkStack* top, char x){LinkStack* node;node = (LinkStack*)malloc(sizeof(LinkStack));node->data = x;node->next = top->next;top->next = node;}运行结果:4、源程序:#include <stdio.h>#include<stdlib.h>#include<string.h>#define MAXSIZE 20typedef int elemtype;typedef struct QueueNode{elemtype data;struct QueueNode* next;}LinkedQueueNode;typedef struct LQueue{LinkedQueueNode* front;LinkedQueueNode* rear;}LQueue, *LinkedQueue;typedef struct Person{char name[MAXSIZE];char sex;}Person;typedef char* ElemType;//链队初始化LinkedQueue Init_LinkedQueue(){LinkedQueue Q = (LinkedQueue)malloc(sizeof(LQueue));LinkedQueueNode * head = (LinkedQueueNode *)malloc(sizeof(LinkedQueueNode));if (head != NULL && Q != NULL){head->next = NULL;Q->front = head;Q->rear = head;printf("输入参与者的姓名,性别\n");for (i = 0; i < num; i++){printf("输入第%d个舞者的名字:\n", i + 1);scanf_s("%s", &dancer[i].name, 10);printf("输入第%d个人的性别(F/M):\n", i + 1);scanf_s("%s", &dancer[i].sex, 10);while (dancer[i].sex != 'F' && dancer[i].sex != 'M'){printf("输入错误,请重新输入第%d个人的性别(F/M):\n", i + 1);scanf_s("%s", &dancer[i].sex, 10);}}DancePartner(dancer, num);break;case 0:printf("感谢你的使用!\n");break;default:printf("无此选项!\n");break;}} while (n != 0);return 0;}运行结果:。
国家开放大学《数据结构》课程实验报告(实验3 ——栈、队列、递归设计)参考答案

x=Pop(s); /*出栈*/
printf("%d ",x);
InQueue(sq,x); /*入队*/
}
printf("\n");
printf("(10)栈为%s,",(StackEmpty(s)?"空":"非空"));
printf("队列为%s\n",(QueueEmpty(sq)?"空":"非空"));
ElemType Pop(SeqStack *s); /*出栈*/
ElemType GetTop(SeqStack *s); /*取栈顶元素*/
void DispStack(SeqStack *s); /*依次输出从栈顶到栈底的元素*/
void DispBottom(SeqStack *s); /*输出栈底元素*/
} SeqQueue; /*定义顺序队列*/
void InitStack(SeqStack *s); /*初始化栈*/
int StackEmpty(SeqStack *s); /*判栈空*/
int StackFull(SeqStack *s); /*判栈满*/
void Push(SeqStack *s,ElemType x); /*进栈*/
sq=(SeqQueue *)malloc(sizeof(SeqQueue));
InitQueue(sq);
printf("(8)队列为%s\n",(QueueEmpty(sq)?"空":"非空"));
printf("(9)出栈/入队的元素依次为:");
数据结构实验报告

《数据结构》实验报告姓名:学号:班级:学院:实验一单链表实验(一)实验目的1.理解线性表的链式存储结构。
2.熟练掌握动态链表结构及有关算法的设计。
3.根据具体问题的需要,设计出合理的表示数据的链表结构,并设计相关算法。
(二)实验任务编写算法实现下列问题的求解1.求链表中第i个结点的指针(函数),若不存在,则返回NULL。
2.在第i个结点前插入值为x的结点。
3.删除链表中第i个元素结点。
4.在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性。
5.将单链表L中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后再将这两个新链表同时输出在屏幕上,并保留原链表的显示结果,以便对照求解结果。
6.求两个递增有序链表L1和L2中的公共元素,并以同样方式连接成链表L3。
(三)主要仪器设备PC机,Windows操作平台,Visual C++(四)实验分析顺序表操作:定义一个顺序表类,该类包括顺序表的存储空间、存储容量和长度,以及构造、插入、删除、遍历等操作的方法(五)源程序头文件文件名:linklist.h#include<iostream>using namespace std;struct node{int data;node *next;};class list{public:list();int length()const{return count; //求链表长度}~list();void create(); //链表构建,以0为结束标志void output(); //链表输出int get_element(const int i)const; //按序号取元素node *locate(const int x) const; //搜索对应元素int insert(const int i,const int x); //插入对应元素int delete_element(const int i); //删除对应元素node *get_head(){return head; //读取头指针}void insert2(const int x);friend void SplitList(list L1, list&L2, list &L3);friend void get_public(list L1, list L2, list &L3);private:int count;node *head;};list::list(){head=new node;head->next=NULL;count=0;}void list::create() //链表构建,以0为结束标志{int x;cout<<"请输入当前链表,以0为结束符。
《数据结构》实验1实验报告

南京工程学院实验报告<班级>_<学号>_<实验X>.RAR文件形式交付指导老师。
一、实验目的1.熟悉上机环境,进一步掌握语言的结构特点。
2.掌握线性表的顺序存储结构的定义及实现。
3.掌握线性表的链式存储结构——单链表的定义及实现。
4.掌握线性表在顺序存储结构即顺序表中的各种基本操作。
5.掌握线性表在链式存储结构——单链表中的各种基本操作。
二、实验内容1.顺序线性表的建立、插入及删除。
2.链式线性表的建立、插入及删除。
三、实验步骤1.建立含n个数据元素的顺序表并输出该表中各元素的值及顺序表的长度。
2.利用前面的实验先建立一个顺序表L={21,23,14,5,56,17,31},然后在第i个位置插入元素68。
3.建立一个带头结点的单链表,结点的值域为整型数据。
要求将用户输入的数据按尾插入法来建立相应单链表。
四、程序主要语句及作用程序1的主要代码(附简要注释)public struct sequenlist{public const int MAXSIZE=1024; /*最大值为1024*/public elemtype[] vec;public int len; /* 顺序表的长度 */public sequenlist( int n){vec=new elemtype[MAXSIZE ];len = n;}};class Program{static void Main(string[] args){sequenlist list1 = new sequenlist(5);for (int i = 0; i < 5; i++){list1.vec[i] = i;}for (int i = 0; i < 5; i++){Console.Write("{0}---", list1.vec[i]) ;}Console.WriteLine("\n");Console.WriteLine("表长:{0}\n",list1.len );Console.ReadKey();}}程序2的主要代码(附简要注释)public void insertlist(int i, int x){if (len >= MAXSIZE)throw new Exception("上溢"); /*长度大于最大值则抛出异常*/if (i < 1 || i > len + 1)throw new Exception("位置");/插入位置小于1或大于len+1则抛出插入位置错误的异常for (int j = len; j >= i; j--)vec[j] = vec[j - 1]; //注意第j个元素存在数组下标为j-1处vec[i - 1] = x;len++;}};class Program{static void Main(string[] args){sequenlist list2 = new sequenlist(7);list2.vec[0] = 21;list2.vec[1] = 23;list2.vec[2] = 14;list2.vec[3] = 5;list2.vec[4] = 56;list2.vec[5] = 17;list2.vec[6] = 31;Console.Write("请输入第i个位置插入元素:");int loc =Convert.ToInt32( Console.ReadLine());Console.Write("请输入第{0}个位置插入的元素:", loc);int ele = Convert.ToInt32(Console.ReadLine());Console.WriteLine("插入前的线性表:");for (int i = 0; i < list2.len ; i++){Console.Write("{0}---", list2.vec[i]);}Console.WriteLine("\n");list2.insertlist(loc, ele);Console.WriteLine("插入后的线性表:");for (int i = 0; i < list2.len ; i++){Console.Write("{0}---", list2.vec[i]);}Console.WriteLine("\n");Console.ReadKey();}}程序3的主要代码(附简要注释)class Node{private int num;public int Num{set { num = value; }/输入值get { return num; }/获得值}private Node next;public Node Next{set { next = value; }get { return next; }}}class Pp{static void Main(string[] args){Node head;Node tempNode, tempNode1;int i;head = new Node();Console.WriteLine("输入六项数据:\n");Console.Write("输入第1项数据:");head.Num = Convert.ToInt32(Console.ReadLine());head.Next = null;tempNode = head;for (i = 1; i < 6; i++){tempNode1 = new Node();Console.Write("输入第{0}项数据:",i+1);tempNode1.Num = Convert.ToInt32(Console.ReadLine());/插入项转换为整形数值 tempNode1.Next = null;tempNode.Next = tempNode1;tempNode = tempNode.Next;}Console.WriteLine("线性表:");tempNode = head;for (i = 0; i < 6; i++){Console.Write("{0}", tempNode.Num);if (i < 5){Console.Write("--");}tempNode = tempNode.Next;}Console.ReadKey();}}五、程序运行结果截图程序1程序2程序3六、收获,体会及问题(写得越详细、越个性化、越真实越好,否则我不知道你做这个实验的心路历程,也就无法充分地判断你是否是独立完成的这个实验、你是否在做这个实验时进行了认真仔细地思考、通过这个实验你是否在实践能力上得到了提高)这次试验刚开始做时完全不知道从哪下手,才刚上了几节课,对于线性表、链式表都不是理解的很透彻,不知道用哪个软件编写程序。
数据结构 实验报告

数据结构实验报告一、实验目的数据结构是计算机科学中非常重要的一门课程,通过本次实验,旨在加深对常见数据结构(如链表、栈、队列、树、图等)的理解和应用,提高编程能力和解决实际问题的能力。
二、实验环境本次实验使用的编程语言为C++,开发工具为Visual Studio 2019。
操作系统为 Windows 10。
三、实验内容1、链表的实现与操作创建一个单向链表,并实现插入、删除和遍历节点的功能。
对链表进行排序,如冒泡排序或插入排序。
2、栈和队列的应用用栈实现表达式求值,能够处理加、减、乘、除和括号。
利用队列实现银行排队系统的模拟,包括顾客的到达、服务和离开。
3、二叉树的遍历与操作构建一棵二叉树,并实现前序、中序和后序遍历。
进行二叉树的插入、删除节点操作。
4、图的表示与遍历用邻接矩阵和邻接表两种方式表示图。
实现图的深度优先遍历和广度优先遍历。
四、实验步骤及结果1、链表的实现与操作首先,定义了链表节点的结构体:```cppstruct ListNode {int data;ListNode next;ListNode(int x) : data(x), next(NULL) {}};```插入节点的函数:```cppvoid insertNode(ListNode& head, int val) {ListNode newNode = new ListNode(val);head = newNode;} else {ListNode curr = head;while (curr>next!= NULL) {curr = curr>next;}curr>next = newNode;}}```删除节点的函数:```cppvoid deleteNode(ListNode& head, int val) {if (head == NULL) {return;}ListNode temp = head;head = head>next;delete temp;return;}ListNode curr = head;while (curr>next!= NULL && curr>next>data!= val) {curr = curr>next;}if (curr>next!= NULL) {ListNode temp = curr>next;curr>next = curr>next>next;delete temp;}}```遍历链表的函数:```cppvoid traverseList(ListNode head) {ListNode curr = head;while (curr!= NULL) {std::cout << curr>data <<"";curr = curr>next;}std::cout << std::endl;}```对链表进行冒泡排序的函数:```cppvoid bubbleSortList(ListNode& head) {if (head == NULL || head>next == NULL) {return;}bool swapped;ListNode ptr1;ListNode lptr = NULL;do {swapped = false;ptr1 = head;while (ptr1->next!= lptr) {if (ptr1->data > ptr1->next>data) {int temp = ptr1->data;ptr1->data = ptr1->next>data;ptr1->next>data = temp;swapped = true;}ptr1 = ptr1->next;}lptr = ptr1;} while (swapped);}```测试结果:创建了一个包含 5、3、8、1、4 的链表,经过排序后,输出为 1 3 4 5 8 。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
数据结构实验报告-答案数据结构(C语言版)实验报告专业班级学号姓名实验1实验题目:单链表的插入和删除实验目的:了解和掌握线性表的逻辑结构和链式存储结构,掌握单链表的基本算法及相关的时间性能分析。
实验要求:建立一个数据域定义为字符串的单链表,在链表中不允许有重复的字符串;根据输入的字符串,先找到相应的结点,后删除之。
实验主要步骤:1、分析、理解给出的示例程序。
2、调试程序,并设计输入数据(如:bat,cat,eat,fat,hat,jat,lat,mat,#),测试程序的如下功能:不允许重复字符串的插入;根据输入的字符串,找到相应的结点并删除。
3、修改程序:(1)增加插入结点的功能。
(2)将建立链表的方法改为头插入法。
程序代码:#include“stdio.h“#include“string.h“#include“stdlib.h“#include“ctype. h“typedefstructnode//定义结点{chardata[10];//结点的数据域为字符串structnode*next;//结点的指针域}ListNode;typedefListNode*LinkList;//自定义LinkList单链表类型LinkListCreatListR1();//函数,用尾插入法建立带头结点的单链表LinkListCreatList(void);//函数,用头插入法建立带头结点的单链表ListNode*LocateNode();//函数,按值查找结点voidDeleteList();//函数,删除指定值的结点voidprintlist();//函数,打印链表中的所有值voidDeleteAll();//函数,删除所有结点,释放内存ListNode*AddNode();//修改程序:增加节点。
用头插法,返回头指针//==========主函数==============voidmain(){charch[10],num[5];LinkListhead;head=C reatList();//用头插入法建立单链表,返回头指针printlist(head);//遍历链表输出其值printf(“Deletenode(y/n):“);//输入“y“或“n“去选择是否删除结点scanf(“%s“,num);if(strcmp(num,“y“)==0||strcmp(num,“Y“)==0){printf(“PleaseinputDelete_data:“);scanf(“%s“,ch);//输入要删除的字符串DeleteList(head,ch);printlist(head);}printf(“Addnode?(y/n):“);//输入“y“或“n“去选择是否增加结点scanf(“%s“,num);if(strcmp(num,“y“)==0||strcmp(num,“Y“)==0){head=A ddNode(head);}printlist(head);DeleteAll(head);//删除所有结点,释放内存}//==========用尾插入法建立带头结点的单链表===========LinkListCreatListR1(void){charch[10];LinkListhead=(Li nkList)malloc(sizeof(ListNode));//生成头结点ListNode*s,*r,*pp;r=head;r->next=NULL;printf(“Input#toend“);//输入“#“代表输入结束printf(“\nPleaseinputN ode_data:“);scanf(“%s“,ch);//输入各结点的字符串while(strcmp(ch,“#“)!=0){pp=LocateNode(head,ch);//按值查找结点,返回结点指针if(pp==NULL){//没有重复的字符串,插入到链表中s=(ListNode*)malloc(sizeof(ListNode));strcpy(s->data,ch);r->next=s;r=s; r->next=NULL;}printf(“Input#toend“);printf(“PleaseinputNode_data:“);scanf(“%s“,ch);}returnhead;//返回头指针}//==========用头插入法建立带头结点的单链表===========LinkListCreatList(void){charch[100];LinkListhead,p;head =(LinkList)malloc(sizeof(ListNode));head->next=NULL;while(1){printf(“Input#toend“);printf(“PleaseinputNode_data:“);scanf(“%s“,ch);if(strcmp (ch,“#“)){if(LocateNode(head,ch)==NULL){strcpy(head->data,ch);p=(Li nkList)malloc(sizeof(ListNode));p->next=head;head=p;}}elsebreak;}retu rnhead;}//==========按值查找结点,找到则返回该结点的位置,否则返回NULL==========ListNode*LocateNode(LinkListhead,char*key){List Node*p=head->next;//从开始结点比较while(p!=NULL//扫描下一个结点returnp;//若p=NULL则查找失败,否则p指向找到的值为key的结点}//==========修改程序:增加节点=======ListNode*AddNode(LinkListhead){charch[10];ListNode*s,*pp ;printf(“\nPleaseinputaNewNode_data:“);scanf(“%s“,ch);//输入各结点的字符串pp=LocateNode(head,ch);//按值查找结点,返回结点指针printf(“ok2\n“);if(pp==NULL){//没有重复的字符串,插入到链表中s=(ListNode*)malloc(sizeof(ListNode));strcpy(s->data,ch);printf(“ok3\n“);s->next=head->next;head->next=s;}returnhead;}//==========删除带头结点的单链表中的指定结点=======voidDeleteList(LinkListhead,char*key){ListNode*p,*r,*q=hea d;p=LocateNode(head,key);//按key值查找结点的if(p==NULL){//若没有找到结点,退出printf(“positionerror”);exit(0);}while(q->next!=p)//p 为要删除的结点,q为p的前结点q=q->next;r=q->next;q->next=r->next;free(r);//释放结点}//===========打印链表=======voidprintlist(LinkListhead){ListNode*p=head->next;//从开始结点打印while(p){printf(“%s,“,p->data);p=p->next;}printf(“\n“);}//==========删除所有结点,释放空间===========voidDeleteAll(LinkListhead){ListNode*p=head,*r;while( p->next){r=p->next;free(p);p=r;}free(p);}实验结果:Input#toendPleaseinputNode_data:batInput#toendPleaseinputNode_data: catInput#toendPleaseinputNode_data:eatInput#toendPleaseinputNode_da ta:fatInput#toendPleaseinputNode_data:hatInput#toendPleaseinputNode_ data:jatInput#toendPleaseinputNode_data:latInput#toendPleaseinputNode _data:matInput#toendPleaseinputNode_data:#mat,lat,jat,hat,fat,eat,cat,bat ,Deletenode(y/n):yPleaseinputDelete_data:hatmat,lat,jat,fat,eat,cat,bat,Ins ertnode(y/n):yPleaseinputInsert_data:putposition:5mat,lat,jat,fat,eat,put,c at,bat,请按任意键继续...示意图:latjathatfateatcatbatmatNULLheadlatjathatfateatcatbatmatheadlatjatfateat putcatbatmatheadNULLNULL心得体会:本次实验使我们对链表的实质了解更加明确了,对链表的一些基本操作也更加熟练了。
另外实验指导书上给出的代码是有一些问题的,这使我们认识到实验过程中不能想当然的直接编译执行,应当在阅读并完全理解代码的基础上再执行,这才是实验的意义所在。
实验2实验题目:二叉树操作设计和实现实验目的:掌握二叉树的定义、性质及存储方式,各种遍历算法。
实验要求:采用二叉树链表作为存储结构,完成二叉树的建立,先序、中序和后序以及按层次遍历的操作,求所有叶子及结点总数的操作。
实验主要步骤:1、分析、理解程序。
2、调试程序,设计一棵二叉树,输入完全二叉树的先序序列,用#代表虚结点(空指针),如ABD###CE##F##,建立二叉树,求出先序、中序和后序以及按层次遍历序列,求所有叶子及结点总数。
实验代码#include“stdio.h“#include“stdlib.h“#include“string.h“#defineMax20//结点的最大个数typedefstructnode{chardata;structnode*lchild,*rchild;}BinTNode;//自定义二叉树的结点类型typedefBinTNode*BinTree;//定义二叉树的指针intNodeNum,leaf;//NodeNum为结点数,leaf为叶子数//==========基于先序遍历算法创建二叉树==============//=====要求输入先序序列,其中加入虚结点“#“以示空指针的位置==========BinTreeCreatBinTree(void){BinTreeT;charch;if((ch=getcha r())==#)return(NULL);//读入#,返回空指针else{T=(BinTNode*)malloc(sizeof(BinTNode));//生成结点T->data=ch;T->lchild=CreatBinTree();//构造左子树T->rchild=CreatBinTree();//构造右子树return(T);}}//========NLR先序遍历=============voidPreorder(BinTreeT){if(T){printf(“%c“,T->data);//访问结点Preorder(T->lchild);//先序遍历左子树Preorder(T->rchild);//先序遍历右子树}}//========LNR中序遍历===============voidInorder(BinTreeT){if(T){Inorder(T->lchild);//中序遍历左子树printf(“%c“,T->data);//访问结点Inorder(T->rchild);//中序遍历右子树}}//==========LRN后序遍历============voidPostorder(BinTreeT){if(T){Postorder(T->lchild);//后序遍历左子树Postorder(T->rchild);//后序遍历右子树printf(“%c“,T->data);//访问结点}}//=====采用后序遍历求二叉树的深度、结点数及叶子数的递归算法========intTreeDepth(BinTreeT){inthl,hr,max;if(T){hl=TreeDepth(T-> lchild);//求左深度hr=TreeDepth(T->rchild);//求右深度max=hl>hr?hl:hr;//取左右深度的最大值NodeNum=NodeNum+1;//求结点数if(hl==0//若左右深度为0,即为叶子。