数据结构程序Word下载.docx

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

数据结构程序Word下载.docx

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

数据结构程序Word下载.docx

n-1;

q>

=p;

q--)

element[q+1]=palist->

element[q];

element[p]=x;

n=palist->

n+1;

return1;

intdeleteP_seq(PSeqListpalist,intp)

n-1)

{

for(q=p;

q<

n-1;

q++)

element[q]=palist->

element[q+1];

LinkListcreateNullList_link(void)

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

//申请表头结点空间

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

if(llist!

llist->

link=NULL;

elseprintf("

//创建失败

return(llist);

intinsertPost_link(LinkListllist,Node*p,DataTypex){

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

Node*q=(Node*)malloc(sizeof(structNode));

//申请新结点

if(q==NULL){

return(0);

else{

q->

info=x;

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&

&

link->

info!

=x)

p=p->

//找值为x的结点的前驱结点的存储位置

if(p->

link==NULL){//没找到值为x的结点

}else{

q=p->

//找到值为x的结点

link=q->

//删除该结点

free(q);

#include<

stdio.h>

stdlib.h>

#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的元素的值

Outelement%d\n"

w);

//元素出列

deleteP_seq(palist,s1);

//删除出列的元素

voidmain()

inti,n,s,m;

\npleaseinputthevalues(<

100)ofn="

scanf("

%d"

&

n);

pleaseinputthevaluesofs="

s);

pleaseinputthevaluesofm="

m);

PSeqListjos_alist=createNullList_seq(n);

//创建空顺序表

if(jos_alist!

=NULL)

for(i=0;

i<

n;

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(i<

n&

j<

t->

n)//反复比较

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("

);

else

pastack->

t=pastack->

t+1;

s[pastack->

t]=x;

voidpop_seq(PSeqStackpastack)

if(pastack->

t==-1)

Underflow!

t-1;

voidpush_link(PLinkStackplstack,DataTypex)

PNodep;

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

if(p==NULL)printf("

link=plstack->

top;

plstack->

top=p;

voidpop_link(PLinkStackplstack)

if(isEmptyStack_link(plstack))printf("

Emptystackpop.\n"

p=plstack->

top=plstack->

top->

free(p);

mazeFrame(void){

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

把入口位置压入栈中;

while栈不空{

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

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

取下一个试探位置;

if(下一个位置是出口)

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

if(下一个位置是通道)

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

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

初始设置:

当前位置为[x1,y1]

while!

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

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

当前位置=下一位置;

当前位置设为已走标志;

当前位置=上一位置;

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

输出当前走的路径

输出:

不存在路径

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

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

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

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

if(当前位置=终点)then

voidenQueue_seq(PSeqQueuepaqu,DataTypex)

if((paqu->

r+1)%paqu->

MAXNUM==paqu->

f)

Fullqueue.\n"

paqu->

q[paqu->

r]=x;

r=(paqu->

MAXNUM;

voiddeQueue_seq(PSeqQueuepaqu)

if(paqu->

f==paqu->

r)

EmptyQueue.\n"

f=(paqu->

f+1)%paqu->

voidenQueue_link(PLinkQueueplqu,Datatypex)

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

//申请新结点空间

if(p==NULL)printf("

"

//填写新结点信息

if(plqu->

f==NULL)plqu->

f=p;

//插入前是空队列

elseplqu->

r->

link=p;

//将新结点插入

plqu->

r=p;

//修改对尾指针

voiddeQueue_link(PLinkQueueplqu)

if(plqu->

f==NULL)

Emptyqueue.\n"

//队列已空

{

p=plqu->

f;

f=p->

//修改队头指针

//释放已经删除结点空间

voidpreOrder(BinTreet){

if(t==NULL)return;

visit(root(t));

preOrder(leftChild(t));

preOrder(rightChild(t));

voidinOrder(BinTreet){

inOrder(leftChild(t));

inOrder(rightChild(t));

voidpostOrder(BinTreet){

postOrder(leftChild(t));

postOrder(rightChild(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!

visit(root(c));

//访问

push(s,rightChild(c));

//右子树进栈

push(s,leftChild(c));

//左子树进栈

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

RecordNodetemp;

for(i=1;

pvector->

i++)//依次插入记录R1,R2…Rn-1

temp=pvector->

record[i];

//本次待插入的记录

j=i-1;

//由后向前找插入位置

while((temp.key<

record[j].key)&

(j>

=0))

record[j+1]=pvector->

record[j];

j--;

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

if(j!

=(i-1))pvector->

record[j+1]=temp;

voidselectSort(SortObject*pvector)

inti,j,k;

i++)//做n-1趟选择排序

k=i;

for(j=i+1;

j++)//在无序区内找出排序码最小的记录Rk

if(pvector->

record[j].key<

record[k].key)

k=j;

if(k!

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

record[i]=pvector->

record[k];

record[k]=temp;

}

voidbubbleSort(SortObject*pvector)

inti,j,noswap;

for(i=0;

i<

pvector->

i++)//做n-1次起泡

noswap=TRUE;

//置交换标志初态

for(j=0;

j<

n-i-1;

j++)

record[j+1].key<

record[j].key)//从前向后扫描

//交换记录

record[j]=pvector->

record[j+1];

noswap=FALSE;

//修改交换标志

if(noswap)break;

//本趟起泡未发生记录交换,算法结束

voidquickSort(SortObject*pvector,intL,intR)

if(L>

=R)return;

//只有一个记录或无记录,则无须排序

i=L;

j=R;

temp=pvector->

while(i!

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

while((pvector->

record[j].key>

=temp.key)&

(j>

i))j--;

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

if(i<

j)pvector->

record[i++]=pvector->

record[j];

record[i].key<

i))i++;

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

record[j--]=pvector->

record[i]=temp;

//找到Rl的最终位置

quickSort(pvector,L,i-1);

//递归处理左区间

quickSort(pvector,i+1,R);

//递归处理右区间

voidmergeSort(SortObject*pvector)

RecordNoderecord[pvector->

n];

intlength=1;

while(length<

mergePass(pvector->

record,record,pvector->

n,length);

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

length*=2;

mergePass(record,pvector->

record,pvector->

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

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

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

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

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