LinkList_Stack(链式栈)
来源:互联网 发布:ubuntu修改hosts翻墙 编辑:程序博客网 时间:2024/06/06 05:35
#include<stdio.h>
typedef int Datatype;
typedef struct link_node {
Datatype info;
struct link_node *next;
}node;
node *init()
{
return NULL;
}
int empty(node *top)
{
return (!top ? 1 : 0);
}
Datatype read(node *top)
{
if (!empty(top))
return top->info;
else
{
printf("栈为空!\n");
return NULL;
}
}
void display(node *top)
{
if (!top)
printf("栈为空,无法进行输出!\n");
else
{
node *p = top;
while (p)
{
printf("%5d", p->info);
p = p->next;
}
}
}
node *insert(node *top, Datatype x)
{
node *p, *q;
p = (node *)malloc(sizeof(node));
p->info = x;
p->next = NULL;
if (top == NULL)
top = p;
else
{
p->next = top;
top = p;
}
return top;
}
node *dele(node *top)
{
node *p = top;
if (!p)
printf("栈为空,无法进行删除!\n");
else
{
top = top->next;
free(p);
}
return top;
}
void main()
{
node *top;
top = init();
int i;;
for (i = 0; i<10; i++)
top = insert(top, i * 2);
display(top);
putchar('\n');
printf("栈顶结点的值为:%d\n", top->info);
top = insert(top, 666);
printf("666进栈:");
display(top);
putchar('\n');
top= dele(top);
printf("删除栈顶的结点后为:");
display(top);
putchar('\n');
top = dele(top);
printf("删除栈顶的结点后为:");
display(top);
putchar('\n');
}
typedef int Datatype;
typedef struct link_node {
Datatype info;
struct link_node *next;
}node;
node *init()
{
return NULL;
}
int empty(node *top)
{
return (!top ? 1 : 0);
}
Datatype read(node *top)
{
if (!empty(top))
return top->info;
else
{
printf("栈为空!\n");
return NULL;
}
}
void display(node *top)
{
if (!top)
printf("栈为空,无法进行输出!\n");
else
{
node *p = top;
while (p)
{
printf("%5d", p->info);
p = p->next;
}
}
}
node *insert(node *top, Datatype x)
{
node *p, *q;
p = (node *)malloc(sizeof(node));
p->info = x;
p->next = NULL;
if (top == NULL)
top = p;
else
{
p->next = top;
top = p;
}
return top;
}
node *dele(node *top)
{
node *p = top;
if (!p)
printf("栈为空,无法进行删除!\n");
else
{
top = top->next;
free(p);
}
return top;
}
void main()
{
node *top;
top = init();
int i;;
for (i = 0; i<10; i++)
top = insert(top, i * 2);
display(top);
putchar('\n');
printf("栈顶结点的值为:%d\n", top->info);
top = insert(top, 666);
printf("666进栈:");
display(top);
putchar('\n');
top= dele(top);
printf("删除栈顶的结点后为:");
display(top);
putchar('\n');
top = dele(top);
printf("删除栈顶的结点后为:");
display(top);
putchar('\n');
}
阅读全文
0 0
- LinkList_Stack(链式栈)
- 栈(链式)
- 栈(链式)
- 栈(三):链式栈
- (二)调试链式栈
- 链式存储结构(栈)
- 数据结构5(链式栈)
- 数据结构栈(链式实现)
- 链式栈(c++实现)
- Java栈(链式实现)
- 数据结构(顺序单链表、链式单链表、顺序栈、链式栈、顺序队列、链式队列)
- 链式栈
- 链式栈
- 链式栈
- 链式栈
- 链式栈
- 链式栈
- 链式栈
- <C++ Primer_5th>习题_3.22
- 167. Two Sum II
- <C++ Primer_5th>习题_3.23
- Spring框架-第一弹
- ios-frame和bounds
- LinkList_Stack(链式栈)
- 第2篇 SercureCRT使用root远程登录ubuntu16.04
- <C++ Primer_5th>习题_3.24
- CodeForces
- <C++ Primer_5th>习题_3.25
- <C++ Primer_5th>习题_3.31
- 3 文本编辑器vim
- 基于Excel的QR二维码生成工具——原理及算法详解(之七)
- <C++ Primer_5th>习题_3.35