用链表仿真的堆栈存储

#include<stdlib.h>
#include<stdio.h>

struct node
{
   int data;
   struct node *next;
};

typedef struct node list;
typedef list *link;
link stack=NULL;

//打印堆栈数据
void print_stack()
{
link temp=NULL;
temp=stack;
 
if(temp==NULL)
     printf("\nthe stack is empty!!\n");
else
      {
         while(temp!=NULL)
             {
             printf("[%d]",temp->data);
             temp=temp->next;
         }
         printf("\n");
    }
}
//存入堆栈数据
void push(int value)
{
    link newnode;
    printf("\nthe stack content before (top->bottom)");
    print_stack();
    newnode=(link) malloc(sizeof(list));
    newnode->data=value;
    newnode->next=stack;
    stack=newnode;
    print_stack();

}
void pop()
{ 
    link top;
    printf("the stack content before(top->bottom)\n");
    print_stack();
    if(stack!=NULL)
        {
        top=stack;
        stack=stack->next;
        print_stack();
        free(top);
        
    }
}

void main()
{
   link pointer;
   int i=1,value=100;
   for(;i<=5;i++)
       push(i*value);
   for(;i>=1;i--)
       pop();
}
原文地址:https://www.cnblogs.com/hao02171990/p/3031043.html