数据结构上机实验源文件

合集下载

数据结构上机报告3个

数据结构上机报告3个

《数据结构》实验报告一题目:快速排序班级:学号:姓名:日期:程序名:一、上机实验的问题和要求:一趟快速排序示例,关键字序列:{52, 49, 80, 36, 14, 58, 61, 97, 23, 75}二、程序设计的基本思想,原理和算法描述:基本思想快速排序是通过比较关键字,以某个记录为界(称为支点记录),将待排序列分成两个部分。

其中一部分所有记录的关键字大于或等于支点记录的关键字,另一部分所有关键字小于支点记录的关键字。

将待排序列按关键字以支点记录分成两部分的过程,称为一次划分,或一趟快速排序三、源程序及注释:int partition(Record r[ ], int low, int high){int k;r[0]=r[low];k=r[low].key;while(low<high){while(low<high&&r[high].key>=k)high--;r[low]=r[high];while(low<high&&r[low].key<k)low++;r[high]=r[low];}r[low]=r[0];return low;}void main(){Record r[size];int i=1, j, k, low=1, high;printf("输入关键字序列:\n");scanf("%d", &k);while(k!=0){r[i++].key=k;scanf("%d", &k);}high=i-1;partition(r, low, high);printf("一趟快速排序之后的关键字序列为:\n");for(i=1;i<=high;i++)printf("%d ", r[i].key);printf("\n");}四、运行输出结果:五、调试和运行程序过程中产生的问题及采取的措施:六、对算法的程序的讨论、分析,改进设想,其它经验教训:七、对实验方式、组织、设备、题目的意见和建议:。

东华大学数据结构上机实验报告

东华大学数据结构上机实验报告

东华大学信息科学与技术学院实验报告实验名称:线性表的基本操作指导教师:学生姓名:学生学号:实验日期:2012/11一、实验目的1、熟悉C或VC++语言上机环境。

2、会定义线性表的顺序存储结构和链式存储结构。

3、熟悉顺序表和单链表的一些基本操作和应用。

4、加深对线性表的理解,逐步培养解决实际问题的编程能力。

二、实验环境运行C或VC++的微机。

三、实验内容:分别使用顺序表和单链表存储结构实现以下操作:1.建立线性表L={12,13,21,24,28,31,42,77};2.在第5个元素之前插入26;3.删除第5个元素28;4.查找28。

四、实验设计思路及算法流程(一)使用顺序表实现操作:建立顺序表,完成顺序表的基本操作:初始化、插入、删除、输出、查找元素、判线性表是否为空;问题分析:利用顺序表,设计一组输入数据,能够对顺序表进行如下操作:创建一个新的顺序表,实现动态空间分配的初始化;已给定的值插入到指定位置和删除指定位置的值,形成有序顺序表;按值查找,根据给定数据元素的值,查找该元素的位置,对查找结果进行返回;实现顺序表的各个元素的输出;“初始化算法”的操作结果:构造一个空的顺序线性表,对顺序表的空间进行动态管理,实现动态分配、回收和增加存储空间;“位置插入算法”的初始条件:顺序线性表L已存在,给定的元素位置为i,且1≤i≤ListLength(L)+1 ;其操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1;“位置删除算法”的初始条件:顺序线性表L已存在,1≤i≤ListLength(L) ;其操作结果:删除L的第i个数据元素,并用e返回其值,L的长度减1 ;“输出算法”的初始条件:顺序线性表L已存在;其操作结果:依次对L的每个数据元素进行输出;“按值查找算法”初始条件:顺序线性表L已存在,元素值为e;其操作结果:返回L 中数据元素值为e的元素位置;线性表的顺序存储结构的定义及其基本操作的参考程序(顺序表)文件一:pubuse. h 是公共使用的常量定义和系统函数调用声明。

数据结构上机实验报告

数据结构上机实验报告

实验一线性表抽象数据类型的实现【实验内容】1.利用程序设计语言实现单链表的抽象数据类型。

2.明白单链表头结点在程序语言的表示及用法。

3.掌握单链表的插入、删除、输出等运算。

【实验方法与步骤】个人程序如下:#include <stdio.h>#include <stdlib.h>typedef struct node{int nDate;struct node *pstnext;}Node;//链表输出董宝超void output(Node *head){Node *p = head->pstnext;while(NULL != p){printf("%d ", p->nDate);p = p->pstnext;}printf("\r\n");}//创建链表王宁宁Node* creat(){Node *head = NULL, *p = NULL, *s = NULL;int Date = 0, cycle = 1;head = (Node*)malloc(sizeof(Node));if(NULL == head){printf("分配内存失败\r\n");return NULL;}head->pstnext = NULL;p = head;while(cycle){printf("请输入数据且当输入数据为0时结束输入\r\n");scanf("%d", &Date);if(0 != Date){s = (Node*)malloc(sizeof(Node)); if(NULL == s){printf("分配内存失败\r\n");return NULL;}s->nDate = Date;p->pstnext = s;p = s;}else{cycle = 0;}}p->pstnext = NULL;return(head);}//在指定元素之后插入新结点张容void insert_2(Node *head, int i, int Newdate) {Node *pre = head, *New = NULL;int j = 0;while(NULL != pre->pstnext && j < i){pre = pre->pstnext;j++;}if(j == i){New = (Node*)malloc(sizeof(Node));if(NULL == New){printf("分配内存失败\r\n");return;}New->nDate = Newdate;New->pstnext = pre->pstnext;pre->pstnext = New;}else{printf("插入位置不存在\r\n");}}void main()//兰洋王克{int date, num; //待查找数据int i3; //指定删除元素的位置int i1, i2, Newdate_1, Newdate_2; //待插入的新数据 int Delete_date, k; //待删除的数据与其个数Node *Head = NULL; //定义头结点Node *Head_New = NULL;//链表建立Head = creat();printf("输出建立的单链表\r\n");output(Head);//在指定第i2个元素之后插入新元素Newdateprintf("在指定第i个元素之后插入新元素Newdate"); printf("请输入i与元素且以逗号间隔\r\n");scanf("%d,%d", &i2, &Newdate_2);insert_2(Head, i2, Newdate_2);printf("插入后新链表\r\n");output(Head);return;}总程序如下:#include <stdio.h>#include <stdlib.h>typedef struct node{int nDate;struct node *pstnext;}Node;//链表输出void output(Node *head){Node *p = head->pstnext;while(NULL != p){printf("%d ", p->nDate);p = p->pstnext;}printf("\r\n");}//链表建立Node* creat(){Node *head = NULL, *p = NULL, *s = NULL; int Date = 0, cycle = 1;head = (Node*)malloc(sizeof(Node));if(NULL == head){printf("分配内存失败\r\n");return NULL;}head->pstnext = NULL;p = head;while(cycle){printf("请输入数据且当输入数据为0时结束输入\r\n"); scanf("%d", &Date);if(0 != Date){s = (Node*)malloc(sizeof(Node));if(NULL == s){printf("分配内存失败\r\n");return NULL;}s->nDate = Date;p->pstnext = s;p = s;}else{cycle = 0;}}p->pstnext = NULL;return(head);}//单链表测长void length(Node *head){Node *p = head->pstnext;int j=0;while(NULL != p){p = p->pstnext;j++;}printf("%d\r\n", j);}//链表按值查找void research_Date(Node *head, int date) {Node *p;int n=1;p = head->pstnext;while(NULL != p && date != p->nDate){p = p->pstnext;++n;}if(NULL == p){printf("链表中没有找到该值");}else if(date == p->nDate){printf("要查找的值%d在链表中第%d个位置\r\n", date, n); }return;}//按序号查找void research_Number(Node *head, int Num){Node *p=head;int i = 0;while(NULL != p && i < Num){p = p->pstnext;i++;}if(p == NULL){printf("查找位置不合法\r\n");}else if(i == 0){printf("查找位置为头结点\r\n");}else if(i == Num){printf("第%d个位置数据为%d\r\n", i, p->nDate); }}//在指定元素之前插入新结点void insert_1(Node *head, int i, int Newdate) {Node *pre = head, *New = NULL;int j = 0;while(NULL != pre && j < i-1){pre = pre->pstnext;j++;}if(NULL == pre || j > i-1){printf("插入位置不存在\r\n");}else{New = (Node*)malloc(sizeof(Node));if(NULL == New){printf("分配内存失败\r\n");return;}New->nDate = Newdate;New->pstnext = pre->pstnext;pre->pstnext = New;}}//在指定元素之后插入新结点void insert_2(Node *head, int i, int Newdate) {Node *pre = head, *New = NULL;int j = 0;while(NULL != pre->pstnext && j < i){pre = pre->pstnext;j++;}if(j == i){New = (Node*)malloc(sizeof(Node)); if(NULL == New){printf("分配内存失败\r\n");return;}New->nDate = Newdate;New->pstnext = pre->pstnext;pre->pstnext = New;}else{printf("插入位置不存在\r\n");}}//删除指定结点void Delete_1(Node *head, int i3) {Node *p = head, *pre = NULL;int j = 0;while(NULL != p && j < i3){pre = p;p = p->pstnext;j++;}if(NULL == p){printf("删除位置不存在\r\n");}else{pre->pstnext = p->pstnext;free(p);}}//指定删除单链表中某个数据,并统计删除此数据的个数int Delete_2(Node *head, int Delete_date){int count = 0;Node *p = head, *q;while(NULL != p->pstnext){q = p->pstnext;if(q->nDate == Delete_date){p->pstnext = q->pstnext;free(q);++count;}else{p = q;}}return count;}//链表逆置void Reverse_list(Node *head){Node *q, *s;if(NULL == head->pstnext || NULL == head->pstnext->pstnext) {return;}q = head->pstnext->pstnext;head->pstnext->pstnext = NULL;while(NULL != q){s = q->pstnext;q->pstnext = head->pstnext;head->pstnext = q;q = s;}}//单链表的连接void connect_list(Node *head, Node *head_New) {Node *p = head;while(NULL != p->pstnext){p = p->pstnext;}p->pstnext = head_New->pstnext;}//单链表销毁void destroy_list(Node* head){while (NULL != head){Node* temp = head;head = head->pstnext;free(temp);}}void main(){int date, num; //待查找数据int i3; //指定删除元素的位置int i1, i2, Newdate_1, Newdate_2; //待插入的新数据 int Delete_date, k; //待删除的数据与其个数Node *Head = NULL; //定义头结点Node *Head_New = NULL;//链表建立Head = creat();printf("输出建立的单链表\r\n");output(Head);//单链表测长printf("单链表长度为\r\n");length(Head);//链表按值查找printf("请输入待查找的数据\r\n");scanf("%d", &date);research_Date(Head, date);//链表按序号查找printf("请输入待查找序号\r\n");scanf("%d", &num);research_Number(Head, num);//在指定第i1个元素之前插入新元素Newdateprintf("在指定第i个元素之前插入新元素Newdate"); printf("请输入i与元素且以逗号间隔(eg:2,23)\n"); scanf("%d,%d", &i1, &Newdate_1);insert_1(Head, i1, Newdate_1);printf("插入后新链表\r\n");output(Head);//在指定第i2个元素之后插入新元素Newdateprintf("在指定第i个元素之后插入新元素Newdate"); printf("请输入i与元素且以逗号间隔(eg:2,23)\n"); scanf("%d,%d", &i2, &Newdate_2);insert_2(Head, i2, Newdate_2);printf("插入后新链表\r\n");output(Head);//指定删除i3元素printf("删除元素的位置\r\n");scanf("%d", &i3);Delete_1(Head, i3);printf("删除后新链表\r\n");output(Head);//指定删除单链表中某个数据,并统计删除此数据的个数 printf("请输入待删除的元素\r\n");scanf("%d", &Delete_date);k = Delete_2(Head, Delete_date);printf("删除后新链表\r\n");output(Head);printf("删除指定元素在链表中的个数为:");printf("%d\r\n", k);//单链表逆置Reverse_list(Head);printf("逆置后输出\r\n");output(Head);//单链表的连接printf("建立一个新链表\r\n");Head_New = creat();printf("输出新链表");output(Head);printf("将新链表连接到原来链表的尾部并输出\r\n"); connect_list(Head, Head_New);output(Head);destroy_list(Head);return;}【实验结果】实验二表达式求值的实现【实验内容】1.用程序设计语言实现栈的抽象数据类型。

