主题:数据结构--栈的应用
[b][size=4][color=FF00FF]我的blog,[url=http://www.ebainaoo.cn]http://www.ebainaoo.cn[/url]欢迎大家来踩[/color][/size][/b]
/*这一次写到栈了,继续加油....不知道还有没有什么地方可以改进
听别人说用主函数用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);
}
/*这一次写到栈了,继续加油....不知道还有没有什么地方可以改进
听别人说用主函数用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);
}