和 小北 同学 没他强 大家帮我看下 一样是作业哈 (才学习)
#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");
}
}