数据结构上机实验源代码

数据结构上机实验源代码

数据结构上机实验源代码栈的应用十进制数转换为八进制数,逆序输出所输入的数实验代码://stack.h,头文件class stack{public:stack();bool empty()const;bool full()const;error_code gettop(elementtype &x)const;error_code push(const elementtype x);error_code pop();private:int count;elementtype data[maxlen];};stack::stack(){count=0;}bool stack::empty()const{return count==0;}bool stack::full()const{return count==maxlen;}error_code stack::gettop(elementtype &x)const{if(empty())return underflow;else{x=data[count-1];return success;}}error_code stack::push(const elementtype x){if(full())return overflow;data[count]=x;count++;return success;}error_code stack::pop(){if(empty())return underflow;count--;return success;}//主程序#include<iostream.h>enum error_code{overflow,underflow,success};typedef int elementtype;const int maxlen=20;#include"stack.h"void read_write() //逆序输出所输入的数{stack s;int i;int n,x;cout<<"please input num int n:";cin>>n;for(i=1;i<=n;i++){cout<<"please input a num:";cin>>x;s.push(x);}while(!s.empty()){s.gettop(x);cout<<x<<" ";s.pop();}cout<<endl;}void Dec_to_Ocx(int n) //十进制转换为八进制{stack s1;int mod,x;while(n!=0){mod=n%8;s1.push(mod);n=n/8;}cout<<"the ocx of the dec is:";while(!s1.empty()){s1.gettop(x);cout<<x;s1.pop();}cout<<endl;}void main(){int n;// read_write();cout<<"please input a dec:";cin>>n;Dec_to_Ocx(n);}队列的应用打印n行杨辉三角实验代码://queue.hclass queue{public:queue(){count=0;front=rear=0;}bool empty(){return count==0;}bool full(){return count==maxlen-1;}error_code get_front(elementtype &x){if(empty())return underflow;x=data[(front+1)%maxlen];return success;}error_code append(const elementtype x){if(full())return overflow;rear=(rear+1)%maxlen;data[rear]=x;count++;return success;}error_code serve(){if(empty())return underflow;front=(front+1)%maxlen;count--;return success;}private:int count;int front;int rear;int data[maxlen];};//主程序#include<iostream.h>enum error_code{overflow,underflow,success};typedef int elementtype;const int maxlen=20;#include"queue.h"void out_number(int n) //打印前n行的杨辉三角{int s1,s2;int i;int j;int k;queue q;for(i=1;i<=(n-1)*2;i++)cout<<" ";cout<<"1 "<<endl;q.append(1);for(i=2;i<=n;i++){s1=0;for(k=1;k<=(n-i)*2;k++)cout<<" ";for(j=1;j<=i-1;j++){q.get_front(s2);q.serve();cout<<s1+s2<<" ";q.append(s1+s2);s1=s2;}cout<<"1 "<<endl;q.append(1);}}void main(){int n;cout<<"please input n:";cin>>n;out_number(n);}单链表实验实验目的:实验目的(1)理解线性表的链式存储结构。

qdr数据结构上机实验报告8-

qdr数据结构上机实验报告8-

数据结构上机实验报告8—AAXXX课程名称:数据结构年级:09实验日期:姓名:学号:04班级:实验名称:图实验序号:实验八成员人数:1一、实验目的及要求1.了解C语言程序的特点2.掌握简单的C语言程序结构3.熟练掌握运行C程序的步骤和方法4. 掌握图的存储表示方法、图的遍历二、实验环境三、实验内容1)使用邻接表表示图,并对图进行深度优先遍历和广度优先遍历。

四、算法描述及实验步骤用算法表示方法,流程图等形式表达算法设计思想与算法实现步骤五、调试过程及实验结果六、总结熟悉图结构,了解图的存储和遍历七、附录(源程序清单)#include<stdlib.h>#include<stdio.h>int visited[10];typedef struct ArcNode{int adjvex;struct ArcNode *next;}Edge;typedef struct VNode{char vertex;Edge *first;}Vertex;typedef Vertex AdjList[10];typedef struct{AdjList adj;int n,e;}Algraph;void creatALG(Algraph *g){int i,j,k;Edge *s=NULL;printf("please input vertex:");scanf("%d",&(g->n));printf("please input edges:");scanf("%d",&(g->e));getchar();for(i=0;i<g->n;i++){g->adj[i].vertex=i+48;g->adj[i].first=NULL;}for(k=0;k<g->e;k++){printf("please the %d edge:",k+1); scanf("%d %d",&i,&j);getchar();s=(Edge *)malloc(sizeof(Edge));s->adjvex=j;s->next=g->adj[i].first;g->adj[i].first=s;}}void DFSG(Algraph *g,int i){Edge *p;printf("%c ",g->adj[i].vertex);visited[i]=1;p=g->adj[i].first;while(p){if(!visited[p->adjvex])DFSG(g,p->adjvex);p=p->next;}}void Depth_First(Algraph *g){int i;for(i=0;i<g->n;i++)visited[i]=0;for(i=0;i<g->n;i++)if(!visited[i])DFSG(g,i);}void Breadth_First(Algraph *g,int i){Edge *p;int q[10],f=0,r=0;int j,v;for(j=0;j<g->n;j++)visited[j]=0;visited[i]=1;printf("%c ",g->adj[i].vertex);q[r++]=i;while(r!=f){v=q[f++];p=g->adj[v].first;while(p){if(!visited[p->adjvex]){visited[p->adjvex]=1;printf("%c ",g->adj[p->adjvex].vertex);q[r++]=p->adjvex;}p=p->next;}}}void main(){Algraph G;creatALG(&G);printf("\nDepth_First Search:");Depth_First(&G);printf("\nBreadth_First Search:");Breadth_First(&G,0);}。

数据结构上机实验程序

数据结构上机实验程序

