数据结构程序.docx

上传人:b****5 文档编号:5033169 上传时间:2022-12-12 格式:DOCX 页数:14 大小:19.61KB
下载 相关 举报
数据结构程序.docx_第1页
第1页 / 共14页
数据结构程序.docx_第2页
第2页 / 共14页
数据结构程序.docx_第3页
第3页 / 共14页
数据结构程序.docx_第4页
第4页 / 共14页
数据结构程序.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

数据结构程序.docx

《数据结构程序.docx》由会员分享,可在线阅读,更多相关《数据结构程序.docx(14页珍藏版)》请在冰豆网上搜索。

数据结构程序.docx

数据结构程序

PSeqListcreateNullList_seq(intm)

{

PSeqListpalist=(PSeqList)malloc(sizeof(structSeqList));

if(palist!

=NULL)

{

palist->element=(DataType*)malloc(sizeof(DataType)*m);

if(palist->element)

{

palist->MAXNUM=m;

palist->n=0;

return(palist);

}

elsefree(palist);

}

printf("Outofspace!

!

!

\n");

returnNULL;

}

intinsertPre_seq(PSeqListpalist,intp,DataTypex)

{

intq;

if(palist->n>=palist->MAXNUM)

{

printf("Overflow!

\n");return0;

}

if(p<0||p>palist->n)

{

printf("Notexist!

\n");return0;

}

for(q=palist->n-1;q>=p;q--)

palist->element[q+1]=palist->element[q];

palist->element[p]=x;

palist->n=palist->n+1;

return1;

}

intdeleteP_seq(PSeqListpalist,intp)

{

intq;

if(p<0||p>palist->n-1)

{

printf("Notexist!

\n");

return0;

}

for(q=p;qn-1;q++)

palist->element[q]=palist->element[q+1];

palist->n=palist->n-1;

return1;

}

LinkListcreateNullList_link(void)

//创建一个带头结点的空链表

{

//申请表头结点空间

LinkListllist=(LinkList)malloc(sizeof(structNode));

if(llist!

=NULL)

llist->link=NULL;

elseprintf("Outofspace!

\n");//创建失败

return(llist);

}

intinsertPost_link(LinkListllist,Node*p,DataTypex){

//在llist带头结点的单链表中,p所指结点后面插入元素x

Node*q=(Node*)malloc(sizeof(structNode));//申请新结点

if(q==NULL){

printf("Outofspace!

!

!

\n");

return(0);

}

else{

q->info=x;q->link=p->link;p->link=q;

return

(1);

}

}

intdeleteV_link(LinkListllist,DataTypex){

//在llist带有头结点的单链表中删除第一个值为x的结点

Node*p,*q;

p=llist;

if(p==NULL)return(0);

while(p->link!

=NULL&&p->link->info!

=x)

p=p->link;//找值为x的结点的前驱结点的存储位置

if(p->link==NULL){//没找到值为x的结点

printf("Notexist!

\n");return1;

}else{

q=p->link;//找到值为x的结点

p->link=q->link;//删除该结点

free(q);return1;

}

}

#include

#include

#include"SeqList.h“

voidjosephus_seq(PSeqListpalist,ints,intm)

{

ints1,i,w;

for(i=palist->n;i>0;i--)//找出列的元素

{

s1=(s1-1+m-1)%i;

w=palist->element[s1];//求下标为s1的元素的值

printf("Outelement%d\n",w);//元素出列

deleteP_seq(palist,s1);//删除出列的元素

}

}

voidmain()

{

inti,n,s,m;

printf("\npleaseinputthevalues(<100)ofn=");

scanf("%d",&n);

printf("pleaseinputthevaluesofs=");

scanf("%d",&s);

printf("pleaseinputthevaluesofm=");

scanf("%d",&m);

PSeqListjos_alist=createNullList_seq(n);//创建空顺序表

if(jos_alist!

=NULL)

{

for(i=0;i

insertPre_seq(jos_alist,i,i+1);

josephus_seq(jos_alist,s,m);

free(jos_alist->element);

free(jos_alist);

}

}

intindex(PSeqStringt,PSeqStringp)

{

inti,j;//i为p串中当前字符的下标,j为t串中当前字符的下标

i=0;j=0;//初始化

while(in&&jn)//反复比较

if(p->c[i]==t->c[j])//继续匹配下一个字符

{i++;j++;}

else//主串、子串的i、j值回溯,重新开始下一次匹配

{j=j-i+1;i=0;}

if(i>=p->n)

return(j-p->n+1);//匹配成功,返回p中第一个字符在t中的序号

else

return(0);//匹配失败

}

voidpush_seq(PSeqStackpastack,DataTypex)

{

if(pastack->t>=pastack->MAXNUM-1)

printf("Overflow!

\n");

else

{

pastack->t=pastack->t+1;

pastack->s[pastack->t]=x;

}

}

voidpop_seq(PSeqStackpastack)

{

if(pastack->t==-1)

printf("Underflow!

\n");

else

pastack->t=pastack->t-1;

}

voidpush_link(PLinkStackplstack,DataTypex)

{

PNodep;

p=(PNode)malloc(sizeof(structNode));

if(p==NULL)printf("Outofspace!

\n");

else

{

p->info=x;p->link=plstack->top;plstack->top=p;

}

}

voidpop_link(PLinkStackplstack)

{

PNodep;

if(isEmptyStack_link(plstack))printf("Emptystackpop.\n");

else

{

p=plstack->top;plstack->top=plstack->top->link;

free(p);

}

}

mazeFrame(void){

创建一个(保存探索过程的)空栈;

把入口位置压入栈中;

while栈不空{

取栈顶位置并设置为的当前位置;

while当前位置存在试探可能{

取下一个试探位置;

if(下一个位置是出口)

打印栈中保存的探索过程然后返回;

if(下一个位置是通道)

把当前位置进栈并且设置新的当前位置;

}

}

}

voidmazePath(int*maze[],intx1,inty1,intx2,inty2,intM,intN){

初始设置:

当前位置为[x1,y1]

while!

((当前位置为终点)||(回到原点,并无路可走)){

if(下一个位置可以走){

当前位置=下一位置;

当前位置设为已走标志;

}

else

当前位置=上一位置;

}

if(当前位置==终点)then

输出当前走的路径

else

输出:

不存在路径

}

voidmazePath(int*maze[],intx1,inty1,intx2,inty2,intM,intN){

初始设置:

当前位置为[x1,y1]

while!

((当前位置为终点)||(回到原点,并无路可走)){

while(存在下一个可走的位置){

遍历当前位置的四个方向,找到下一位置

当前位置=下一位置;

当前位置设为已走标志;

}

else

当前位置=上一位置;

}

if(当前位置==终点)then

输出当前走的路径

else

输出:

不存在路径

}

voidmazePath(int*maze[],intx1,inty1,intx2,inty2,intM,intN){

初始设置:

当前位置为[x1,y1]

while!

(回到原点,并无路可走)){

while(存在下一个可走的位置){

遍历当前位置的四个方向,找到下一位置

if(当前位置=终点)then

输出当前走的路径

else{

当前位置=下一位置;

当前位置设为已走标志;

}

}

else

当前位置=上一位置;

}

输出:

不存在路径

}

voidenQueue_seq(PSeqQueuepaqu,DataTypex)

{

if((paqu->r+1)%paqu->MAXNUM==paqu->f)

printf("Fullqueue.\n");

else

{

paqu->q[paqu->r]=x;

paqu->r=(paqu->r+1)%paqu->MAXNUM;

}

}

voiddeQueue_seq(PSeqQueuepaqu)

{

if(paqu->f==paqu->r)

printf("EmptyQueue.\n");

else

paqu->f=(paqu->f+1)%paqu->MAXNUM;

}

voidenQueue_link(PLinkQueueplqu,Datatypex)

{

PNodep;

p=(PNode)malloc(sizeof(structNode));//申请新结点空间

if(p==NULL)printf("Outofspace!

");

else

{

p->info=x;p->link=NULL;//填写新结点信息

if(plqu->f==NULL)plqu->f=p;//插入前是空队列

elseplqu->r->link=p;//将新结点插入

plqu->r=p;//修改对尾指针

}

}

voiddeQueue_link(PLinkQueueplqu)

{

PNodep;

if(plqu->f==NULL)

printf("Emptyqueue.\n");//队列已空

else

{

p=plqu->f;

plqu->f=p->link;//修改队头指针

free(p);//释放已经删除结点空间

}

}

voidpreOrder(BinTreet){

if(t==NULL)return;

visit(root(t));

preOrder(leftChild(t));

preOrder(rightChild(t));

}

voidinOrder(BinTreet){

if(t==NULL)return;

inOrder(leftChild(t));

visit(root(t));

inOrder(rightChild(t));

}

voidpostOrder(BinTreet){

if(t==NULL)return;

postOrder(leftChild(t));

postOrder(rightChild(t));

visit(root(t));

}

voidnPreOrder(BinTreet)

{

Stacks;//栈元素的类型是BinTree

BinTreeNode*c;

if(t==NULL)return;

s=createEmptyStack();

push(s,t);

while(!

isEmptyStack(s))//每当栈不空

{

c=top(s);pop(s);//取栈顶,出栈

if(c!

=NULL)

{

visit(root(c));//访问

push(s,rightChild(c));//右子树进栈

push(s,leftChild(c));//左子树进栈

}

}

}

voidinsertSort(SortObject*pvector)//按递增序进行直接插入排序

{

inti,j;RecordNodetemp;

for(i=1;in;i++)//依次插入记录R1,R2…Rn-1

{

temp=pvector->record[i];//本次待插入的记录

j=i-1;

//由后向前找插入位置

while((temp.keyrecord[j].key)&&(j>=0))

{

pvector->record[j+1]=pvector->record[j];

j--;//将排序码大于ki的记录后移

}

if(j!

=(i-1))pvector->record[j+1]=temp;

}

}

voidselectSort(SortObject*pvector)

{

inti,j,k;RecordNodetemp;

for(i=0;in-1;i++)//做n-1趟选择排序

{

k=i;

for(j=i+1;jn;j++)//在无序区内找出排序码最小的记录Rk

if(pvector->record[j].keyrecord[k].key)

k=j;

if(k!

=i)//记录Rk与Ri互换

{

temp=pvector->record[i];

pvector->record[i]=pvector->record[k];

pvector->record[k]=temp;

}

}

}

voidbubbleSort(SortObject*pvector)

{

inti,j,noswap;RecordNodetemp;

for(i=0;in-1;i++)//做n-1次起泡

{

noswap=TRUE;//置交换标志初态

for(j=0;jn-i-1;j++)

if(pvector->record[j+1].keyrecord[j].key)//从前向后扫描

{

temp=pvector->record[j];//交换记录

pvector->record[j]=pvector->record[j+1];

pvector->record[j+1]=temp;

noswap=FALSE;//修改交换标志

}

if(noswap)break;//本趟起泡未发生记录交换,算法结束

}

}

voidquickSort(SortObject*pvector,intL,intR)

{

inti,j;RecordNodetemp;

if(L>=R)return;//只有一个记录或无记录,则无须排序

i=L;j=R;

temp=pvector->record[i];

while(i!

=j)//寻找Rl的最终位置

{

while((pvector->record[j].key>=temp.key)&&(j>i))j--;

//从右向左扫描,查找第1个排序码小于temp.key的记录

if(irecord[i++]=pvector->record[j];

while((pvector->record[i].key<=temp.key)&&(j>i))i++;

//从左向右扫描,查找第1个排序码大于temp.key的记录

if(irecord[j--]=pvector->record[i];

}

pvector->record[i]=temp;//找到Rl的最终位置

quickSort(pvector,L,i-1);//递归处理左区间

quickSort(pvector,i+1,R);//递归处理右区间

voidmergeSort(SortObject*pvector)

{

RecordNoderecord[pvector->n];

intlength=1;

while(lengthn)

{

mergePass(pvector->record,record,pvector->n,length);

//一趟归并,结果放在r1中

length*=2;

mergePass(record,pvector->record,pvector->n,length);

//一趟归并,结果放在r中

length*=2;

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 军事

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1