栈的应用
来源:互联网 发布:怎么隐藏域名信息 编辑:程序博客网 时间:2024/05/17 23:50
*这一次写到栈了,继续加油....不知道还有没有什么地方可以改进
听别人说用主函数用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);
}
- 栈&栈的应用
- 栈的应用
- 栈的应用
- 简单的栈应用
- 栈的应用
- 栈的简单应用
- 栈的应用
- 栈的应用
- hdu1022栈的应用
- 栈的应用举例
- 栈的应用
- 栈的简单应用
- 数据结构栈的应用
- 栈的简单应用
- 栈的简单应用
- 栈的应用
- 栈的应用
- 栈的经典应用
- 中国移动:正同苹果商谈引进iPhone
- 了解 Java EE 5
- 老友记台词学习笔记 SE01EP01(七)
- Java语言中的两种异常
- 协议和路径信息
- 栈的应用
- Fedora 7+Tomcat 6.0.13+Apache 2.2.4+connectors整合过程
- 当前流行的J2EE WEB应用架构分析(转)
- 深入理解C语言指针的奥秘
- 解金山公司面试题
- 用dreamweaver来做flash透明效果
- 查看完整版本: 使用C语言编写提取通用shellcode的程序
- 关于阶乘的四个JAVA算法。
- Enable ASP.NET in IIS