标签:
//c语言实现栈及其基本操作
//杨鑫
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef int ElementType;
typedef struct node {
ElementType data;
struct node *next;
}StackNode, *LinkStack;
void init_stack(LinkStack top)
{
top->next = NULL;
}
int is_empty(LinkStack top)
{
if(top->next == NULL)
return TRUE;
return FALSE;
}
int push(LinkStack top, ElementType element)
{
StackNode *temp;
temp = (StackNode *)malloc(sizeof(StackNode));
if(temp == NULL) return FALSE;
temp->data = element;
temp->next = top->next;
top->next = temp;
return TRUE;
}
int pop(LinkStack top, ElementType *element)
{
if(is_empty(top)) return FALSE;
StackNode *temp = top->next;
*element = temp->data;
top->next = temp->next;
free(temp);
return TRUE;
}
void get_top(LinkStack top, ElementType *element)
{
*element = top->next->data;
}
int main()
{
LinkStack s;
int count = 0;
s = (LinkStack)malloc(sizeof(StackNode));
init_stack(s);
int i = 0;
printf("正在进行压栈操作,请稍后。\n");
for(i=0; i<100; i++)
push(s,i);
printf("已经数据0 ~ 99存储到了栈中!\n");
int rslt;
printf("正在出栈\n");
printf("将栈内元素依次输出!\n");
printf("栈中元素如下:!\n");
while (!is_empty(s))
{
if(count % 5 == 0)
printf("\n");
pop(s,&rslt);
printf("%d\t",rslt);
count++;
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/u012965373/article/details/45815087