栈的C语言实现
来源:互联网 发布:深圳朗尼科道闸软件 编辑:程序博客网 时间:2024/05/01 14:27
1。栈的C语言实现-数组实现
#include <stdio.h>
#include <stdlib.h>
#define DEP 3
int stack[DEP];
int top = -1;
void push(int value)
{
if(top < DEP - 1)
stack[++top] = value;
else
printf("stack is full/n");
}
int pop()
{
int temp;
if(top >= 0)
{
temp = stack[top];
top --;
return temp;
}
else
{
printf("stack is empty/n");
return -1;
}
}
void stack_print()
{
int temp = top;
if(temp < 0){
printf("stack is empty/n");
return;
}
while(temp >= 0)
{
printf("[%i]", stack[temp--]);
}
printf("/n");
}
int main(int argc, char *argv[])
{
int select, temp;
do
{
printf("1.Input a data/n");
printf("2.Output a data/n");
printf("3.Exit/n");
printf("Please input u choice:");
scanf("%d", &select);
switch(select)
{
case 1:
printf("Please input data:");
scanf("%d", &temp);
printf("/nBefor:/n");
stack_print();
push(temp);
printf("/nAfter:/n");
stack_print();
break;
case 2:
printf("/nBefor:/n");
stack_print();
if((temp = pop()) != -1)
printf("pop[%i]/n",temp);
printf("/nAfter:/n");
stack_print();
break;
default:
break;
}
}while(select != 3);
return 0;
}
2。栈的C语言实现-链表实现
#include <stdio.h>
#include <stdlib.h>
struct s_node
{
int data;
struct s_node *next;
};
typedef struct s_node node;
typedef node* link;
link stack = NULL;
void push(int value)
{
link newnode = (link)malloc(sizeof(node));
newnode->data = value;
newnode->next = stack;
stack = newnode;
}
int pop()
{
int temp;
if(stack != NULL)
{
temp = stack->data;
stack = stack->next;
return temp;
}
else
{
printf("empty stack");
return -1;
}
}
void stack_print()
{
link temp = stack;
while(temp != NULL)
{
printf("[%i]", temp->data);
temp = temp->next;
}
printf("/n");
}
int main(int argc, char *argv[])
{
int select, temp;
do{
printf("1.Input a data/n");
printf("2.Output a data/n");
printf("3.Exit/n");
printf("Please input u choice:");
scanf("%d", &select);
switch(select)
{
case 1:
printf("Please input data:");
scanf("%d", &temp);
printf("/nBefor:/n");
stack_print();
push(temp);
printf("/nAfter:/n");
stack_print();
break;
case 2:
printf("/nBefor:/n");
stack_print();
if((temp = pop()) != -1)
printf("pop[%i]/n",temp);
printf("/nAfter:/n");
stack_print();
break;
default:
break;
}
}while(select != 3);
return 0;
}
- 栈的C语言实现
- 栈的C语言实现
- C语言栈的实现
- 栈的C语言实现
- 栈的C语言实现
- 栈的c语言实现
- 栈的C语言实现
- 栈的c语言实现
- c语言栈的实现
- 栈的实现(C语言实现)
- 顺序栈的C语言实现
- 顺序栈的实现(C语言)
- C语言实现堆栈(栈)的数据结构
- 用c语言实现的链式栈
- 顺序栈的C语言实现
- C语言,简单栈的实现 Stack
- C语言 ----- 动态栈的算法实现
- 顺序栈的实现C语言
- 10句话说尽中国
- C#设计模式(9)-Prototype Pattern
- RIA的概念
- boost测试——string_algo(zz)
- 王道论坛 欢迎欢迎
- 栈的C语言实现
- ORACLE高效分页存储过程代码
- windows mobile WM 6.1MMS subject不超过40个字符添加方法
- javascript正则表达式练习
- 第一次
- ASP 中常见的错误 80004005 信息
- Android开发入门(-)之开发平台搭建
- 【原创】远程连接Orcle 10g数据库
- java代码生成器