级本科数据结构实验指导书.docx
《级本科数据结构实验指导书.docx》由会员分享,可在线阅读,更多相关《级本科数据结构实验指导书.docx(26页珍藏版)》请在冰豆网上搜索。
级本科数据结构实验指导书
《数据结构与算法》实验指导书
实验一线性表的实验
一、实验目的
1、掌握用VisualC++6.0上机调试顺序表的基本方法。
2、掌握顺序表的基本操作,插入、删除、查找、以及有序顺序表的合并等算法的实现。
3、掌握用VisualC++6.0上机调试单链表的基本方法。
4、掌握单链表的插入、删除、查找、求表长以及有序单链表的合并算法的实现。
5、进一步掌握循环单链表和双链表的插入、删除、查找算法的实现。
二、实验内容
(一)完成下列程序,该程序生成一个如表1所示的顺序表,并在第2个位置插入如表2所示的数据元素,删除在第3个位置的数据元素,显示顺序表的每个元素。
要求生成顺序表时,从键盘上读取数据元素,用顺序存储结构实现存储。
表1
学号
姓名
性别
年龄
2013001
张珊
女
19
2013002
李思
2013004
王强
男
20
2013005
赵括
21
2013006
刘刚
表2
2013003
陈琪
typedefstruct
{DataTypelist[MaxSize];
intsize;
}SeqList;
voidListInitiate(SeqList*L)
{L->size=0;/*定义初始数据元素个数*/
}
intListLength(SeqListL)
{returnL.size;
intListInsert(SeqList*L,inti,DataTypex)
{intj;
for(j=L->size;j>i;j--)L->list[j]=L->list[j-1];L->list[i]=x;/*插入x*/
L->size++;/*元素个数加1*/
return1;
intListDelete(SeqList*L,inti,DataType*x)
*x=L->list[i];/*保存删除的元素到x中*/
for(j=i+1;j<=L->size-1;j++)L->list[j-1]=L->list[j];
L->size--;/*数据元素个数减1*/
intListGet(SeqListL,inti,DataType*x)
{if(i<0||i>L.size-1)
{printf("参数i不合法!
\n");
return0;
else
{
*x=L.list[i];return1;
(二)已知顺序表la和lb中的数据元素按非递减有序排列,将la和lb表中的数据元素,合并成为一个新的顺序表lc,要求lc中的数据元素仍按非递减有序排列,并且不破坏la和lb表。
(三)完成下列程序,该程序构建如表3所示的带头结点的单链表h,在单链表h中第3个数据元素之前插入如表4所示的数据元素,删除第4个数据元素。
要求生成单链表时,从键盘上读取数据元素,用链式存储结构实现存储。
表3
员工编号
职位
001
文员
002
销售员
004
经理
005
秘书
006
表4
typedefstructNode
{DataTypedata;structNode*next;
}SLNode;
voidListInitiate(SLNode**head)
{*head=(SLNode*)malloc(sizeof(SLNode));
(*head)->next=NULL;
intListLength(SLNode*head)
SLNode*p=head;
intsize=0;
while(p->next!
=NULL)
{p=p->next;
size++;
returnsize;
intListInsert(SLNode*head,inti,DataTypex)
{SLNode*p,*q;intj;
p=head;j=-1;
=NULL&&j{p=p->next;j++;}if(j!=i-1){printf(“插入位置参数错!”);return0;}q=(SLNode*)malloc(sizeof(SLNode));q->data=x;q->next=p->next;p->next=q;return1;}intListDelete(SLNode*head,inti,DataType*x){SLNode*p,*s;intj;p=head;j=-1;while(p->next!=NULL&&p->next->next!=NULL&&j{p=p->next;j++;}if(j!=i-1){printf(“插入位置参数错!”);return0;}s=p->next;*x=s->data;p->next=p->next->next;free(s);return1;}intListGet(SLNode*head,inti,DataType*x){SLNode*p;intj;p=head;j=-1;while(p->next!=NULL&&j{p=p->next;j++;}if(j!=i){printf(“取元素位置参数错!”);return0;}*x=p->data;return1;}voidDestroy(SLNode**head){SLNode*p,*p1;p=*head;while(p!=NULL){p1=p;p=p->next;free(p1);}*head=NULL;}(四)已知单链表la和lb中的数据元素按非递减有序排列,将la和lb中的数据元素,合并为一个新的单链表lc,lc中的数据元素仍按非递减有序排列。要求不破坏la表和lb表的结构。(五)约瑟夫环程序:设有N个人围坐一圈,现从某个人开始报数,数到M的人出列,接着从出列的下一个人开始重新报数,数到M的人以出列,如此下去,直到所有人都出列为此。试设计确定他们的出列次序序列的程序。要求选择单向循环链表作为存储结构模拟整个过程,并依次输出列的各人的编号。如n=8,m=4时,若从第一个人,设每个人的编号依次为1,2,3,…开始报数,则得到的出列次序为4,8,5,2,1,3,7,6,实验二栈、队列的实现及应用一、实验目的1、掌握栈和队列的顺序存储结构和链式存储结构,以便在实际背景下灵活运用。2、掌握栈和队列的特点,即先进后出与先进先出的原则。3、掌握栈和队列的基本操作实现方法。二、实验内容(一)完成下列程序,该程序实现栈的顺序存储结构,构建顺序栈(栈中的元素依次为R,S,Y,F,C,T),依次进行进栈和出栈操作,判断栈空和栈满操作,返回栈顶元素操作。要求生成顺序栈时,从键盘上读取数据元素。typedefstruct{DataTypestack[MaxStackSize];inttop;}SeqStack;voidStackInitiate(SeqStack*S){S->top=0;}intStackNotEmpty(SeqStackS){if(S.top<=0)return0;elsereturn1;}intStackPush(SeqStack*S,DataTypex){if(S->top>=MaxStackSize){printf("堆栈已满无法插入!\n");return0;}else{S->stack[S->top]=x;S->top++;return1;}}intStackPop(SeqStack*S,DataType*d){if(S->top<=0){printf("堆栈已空无数据元素出栈!\n");return0;}else{S->top--;*d=S->stack[S->top];return1;}}intStackTop(SeqStackS,DataType*d){if(S.top<=0){printf("堆栈已空!\n");return0;}else{*d=S.stack[S.top-1];return1;}}(二)完成下列程序,该程序实现栈的链式存储结构,构建链栈(栈中的元素依次为China,Japan,France,India,Australia),依次进行进栈和出栈操作,判断栈空和栈满操作,返回栈顶元素操作。要求生成链栈时,从键盘上读取数据元素。typedefstructsnode{DataTypedata;structsnode*next;}LSNode;voidStackInitiate(LSNode**head){*head=(LSNode*)malloc(sizeof(LSNode));(*head)->next=NULL;}intStackNotEmpty(LSNode*head){if(head->next==NULL)return0;elsereturn1;}intStackPush(LSNode*head,DataTypex){LSNode*p;p=(LSNode*)malloc(sizeof(LSNode));p->data=x;p->next=head->next;head->next=p;return1;}intStackPop(LSNode*head,DataType*d){LSNode*p=head->next;if(p==NULL){printf("堆栈已空出错!");return0;}head->next=p->next;*d=p->data;free(p);return1;}intStackTop(LSNode*head,DataType*d){LSNode*p=head->next;if(p==NULL){printf("堆栈已空出错!");return0;}*d=p->data;return1;}voidDestroy(LSNode*head){LSNode*p,*p1;p=head;while(p!=NULL){p1=p;p=p->next;free(p1);}}(三)利用顺序栈实现数制转换,输入十进制整数,分别将其转换为八进制数和二进制数。(四)完成下列程序,该程序实现循环队列的存储和基本操作,构建循环队列(队列中的元素依次为John,Mary,Linda,Mike,Paul),依次进行判断队列是否为空和满操作、入队和出队操作、取得队首元素操作。typedefstruct{DataTypequeue[MaxQueueSize];intrear;intfront;intcount;}SeqCQueue;voidQueueInitiate(SeqCQueue*Q){Q->rear=0;Q->front=0;Q->count=0;}intQueueNotEmpty(SeqCQueueQ){if(Q.count!=0)return1;elsereturn0;}intQueueAppend(SeqCQueue*Q,DataTypex){if(Q->count>0&&Q->rear==Q->front){printf("队列已满无法插入!\n");return0;}else{Q->queue[Q->rear]=x;Q->rear=(Q->rear+1)%MaxQueueSize;Q->count++;return1;}}intQueueDelete(SeqCQueue*Q,DataType*d){if(Q->count==0){printf("队列已空无数据元素出队列!\n");return0;}else{*d=Q->queue[Q->front];Q->front=(Q->front+1)%MaxQueueSize;Q->count--;return1;}}intQueueGet(SeqCQueueQ,DataType*d){if(Q.count==0){printf("队列已空无数据元素可取!\n");return0;}else{*d=Q.queue[Q.front];return1;}}实验三二叉树的操作及应用一、实验目的1、进一步掌握指针变量、动态变量的含义。2、掌握二叉树的结构特性,以及各种存储结构的特点和适用范围。3、掌握用指针类型描述、访问和处理二叉树的运算。二、实验内容(一)完成下列程序,该程序以二叉链表作存储结构,构建如图1所示的二叉树,并依次进行二叉树的前序、中序、后序及层次遍历。图1typedefstructNode{DataTypedata;structNode*leftChild;structNode*rightChild;}BiTreeNode;/*初始化*/voidInitiate(BiTreeNode**root){*root=(BiTreeNode*)malloc(sizeof(BiTreeNode));(*root)->leftChild=NULL;(*root)->rightChild=NULL;}voidPreOrder(BiTreeNode*t,voidVisit(DataTypeitem))/*前序遍历二叉树t,访问操作为Visit()函数*/{if(t!=NULL){Visit(t->data);PreOrder(t->leftChild,Visit);PreOrder(t->rightChild,Visit);}}voidInOrder(BiTreeNode*t,voidVisit(DataTypeitem))/*中序t*/{if(t!=NULL){InOrder(t->leftChild,Visit);Visit(t->data);InOrder(t->rightChild,Visit);}}voidPostOrder(BiTreeNode*t,voidVisit(DataTypeitem))/*后序*/{if(t!=NULL){PostOrder(t->leftChild,Visit);PostOrder(t->rightChild,Visit);Visit(t->data);}}(二)完成下列程序,该程序以二叉链表作存储结构,构建如图2所示二叉树,计算二叉树深度、所有结点总数、叶子结点数、双孩子结点个数、单孩子结点个数。图2intBTreeDepth(BiTreeNode*BT){intleftdep,rightdep;if(BT==NULL)return(0);else{leftdep=BTreeDepth(BT->leftChild);rightdep=BTreeDepth(BT->rightChild);if(leftdep>rightdep)return(leftdep+1);elsereturn(rightdep+1);}}intnodecount(BiTreeNode*BT){if(BT==NULL)return(0);elsereturn(nodecount(BT->leftChild)+nodecount(BT->rightChild)+1);}intleafcount(BiTreeNode*BT){if(BT==NULL)return(0);elseif(BT->leftChild==NULL&&BT->rightChild==NULL)return(1);elsereturn(leafcount(BT->leftChild)+leafcount(BT->rightChild));}intnotleafcount(BiTreeNode*BT){if(BT==NULL)return(0);elseif(BT->leftChild==NULL&&BT->rightChild==NULL)return(0);elsereturn(notleafcount(BT->leftChild)+notleafcount(BT->rightChild)+1);}intonesoncount(BiTreeNode*BT){if(BT==NULL)return(0);elseif((BT->leftChild==NULL&&BT->rightChild!=NULL)||(BT->leftChild!=NULL&&BT->rightChild==NULL))return(onesoncount(BT->leftChild)+onesoncount(BT->rightChild)+1);elsereturn(onesoncount(BT->leftChild)+onesoncount(BT->rightChild));}inttwosoncount(BiTreeNode*BT){if(BT==NULL)return(0);elseif(BT->leftChild==NULL||BT->rightChild==NULL)return(twosoncount(BT->leftChild)+twosoncount(BT->rightChild));elseif(BT->leftChild!=NULL&&BT->rightChild!=NULL)return(twosoncount(BT->leftChild)+twosoncount(BT->rightChild)+1);}(三)用非递归方式遍历图2所示的二叉树(先序、中序或后序),输出遍历序列。 实验四图的操作及应用一、实验目的1、理解图的基本概念及术语。2、掌握图的两种存储结构(邻接矩阵和邻接表)的表示方法。3、熟练掌握图的两种遍历(深度优先搜索遍历和广度优先搜索遍历)的算法思想、步骤,并能列出在两种存储结构上按上述两种遍历算法得到的序列。4、理解最小生成树的概念,能按Prim算法构造最小生成树。二、实现内容(一)完成下列程序,该程序构造如图1所示的图的邻接矩阵存储结构。图1/*邻接矩阵*/typedefstruct{SeqListVertices;intedge[MaxVertices][MaxVertices];intnumOfEdges;}AdjMGraph;voidInitiate(AdjMGraph*G,intn){inti,j;for(i=0;ifor(j=0;j{if(i==j)G->edge[i][j]=0;elseG->edge[i][j]=MaxWeight;}G->numOfEdges=0;/*边的条数置为0*/ListInitiate(&G->Vertices);/*顺序表初始化*/}voidInsertVertex(AdjMGraph*G,DataTypevertex){ListInsert(&G->Vertices,G->Vertices.size,vertex);}voidInsertEdge(AdjMGraph*G,intv1,intv2,intweight){if(v1<0||v1>G->Vertices.size||v2<0||v2>G->Vertices.size){printf("参数v1或v2越界出错!\n");exit(1);}G->edge[v1][v2]=weight;G->numOfEdges++;}typedefstruct{introw;//行下标intcol;//列下标intweight;//权值}RowColWeight;voidCreatGr
{p=p->next;j++;
if(j!
=i-1)
{printf(“插入位置参数错!
”);
q=(SLNode*)malloc(sizeof(SLNode));
q->data=x;q->next=p->next;p->next=q;
intListDelete(SLNode*head,inti,DataType*x)
{SLNode*p,*s;intj;
=NULL&&p->next->next!
=NULL&&j{p=p->next;j++;}if(j!=i-1){printf(“插入位置参数错!”);return0;}s=p->next;*x=s->data;p->next=p->next->next;free(s);return1;}intListGet(SLNode*head,inti,DataType*x){SLNode*p;intj;p=head;j=-1;while(p->next!=NULL&&j{p=p->next;j++;}if(j!=i){printf(“取元素位置参数错!”);return0;}*x=p->data;return1;}voidDestroy(SLNode**head){SLNode*p,*p1;p=*head;while(p!=NULL){p1=p;p=p->next;free(p1);}*head=NULL;}(四)已知单链表la和lb中的数据元素按非递减有序排列,将la和lb中的数据元素,合并为一个新的单链表lc,lc中的数据元素仍按非递减有序排列。要求不破坏la表和lb表的结构。(五)约瑟夫环程序:设有N个人围坐一圈,现从某个人开始报数,数到M的人出列,接着从出列的下一个人开始重新报数,数到M的人以出列,如此下去,直到所有人都出列为此。试设计确定他们的出列次序序列的程序。要求选择单向循环链表作为存储结构模拟整个过程,并依次输出列的各人的编号。如n=8,m=4时,若从第一个人,设每个人的编号依次为1,2,3,…开始报数,则得到的出列次序为4,8,5,2,1,3,7,6,实验二栈、队列的实现及应用一、实验目的1、掌握栈和队列的顺序存储结构和链式存储结构,以便在实际背景下灵活运用。2、掌握栈和队列的特点,即先进后出与先进先出的原则。3、掌握栈和队列的基本操作实现方法。二、实验内容(一)完成下列程序,该程序实现栈的顺序存储结构,构建顺序栈(栈中的元素依次为R,S,Y,F,C,T),依次进行进栈和出栈操作,判断栈空和栈满操作,返回栈顶元素操作。要求生成顺序栈时,从键盘上读取数据元素。typedefstruct{DataTypestack[MaxStackSize];inttop;}SeqStack;voidStackInitiate(SeqStack*S){S->top=0;}intStackNotEmpty(SeqStackS){if(S.top<=0)return0;elsereturn1;}intStackPush(SeqStack*S,DataTypex){if(S->top>=MaxStackSize){printf("堆栈已满无法插入!\n");return0;}else{S->stack[S->top]=x;S->top++;return1;}}intStackPop(SeqStack*S,DataType*d){if(S->top<=0){printf("堆栈已空无数据元素出栈!\n");return0;}else{S->top--;*d=S->stack[S->top];return1;}}intStackTop(SeqStackS,DataType*d){if(S.top<=0){printf("堆栈已空!\n");return0;}else{*d=S.stack[S.top-1];return1;}}(二)完成下列程序,该程序实现栈的链式存储结构,构建链栈(栈中的元素依次为China,Japan,France,India,Australia),依次进行进栈和出栈操作,判断栈空和栈满操作,返回栈顶元素操作。要求生成链栈时,从键盘上读取数据元素。typedefstructsnode{DataTypedata;structsnode*next;}LSNode;voidStackInitiate(LSNode**head){*head=(LSNode*)malloc(sizeof(LSNode));(*head)->next=NULL;}intStackNotEmpty(LSNode*head){if(head->next==NULL)return0;elsereturn1;}intStackPush(LSNode*head,DataTypex){LSNode*p;p=(LSNode*)malloc(sizeof(LSNode));p->data=x;p->next=head->next;head->next=p;return1;}intStackPop(LSNode*head,DataType*d){LSNode*p=head->next;if(p==NULL){printf("堆栈已空出错!");return0;}head->next=p->next;*d=p->data;free(p);return1;}intStackTop(LSNode*head,DataType*d){LSNode*p=head->next;if(p==NULL){printf("堆栈已空出错!");return0;}*d=p->data;return1;}voidDestroy(LSNode*head){LSNode*p,*p1;p=head;while(p!=NULL){p1=p;p=p->next;free(p1);}}(三)利用顺序栈实现数制转换,输入十进制整数,分别将其转换为八进制数和二进制数。(四)完成下列程序,该程序实现循环队列的存储和基本操作,构建循环队列(队列中的元素依次为John,Mary,Linda,Mike,Paul),依次进行判断队列是否为空和满操作、入队和出队操作、取得队首元素操作。typedefstruct{DataTypequeue[MaxQueueSize];intrear;intfront;intcount;}SeqCQueue;voidQueueInitiate(SeqCQueue*Q){Q->rear=0;Q->front=0;Q->count=0;}intQueueNotEmpty(SeqCQueueQ){if(Q.count!=0)return1;elsereturn0;}intQueueAppend(SeqCQueue*Q,DataTypex){if(Q->count>0&&Q->rear==Q->front){printf("队列已满无法插入!\n");return0;}else{Q->queue[Q->rear]=x;Q->rear=(Q->rear+1)%MaxQueueSize;Q->count++;return1;}}intQueueDelete(SeqCQueue*Q,DataType*d){if(Q->count==0){printf("队列已空无数据元素出队列!\n");return0;}else{*d=Q->queue[Q->front];Q->front=(Q->front+1)%MaxQueueSize;Q->count--;return1;}}intQueueGet(SeqCQueueQ,DataType*d){if(Q.count==0){printf("队列已空无数据元素可取!\n");return0;}else{*d=Q.queue[Q.front];return1;}}实验三二叉树的操作及应用一、实验目的1、进一步掌握指针变量、动态变量的含义。2、掌握二叉树的结构特性,以及各种存储结构的特点和适用范围。3、掌握用指针类型描述、访问和处理二叉树的运算。二、实验内容(一)完成下列程序,该程序以二叉链表作存储结构,构建如图1所示的二叉树,并依次进行二叉树的前序、中序、后序及层次遍历。图1typedefstructNode{DataTypedata;structNode*leftChild;structNode*rightChild;}BiTreeNode;/*初始化*/voidInitiate(BiTreeNode**root){*root=(BiTreeNode*)malloc(sizeof(BiTreeNode));(*root)->leftChild=NULL;(*root)->rightChild=NULL;}voidPreOrder(BiTreeNode*t,voidVisit(DataTypeitem))/*前序遍历二叉树t,访问操作为Visit()函数*/{if(t!=NULL){Visit(t->data);PreOrder(t->leftChild,Visit);PreOrder(t->rightChild,Visit);}}voidInOrder(BiTreeNode*t,voidVisit(DataTypeitem))/*中序t*/{if(t!=NULL){InOrder(t->leftChild,Visit);Visit(t->data);InOrder(t->rightChild,Visit);}}voidPostOrder(BiTreeNode*t,voidVisit(DataTypeitem))/*后序*/{if(t!=NULL){PostOrder(t->leftChild,Visit);PostOrder(t->rightChild,Visit);Visit(t->data);}}(二)完成下列程序,该程序以二叉链表作存储结构,构建如图2所示二叉树,计算二叉树深度、所有结点总数、叶子结点数、双孩子结点个数、单孩子结点个数。图2intBTreeDepth(BiTreeNode*BT){intleftdep,rightdep;if(BT==NULL)return(0);else{leftdep=BTreeDepth(BT->leftChild);rightdep=BTreeDepth(BT->rightChild);if(leftdep>rightdep)return(leftdep+1);elsereturn(rightdep+1);}}intnodecount(BiTreeNode*BT){if(BT==NULL)return(0);elsereturn(nodecount(BT->leftChild)+nodecount(BT->rightChild)+1);}intleafcount(BiTreeNode*BT){if(BT==NULL)return(0);elseif(BT->leftChild==NULL&&BT->rightChild==NULL)return(1);elsereturn(leafcount(BT->leftChild)+leafcount(BT->rightChild));}intnotleafcount(BiTreeNode*BT){if(BT==NULL)return(0);elseif(BT->leftChild==NULL&&BT->rightChild==NULL)return(0);elsereturn(notleafcount(BT->leftChild)+notleafcount(BT->rightChild)+1);}intonesoncount(BiTreeNode*BT){if(BT==NULL)return(0);elseif((BT->leftChild==NULL&&BT->rightChild!=NULL)||(BT->leftChild!=NULL&&BT->rightChild==NULL))return(onesoncount(BT->leftChild)+onesoncount(BT->rightChild)+1);elsereturn(onesoncount(BT->leftChild)+onesoncount(BT->rightChild));}inttwosoncount(BiTreeNode*BT){if(BT==NULL)return(0);elseif(BT->leftChild==NULL||BT->rightChild==NULL)return(twosoncount(BT->leftChild)+twosoncount(BT->rightChild));elseif(BT->leftChild!=NULL&&BT->rightChild!=NULL)return(twosoncount(BT->leftChild)+twosoncount(BT->rightChild)+1);}(三)用非递归方式遍历图2所示的二叉树(先序、中序或后序),输出遍历序列。 实验四图的操作及应用一、实验目的1、理解图的基本概念及术语。2、掌握图的两种存储结构(邻接矩阵和邻接表)的表示方法。3、熟练掌握图的两种遍历(深度优先搜索遍历和广度优先搜索遍历)的算法思想、步骤,并能列出在两种存储结构上按上述两种遍历算法得到的序列。4、理解最小生成树的概念,能按Prim算法构造最小生成树。二、实现内容(一)完成下列程序,该程序构造如图1所示的图的邻接矩阵存储结构。图1/*邻接矩阵*/typedefstruct{SeqListVertices;intedge[MaxVertices][MaxVertices];intnumOfEdges;}AdjMGraph;voidInitiate(AdjMGraph*G,intn){inti,j;for(i=0;ifor(j=0;j{if(i==j)G->edge[i][j]=0;elseG->edge[i][j]=MaxWeight;}G->numOfEdges=0;/*边的条数置为0*/ListInitiate(&G->Vertices);/*顺序表初始化*/}voidInsertVertex(AdjMGraph*G,DataTypevertex){ListInsert(&G->Vertices,G->Vertices.size,vertex);}voidInsertEdge(AdjMGraph*G,intv1,intv2,intweight){if(v1<0||v1>G->Vertices.size||v2<0||v2>G->Vertices.size){printf("参数v1或v2越界出错!\n");exit(1);}G->edge[v1][v2]=weight;G->numOfEdges++;}typedefstruct{introw;//行下标intcol;//列下标intweight;//权值}RowColWeight;voidCreatGr
s=p->next;*x=s->data;p->next=p->next->next;
free(s);
intListGet(SLNode*head,inti,DataType*x)
{SLNode*p;
intj;
=NULL&&j
=i)
{printf(“取元素位置参数错!
*x=p->data;
voidDestroy(SLNode**head)
{SLNode*p,*p1;
p=*head;
while(p!
{p1=p;p=p->next;
free(p1);
*head=NULL;
(四)已知单链表la和lb中的数据元素按非递减有序排列,将la和lb中的数据元素,合并为一个新的单链表lc,lc中的数据元素仍按非递减有序排列。
要求不破坏la表和lb表的结构。
(五)约瑟夫环程序:
设有N个人围坐一圈,现从某个人开始报数,数到M的人出列,接着从出列的下一个人开始重新报数,数到M的人以出列,如此下去,直到所有人都出列为此。
试设计确定他们的出列次序序列的程序。
要求选择单向循环链表作为存储结构模拟整个过程,并依次输出列的各人的编号。
如n=8,m=4时,若从第一个人,设每个人的编号依次为1,2,3,…开始报数,则得到的出列次序为4,8,5,2,1,3,7,6,
实验二栈、队列的实现及应用
1、掌握栈和队列的顺序存储结构和链式存储结构,以便在实际背景下灵活运用。
2、掌握栈和队列的特点,即先进后出与先进先出的原则。
3、掌握栈和队列的基本操作实现方法。
(一)完成下列程序,该程序实现栈的顺序存储结构,构建顺序栈(栈中的元素依次为R,S,Y,F,C,T),依次进行进栈和出栈操作,判断栈空和栈满操作,返回栈顶元素操作。
要求生成顺序栈时,从键盘上读取数据元素。
{DataTypestack[MaxStackSize];
inttop;
}SeqStack;
voidStackInitiate(SeqStack*S)
S->top=0;
intStackNotEmpty(SeqStackS)
if(S.top<=0)return0;
elsereturn1;
intStackPush(SeqStack*S,DataTypex)
{if(S->top>=MaxStackSize)
printf("堆栈已满无法插入!
{S->stack[S->top]=x;
S->top++;
intStackPop(SeqStack*S,DataType*d)
{if(S->top<=0)
{printf("堆栈已空无数据元素出栈!
{S->top--;*d=S->stack[S->top];
intStackTop(SeqStackS,DataType*d)
{if(S.top<=0)
{printf("堆栈已空!
{*d=S.stack[S.top-1];
(二)完成下列程序,该程序实现栈的链式存储结构,构建链栈(栈中的元素依次为China,Japan,France,India,Australia),依次进行进栈和出栈操作,判断栈空和栈满操作,返回栈顶元素操作。
要求生成链栈时,从键盘上读取数据元素。
typedefstructsnode
DataTypedata;
structsnode*next;
}LSNode;
voidStackInitiate(LSNode**head)
*head=(LSNode*)malloc(sizeof(LSNode));
intStackNotEmpty(LSNode*head)
if(head->next==NULL)return0;
intStackPush(LSNode*head,DataTypex)
LSNode*p;
p=(LSNode*)malloc(sizeof(LSNode));
p->data=x;
p->next=head->next;
head->next=p;
intStackPop(LSNode*head,DataType*d)
{LSNode*p=head->next;
if(p==NULL)
{printf("堆栈已空出错!
");
head->next=p->next;
*d=p->data;
free(p);
intStackTop(LSNode*head,DataType*d)
LSNode*p=head->next;
printf("堆栈已空出错!
voidDestroy(LSNode*head)
LSNode*p,*p1;
p=head;
p1=p;
p=p->next;
(三)利用顺序栈实现数制转换,输入十进制整数,分别将其转换为八进制数和二进制数。
(四)完成下列程序,该程序实现循环队列的存储和基本操作,构建循环队列(队列中的元素依次为John,Mary,Linda,Mike,Paul),依次进行判断队列是否为空和满操作、入队和出队操作、取得队首元素操作。
DataTypequeue[MaxQueueSize];
intrear;
intfront;
intcount;
}SeqCQueue;
voidQueueInitiate(SeqCQueue*Q)
Q->rear=0;
Q->front=0;
Q->count=0;
intQueueNotEmpty(SeqCQueueQ)
if(Q.count!
=0)return1;
elsereturn0;
intQueueAppend(SeqCQueue*Q,DataTypex)
if(Q->count>0&&Q->rear==Q->front)
{printf("队列已满无法插入!
{Q->queue[Q->rear]=x;
Q->rear=(Q->rear+1)%MaxQueueSize;
Q->count++;
intQueueDelete(SeqCQueue*Q,DataType*d)
if(Q->count==0)
{printf("队列已空无数据元素出队列!
{*d=Q->queue[Q->front];
Q->front=(Q->front+1)%MaxQueueSize;
Q->count--;
intQueueGet(SeqCQueueQ,DataType*d)
if(Q.count==0)
{printf("队列已空无数据元素可取!
{*d=Q.queue[Q.front];
实验三二叉树的操作及应用
1、进一步掌握指针变量、动态变量的含义。
2、掌握二叉树的结构特性,以及各种存储结构的特点和适用范围。
3、掌握用指针类型描述、访问和处理二叉树的运算。
(一)完成下列程序,该程序以二叉链表作存储结构,构建如图1所示的二叉树,并依次进行二叉树的前序、中序、后序及层次遍历。
图1
{DataTypedata;
structNode*leftChild;
structNode*rightChild;
}BiTreeNode;
/*初始化*/
voidInitiate(BiTreeNode**root)
{*root=(BiTreeNode*)malloc(sizeof(BiTreeNode));
(*root)->leftChild=NULL;
(*root)->rightChild=NULL;
voidPreOrder(BiTreeNode*t,voidVisit(DataTypeitem))
/*前序遍历二叉树t,访问操作为Visit()函数*/
{if(t!
{Visit(t->data);
PreOrder(t->leftChild,Visit);
PreOrder(t->rightChild,Visit);
voidInOrder(BiTreeNode*t,voidVisit(DataTypeitem))/*中序t*/
if(t!
{InOrder(t->leftChild,Visit);
Visit(t->data);
InOrder(t->rightChild,Visit);
voidPostOrder(BiTreeNode*t,voidVisit(DataTypeitem))/*后序*/
{PostOrder(t->leftChild,Visit);
PostOrder(t->rightChild,Visit);
(二)完成下列程序,该程序以二叉链表作存储结构,构建如图2所示二叉树,计算二叉树深度、所有结点总数、叶子结点数、双孩子结点个数、单孩子结点个数。
图2
intBTreeDepth(BiTreeNode*BT)
intleftdep,rightdep;
if(BT==NULL)
return(0);
leftdep=BTreeDepth(BT->leftChild);
rightdep=BTreeDepth(BT->rightChild);
if(leftdep>rightdep)
return(leftdep+1);
return(rightdep+1);
intnodecount(BiTreeNode*BT)
return(nodecount(BT->leftChild)+nodecount(BT->rightChild)+1);
intleafcount(BiTreeNode*BT)
elseif(BT->leftChild==NULL&&BT->rightChild==NULL)
return
(1);
return(leafcount(BT->leftChild)+leafcount(BT->rightChild));
intnotleafcount(BiTreeNode*BT)
return(notleafcount(BT->leftChild)+notleafcount(BT->rightChild)+1);
intonesoncount(BiTreeNode*BT)
elseif((BT->leftChild==NULL&&BT->rightChild!
=NULL)||
(BT->leftChild!
=NULL&&BT->rightChild==NULL))
return(onesoncount(BT->leftChild)+onesoncount(BT->rightChild)+1);
return(onesoncount(BT->leftChild)+onesoncount(BT->rightChild));
inttwosoncount(BiTreeNode*BT)
elseif(BT->leftChild==NULL||BT->rightChild==NULL)
return(twosoncount(BT->leftChild)+twosoncount(BT->rightChild));
elseif(BT->leftChild!
=NULL&&BT->rightChild!
return(twosoncount(BT->leftChild)+twosoncount(BT->rightChild)+1);
(三)用非递归方式遍历图2所示的二叉树(先序、中序或后序),输出遍历序列。
实验四图的操作及应用
1、理解图的基本概念及术语。
2、掌握图的两种存储结构(邻接矩阵和邻接表)的表示方法。
3、熟练掌握图的两种遍历(深度优先搜索遍历和广度优先搜索遍历)的算法思想、步骤,并能列出在两种存储结构上按上述两种遍历算法得到的序列。
4、理解最小生成树的概念,能按Prim算法构造最小生成树。
二、实现内容
(一)完成下列程序,该程序构造如图1所示的图的邻接矩阵存储结构。
/*邻接矩阵*/
SeqListVertices;
intedge[MaxVertices][MaxVertices];
intnumOfEdges;
}AdjMGraph;
voidInitiate(AdjMGraph*G,intn)
inti,j;
for(i=0;ifor(j=0;j{if(i==j)G->edge[i][j]=0;elseG->edge[i][j]=MaxWeight;}G->numOfEdges=0;/*边的条数置为0*/ListInitiate(&G->Vertices);/*顺序表初始化*/}voidInsertVertex(AdjMGraph*G,DataTypevertex){ListInsert(&G->Vertices,G->Vertices.size,vertex);}voidInsertEdge(AdjMGraph*G,intv1,intv2,intweight){if(v1<0||v1>G->Vertices.size||v2<0||v2>G->Vertices.size){printf("参数v1或v2越界出错!\n");exit(1);}G->edge[v1][v2]=weight;G->numOfEdges++;}typedefstruct{introw;//行下标intcol;//列下标intweight;//权值}RowColWeight;voidCreatGr
for(j=0;j{if(i==j)G->edge[i][j]=0;elseG->edge[i][j]=MaxWeight;}G->numOfEdges=0;/*边的条数置为0*/ListInitiate(&G->Vertices);/*顺序表初始化*/}voidInsertVertex(AdjMGraph*G,DataTypevertex){ListInsert(&G->Vertices,G->Vertices.size,vertex);}voidInsertEdge(AdjMGraph*G,intv1,intv2,intweight){if(v1<0||v1>G->Vertices.size||v2<0||v2>G->Vertices.size){printf("参数v1或v2越界出错!\n");exit(1);}G->edge[v1][v2]=weight;G->numOfEdges++;}typedefstruct{introw;//行下标intcol;//列下标intweight;//权值}RowColWeight;voidCreatGr
if(i==j)G->edge[i][j]=0;
elseG->edge[i][j]=MaxWeight;
G->numOfEdges=0;/*边的条数置为0*/
ListInitiate(&G->Vertices);/*顺序表初始化*/
voidInsertVertex(AdjMGraph*G,DataTypevertex)
ListInsert(&G->Vertices,G->Vertices.size,vertex);
voidInsertEdge(AdjMGraph*G,intv1,intv2,intweight)
if(v1<0||v1>G->Vertices.size||v2<0||v2>G->Vertices.size)
{printf("参数v1或v2越界出错!
exit
G->edge[v1][v2]=weight;
G->numOfEdges++;
introw;//行下标
intcol;//列下标
intweight;//权值
}RowColWeight;
voidCreatGr
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1