5844吴波数据结构实验四.docx

上传人:b****0 文档编号:17761806 上传时间:2023-08-03 格式:DOCX 页数:10 大小:878.60KB
下载 相关 举报
5844吴波数据结构实验四.docx_第1页
第1页 / 共10页
5844吴波数据结构实验四.docx_第2页
第2页 / 共10页
5844吴波数据结构实验四.docx_第3页
第3页 / 共10页
5844吴波数据结构实验四.docx_第4页
第4页 / 共10页
5844吴波数据结构实验四.docx_第5页
第5页 / 共10页
5844吴波数据结构实验四.docx_第6页
第6页 / 共10页
5844吴波数据结构实验四.docx_第7页
第7页 / 共10页
5844吴波数据结构实验四.docx_第8页
第8页 / 共10页
5844吴波数据结构实验四.docx_第9页
第9页 / 共10页
5844吴波数据结构实验四.docx_第10页
第10页 / 共10页
亲,该文档总共10页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

5844吴波数据结构实验四.docx

《5844吴波数据结构实验四.docx》由会员分享,可在线阅读,更多相关《5844吴波数据结构实验四.docx(10页珍藏版)》请在冰点文库上搜索。

5844吴波数据结构实验四.docx

5844吴波数据结构实验四

数据结构实验报告

实验三栈和队列的应用

 

姓名:

吴波

学号:

20115844

班级:

计科2011-01

设计时间:

8/52013

 

1.判断一个表达式中括号是否配对

源代码:

#include

#include

#defineMaxsize100/*数组最大界限*/

typedefcharElemType;/*数据元素类型*/

typedefstruct

{

ElemTypea[Maxsize];/*一维数组子域*/

inttop;/*栈顶指针子域*/

}SqStack;/*栈的顺序结构体类型*/

SqStacks1;//定义一个

/*函数声明*/

voidinit_s(SqStack*s);

voidout_s(SqStacks);

voidpush(SqStack*s,ElemTypee);

boolempty(SqStack*s);

ElemTypepop(SqStack*s);

/*主函数*/

int

main(void)

{

inti,k=0;//k作为只有“】”的标记

ElemTypee;

ElemTypech[Maxsize];

while

(1)

{//无线循环做判断

init_s(&s1);/*初始化一个空栈*/

k=0;//初始化标志;

printf("请输入要判断的表达式,按回车键结束:

\n");

gets(ch);

for(i=0;ch[i]!

='\0';i++)

{

if(ch[i]=='[')

push(&s1,ch[i]);

elseif(ch[i]==']')

{

if((empty(&s1)==1))

k=1;

else

pop(&s1);

}

else;

}

if(k==1||(empty(&s1)==0))

printf("\n【】符号数量不配对\n");

elseif((empty(&s1)==1))

printf("\n【】符号数量配对\n");

else;

}

}

/*初始化空栈函数*/

void

init_s(SqStack*s)

{

s->top=-1;

}

/*判空函数*/

bool

empty(SqStack*s)

{

if(s->top==-1)

return1;

else

return0;

}

/*输出栈的内容*/

void

out_s(SqStacks)

{

charch;

inti;/*千万不能修改栈顶指针top*/

if(s.top==-1)

printf("\nStackisNULL.");

else

{

i=s.top;

while(i!

=-1)

{

printf("\ndata%d=%c",i+1,s.a[i]);

i--;

}

}

}

/*进栈函数*/

void

push(SqStack*s,ElemTypee)

{

if(s->top==Maxsize-1)

printf("你输入的表达式超出范围");

else

{

s->top++;

s->a[s->top]=e;

}

}

/*出栈函数*/

ElemType

pop(SqStack*s)

{

ElemTypex;

if(s->top==-1)

{

printf("\nStackisUnderflow!

");

x=-1;

}

else

{

x=s->a[s->top];

s->top--;

}

return(x);

}

运行结果:

2.判断一个字符序列是否为回文,请用链队实现。

#include

#include

#include

#include

#defineMaxsize200

typedefcharElemType;

typedefstructQNode

{

ElemTypedata;

structQNode*next;

}QNode;

typedefstruct

{

QNode*front,*rear;

}L_Queue;

L_QueueQ1;//定义一个链头

/*函数定义*/

voidinit_Q(L_Queue*Q);

voidout_Q(L_QueueQ);

voidEnQueue(L_Queue*Q,ElemTypee);

voiddisplay(L_QueueQ);

boolTest(L_QueueQ,inti,intMax);

ElemTypefind(L_Queue*Q,intn);

voidDeQueue(L_Queue*Q);

/*主函数*/

int

main(void)

{

ElemTypee;

while

(1)

{//设置无线循环;

init_Q(&Q1);//初始化队列;

intk=0;

printf

("请输入一个要判断的字符串,按回车判断是否为回文!

\n");

while((e=getchar())!

='\n')

{

EnQueue(&Q1,e);

k++;

}

display(Q1);

if(Test(Q1,1,k)==1)

printf("\n您输入的字符是回文格式\n");//k表示个数,1表示从一开始比较,

else

printf("\n您输入的字符不是回文格式\n");

}

}

/*初始化函数*/

void

init_Q(L_Queue*Q)

{

QNode*p;

p=(QNode*)malloc(sizeof(QNode));

p->next=NULL;

Q->front=p;

Q->rear=p;

}

/*进队函数*/

void

EnQueue(L_Queue*Q,ElemTypee)

{

QNode*s;

s=(QNode*)malloc(sizeof(QNode));

s->data=e;

s->next=NULL;

Q->rear->next=s;

Q->rear=s;

}

/*按位查寻字符函数*/

ElemType

find(L_Queue*Q,intn)

{

QNode*q=Q->front;

inti=0;

for(i=0;i

{

q=q->next;

}

returnq->data;

}

//输出队列函数//

void

display(L_QueueQ)

{

QNode*q=Q.front;

while(q->next!

=NULL)

{

q=q->next;

printf("%c\t",q->data);

}

}

//用递归的方法测试是否相同函数//

bool

Test(L_QueueQ,inti,intMax)

{

if(i>Max)

returntrue;

elseif(find(&Q1,i)==find(&Q1,Max-i+1))

returnTest(Q,i+1,Max);

else

returnfalse;

}

运行结果:

 

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

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2