链栈#include<iostream.h>#include<stdlib.h>typedef int ElemType;struct SNode{ElemType data;SNode* next;};void InitStack(SNode*& HS){HS=NULL;//将栈置空}void Push(SNode*& HS,const ElemType& item) {//为插入元素获取动态结点SNode* newptr=new SNode;//给新分配的结点赋值newptr->data=item;//向栈顶插入新结点newptr->next=HS;HS=newptr;}ElemType Pop(SNode*& HS){if(HS==NULL){cerr<<"Linked stack is empty!"<<endl;exit(1);}SNode*p=HS;HS=HS->next;ElemType temp=p->data;delete p;return temp;}ElemType Peek(SNode* HS){if(HS==NULL){cerr<<"Linked stack is empty!"<<endl;exit(1);}return HS->data;//返回栈顶结点的值}bool EmptyStack(SNode* HS){return HS==NULL;}void ClearStack(SNode*& HS){SNode *cp,*np;cp=HS;//给cp指针赋初值,使之指向栈顶结点while(cp!=NULL){//从栈顶到栈底依次删除每个结点np=cp->next;delete cp;cp=np;}HS=NULL;}void main(){SNode* a;InitStack(a);int x;cin>>x;while(x!=-1){Push(a,x);cin>>x;}while(!EmptyStack(a))//栈不为空时依次退栈打印出来cout<<Pop(a)<<" ";cout<<endl;ClearStack(a);}顺序栈#include<iostream.h>#include<stdlib.h>typedef int ElemType;struct Stack{ElemType *stack;int top;int MaxSize;};void InitStack(Stack& S){//初始设置栈空间大小为10个元素位置S.MaxSize=10;//动态空间分配,若分配失败侧退出运行S.stack=new ElemType[S.MaxSize];if(!S.stack){cerr<<"动态存储分配失败!"<<endl;exit(1);}S.top=-1;}void Push(Stack& S,ElemType item){if(S.top==S.MaxSize-1){int k=sizeof(ElemType);S.stack=(ElemType*)realloc(S.stack,2*S.MaxSize*k);S.MaxSize=2*S.MaxSize;}//栈顶指针后移一个位置S.top++;//新元素插入栈顶S.stack[S.top]=item;}ElemType Pop(Stack& S){//若栈空则退出运行if(S.top==-1){cerr<<"Stack is empty!"<<endl;exit(1);}//栈顶指针减1表示退栈S.top--;//返回原栈顶元素的值return S.stack[S.top+1];}ElemType Peek(Stack& S){if(S.top==-1){cerr<<"Stack is empty!"<<endl;exit(1);}return S.stack[S.top];}bool EmptyStack(Stack& S){return S.top==-1;}void ClearStack(Stack& S){if(S.stack){delete []S.stack;S.stack=0;}S.top=-1;S.MaxSize=0;}void main(){Stack s;InitStack(s);int a[8]={4,5,6,20,42,30,58,65};int i;for(i=0;i<8;i++) Push(s,a[i]);cout<<Pop(s);cout<<' '<<Pop(s)<<endl;Push(s,60);cout<<Peek(s);cout<<' '<<Pop(s)<<endl;while(!EmptyStack(s)) cout<<Pop(s)<<' ';cout<<endl;ClearStack(s);}栈的应用----运算#include<iostream.h>#include<stdlib.h>typedef char ElemType;struct Stack{ElemType *stack;int top;int MaxSize;};void InitStack(Stack& S){//初始设置栈空间大小为10个元素位置S.MaxSize=10;//动态存储空间分配,若分配失败则退出运行S.stack=new ElemType[S.MaxSize];if(!S.stack){cerr<<"动态存储分配失败!"<<endl;exit(1);}S.top=-1;}void Push(Stack& S,ElemType item){//若栈空间用完则自动扩大2倍空间,原有栈内容不变if(S.top==S.MaxSize-1){int k=sizeof(ElemType);S.stack=(ElemType*)realloc(S.stack,2*S.MaxSize*k);S.MaxSize=2*S.MaxSize;}S.top++;S.stack[S.top]=item;}ElemType Pop(Stack& S){if(S.top==-1){cerr<<"Stack is empty!"<<endl;exit(1);}S.top--;return S.stack[S.top+1];}ElemType Peek(Stack& S){if(S.top==-1)cerr<<"Stack is empty!"<<endl;exit(1);}return S.stack[S.top];}bool EmptyStack(Stack& S){return S.top==-1;}void ClearStack(Stack& S){if(S.stack){delete[]S.stack;S.stack=0;}S.top=-1;S.MaxSize=0;}int Precedence(char op){switch(op){case '+':case '-':return 1;case '*':case '/':return 2;case '(':case '@':default:return 0;}}void Change(char*S1,char*S2){Stack R;InitStack(R);Push(R,'@');int i=0,j=0;char ch=S1[i];while(ch!='\0')if(ch==' ') ch=S1[++i];else if(ch=='('){Push(R,ch); ch=S1[++i];}else if(ch==')'){while(Peek(R)!='(') S2[j++]=Pop(R);Pop(R);ch=S1[++i];}else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){char w=Peek(R);while(Precedence(w)>=Precedence(ch)){S2[j++]=w;Pop(R);w=Peek(R);}Push(R,ch);ch=S1[++i];}else{if((ch<'0'||ch>'9')&& ch!='.'){cout<<"中缀表达式错误!"<<endl;exit(1);}while((ch>='0'&&ch<='9')||ch=='.'){S2[j++]=ch;ch=S1[++i];}S2[j++]=' ';}}ch=Pop(R);while(ch!='@'){if(ch=='('){cerr<<"expression error!"<<endl;exit(1);}else{S2[j++]=ch;ch=Pop(R);}}S2[j++]='\0';}void main(){char a[30];char b[30];cout<<"请输入一个中缀算术表达式:"<<endl;cin.getline(a,sizeof(a));Change(a,b);cout<<"对应的后缀算术表达式: "<<endl;cout<<b<<endl;}。

数据结构与算法上机实验报告

数据结构与算法上机实验报告

