文章给出用c语言实现数据结构中堆栈功能的源程序,程序在visual c++6.0环境中调试通过,并提供源代码文本下载:
源代码下载:实现链栈结构的 C 语言源代码.cpp
// stacklisk.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "malloc.h"
//定义链栈节点类型
typedef struct node
{
int value;
struct node *next;
}NODE;
//定义链栈节点类型变量,栈顶,临时,栈底指针变量
NODE *top,*s,*bottom ;
//定义空栈
void empty()
{
top=NULL;
}
//压栈
NODE *push(int element_value)
{
s=(NODE*)malloc(sizeof(NODE));
s->value=element_value;
s->next=top;
//返回栈底指针
if(top==NULL)
{
bottom=s;
}
top=s;
return top;
}
//出栈
void pop()
{
int y;
if(top==NULL)
{
printf("stack is empty ");
return;
}
s=top;
y=top->value;
top=top->next;
free(s);
printf("pop element value is %d ",y);
}
void main()
{
int b,n,i;
empty();
printf("How many times do you want to push and to pop? ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the %d times push value:",i);
scanf("%d",&b);
push(b);
}
printf(" The value of bottom=%d ",bottom->value);
for(i=1;i<=n;i++)
{
pop();
}
//测试空栈
pop() ;
}