主题:再发一贴(数据结构--顺序循环队列)
[color=0000FF]呵呵,各位编程爱好者辛苦了,本人除了爱好编程外同时也爱好制作网页,我的个人网站开通了,呵呵,希望各位朋友们光临,还希望能和爱好网页的朋友们互相交流http://online366.cctve.cn/ 我们是学生创业精英联盟[/color]
/*这一次写到栈了,继续加油....不知道还有没有什么地方可以改进
听别人说用主函数用void main()还是int main()好一些?有什么区别*/
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "conio.h"
#define MAX 50
typedef struct node
{
int data;
struct node *next;
}Stacknode,*Linkstack;
Linkstack Init_Linkstack()
{
return NULL;
}
Linkstack Push_Linkstack(Linkstack top,int x)
{
Stacknode *s;
s=new Stacknode;
s->data=x;
s->next=top;
top=s;
return top;
}
Linkstack Pop_Linkstack(Linkstack top,int *x)
{
Stacknode *s;
if(top==NULL)
return NULL;
else
{
*x=top->data;
s=top;
top=top->next;
free(s);
return top;
}
}
void Print_Linkstack(Linkstack top)
{
Stacknode *s;
s=top;
while(s->next!=NULL)
{
printf("%d->",s->data);
s=s->next;
}
printf("%d",s->data);
}
typedef struct
{
int data[MAX];
int top;
}Seqstack;
Seqstack *Init_Seqstack()
{
Seqstack *s;
s=new Seqstack;
s->top=-1;
return s;
}
int Empty_Seqstack(Seqstack *s)
{
if(s->top==-1)
return 1;
else
return 0;
}
int Push_Seqstack(Seqstack *s,int x)
{
if(s->top==MAX-1)
return 0;
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
int Pop_Seqstack(Seqstack *s,int *x)
{
if(Empty_Seqstack(s))
return 0;
else
{
*x=s->data[s->top];
s->top--;
return 1;
}
}
void coversion(int N,int r)
{
int x;
Seqstack *s;
s=Init_Seqstack();
printf("\n十进制%d转换为%d进制为:",N,r);
while(N)
{
Push_Seqstack(s,N%r);
N=N/r;
}
while(!Empty_Seqstack(s))
{
Pop_Seqstack(s,&x);
printf("%d",x);
}
}
void main()
{
Linkstack top;
int N,r;
int k,x;
top=Init_Linkstack();
do{
printf("\n\n\n");
printf("\t\t\t 栈应用子系统 \n");
printf("\t\t\t*************************\n");
printf("\t\t\t** 1--链式进栈 **\n");
printf("\t\t\t** 2--链式出栈 **\n");
printf("\t\t\t** 3--链栈显示 **\n");
printf("\t\t\t** 4--进制转换 **\n");
printf("\t\t\t** 0--返 回 **\n");
printf("\t\t\t*************************\n");
printf("\t\t\t 请选择菜单(0-3):");
scanf("%d",&k);
switch(k)
{
case 1:
printf("\n请输入进栈的元素x:");
scanf("%d",&x);
top=Push_Linkstack(top,x);
printf("\n元素已进栈!按任意键返回..");
getch();
system("cls");
break;
case 2:
top=Pop_Linkstack(top,&x);
printf("\n头元素已出栈!按任意键返回..");
getch();
system("cls");
break;
case 3:
printf("\n链栈元素为:");
Print_Linkstack(top);
printf("\n按任意键返回..");
getch();
system("cls");
break;
case 4:
printf("\n请输入十进制N和要转换的进制r(N,r):");
scanf("%d,%d",&N,&r);
coversion(N,r);
printf("\n按任意键返回..");
getch();
system("cls");
break;
}
}while(k!=0);
}
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "conio.h"
#define MAX 80
typedef struct
{
int data[MAX];
int front,rear;
int num;
}SeQue;
SeQue *Init_SeQue()
{
SeQue *s;
s=new SeQue;
s->front=s->rear=MAX-1;
s->num=0;
return s;
}
int Empty_SeQue(SeQue *s)
{
if(s->num==0)
return 1;
else
return 0;
}
int In_SeQue(SeQue *s,int x)
{
if(s->num==MAX)
return 0;
else
{
s->rear=(s->rear+1)%MAX;
s->data[s->rear]=x;
s->num++;
return 1;
}
}
int Out_SeQue(SeQue *s,int *x)
{
if(Empty_SeQue(s))
return 0;
else
{
s->front=(s->front+1)%MAX;
*x=s->data[s->front];
s->num--;
return 1;
}
}
void Print_SeQue(SeQue *s)
{
int i,n;
i=(s->front+1)%MAX;
n=s->num;
while(n>0)
{ printf("%d ",s->data[i]);
i=(i+1)%MAX;
n--;
}
}
void main()
{
SeQue *s;
int k,flag,x;
s=Init_SeQue();
do{
printf("\n\n\n");
printf("\t\t\t 循环顺序队列\n");
printf("\t\t\t***********************\n");
printf("\t\t\t** 1-入 队 **\n");
printf("\t\t\t** 2-出 队 **\n");
printf("\t\t\t** 3-判 队 空 **\n");
printf("\t\t\t** 4-队列显示 **\n");
printf("\t\t\t** 0-返 回 **\n");
printf("\t\t\t***********************\n");
printf("\t\t\t 请输入菜单项(0-4):");
scanf("%d",&k);
switch(k)
{
case 1:
printf("\n请输入入队元素:");
scanf("%d",&x);
flag=In_SeQue(s,x);
if(flag==0)
printf("\n队满不能入队!按任意键返回..");
else
printf("\n元素已入队!按任意键返回..");
getch();
system("cls");
break;
case 2:
flag=Out_SeQue(s,&x);
if(flag==0)
printf("\n队列空出队失败!按任意键返回..");
else
printf("\n队列头元素已出队~!按任意键返回..");
getch();
system("cls");
break;
case 3:
flag=Empty_SeQue(s);
if(flag==1)
printf("\n该队列为空!按任意键返回..");
else
printf("\n该队列不为空!按任意键返回..");
getch();
system("cls");
break;
case 4:
printf("\n该队列元素为:");
Print_SeQue(s);
printf("\n按任意键返回..");
getch();
system("cls");
break;
}
}while(k!=0);
}
/*这一次写到栈了,继续加油....不知道还有没有什么地方可以改进
听别人说用主函数用void main()还是int main()好一些?有什么区别*/
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "conio.h"
#define MAX 50
typedef struct node
{
int data;
struct node *next;
}Stacknode,*Linkstack;
Linkstack Init_Linkstack()
{
return NULL;
}
Linkstack Push_Linkstack(Linkstack top,int x)
{
Stacknode *s;
s=new Stacknode;
s->data=x;
s->next=top;
top=s;
return top;
}
Linkstack Pop_Linkstack(Linkstack top,int *x)
{
Stacknode *s;
if(top==NULL)
return NULL;
else
{
*x=top->data;
s=top;
top=top->next;
free(s);
return top;
}
}
void Print_Linkstack(Linkstack top)
{
Stacknode *s;
s=top;
while(s->next!=NULL)
{
printf("%d->",s->data);
s=s->next;
}
printf("%d",s->data);
}
typedef struct
{
int data[MAX];
int top;
}Seqstack;
Seqstack *Init_Seqstack()
{
Seqstack *s;
s=new Seqstack;
s->top=-1;
return s;
}
int Empty_Seqstack(Seqstack *s)
{
if(s->top==-1)
return 1;
else
return 0;
}
int Push_Seqstack(Seqstack *s,int x)
{
if(s->top==MAX-1)
return 0;
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
int Pop_Seqstack(Seqstack *s,int *x)
{
if(Empty_Seqstack(s))
return 0;
else
{
*x=s->data[s->top];
s->top--;
return 1;
}
}
void coversion(int N,int r)
{
int x;
Seqstack *s;
s=Init_Seqstack();
printf("\n十进制%d转换为%d进制为:",N,r);
while(N)
{
Push_Seqstack(s,N%r);
N=N/r;
}
while(!Empty_Seqstack(s))
{
Pop_Seqstack(s,&x);
printf("%d",x);
}
}
void main()
{
Linkstack top;
int N,r;
int k,x;
top=Init_Linkstack();
do{
printf("\n\n\n");
printf("\t\t\t 栈应用子系统 \n");
printf("\t\t\t*************************\n");
printf("\t\t\t** 1--链式进栈 **\n");
printf("\t\t\t** 2--链式出栈 **\n");
printf("\t\t\t** 3--链栈显示 **\n");
printf("\t\t\t** 4--进制转换 **\n");
printf("\t\t\t** 0--返 回 **\n");
printf("\t\t\t*************************\n");
printf("\t\t\t 请选择菜单(0-3):");
scanf("%d",&k);
switch(k)
{
case 1:
printf("\n请输入进栈的元素x:");
scanf("%d",&x);
top=Push_Linkstack(top,x);
printf("\n元素已进栈!按任意键返回..");
getch();
system("cls");
break;
case 2:
top=Pop_Linkstack(top,&x);
printf("\n头元素已出栈!按任意键返回..");
getch();
system("cls");
break;
case 3:
printf("\n链栈元素为:");
Print_Linkstack(top);
printf("\n按任意键返回..");
getch();
system("cls");
break;
case 4:
printf("\n请输入十进制N和要转换的进制r(N,r):");
scanf("%d,%d",&N,&r);
coversion(N,r);
printf("\n按任意键返回..");
getch();
system("cls");
break;
}
}while(k!=0);
}
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "conio.h"
#define MAX 80
typedef struct
{
int data[MAX];
int front,rear;
int num;
}SeQue;
SeQue *Init_SeQue()
{
SeQue *s;
s=new SeQue;
s->front=s->rear=MAX-1;
s->num=0;
return s;
}
int Empty_SeQue(SeQue *s)
{
if(s->num==0)
return 1;
else
return 0;
}
int In_SeQue(SeQue *s,int x)
{
if(s->num==MAX)
return 0;
else
{
s->rear=(s->rear+1)%MAX;
s->data[s->rear]=x;
s->num++;
return 1;
}
}
int Out_SeQue(SeQue *s,int *x)
{
if(Empty_SeQue(s))
return 0;
else
{
s->front=(s->front+1)%MAX;
*x=s->data[s->front];
s->num--;
return 1;
}
}
void Print_SeQue(SeQue *s)
{
int i,n;
i=(s->front+1)%MAX;
n=s->num;
while(n>0)
{ printf("%d ",s->data[i]);
i=(i+1)%MAX;
n--;
}
}
void main()
{
SeQue *s;
int k,flag,x;
s=Init_SeQue();
do{
printf("\n\n\n");
printf("\t\t\t 循环顺序队列\n");
printf("\t\t\t***********************\n");
printf("\t\t\t** 1-入 队 **\n");
printf("\t\t\t** 2-出 队 **\n");
printf("\t\t\t** 3-判 队 空 **\n");
printf("\t\t\t** 4-队列显示 **\n");
printf("\t\t\t** 0-返 回 **\n");
printf("\t\t\t***********************\n");
printf("\t\t\t 请输入菜单项(0-4):");
scanf("%d",&k);
switch(k)
{
case 1:
printf("\n请输入入队元素:");
scanf("%d",&x);
flag=In_SeQue(s,x);
if(flag==0)
printf("\n队满不能入队!按任意键返回..");
else
printf("\n元素已入队!按任意键返回..");
getch();
system("cls");
break;
case 2:
flag=Out_SeQue(s,&x);
if(flag==0)
printf("\n队列空出队失败!按任意键返回..");
else
printf("\n队列头元素已出队~!按任意键返回..");
getch();
system("cls");
break;
case 3:
flag=Empty_SeQue(s);
if(flag==1)
printf("\n该队列为空!按任意键返回..");
else
printf("\n该队列不为空!按任意键返回..");
getch();
system("cls");
break;
case 4:
printf("\n该队列元素为:");
Print_SeQue(s);
printf("\n按任意键返回..");
getch();
system("cls");
break;
}
}while(k!=0);
}