printf("\n");
}
system("pause");
}
intinsert_a(List*b,inte)
{
intj;
if(b->length+1>N)//当前length已经=N了,即数组已满
return0;
for(j=b->length-1;j>=0;j--)
{
if(ea[j])
{
b->a[j+1]=b->a[j];
}
elsebreak;
}
b->a[j+1]=e;
b->length++;
return1;
}
程序运行轨迹:
length(1-10)=5
pleaseenternumberstolist:
1233566778
NumbersinList:
1233566778
pleaseenteranumberyouwanttoinsertintoList:
24
NumbersinList:
122433566778
内容2:
(1)建立链表
说明:
建立链表是从无到有地建立起一个链表,即一个一个地输入各结点数据,并建立起前后相互链接的关系。
首先建立一个空链表,然后依次生成各结点,新生成结点始终插入在链表的第1个位置(头插法)。
我的程序:
#defineNULL0
typedefstructLNode
{intdata;
structLNode*next;
}LNode,*Linklist;
LinklistcreateL_InReverseOrder(intn)//逆序创建单链表
{
LinklistL,p;//定义所需变量
inti;
L=(Linklist)malloc(sizeof(LNode));
L->next=NULL;
for(i=n;i>0;--i)
{
p=(Linklist)malloc(sizeof(LNode));
scanf("%d",&p->data);
//将结点P插入在头结点之后
p->next=L->next;
L->next=p;
}
return(L);
}
(2)遍历链表
说明:
遍历链表是将链表中各结点的数据依次显示。
设一个指针变量p,先指向第1个结点,显示p所指结点的数据,然后p后移一个结点再显示之,直到链表尾结点。
我的程序:
voidprint_L(LinklistL)
{Linklistp;
printf("Thelistis:
\n");
p=L->next;
while(p)
{printf("%3d",p->data);
p=p->next;//指针后移一个结点
}
printf("\n");
}
(3)删除链表中的结点
说明:
删除链表中的结点是从第1个结点开始,检查该结点的数据是否等于要删除的数据,如果相等就将该结点删除,如不相等,则后移一个结点,如此进行下去,直到表尾为止。
我的程序:
voidListDelete_X(Linklist*L,intx)//注意:
此时L是链表的二级指针!
!
{
Linklistp,q;
p=*L;//p是头指针
q=p->next;//q是指向首结点的指针
if(!
q)//空表
{
printf("Erroroperation:
theListisempty!
");
return;
}
while(q&&q->data!
=x)//说明:
这种做法只能删除第一个值为X的结点
{
p=q;
q=q->next;
}
if(q)//q不为空,但退出了循环,说明找到x了
{
//删除q所指结点
p->next=q->next;
free(q);
}
elseprintf("%ddoesnotexist!
",x);
}
(4)利用下面的main()函数,测试上述各算法。
我的程序:
main()
{
intn,x;
LinklistL,p;
printf("inputn:
");
scanf("%d%*c",&n);//输入线性表中元素的个数
L=createL_InReverseOrder(n);//按逆位序创建线性表
print_L(L);//打印线性表
printf("inputthedatathatyouwanttodelete:
");
scanf("%d",&x);//输入要删除的数x
ListDelete_X(&L,x);//在线性表L中删除第一个值为x的结点
print_L(L);//打印删除后的线性表
system("pause");
}
程序运行轨迹:
//第一次测试
inputn:
5
23439860
Thelistis:
06984323
inputthedatathatyouwanttodelete:
6
Thelistis:
0984323
//第二次测试
inputn:
5
23436688-12
Thelistis:
-1288664323
inputthedatathatyouwanttodelete:
3
3doesnotexist!
Thelistis:
-1288664323
实验二栈和队列及其应用
实验目的
1、深入了解栈和队列的特性。
2、掌握栈和队列基本操作的实现。
实验内容
1、实现顺序存储结构上的栈的初始化、入栈和出栈操作,并进行测试。
2、实现链式存储结构上的队列的初始化、入队和出队操作,并进行测试。
实验提示
在数据结构实验中,程序中经常会用到像NULL、OK等符号常量,为以后编程方便起见,将这些程序中经常用到的符号常量编写成一个头文件mydefine.h,这样以后编程时需要这些符号常量时,只要将该头文件包含在程序中即可。
头文件mydefine.h的内容如下:
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
#defineOVERFLOW-2
#defineNULL0
内容1
1、程序内容如下:
(1)文件包含命令:
包含头文件mydefine.h
(2)定义程序中需要用到的其他符号常量
(3)栈的顺序存储结构描述
(4)栈的初始化函数:
voidinitStack(sqstack*s)
(5)入栈函数:
intpush(sqstack*s,ElemTypex)
(6)出栈函数:
intpop(sqstack*s,ElemType*x)
(7)主函数:
main()
2、利用如下主函数:
main()进行测试,写出测试的输入序列和输出序列。
typedefintElemType;
main()
{sqstacks;
ElemTypex,e;
clrscr();
initStack(&s);
scanf("%d",&x);
push(&s,x);
scanf("%d",&x);
push(&s,x);
pop(&s,&e);
printf("out:
%d\n",e);
scanf("%d",&x);
push(&s,x);
pop(&s,&e);
printf("out:
%d\n",e);
pop(&s,&e);
printf("out:
%d\n",e);
}
我的程序:
//mydefine.h
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
#defineOVERFLOW-2
/*注:
在这里没有定义NULL为0,因为如果加上话在编译下面的SqStack.c时会报错,应该是在stdio.h这个头文件中已经定义过NULL了*/
//SqStack.c
#include
#include
#include"mydefine.h"
#defineSTACK_INIT_SIZE10//存储空间初始化分配量
#defineSTACK_INCREMENT2//存储空间分配增量
typedefintStatus;
typedefintSElemType;
typedefstructSqStack
{SElemType*base;
SElemType*top;//栈底指针
intstacksize;
}SqStack;
voidInitStack(SqStack*S)//初始化
{//构造空栈
printf("begininit...\n");
S->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!
S->base)
exit(OVERFLOW);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
printf("endofinit.\n");
}
voidPush(SqStack*S,SElemTypee)//入栈
{
printf("inPush()...\n");
if(S->top-S->base==S->stacksize)//栈满
{
S->base=(SElemType*)realloc(S->base,(S->stacksize+STACK_INCREMENT)*sizeof(SElemType));
if(!
S->base)
exit(OVERFLOW);
S->top=S->base+S->stacksize;/*修改栈顶指针,因为这时栈可能已经不在原来的位置了,所以栈顶指针的值一定要记得修改!
!
*/
S->stacksize+=STACK_INCREMENT;
}
*S->top=e;//将e入栈,成为新的栈顶元素
S->top++;/*这步别忘了!
本来可以和上一步写成一句的,但是为了方便理解,还是分开写吧*/
printf("leavePush().\n");
}
StatusPop(SqStack*S,SElemType*e)//出栈
{
if(S->top==S->base)//栈空
returnERROR;
*e=*--(S->top);//将栈顶元素赋给e,栈顶指针下移
returnOK;
}
main()
{
printf("inmian()...\n");
SqStacks;
SElemTypex,e;
InitStack(&s);
printf("inputanumberotaddtothestack:
");
scanf("%d",&x);
Push(&s,x);
printf("inputanumberotaddtothestack:
");
scanf("%d",&x);
Push(&s,x);
Pop(&s,&e);
printf("out:
%d\n",e);
printf("inputanumberotaddtothestack:
");
scanf("%d",&x);
Push(&s,x);
Pop(&s,&e);
printf("out:
%d\n",e);
Pop(&s,&e);
printf("out:
%d\n",e);
system("pause");
}
程序轨迹:
inmian()...
begininit...
endofinit.
inputanumberotaddtothestack:
32
inPush()...
leavePush().
inputanumberotaddtothestack:
43
inPush()...
leavePush().
out:
43
inputanumberotaddtothestack:
6
inPush()...
leavePush().
out:
6
out:
32
内容2
1、程序内容如下:
(1)文件包含命令:
包含头文件mydefine.h
(2)定义程序中需要用到的其他符号常量
(3)队列的链式存储结构描述
(4)队列的初始化函数:
intInitQueue(linkQueue*Q)
(5)入队函数:
intEnQueue(linkQueue*Q,ElemTypee)
(6)出队函数:
intDeQueue(linkQueue*Q,ElemType*e)
(7)主函数:
main()
2、利用如下主函数:
main()进行测试,写出测试的输入序列和输出序列。
main()
{linkQueueQ;
ElemTypex,e;
clrscr();
InitQueue(&Q);
scanf("%d",&x);
EnQueue(&Q,x);
scanf("%d",&x);
EnQueue(&Q,x);
DeQueue(&Q,&e);
printf("out:
%d\n",e);
scanf("%d",&x);
EnQueue(&Q,x);
DeQueue(&Q,&e);
printf("out:
%d\n",e);
DeQueue(&Q,&e);
printf("out:
%d\n",e);
}
我的程序:
//mydefine.h
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
#defineOVERFLOW-2
/*注:
在这里没有定义NULL为0,因为如果加上话在编译下面的SqStack.c时会报错,应该是在stdio.h这个头文件中已经定义过NULL了*/
//Queue.c
#include
#include
#include"mydefine.h"
typedefintElemType;
typedefintStatus;
typedefstructQNode
{
ElemTypedata;
structQNode*next;
}QNode,*QueuePtr;
typedefstruct
{
QueuePtrfront;
QueuePtrrear;
}LinkQueue;
StatusInitQueue(LinkQueue*Q)
{
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if(!
Q->front)
{
exit(OVERFLOW);
}
Q->front->next=NULL;
returnOK;
}
StatusEnQueue(LinkQueue*Q,ElemTypee)//队列的播入只能在队尾插入
{
QueuePtrq=(QueuePtr)malloc(sizeof(QNode));
if(!
q)
{
printf("NOenoughspace,cannotenQueue!
");
returnERROR;
}
q->data=e;
q->next=NULL;
Q->rear->next=q;
Q->rear=q;//这步至关重要!
!
千万别忘了!
!
returnOK;
}
intDeQueue(LinkQueue*Q,ElemType*e)//队列的删除只能在队头删除
{
//若队不空则删除队头元素,并用e返回其值,并返回OK,否则返回ERROR
if(Q->front==Q->rear){
printf("Thequeueisempty,noelementfordeleting!
");
returnERROR;
}
//不空
QueuePtrq=Q->front->next;
*e=q->data;//用e返回其值
Q->front->next=q->next;
//别忘了特殊情况:
删除的是队列中的最后一个元素.此时要考虑队尾指针的丢失问题
if(q==Q->rear)
{
Q->rear=Q->front;//删除了最后一个元素后队列再次成为空队列
}
free(q);
returnOK;
}
//测试
main()
{
LinkQueueQ;
ElemTypex,e;
InitQueue(&Q);
printf("inputanumberotaddtothequeue:
");
scanf("%d",&x);
EnQueue(&Q,x);
printf("inputanumberotaddtothequeue:
");
scanf("%d",&x);
EnQueue(&Q,x);
DeQueue(&Q,&e);
printf("out:
%d\n",e);
printf("inputanumberotaddtothequeue:
");
scanf("%d",&x);
EnQueue(&Q,x);
DeQueue(&Q,&e);
printf("out:
%d\n",e);
DeQueue(&Q,&e);
printf("out:
%d\n",e);
system("pause");
}
程序运行轨迹:
inputanumberotaddtothequeue:
32
inputanumberotaddtothequeue:
565
out:
32
inputanumberotaddtothequeue:
8
out:
565
out:
8
实验三二叉树的存储与遍历
实验目的
1、掌握二叉树的非线性和递归性特点
2、掌握二叉树的存储结构
3、掌握二叉树遍历操作的实现方法
实验内容
建立二叉树的二叉链表表示,并选择一种策略(先序、中序或后序)进行遍历
实验提示
(1)采用链式存储结构建立二叉树,并按先序输入二叉树的结点序列。
例如建立如图所示的二叉树。
建立时按先序输入的结点序列为:
abc000de0f00g00
(2)二叉树的建立、先序遍历、中序遍历、后序遍历均采用递归方式实现。
(3)主函数中对各项功能进行测试。
我的程序:
#include
#include
#defineOVERFLOW-2
typedefcharDataType;
typedefstructBNode{//定义二叉链表结点结构
DataTypedata;
structBNode*lchild,*rchild;
}BNode;
typedefBNode*BinTree;
voidcreateBinTree(BinTree*T)
{
charc;
printf("\ninputanode:
");
scanf("%c%*c",&c);
if(c!
='#')
{
*T=(BinTree)malloc(sizeof(BNode));
(*T)->data=c;
createBinTree(&(*T)->lchild);
createBinTree(&(*T)->rchild);
}
else
*T=NULL;
}
voidtraverseBinTree(BinTreebt){
if(bt){
printf("%c\t",bt->data);
traverseBinTree(bt->lchild);
traverseBinTree(bt->rchild);
}
}
main(){