| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1172 人关注过本帖
标题:数据结构(栈。队列)程序
取消只看楼主 加入收藏
andself
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-9-23
收藏
 问题点数:0 回复次数:3 
数据结构(栈。队列)程序

和 小北 同学 没他强 大家帮我看下 一样是作业哈 (才学习)

#include<stdio.h>
#define maxsize 10
#define M 10
typedef struct
{
int data[maxsize];
int front;
int rear;
}sqqueue;

int sqinit(sqqueue *p) /*装入队列*/
{
p->front=0;
p->rear=0;
return 1;
}

int enqueue(sqqueue *q, int e) /*入队*/
{
if((q->rear+1)%maxsize==q->front)
return 0;
else
q->data[q->rear]=e;
q->rear=(q->rear+1)%maxsize;
return 1;
}

int dequeue(sqqueue *q) /*出队*/
{
int e;
if (q->front==q->rear)
return 0;
e=q->data[q->front];
q->front=(q->front+1)%maxsize;
return e;
}

int empty(sqqueue *q)
{
int v;
if (q->front==q->rear)
v=1;
else
v=0;

return v;
}

int gethead(sqqueue *q)
{
int e;
if (q->front==q->rear)
e=-1;
else
e=q->data[q->front];
return e;
}

void display_sqq(sqqueue *q)
{
int s;
s=q->front;
printf("The sequeue is :\n");
if (q->front==q->rear)
printf("The sequeue is empty!\n");
else
{
while(s<q->rear)
{
printf("-->%d", q->data[s]);
s=(s+1)%maxsize;
}
printf("\n");
}
}

menu_Sqque()
{
int select;
printf(" _______________________________\n");
printf(" | |\n");
printf(" | * SQUEUE MENU * |\n");
printf(" | *(1)**** Enqueue |\n");
printf(" | *(2)**** Dequeue |\n");
printf(" | *(3)**** Empty |\n");
printf(" | *(4)**** Gethead |\n");
printf(" | *(5)**** Display |\n");
printf(" | *(6)**** MAIN MENU |\n");
printf(" |_______________________________|\n\n");
printf("Please select (1--6): ");
scanf("%d",&select);
return(select);
}


Sqque(sqqueue *head)
{
int n,i,m;
int x,y,select;
int xq,a=1,ch=1;
clrscr();
printf("Create a empty sequeue.\n");
sqinit(head);
printf("Please input the sequeue's length:(maxsize=%d)\n",maxsize);
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Please input %d'th sequeue value:\n",a);
a++;
scanf("%d",&m);
enqueue(head,m);
}
while(ch)
{
clrscr();
printf("head-->rear:%d\n",head->rear);
printf("head-->front:%d\n",head->front);
display_sqq(head);
select=menu_Sqque();
if(select>0&&select<7)
{
if(select==1)
{
printf("Please input a value :\n ");
scanf("%d",&x);
enqueue(head,x);
display_sqq(head);
}
else if(select==2)
{
dequeue(head);
display_sqq(head);
}
else if(select==3)
{
if(empty(head))
printf("The sequeue is empty\n");
else
printf("The sequeue is full\n");
}
else if(select==4)
{
y=gethead(head);
printf("Output head value:%d\n",y);
}
else if(select==5)
{
display_sqq(head);
}
else if(select==6) return(1);
printf("(1)Go on (0)EXIT\n");
scanf("%d",&ch);
}
else printf("ERROR\n\n");
}

}


typedef struct
{
int stack[M];
int top;
}stackstru;

init(stackstru *s) /*装入栈*/
{
s->top=0;
return 1;
}

int push(stackstru *s,int x) /*入栈操作*/
{
if (s->top==M)
printf("The stack is overflow!\n");
else
{
s->top=s->top+1;
s->stack[s->top]=x;
}
}

void display_stac(stackstru *s) /*显示栈所有数据*/
{
if(s->top==0)
printf("The stack is empty!\n");
else
{
while(s->top!=0)
{
printf("%d-->",s->stack[s->top]);
s->top=s->top-1;
}
}
}

int pop(stackstru *s) /*出栈操作并返回被删除的那个记录*/
{
int y;
if(s->top==0)
printf("The stack is empty!\n");
else
{
y=s->stack[s->top];
s->top=s->top-1;
return y;
}
}

int gettop(stackstru *s) /*得到栈顶数*/
{
int e;
if(s->top==0)
return 0;
else
e=s->stack[s->top];
return e;
}

menu_stacks()
{
int select;
printf(" _______________________________\n");
printf(" | |\n");
printf(" | * STACKSTRU MENU * |\n");
printf(" | *(1)**** display |\n");
printf(" | *(2)**** Push |\n");
printf(" | *(3)**** Pop |\n");
printf(" | *(4)**** Gettop |\n");
printf(" | *(5)**** MAIN MENU |\n");
printf(" |_______________________________|\n\n");
printf("Please select (1--5): ");
scanf("%d",&select);
return(select);
}

Stacks(stackstru *p)
{
int n,i,k;
int h,x1,x2;
int select,a=1,b=1;
clrscr();
printf("Create a empty stack!\n");
init(p);
printf("Input a stack length(maxsize=%d):\n",M);
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Input the %d'th stack value:\n",b);
b++;
scanf("%d",&k);
push(p,k);
}

while(a)
{
clrscr();
printf("\n\n\n");
select=menu_stacks();
if(select>0&&select<6)
{
if(select==1)
{
display_stac(p);

}
else if(select==2)
{
printf("Input a push value:\n");
scanf("%d",&h);
push(p,h);
display_stac(p);

}
else if(select==3)
{
x1=pop(p);
printf("x1-->%d\n",x1);
display_stac(p);

}
else if(select==4)
{
x2=gettop(p);
printf("x2-->%d\n",x2);

}

else if(select==5) return(1);
printf("\n(1)Go on (0)EXIT\n");
scanf("%d",&a);
}
else printf("ERROR\n\n");
}
}


main_menu()
{
int select;
printf(" _______________________________\n");
printf(" | |\n");
printf(" | * MAIN MENU * |\n");
printf(" | *(1)**** Sqqueue |\n");
printf(" | *(2)**** Stackstru |\n");
printf(" | *(3)**** EXIT |\n");
printf(" |_______________________________|\n\n");
printf("Please select (1--3): ");
scanf("%d",&select);
return(select);
}


main()
{
int select;
stackstru *p;
sqqueue *head;
while(1)
{
clrscr();
select=main_menu();
if(select>0&&select<4)
{
if(select==1)
Sqque(head);
else if(select==2)
Stacks(p);
else if(select==3)
return(1);
}
else printf("ERROR");
}
}

搜索更多相关主题的帖子: 数据结构 队列 int rear front 
2007-10-23 17:08
andself
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-9-23
收藏
得分:0 
因为才学 好多地方不是很好 可能有些地方 还有算法错误  大家 帮忙看看

2007-10-23 17:13
andself
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-9-23
收藏
得分:0 

(回复4楼)没有 我自己想的 感觉可以就这样弄了 可以运行 正常呢 但不知道 有什么后遗症么?


2007-10-23 22:24
andself
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-9-23
收藏
得分:0 
还有 栈那里的 输出 那里有错误 老是显示 栈空
不知道 怎么改 大家帮忙

2007-10-26 12:48
快速回复:数据结构(栈。队列)程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025825 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved