码迷,mamicode.com
首页 > 其他好文 > 详细

链栈实现

时间:2016-10-17 21:00:44      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
#include<cstdio>
#include<cstdlib>

using namespace std;
#define TRUE 1
#define FALSE 0
typedef int ElemType;

typedef struct node
{
    ElemType data;
    struct node *next;
}StackNode, *LinkStack;

void InitStack(LinkStack top)
{
    top->next=NULL;
}

int IsEmpty(LinkStack top)
{
    if(top->next==NULL)
        return TRUE;
    return FALSE;
}

int Push(LinkStack top, ElemType e)
{
    StackNode *temp;
    temp=(LinkStack)malloc(sizeof(StackNode));
    if(temp==NULL) return FALSE;
    temp->data=e;
    temp->next=top->next;
    top->next=temp;
    return TRUE;
}

int Pop(LinkStack top, ElemType *e)
{
    if(IsEmpty(top)) return FALSE;
    StackNode *temp=top->next;
    *e=temp->data;
    top->next=temp->next;
    free(temp);

    return TRUE;
}

void GetTop(LinkStack top, ElemType *e)
{
    *e=top->next->data;
}

int main()
{
    LinkStack s;
    s=(LinkStack)malloc(sizeof(StackNode));
    InitStack(s);
    for(int i=0; i<10; i++)
        Push(s, i);
    int ans;
    while(!IsEmpty(s))
    {
        Pop(s, &ans);
        printf("%d ", ans);
    }
    printf("\n");
    return 0;
}

 

链栈实现

标签:

原文地址:http://www.cnblogs.com/9968jie/p/5970882.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!