数据结构与算法B上机实验报告第1次2011-10-02 顺序表的实现和基本操作第2次2011-10-29 二叉树的实现和递归遍历第3次2011-11-23 内部排序第4次2011-12-dd 实现图从邻接矩阵到邻接表存储转化第一次线性表数据结构一、上机实习题目线性链表操作——插入、删除、合并、排序、查找二数据结构设计(算法设计)源程序(#include <iostream>#define MaxSize 100using namespace std;typedef int ElemType;class SeqList{ElemType list[MaxSize];int length;public:SeqList() {length=0;}void SeqListSort(int i,ElemType x);void SeqListCreat(int n);void SeqListInset(int i,ElemType x);void SeqListDelete(int i);void SeqListMerge();int GetLength(){return length;}int SeqListFind(ElemType x);int SeqListIsEmpty();void SeqListPrint();}Mylist1,Mylist2;//创建顺序表void SeqList::SeqListCreat(int n){ElemType x;cout<<"请输入数据元素:";for (int i=0;i<n;i++){cin>>x;list[i]=x;length++;}}//对顺序表进行排序void SeqList::SeqListSort(int i,ElemType x) {for(int k=0;k<length;k++){for(i=k+1;i<=length;i++){if(list[k]>list[i]){x=list[k];list[k]=list[i];list[i]=x;}}}}//在顺序表L中的第i个位置插入新元素x void SeqList::SeqListInset(int i,ElemType x) {int k;if(length>=MaxSize)cout<<"表已满,无法插入!"<<endl;else if(i<0||i>length)cout<<"参数i不合理!"<<endl;else{for (k=length;k>=i;k--){list[k]=list[k-1];}list[i-1]=x;length++;}}//删除第i个位置的数据元素void SeqList::SeqListDelete(int i){int k;if(!SeqListIsEmpty())cout<<"表已空,无法删除!"<<endl;else if(i<0||i>length)cout<<"参数i不合理!"<<endl;elsefor(k=i-1;k<length;k++)list[k]=list[k+1];length--;}//查找元素x在表中的位置int SeqList::SeqListFind(ElemType x) {int i=0;while(i<length&&list[i]!=x)i++;if(i>length)return -1;elsereturn i+1;}//判断顺序表是否为空int SeqList::SeqListIsEmpty(){if(length<=0)return 0;else return 1;}//将顺序表显示在屏幕上void SeqList::SeqListPrint(){if(!SeqListIsEmpty())cout<<"空表!"<<endl;elsefor(int i=0;i<length;i++)cout<<list[i]<<" ";cout<<endl;}int main(){SeqList Mylist1,Mylist2;int i,n,flag=1,select;ElemType x;cout<<"1. 建立顺序表\n";cout<<"2. 对顺序表进行排序\n";cout<<"3. 求x数值的位置\n";cout<<"4. 在第i个位置插入新元素x\n"; cout<<"5. 删除第i个位置上的数值\n"; cout<<"6. 将两个顺序表合并\n";cout<<"7. 退出\n";cout<<endl;while (flag){cout<<"请选择操作:";cin>>select;switch(select){case 1:cout<<"请输入顺序表1的长度:";cin>>n;Mylist1.SeqListCreat(n);cout<<"你所输入的顺序表1为:";Mylist1.SeqListPrint();cout<<"请输入顺序表2的长度:";cin>>n;Mylist2.SeqListCreat(n);cout<<"你所输入的顺序表2为:";break;case 2:cout<<"请选择所要排序的顺序表1或2:"; cin>>n;if(n==1){Mylist1.SeqListSort(i,x);cout<<"排序后的顺序表1为:";Mylist1.SeqListPrint();}else{Mylist2.SeqListSort(i,x);cout<<"排序后的顺序表2为:";Mylist2.SeqListPrint();}break;case 3:cout<<"请输入x的值:";cin>>x;i=Mylist1.SeqListFind(x);if(i!=-1) cout<<"x的位置为:"<<i<<endl;else cout<<"没有找到!";break;case 4:cout<<"请输入要插入的元素的位置和数值x:"; cin>>i>>x;cout<<"插入后的顺序表为:";Mylist1.SeqListPrint();break;case 5:cout<<"请输入要删除的元素的位置:";cin>>i;Mylist1.SeqListDelete(i);cout<<"删除后的顺序表为:";Mylist1.SeqListPrint();break;case 6:cout<<"合并后的顺序表为:\n";Mylist1.SeqListPrint();Mylist2.SeqListPrint();break;case 7:flag=0;break;}}}三运行结果为:四、上机环境和使用语言(计算机程序实现)Visual C++。

数据结构上机实验

数据结构上机实验

//文件名:exp1-1.cpp#include <stdio.h>#include <math.h>bool prime(int n) //判断正整数n是否为素数{int i;for (i=2;i<=(int)sqrt(n);i++)if (n%i==0)return false; //若n不是素数,则退出并返回false return true;}void main(){int n,i,j=0; //j用于累计素数个数printf("n:");scanf("%d",&n);printf("小于等于%d的素数:\n",n);if (n>2){ printf("%4d",2);j++;}for (i=3;i<=n;i+=2)if (prime(i)){ printf("%4d",i);if (j!=0 && ++j%10==0) //每行最多显示10个素数printf("\n");}printf("\n");}//文件名:exp1-2.cpp#include <stdio.h>int func(int num){int s=0;do{s+=num%10;num/=10;} while(num);return(s);}{int n;printf("输入一个整数:");scanf("%d",&n);printf("各位数字之和:%d\n",func(n));printf("\n");}//文件名:exp1-3.cpp#include <stdio.h>#include <string.h>#define MAX 100 //字符串的最大长度bool func(char s[]){bool flag=true;int i,j,slen=strlen(s); //slen为字符串s的长度for (i=0,j=slen-1;i<j;i++,j--)if (s[i]!=s[j]){flag=false;break;}return(flag);}void main(){char s[MAX];printf("输入一字符串:");scanf("%s",s);if (func(s))printf("%s字符串是回文\n",s);elseprintf("%s字符串不是回文\n",s);}}实验题3//文件名:algo3-1.cpp#include <stdio.h>#include <malloc.h>#define MaxSize 100typedef char ElemType;typedef struct{ElemType data[MaxSize];int top; //栈顶指针} SqStack;void InitStack(SqStack *&s) //初始化栈S{ s=(SqStack *)malloc(sizeof(SqStack));s->top=-1; //栈顶指针置为-1 }void DestroyStack(SqStack *&s) //销毁栈s{free(s);}bool StackEmpty(SqStack *s) //判断栈空{return(s->top==-1);}bool Push(SqStack *&s,ElemType e) //进栈{ if (s->top==MaxSize-1) //栈满的情况,即栈上溢出return false;s->top++; //栈顶指针增1s->data[s->top]=e; //元素e放在栈顶指针处return true;}bool Pop(SqStack *&s,ElemType &e) //出栈{ if (s->top==-1) //栈为空的情况,即栈下溢出return false;e=s->data[s->top]; //取栈顶指针元素的元素s->top--; //栈顶指针减1return true;}bool GetTop(SqStack *s,ElemType &e) //取栈顶元素{ if (s->top==-1) //栈为空的情况,即栈下溢出return false;e=s->data[s->top]; //取栈顶指针元素的元素return true;}//文件名:algo3-2.cpp#include <stdio.h>#include <malloc.h>typedef char ElemType;typedef struct linknode{ElemType data; //数据域struct linknode *next; //指针域} LiStack;void InitStack(LiStack *&s) //初始化栈s{ s=(LiStack *)malloc(sizeof(LiStack));s->next=NULL;}void DestroyStack(LiStack *&s) //销毁栈{ LiStack *p=s,*q=s->next;while (q!=NULL){ free(p);p=q;q=p->next;}free(p); //此时p指向尾节点,释放其空间}bool StackEmpty(LiStack *s) //判断栈是否为空{return(s->next==NULL);}void Push(LiStack *&s,ElemType e) //进栈{ LiStack *p;p=(LiStack *)malloc(sizeof(LiStack));p->data=e; //新建元素e对应的节点*p p->next=s->next; //插入*p节点作为开始节点s->next=p;}bool Pop(LiStack *&s,ElemType &e) //出栈{ LiStack *p;if (s->next==NULL) //栈空的情况return false;p=s->next; //p指向开始节点e=p->data;s->next=p->next; //删除*p节点free(p); //释放*p节点return true;}bool GetTop(LiStack *s,ElemType &e) //取栈顶元素{ if (s->next==NULL) //栈空的情况return false;e=s->next->data;return true;}//文件名:algo3-3.cpp#include <stdio.h>#include <malloc.h>#define MaxSize 5typedef char ElemType;typedef struct{ElemType data[MaxSize];int front,rear; //队首和队尾指针} SqQueue;void InitQueue(SqQueue *&q) //初始化队列{ q=(SqQueue *)malloc (sizeof(SqQueue));q->front=q->rear=0;}void DestroyQueue(SqQueue *&q) //销毁队列{free(q);}bool QueueEmpty(SqQueue *q) //判断队列空{return(q->front==q->rear);}bool enQueue(SqQueue *&q,ElemType e) //进队{if ((q->rear+1)%MaxSize==q->front) //队满上溢出return false;q->rear=(q->rear+1)%MaxSize;q->data[q->rear]=e;return true;}bool deQueue(SqQueue *&q,ElemType &e) //出队{if (q->front==q->rear) //队空下溢出return false;q->front=(q->front+1)%MaxSize;e=q->data[q->front];return true;}//文件名:algo3-4.cpp#include <stdio.h>#include <malloc.h>typedef char ElemType;typedef struct qnode{ElemType data;struct qnode *next;} QNode;typedef struct{QNode *front;QNode *rear;} LiQueue;void InitQueue(LiQueue *&q) //初始化队列{ q=(LiQueue *)malloc(sizeof(LiQueue));q->front=q->rear=NULL;}void DestroyQueue(LiQueue *&q) //销毁队列{ QNode *p=q->front,*r; //p指向队头数据节点if (p!=NULL) //释放数据节点占用空间{ r=p->next;while (r!=NULL){ free(p);p=r;r=p->next;}}free(p);free(q); //释放链队节点占用空间}bool QueueEmpty(LiQueue *q) //判断队列是否为空{return(q->rear==NULL);}void enQueue(LiQueue *&q,ElemType e) //进队{ QNode *p;p=(QNode *)malloc(sizeof(QNode));p->data=e;p->next=NULL;if (q->rear==NULL) //若链队为空,则新节点是队首节点又是队尾节点q->front=q->rear=p;else{ q->rear->next=p; //将*p节点链到队尾,并将rear指向它q->rear=p;}}bool deQueue(LiQueue *&q,ElemType &e) //出队{ QNode *t;if (q->rear==NULL) //队列为空return false;t=q->front; //t指向第一个数据节点if (q->front==q->rear) //队列中只有一个节点时q->front=q->rear=NULL;else //队列中有多个节点时q->front=q->front->next;e=t->data;free(t);return true;}//文件名:exp3-1.cpp#include <stdio.h>#include <malloc.h>#define MaxSize 100typedef char ElemType;typedef struct{ElemType data[MaxSize];int top; //栈顶指针} SqStack;extern void InitStack(SqStack *&s);extern void DestroyStack(SqStack *&s);extern bool StackEmpty(SqStack *s);extern bool Push(SqStack *&s,ElemType e);extern bool Pop(SqStack *&s,ElemType &e);extern bool GetTop(SqStack *s,ElemType &e);void main(){ElemType e;SqStack *s;printf("栈s的基本运算如下:\n");printf(" (1)初始化栈s\n");InitStack(s);printf(" (2)栈为%s\n",(StackEmpty(s)?"空":"非空"));printf(" (3)依次进栈元素a,b,c,d,e\n");Push(s,'a');Push(s,'b');Push(s,'c');Push(s,'d');Push(s,'e');printf(" (4)栈为%s\n",(StackEmpty(s)?"空":"非空"));printf(" (5)出栈序列:");while (!StackEmpty(s)){Pop(s,e);printf("%c ",e);}printf("\n");printf(" (6)栈为%s\n",(StackEmpty(s)?"空":"非空"));printf(" (7)释放栈\n");DestroyStack(s);}//文件名:exp3-2.cpp#include <stdio.h>#include <malloc.h>typedef char ElemType;typedef struct linknode{ElemType data; //数据域struct linknode *next; //指针域} LiStack;extern void InitStack(LiStack *&s);extern void DestroyStack(LiStack *&s);extern bool StackEmpty(LiStack *s);extern void Push(LiStack *&s,ElemType e);extern bool Pop(LiStack *&s,ElemType &e);extern bool GetTop(LiStack *s,ElemType &e);void main(){ElemType e;LiStack *s;printf("栈s的基本运算如下:\n");printf(" (1)初始化栈s\n");InitStack(s);printf(" (2)栈为%s\n",(StackEmpty(s)?"空":"非空"));printf(" (3)依次进栈元素a,b,c,d,e\n");Push(s,'a');Push(s,'b');Push(s,'c');Push(s,'d');Push(s,'e');printf(" (4)栈为%s\n",(StackEmpty(s)?"空":"非空"));printf(" (5)出栈序列:");while (!StackEmpty(s)){Pop(s,e);printf("%c ",e);}printf("\n");printf(" (6)栈为%s\n",(StackEmpty(s)?"空":"非空"));printf(" (7)释放栈\n");DestroyStack(s);}//文件名:exp3-4.cpp#include <stdio.h>#include <malloc.h>#define MaxSize 5typedef char ElemType;typedef struct qnode{ElemType data;struct qnode *next;} QNode;typedef struct{QNode *front;QNode *rear;} LiQueue;extern void InitQueue(LiQueue *&q);extern void DestroyQueue(LiQueue *&q);extern bool QueueEmpty(LiQueue *q);extern void enQueue(LiQueue *&q,ElemType e);extern bool deQueue(LiQueue *&q,ElemType &e);void main(){ElemType e;LiQueue *q;printf("链队的基本运算如下:\n");printf(" (1)初始化链队q\n");InitQueue(q);printf(" (2)依次进链队元素a,b,c\n");enQueue(q,'a');enQueue(q,'b');enQueue(q,'c');printf(" (3)链队为%s\n",(QueueEmpty(q)?"空":"非空"));if (deQueue(q,e)==0)printf("\t提示:队空,不能出队\n");elseprintf(" (4)出队一个元素%c\n",e);printf(" (5)依次进链队元素d,e,f\n");enQueue(q,'d');enQueue(q,'e');enQueue(q,'f');printf(" (6)出链队序列:");while (!QueueEmpty(q)){ deQueue(q,e);printf("%c ",e);}printf("\n");printf(" (7)释放链队\n");DestroyQueue(q);}//文件名:exp3-5.cpp#include <stdio.h>#define M 4 //行数#define N 4 //列数#define MaxSize 100 //栈最多元素个数int mg[M+2][N+2]={ //一个迷宫,其四周要加上均为1的外框{1,1,1,1,1,1},{1,0,0,0,1,1},{1,0,1,0,0,1},{1,0,0,0,1,1},{1,1,0,0,0,1},{1,1,1,1,1,1}};struct{int i,j;int di;} Stack[MaxSize],Path[MaxSize]; //定义栈和存放最短路径的数组int top=-1; //栈顶指针int count=1; //路径数计数int minlen=MaxSize; //最短路径长度void mgpath(int xi,int yi,int xe,int ye) //求迷宫路径{int i,j,di,find,k;top++; //进栈Stack[top].i=xi;Stack[top].j=yi;Stack[top].di=-1;mg[xi][yi]=-1; //初始方块进栈while (top>-1) //栈不空时循环{i=Stack[top].i;j=Stack[top].j;di=Stack[top].di;if (i==xe && j==ye) //找到了出口,输出路径{printf("%4d: ",count++);for (k=0;k<=top;k++){printf("(%d,%d) ",Stack[k].i,Stack[k].j);if ((k+1)%5==0) printf("\n\t"); //输出时每5个方块换一行}printf("\n");if (top+1<minlen) //比较找最短路径{for (k=0;k<=top;k++)Path[k]=Stack[k];minlen=top+1;}mg[Stack[top].i][Stack[top].j]=0; //让该位置变为其他路径可走方块top--;i=Stack[top].i;j=Stack[top].j;di=Stack[top].di;}find=0;while (di<4 && find==0) //找下一个可走方块{ di++;switch(di){case 0:i=Stack[top].i-1;j=Stack[top].j;break;case 1:i=Stack[top].i;j=Stack[top].j+1;break;case 2:i=Stack[top].i+1;j=Stack[top].j;break;case 3:i=Stack[top].i,j=Stack[top].j-1;break;}if (mg[i][j]==0) find=1;}if (find==1) //找到了下一个可走方块{ Stack[top].di=di; //修改原栈顶元素的di值top++;Stack[top].i=i;Stack[top].j=j;Stack[top].di=-1;//下一个可走方块进栈mg[i][j]=-1; //避免重复走到该方块}else //没有路径可走,则退栈{mg[Stack[top].i][Stack[top].j]=0; //让该位置变为其他路径可走方块top--;}}printf("最短路径如下:\n");printf("长度: %d\n",minlen);printf("路径: ");for (k=0;k<minlen;k++){printf("(%d,%d) ",Path[k].i,Path[k].j);if ((k+1)%5==0) printf("\n\t"); //输出时每5个方块换一行}printf("\n");}void main(){printf("迷宫所有路径如下:\n");mgpath(1,1,M,N);}//文件名:exap3-6.cpp#include <stdio.h>#include <stdlib.h>#define MaxSize 100int cont=0; //记录解个数typedef struct{int col[MaxSize]; //col[i]存放第i个皇后的列号int top; //栈顶指针} StType; //定义顺序栈类型int count=0;bool place(StType st,int i,int j) //测试(i,j)是否与1~i-1皇后有冲突{int k=1;if (i==1) return true; //放第一个皇后时没有冲突while (k<=i-1) //j=1到k-1是已放置了皇后的列{if ((st.col[k]==j)|| (abs(j-st.col[k])==abs(k-i)))return false;k++;}return true;}void queen(int n) //求解n皇后问题{int i,j,k;bool find;StType st; //定义栈stst.top=0; //初始化栈顶指针st.top++; //将(1,1)进栈st.col[st.top]=1;while (st.top>0) //栈不空时循环{i=st.top; //当前皇后为第i个皇后if (st.top==n) //所有皇后均放好,输出一个解{printf(" 第%d个解:",++count);for (k=1;k<=st.top;k++)printf("(%d,%d) ",k,st.col[k]);printf("\n");}find=false;for (j=1;j<=n;j++)if (place(st,i+1,j)) //在i+1行找到一个放皇后的位置(i+1,j){st.top++;st.col[st.top]=j;find=true;break;}if (find==false) //在i+1行找不到放皇后的位置,回溯{while (st.top>0){if (st.col[st.top]==n) //本行没有可放位置,退栈st.top--;for (j=st.col[st.top]+1;j<=n;j++) //在本行找下一个位置if (place(st,st.top,j)){st.col[st.top]=j;break;}if (j>n) //当前皇后在本行没有可放的位置st.top--; //退栈else //本行找到一个位置后退出回溯break;}}}}void main()int n; //n存放实际皇后个数printf(" 皇后问题(n<20) n=");scanf("%d",&n);if (n>20)printf("n值太大,不能求解\n");else{ printf(" %d皇后问题求解如下:\n",n);queen(n);printf("\n");}}//文件名:exp3-7.cpp#include <stdio.h>#include <malloc.h>typedef struct qnode{int data;struct qnode *next;} QNode; //链队节点类型typedef struct{QNode *front,*rear;} QuType; //链队类型void Destroyqueue(QuType *&qu) //释放链队{QNode *p,*q;p=qu->front;if (p!=NULL) //若链队不空{q=p->next;while (q!=NULL) //释放队中所有的节点{free(p);p=q;q=q->next;}free(p);}free(qu); //释放链队节点}void SeeDoctor()int sel,flag=1,find,no;QuType *qu;QNode *p;qu=(QuType *)malloc(sizeof(QuType)); //创建空队qu->front=qu->rear=NULL;while (flag==1) //循环执行{printf("1:排队2:就诊3:查看排队4.不再排队,余下依次就诊5:下班请选择:");scanf("%d",&sel);switch(sel){case 1: printf(" >>输入病历号:");do{scanf("%d",&no);find=0;p=qu->front;while (p!=NULL && !find){if (p->data==no)find=1;elsep=p->next;}if (find)printf(" >>输入的病历号重复,重新输入:");} while (find==1);p=(QNode *)malloc(sizeof(QNode)); //创建节点p->data=no;p->next=NULL;if (qu->rear==NULL) //第一个病人排队qu->front=qu->rear=p;else{qu->rear->next=p;qu->rear=p; //将*p节点入队}break;case 2: if (qu->front==NULL) //队空printf(" >>没有排队的病人!\n");else //队不空{p=qu->front;printf(" >>病人%d就诊\n",p->data);if (qu->rear==p) //只有一个病人排队的情况qu->front=qu->rear=NULL;elsequ->front=p->next;free(p);}break;case 3:if (qu->front==NULL) //队空printf(" >>没有排列的病人!\n");else //队不空{p=qu->front;printf(" >>排队病人:");while (p!=NULL){printf("%d ",p->data);p=p->next;}printf("\n");}break;case 4:if (qu->front==NULL) //队空printf(" >>没有排列的病人!\n");else //队不空{p=qu->front;printf(" >>病人按以下顺序就诊:");while (p!=NULL){printf("%d ",p->data);p=p->next;}printf("\n");}Destroyqueue(qu); //释放链队flag=0; //退出break;case 5:if (qu->front!=NULL) //队不空printf(" >>请排队的病人明天就医!\n");flag=0; //退出Destroyqueue(qu); //释放链队break;}}}void main(){SeeDoctor();}//文件名:exp3-8.cpp#include <stdio.h>#include <malloc.h>#define N 3 //停车场内最多的停车数#define M 4 //候车场内最多的停车数#define Price 2 //每单位停车费用typedef struct{int CarNo[N]; //车牌号int CarTime[N]; //进场时间int top; //栈指针} SqStack; //定义顺序栈类型typedef struct{int CarNo[M]; //车牌号int front,rear; //队首和队尾指针} SqQueue; //定义循环队类型//以下为顺序栈的基本运算算法void InitStack(SqStack *&s){s=(SqStack *)malloc(sizeof(SqStack));s->top=-1;}bool StackEmpty(SqStack *s){return(s->top==-1);}bool StackFull(SqStack *s){return(s->top==N-1);}bool Push(SqStack *&s,int e1,int e2){if (s->top==N-1)return false;s->top++;s->CarNo[s->top]=e1;s->CarTime[s->top]=e2;return true;}bool Pop(SqStack *&s,int &e1,int &e2){if (s->top==-1)return false;e1=s->CarNo[s->top];e2=s->CarTime[s->top];s->top--;return true;}void DispStack(SqStack *s){int i;for (i=s->top;i>=0;i--)printf("%d ",s->CarNo[i]);printf("\n");}//以下为循环队列的基本运算算法void InitQueue(SqQueue *&q){q=(SqQueue *)malloc (sizeof(SqQueue));q->front=q->rear=0;}bool QueueEmpty(SqQueue *q){return(q->front==q->rear);}bool QueueFull(SqQueue *q) //判断队满{return ((q->rear+1)%M==q->front);}bool enQueue(SqQueue *&q,int e) //进队{if ((q->rear+1)%M==q->front) //队满return false;q->rear=(q->rear+1)%M;q->CarNo[q->rear]=e;return true;}bool deQueue(SqQueue *&q,int &e) //出队{if (q->front==q->rear) //队空的情况return false;q->front=(q->front+1)%M;e=q->CarNo[q->front];return true;}void DispQueue(SqQueue *q) //输出队中元素{int i;i=(q->front+1)%M;printf("%d ",q->CarNo[i]);while ((q->rear-i+M)%M>0){i=(i+1)%M;printf("%d ",q->CarNo[i]);}printf("\n");}void main(){int comm;int no,e1,time,e2;int i,j;SqStack *St,*St1;SqQueue *Qu;InitStack(St);InitStack(St1);InitQueue(Qu);do{printf("输入指令(1:到达2:离开3:停车场4:候车场0:退出):");scanf("%d",&comm);switch(comm){case 1: //汽车到达printf(" 车号到达时间:");scanf("%d%d",&no,&time);if (!StackFull(St)) //停车场不满{Push(St,no,time);printf(" >>停车场位置:%d\n",St->top+1);}else //停车场满{if (!QueueFull(Qu)) //候车场不满{enQueue(Qu,no);printf(" >>候车场位置:%d\n",Qu->rear);}elseprintf(" >>候车场已满,不能停车\n");}break;case 2: //汽车离开printf(" 车号离开时间:");scanf("%d%d",&no,&time);for (i=0;i<=St->top && St->CarNo[i]!=no;i++);if (i>St->top)printf(" >>未找到该编号的汽车\n");else{for (j=i;j<=St->top;j++){Pop(St,e1,e2);Push(St1,e1,e2); //倒车到临时栈St1中}Pop(St,e1,e2); //该汽车离开printf(" >>%d汽车停车费用:%d\n",no,(time-e2)*Price);while (!StackEmpty(St1))//将临时栈St1重新回到St中{Pop(St1,e1,e2);Push(St,e1,e2);}if (!QueueEmpty(Qu)) //队不空时,将队头进栈St{deQueue(Qu,e1);Push(St,e1,time); //以当前时间开始计费}}break;case 3: //显示停车场情况if (!StackEmpty(St)){printf(" >>停车场中的车辆:"); //输出停车场中的车辆DispStack(St);}elseprintf(" >>停车场中无车辆\n");break;case 4: //显示候车场情况if (!QueueEmpty(Qu)){printf(" >>候车场中的车辆:"); //输出候车场中的车辆DispQueue(Qu);}elseprintf(" >>候车场中无车辆\n");break;case 0: //结束if (!StackEmpty(St)){printf(" >>停车场中的车辆:"); //输出停车场中的车辆DispStack(St);}if (!QueueEmpty(Qu)){printf(" >>候车场中的车辆:"); //输出候车场中的车辆DispQueue(Qu);}break;default: //其他情况printf(" >>输入的命令错误\n");break;}} while(comm!=0);}//文件名:algo7-1.cpp#include <stdio.h>#include <malloc.h>#define MaxSize 100typedef char ElemType;typedef struct node{ElemType data; //数据元素struct node *lchild; //指向左孩子struct node *rchild; //指向右孩子} BTNode;void CreateBTNode(BTNode *&b,char *str) //由str串创建二叉链{BTNode *St[MaxSize],*p=NULL;int top=-1,k,j=0;char ch;b=NULL; //建立的二叉树初始时为空ch=str[j];while (ch!='\0') //str未扫描完时循环{switch(ch){case '(':top++;St[top]=p;k=1; break; //为左节点case ')':top--;break;case ',':k=2; break; //为右节点default:p=(BTNode *)malloc(sizeof(BTNode));p->data=ch;p->lchild=p->rchild=NULL;if (b==NULL) //p指向二叉树的根节点b=p;else //已建立二叉树根节点{switch(k){case 1:St[top]->lchild=p;break;case 2:St[top]->rchild=p;break;}}}j++;ch=str[j];}}BTNode *FindNode(BTNode *b,ElemType x) //返回data域为x的节点指针{BTNode *p;if (b==NULL)return NULL;else if (b->data==x)return b;else{p=FindNode(b->lchild,x);if (p!=NULL)return p;elsereturn FindNode(b->rchild,x);}}BTNode *LchildNode(BTNode *p) //返回*p节点的左孩子节点指针{return p->lchild;}BTNode *RchildNode(BTNode *p) //返回*p节点的右孩子节点指针{return p->rchild;}int BTNodeDepth(BTNode *b) //求二叉树b的深度{int lchilddep,rchilddep;if (b==NULL)return(0); //空树的高度为0 else{lchilddep=BTNodeDepth(b->lchild); //求左子树的高度为lchilddeprchilddep=BTNodeDepth(b->rchild); //求右子树的高度为rchilddepreturn (lchilddep>rchilddep)? (lchilddep+1):(rchilddep+1);}}void DispBTNode(BTNode *b) //以括号表示法输出二叉树{if (b!=NULL){printf("%c",b->data);if (b->lchild!=NULL || b->rchild!=NULL){printf("(");DispBTNode(b->lchild);if (b->rchild!=NULL) printf(",");DispBTNode(b->rchild);printf(")");}}}int BTWidth(BTNode *b) //求二叉树b的宽度{struct{int lno; //节点的层次编号BTNode *p; //节点指针} Qu[MaxSize]; //定义顺序非循环队列int front,rear; //定义队首和队尾指针int lnum,max,i,n;front=rear=0; //置队列为空队if (b!=NULL){rear++;Qu[rear].p=b; //根节点指针入队Qu[rear].lno=1; //根节点的层次编号为1while (rear!=front) //队列不为空{front++;b=Qu[front].p; //队头出队lnum=Qu[front].lno;if (b->lchild!=NULL) //左孩子入队{rear++;Qu[rear].p=b->lchild;Qu[rear].lno=lnum+1;}if (b->rchild!=NULL) //右孩子入队{rear++;Qu[rear].p=b->rchild;Qu[rear].lno=lnum+1;}}max=0;lnum=1;i=1;while (i<=rear){n=0;while (i<=rear && Qu[i].lno==lnum){n++;i++;}lnum=Qu[i].lno;if (n>max) max=n;}return max;}elsereturn 0;}int Nodes(BTNode *b) //求二叉树b的节点个数{int num1,num2;if (b==NULL)return 0;else if (b->lchild==NULL && b->rchild==NULL)return 1;else{num1=Nodes(b->lchild);num2=Nodes(b->rchild);return (num1+num2+1);}}int LeafNodes(BTNode *b) //求二叉树b的叶子节点个数{int num1,num2;if (b==NULL)return 0;else if (b->lchild==NULL && b->rchild==NULL)return 1;else{num1=LeafNodes(b->lchild);num2=LeafNodes(b->rchild);return (num1+num2);}}void DestroyBTNode(BTNode *&b){if (b!=NULL){DestroyBTNode(b->lchild);DestroyBTNode(b->rchild);free(b);}}//文件名:exp7-1.cpp#include <stdio.h>typedef char ElemType;typedef struct node{ElemType data; //数据元素struct node *lchild; //指向左孩子struct node *rchild; //指向右孩子} BTNode;extern void CreateBTNode(BTNode *&b,char *str);extern BTNode *FindNode(BTNode *b,ElemType x);extern BTNode *LchildNode(BTNode *p);extern BTNode *RchildNode(BTNode *p);extern int BTNodeDepth(BTNode *b);extern void DispBTNode(BTNode *b);extern int BTWidth(BTNode *b);extern int Nodes(BTNode *b);extern int LeafNodes(BTNode *b);extern void DestroyBTNode(BTNode *&b);void main(){ BTNode *b,*p,*lp,*rp;;CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");printf("二叉树的基本运算如下:\n");printf(" (1)输出二叉树:");DispBTNode(b);printf("\n");printf(" (2)H节点:");p=FindNode(b,'H');if (p!=NULL){ lp=LchildNode(p);if (lp!=NULL)printf("左孩子为%c ",lp->data);elseprintf("无左孩子");rp=RchildNode(p);if (rp!=NULL)printf("右孩子为%c",rp->data);elseprintf("无右孩子");}printf("\n");printf(" (3)二叉树b的深度:%d\n",BTNodeDepth(b));printf(" (4)二叉树b的宽度:%d\n",BTWidth(b));printf(" (5)二叉树b的节点个数:%d\n",Nodes(b));printf(" (6)二叉树b的叶子节点个数:%d\n",LeafNodes(b));printf(" (7)释放二叉树b\n");DestroyBTNode(b);}//文件名:exp7-2.cpp#include <stdio.h>#include <malloc.h>#define MaxSize 100typedef char ElemType;typedef struct node{ElemType data; //数据元素struct node *lchild; //指向左孩子struct node *rchild; //指向右孩子} BTNode;extern void CreateBTNode(BTNode *&b,char *str);extern void DispBTNode(BTNode *b);extern void DestroyBTNode(BTNode *&b);void PreOrder(BTNode *b) //先序遍历的递归算法{if (b!=NULL){printf("%c ",b->data); //访问根节点PreOrder(b->lchild); //递归访问左子树PreOrder(b->rchild); //递归访问右子树}}void PreOrder1(BTNode *b){BTNode *St[MaxSize],*p;int top=-1;if (b!=NULL){top++; //根节点入栈St[top]=b;while (top>-1) //栈不为空时循环{p=St[top]; //退栈并访问该节点top--;printf("%c ",p->data);if (p->rchild!=NULL) //右孩子入栈{top++;St[top]=p->rchild;}if (p->lchild!=NULL) //左孩子入栈{top++;St[top]=p->lchild;}}printf("\n");}}void InOrder(BTNode *b) //中序遍历的递归算法{if (b!=NULL){InOrder(b->lchild); //递归访问左子树printf("%c ",b->data); //访问根节点InOrder(b->rchild); //递归访问右子树}}void InOrder1(BTNode *b){BTNode *St[MaxSize],*p;int top=-1;if (b!=NULL){p=b;while (top>-1 || p!=NULL){while (p!=NULL){top++;St[top]=p;p=p->lchild;}if (top>-1){p=St[top];top--;printf("%c ",p->data);p=p->rchild;}}printf("\n");}}void PostOrder(BTNode *b) //后序遍历的递归算法{if (b!=NULL){PostOrder(b->lchild); //递归访问左子树PostOrder(b->rchild); //递归访问右子树printf("%c ",b->data); //访问根节点}}void PostOrder1(BTNode *b){BTNode *St[MaxSize];BTNode *p;int flag,top=-1; //栈指针置初值if (b!=NULL){do{while (b!=NULL) //将t的所有左节点入栈{top++;St[top]=b;b=b->lchild;}p=NULL; //p指向当前节点的前一个已访问的节点flag=1;while (top!=-1 && flag){b=St[top]; //取出当前的栈顶元素if (b->rchild==p) //右子树不存在或已被访问,访问之{printf("%c ",b->data); //访问*b节点top--;p=b; //p指向则被访问的节点}else{b=b->rchild; //t指向右子树flag=0;}}} while (top!=-1);printf("\n");}}void TravLevel(BTNode *b){BTNode *Qu[MaxSize]; //定义循环队列int front,rear; //定义队首和队尾指针front=rear=0; //置队列为空队列if (b!=NULL)printf("%c ",b->data);rear++; //节点指针进入队列Qu[rear]=b;while (rear!=front) //队列不为空{front=(front+1)%MaxSize;b=Qu[front]; //队头出队列if (b->lchild!=NULL) //输出左孩子,并入队列{printf("%c ",b->lchild->data);rear=(rear+1)%MaxSize;Qu[rear]=b->lchild;}if (b->rchild!=NULL) //输出右孩子,并入队列{printf("%c ",b->rchild->data);rear=(rear+1)%MaxSize;Qu[rear]=b->rchild;}}printf("\n");}void main(){BTNode *b;CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");printf("二叉树b:");DispBTNode(b);printf("\n");printf("层次遍历序列:");TravLevel(b);printf("先序遍历序列:\n");printf(" 递归算法:");PreOrder(b);printf("\n");printf(" 非递归算法:");PreOrder1(b);printf("中序遍历序列:\n");printf(" 递归算法:");InOrder(b);printf("\n");printf(" 非递归算法:");InOrder1(b);printf("后序遍历序列:\n");printf(" 递归算法:");PostOrder(b);printf("\n");printf(" 非递归算法:");PostOrder1(b);DestroyBTNode(b);}//文件名:exp7-3.cpp#include <stdio.h>#include <malloc.h>#define MaxSize 100typedef char ElemType;typedef struct node{。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

数据结构第一、二次上机:#include <stdio.h> #include <malloc.h> #include <stdlib.h>#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0#define INFEASIBLE -1 #define OVERFLOW -2#define list_init_size 100 //线性表存储空间的初始分配量#define LISTINCREMENT 10 //线性表存储空间的分配增量typedef int Status; typedef intElemType;typedef struct{ ElemType *elem; //存储空间基址int length; //当前长度intlistsize; //当前分配的存储容量(以sizeof(ElemType)为单位)}SqList;Status InitList_Sq(SqList&L){ //构造一个空的线性表L L.elem =(ElemType * )malloc(list_init_size*sizeof(ElemType)); if(!L.elem )exit(OVERFLOW);//存储分配失败L.length =0; //空表长度为0 L.listsize =list_init_size;//初始存储容量return OK; }//Initlist_SqStatus ListInsert_Sq(SqList&L,inti,ElemType e){ //在顺序线性表L中第i个位置之前插入新的元素e,//i的合法值为1<=i<=ListLength_Sq(L)+1 ElemType *p,*q,*newbase; //定义指针if(i<1||i>L.length +1) return ERROR; //i值不合法if(L.length>=L.listsize ){ //当前存储空间已满,增加分配newbase=(ElemType * )realloc(L.elem ,(L.listsize+LISTINCREMENT)*sizeof(ElemType));if(!newbase)exit(OVERFLOW); //存储分配失败L.elem =newbase; //新基址L.listsize +=LISTINCREMENT; //增加存储容量} q=&(L.elem [i-1]); //q为插入位置for(p=&(L.elem [L.length -1]);p>=q;--p) *(p+1)=*p; //插入位置及之后的元素右移*q=e; //插入e ++L.length //表长增1 return OK; }//ListInsert_SqStatusListDelete_Sq(SqList&L,inti,ElemType&e){ //在顺序线性表L中删除第i个元素,并用e返回其值//i 的合法值为1<=i<=ListLength_Sq(L) ElemType *p,*q; //定义指针if((i<1) || (i>L.length )) return ERROR; //i值不合法p=&(L.elem [i-1]); //p为被删除元素的位置e=*p; //被删除元素的值赋给 e q=L.elem +L.length -1; //表尾元素的位置for(++p;p<=q;++p) *(p-1)=*p; //被删除元素之后的元素左移--L.length //表长减1 return OK; }//ListDelete_sqvoid display(SqList L) { //定义for循环函数inti; for(i=0;i<=L.length -1;i++) printf("%d\n",L.elem [i]); }intLocateElem_Sq(SqListL,ElemType e) { //在顺序线性表L中查找第1个值与e满足compare()的元素的位序//若找到,则返回其在L中的位序,否则返回0 ElemType *p; inti=1; //i的初值为第一个元素的位序p=L.elem //p的初值为第一个元素的存储位置while(i<=L.length&& *p++!=e) ++i;if(i<=L.length) return i; else return 0; }//LocateElem_SqvoidMergeList_Sq(SqListLa,SqListLb,SqList&Lc ){ //已知顺序线性表La和Lb的元素按值非递减排列//归并La和Lb得到新的顺序线性表Lc,Lc的元素也按非递减排列ElemType *pa,*pb,*pc,*pa_last,*pb_last;pa=La.elempb=Lb.elemLc.listsize=Lc.length=La.length +Lb.length pc=Lc.elem =(ElemType *)malloc(Lc.listsize *sizeof(ElemType)); if(!Lc.elem )exit(OVERFLOW); //存储分配失败pa_last=La.elem +La.length -1; pb_last=Lb.elem +Lb.length -1; while(pa<=pa_last&&pb<=pb_last){ //归并if(*pa<=*pb) *pc++=*pa++; else *pc++=*pb++; } while(pa<=pa_last) *pc++=*pa++; //插入La的剩余元素while(pb<=pb_last) *pc++=*pb++; //插入Lb的剩余元素}//MergeList_Sqvoid main() { /* SqList L;//定义线性表InitList_Sq(L);//调用空表//插入数据ListInsert_Sq(L,1,10); ListInsert_Sq(L,2,20); ListInsert_Sq(L,1,30); ListInsert_Sq(L,3,40); printf("插入后:\n"); display(L);//调用循环函数ListInsert_Sq(L,3,100);//在L表第三个位置插入100 printf("插入后:\n"); display(L);ElemType e;//定义e ListDelete_Sq(L,3,e);//删除L 表的第三个元素,用e表示printf("删除后:\n"); display(L); printf("被删除元素:%d\n\n\n\n",e); */SqListLa,Lb,Lc; InitList_Sq(La); ListInsert_Sq(La,1,3); ListInsert_Sq(La,2,5); ListInsert_Sq(La,3,8); ListInsert_Sq(La,4,11); printf("La插入后:\n"); display(La); InitList_Sq(Lb); ListInsert_Sq(Lb,1,2); ListInsert_Sq(Lb,2,6); ListInsert_Sq(Lb,3,8); ListInsert_Sq(Lb,4,9); ListInsert_Sq(Lb,5,11); ListInsert_Sq(Lb,6,15); ListInsert_Sq(Lb,7,20); printf("Lb插入后:\n"); display(Lb); MergeList_Sq(La,Lb,Lc); printf("归并后:\n"); display(Lc); printf("\n");int a=LocateElem_Sq( Lc, 5); printf("%d\n",a); }第三次上机:#include <stdio.h> #include <malloc.h>#define TRUE 1#define FALSE 0 #define OK 1 #define ERROR 0#define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef intElemType;typedef struct LNode{ ElemType data; struct LNode *next; }LNOde,*LinkList;Status GetElem_L(LinkListL,inti,ElemType&e) { //L 为带头结点的单链表的头指针//当第i个元素存在时,其值赋给e并返回OK,否则返回ERROR LinkList p; p=L->next; int j=1; //初始化,p指向第一个结点,j为计数器while(p&&j<i) { //顺指针向后查找,直到p指向第i个元素或p为空p=p->next; ++j; } if(!p||j>i) return ERROR; //第i个元素不存在e=p->data; //取第i个元素return OK; }//GetElem_LStatus ListInsert_L(LinkList&L,inti,ElemType e) { //在带头结点的单链线性表L中第i个位置之前插入元素e LinkListp,s; p=L; int j=0; while(p&&j<i-1) { p=p->next; ++j; } //寻找第i-1个结点if(!p||j>i-1) return ERROR; //i小于或者大于表长+1 s=(LinkList)malloc(sizeof(LNode)); //生成新结点s->data=e; s->next=p->next; //插入L中p->next=s; return OK; }//ListInsert_L Status ListDelete_L(LinkList&L,inti,ElemType&e) { //在带头结点的单链线性表L中,删除第i个元素,并由e返回其值LinkListp,q; p=L; int j=0; while(p->next&&j<i-1) { //寻找第i个结点,并令p 指向其前趋p=p->next;++j; } if(!(p->next)||j>i-1) return ERROR; //删除位置不合理q=p->next; p->next=q->next; //删除并释放结点e=q->data; free(q); return OK; }//ListDelete_Lvoid CreateList_L(LinkList&L,int n) { //逆位序输入n 个元素的值,建立带表头结点的单链线性表L LinkList p; L=(LinkList)malloc(sizeof(LNode)); L->next =NULL; //先建立一个带头结点的单链表for(inti=n;i>0;--i){ p=(LinkList)malloc(sizeof(LNode)); //生成新结点scanf("%d",&p->data); //输入元素值p->next=L->next L->next =p; //插入到表头}}//CreateList_Lvoid display(LinkList L) { LinkList p=L->next; //定义for循环函数while(p) { printf("%d,",p->data); p=p->next; } printf("\n"); }void main() {LinkList L;CreateList_L(L,3); display(L);ListInsert_L(L,2,100); display(L);ElemType e;ListDelete_L(L,2,e); display(L);printf("被删除的值=%d\n",e);GetElem_L(L,3,e);printf("获取的值=%d\n",e); }第四次上机#include <stdio.h> #include <malloc.h> #include <stdlib.h>#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0#define INFEASIBLE -1 #define OVERFLOW -2typedef intSElemType; typedef int Status;#define STACK_INIT_SIZE 100 //存储空间初始分配量#define STRCKINCREMENT 10 //存储空间分配增量typedef struct{SElemType *base; //在栈构造之前和销毁之后,base的值为NULL SElemType *top; //栈顶指针intstacksize; //当前已分配的存储空间,以元素为单位}SqStack;Status InitStack(SqStack&S){ //构造一个空栈S S.base =(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); if(!S.base )exit(OVERFLOW); //存储分配失败S.top =S.baseS.stacksize =STACK_INIT_SIZE; return OK; }//InitStack Status GetTop(SqStackS,SElemType&e){ //若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR if(S.top ==S.base ) return ERROR; e=*(S.top -1); return OK; }//GetTopStatus Push(SqStack&S,SElemType e){ //插入元素e为新的栈顶元素if(S.top - S.base>=S.stacksize ){ //栈满,追加存储空间S.base =(SElemType * )realloc(S.base ,(S.stacksize +STRCKINCREMENT) * sizeof(SElemType)); if(!S.base )exit(OVERFLOW); //存储分配失败S.top =S.base +S.stacksizeS.stacksize +=STRCKINCREMENT;}*S.top ++=e; return OK; }//PushStatus Pop(SqStack&S,SElemType&e){//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR if(S.top ==S.base )return ERROR; e=*--S.top return OK; }//PopStatus StackEmpty(SqStack S){ if(S.top==S.base) return TRUE; else return ERROR; }void conversion(){//对于输入的任意一个非负十进制整数,打印输出与其等值的八进制数SqStack S; int N; SElemType e; InitStack(S); //构造空栈scanf("%d",&N); while(N){ Push(S,N % 8); N=N/8; } printf("转换成八进制后的数为:"); while(!StackEmpty(S)){ Pop(S,e); printf("%d",e); } printf("\n"); }//conversionvoid main() { SqStack S; SElemTypee,x; InitStack(S); Push(S,5); Push(S,4); Push(S,3);Push(S,2); Push(S,1); GetTop(S,e); printf("栈顶元素为%d\n",e); printf("\n"); Pop(S,x); printf("删除的栈顶元素为%d\n",x); printf("\n"); printf("输入一个十进制数:"); conversion(); }第五次上机/*队列的链式存储*/ #include <stdio.h> #include <malloc.h> #include <stdlib.h>#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0#define INFEASIBLE -1 #define OVERFLOW -2 typedef intQElemType; typedef int Status;typedef struct QNode{ QElemType data; struct QNode *next; }QNode,*QueuePtr;typedef struct{ QueuePtr front; //队头指针InitStack(S); Push(S,5); Push(S,4); Push(S,3);Push(S,2); Push(S,1); GetTop(S,e); printf("栈顶元素为%d\n",e); printf("\n"); Pop(S,x); printf("删除的栈顶元素为%d\n",x); printf("\n"); printf("输入一个十进制数:"); conversion(); }第五次上机/*队列的链式存储*/ #include <stdio.h> #include <malloc.h> #include <stdlib.h>#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0#define INFEASIBLE -1 #define OVERFLOW -2 typedef intQElemType; typedef int Status;typedef struct QNode{ QElemType data; struct QNode *next; }QNode,*QueuePtr;typedef struct{ QueuePtr front; //队头指针free(p); return OK; }void disp(LinkQueue Q) { QueuePtr p; p=Q.front->next; //定义for 循环函数while(p) {printf("%d ",p->data); p=p->next; }printf("\n"); }void main() { LinkQueue Q; QElemType e; InitQueue(Q); printf("插入的元素为:\n"); EnQueue(Q,25); EnQueue(Q,5); EnQueue(Q,12); EnQueue(Q,60); EnQueue(Q,33); disp(Q); printf("删除队头元素后:\n"); DeQueue(Q,e); disp(Q); DestroyQueue(Q); if(DestroyQueue(Q)==1) printf("销毁队列成功!\n"); else printf("销毁队列失败!\n"); }附加:/*队列的顺序存储*/#define MAXQSIZE 100 //最大队列长度typedef struct{ QElemType *base //初始化的动态分配存储空间int front; //头指针,若队列不空,指向队列头元素int rear; //尾指针,若队列不空,指向队列尾元素的下一个位置}SqQueue;Status InitQueue(SqQueue&Q){ //构造一个空队列Q.base =(QElemType *)malloc(MAXQSIZE * sizeof(QElemType)); if(!Q.base )exit(OVERFLOW); //存储分配失败Q.front =Q.rear =0; return OK; } intQueueLenth(SqQueue Q){ //返回Q的元素个数,即队列的长度return(Q.rear -Q.front +MAXQSIZE)%MAXQSIZE; }Status EnQueue(SqQueue&Q,QElemType e){ //插入元素e为Q的新的队尾元素if((Q.rear +1) % MAXQSIZE ==Q.front) return ERROR; //队列满Q.base [Q.rear ]=e; Q.rear =(Q.rear +1) % MAXQSIZE; return OK; }Status DeQueue(SqQueue&Q,QElemType&e){ //若队列不为空,则删除Q的队头元素,用e返回其值,并返回OK;//否则返回ERROR if(Q.front ==Q.rear ) return ERROR; e=Q.base [Q.front ]; Q.front =(Q.front +1)% MAXQSIZE; return OK; } 第六次上机#include <stdio.h> #include <malloc.h> #include <stdlib.h>#define TRUE 1 #define FALSE 0#define OK 1 #define ERROR 0#define INFEASIBLE -1 #define OVERFLOW -2typedef int Status;typedef char TElemType;//二叉树的二叉链表存储表示typedef struct BiTNode{ TElemType data; struct BiTNode *lchild,*rchild; //左右孩子指针}BiTNode,*BiTree;Status CreateBiTree(BiTree&T){ //按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树,//构造二叉树链表表示的二叉树T char ch; scanf("%c",&ch); if(ch==' ') T=NULL; else{ if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) exit(OVERFLOW); T->data =ch; //生成根结点CreateBiTree(T->lchild ); //构造左子树CreateBiTree(T->rchild ); //构造右子树} return OK; }//CreateBiTreevoid PreOrderTraverse(BiTree T){ //先序遍历if(T){ printf("%c ",T->data ); //输出结点PreOrderTraverse(T->lchild ); PreOrderTraverse(T->rchild ); } }void InOrderTraverse(BiTree T){ //中序遍历if(T){ PreOrderTraverse(T->lchild );printf("%c ",T->data ); PreOrderTraverse(T->rchild ); } }void PostOrderTraverse(BiTree T){ //后序遍历if(T){ PreOrderTraverse(T->lchild ); PreOrderTraverse(T->rchild ); printf("%c ",T->data ); } }void main() {BiTree T;CreateBiTree(T);printf("\n PreOrder: \n "); PreOrderTraverse(T); }。

相关文档
最新文档