把我的 C 作业贴出来 实验二 栈和队列

实验二 栈和队列

一、 实验目的

1.熟悉栈和队列的顺序和链式存储结构

2.掌握栈和队列的基本运算

3.能够利用栈和队列的基本运算完成栈和队列应用的运算

二、 实验内容

1.设单链表中存放有n个字符,试编写算法,判断该字符串是否有中心对称的关系,例如xyzzyx是中心对称的字符串。(提示:将单链表中的一半字符先依次进栈,然后依次出栈与单链表中的另一半字符进行比较。)(文件夹:习题10_3

typedef char datatype;

/*定义单链表结构类型*/

typedef struct node

{ datatype data;

struct node *next;

}linklist;

/*定义顺序栈结构类型*/

/*const int maxsize=40;*/

#define maxsize 40

typedef struct

{ datatype elements[maxsize];

int top;

}stack;

#include <stdio.h>

#include <stdlib.h>

#include "Project_10_3.h"

int symmetry( linklist * head , stack * s );

int main(int argc, char *argv[])

{

linklist * head;

stack *s;

datatype str[80];

/*

cin>>str;

*/

gets(str);

head = creat(str);

printlink(head);

setnull(s);printf("%d/n",length(head));

/*

if(symmetry(head,s)) cout<<"字符串/""<<str<<"/"中心对称/n";

else cout<<"字符串/""<<str<<"/"不是中心对称/n";

*/

if(symmetry(head,s))printf("字符串/"%s/"中心对称./n",str);

else printf("字符串/"%s/"不是中心对称./n",str);

puts(str);

system("PAUSE");

return 0;

}

#include <stdio.h>

#include <stdlib.h>

#include "Project_10_3.h"

/*建立具有头结点的单链表*/

linklist* creat( datatype * str )

{ datatype *p=str;

linklist * head ;

linklist *s,*r;

head =(linklist*)malloc(sizeof(linklist));

r=head;

while(*p!='/0')

{

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

s->data=*p;

r->next=s;

r=s;

p++;

}

r->next=NULL;

return head;

}

/*求单链表长度*/

int length(linklist*head)

{

linklist *p=head->next;

int n=0;

while(p!=NULL)

{

n++;

p=p->next;

}

return n;

}

/*输出单链表*/

void printlink(linklist*head)

{ linklist *p = head->next;

while(p!=NULL)

{

/* cout<<p->data;*/

printf("%c",p->data);

p=p->next;

}

/*cout<<endl;*/

printf("/n");

return;

}

#include <stdio.h>

#include <stdlib.h>

#include "Project_10_3.h"

/*置栈空*/

void setnull(stack *s)

{

/*s=new stack;*/

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

s->top=-1;

return;

}

/*顺序栈入栈.h*/

void push(stack*s,datatype e)

{

s->top++;

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

return;

}

/*顺序栈出栈*/

datatype pop(stack*s)

{

datatype temp;

temp=s->elements[s->top];

s->top--;

return temp;

}

#include <stdio.h>

#include <stdlib.h>

#include "Project_10_3.h"

int symmetry(linklist*head,stack*s)

{

int n=length(head)/2;

linklist*p=head->next;

int i;

for(i=0;i<n;i++){

push(s,p->data);

p=p->next;

}

if(length(head)%2==1)p=p->next;

while(p!=NULL)

{

if(pop(s)==p->data)

{

p=p->next;continue;

}

else return 0;

}

return 1;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值