数据结构_链栈的建立与相关操作_C语言源代码

来源:互联网 发布:人工智能行业报告 36 编辑:程序博客网 时间:2024/05/22 04:27
#include<stdio.h>
#include<stdlib.h>
typedef struct Stack
{
        int data;
        struct Stack *next;
}Stack;


void InitStack(Stack * &S)
{
     S=(Stack*)malloc(sizeof(Stack));
     S->next=NULL;
}


int StackEmpty(Stack *S)
{
    return S->next==NULL ? 1 :0;
}


void Push(Stack *S, int e)
{
     Stack *p=(Stack*)malloc(sizeof(Stack));
     p->data = e;
     p->next = S->next;
     S->next = p;
}


int Pop(Stack *S, int &e)
{
    
    Stack *p=NULL;
    if(S->next==NULL)
    {
      return 0;
    }
    
    p = S->next;
    e = p->data;
    S->next = p->next;
    free(p);
    
    return 1;
}
int main(void)
{
    const int N=10;
    int i;
    int e;
    Stack *S;
    InitStack(S);
    for(i=0;i<N;++i)
    {
      Push(S,i);
    }
    
    for(i=0;i<N;++i)
    {
      Pop(S,e);
      printf("data[%d]=%d\n",i,e);
    }
    
    system("pause");
    return 0;
}
0 0
原创粉丝点击