数据结构之链式栈

来源:互联网 发布:青果软件掌上校园 编辑:程序博客网 时间:2024/05/14 09:03
#include<stdio.h>
#include<string.h>
#include<string>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
typedef int status;
typedef int Elemtype;
typedef struct stackNode
{
    Elemtype data;
    struct stackNode *next;
} stackNode,*linkNode;
typedef struct linkStack
{
    linkNode top;
    int stacksize;
} linkStack;
status push(linkStack &s,Elemtype e)
{
    linkNode p,q;
    p=(stackNode *)malloc(sizeof(stackNode));
    if(p==NULL)return ERROR;
    p->data=e;
    p->next=s->top;
    s->top=p;
    s->stacksize++;
    return OK;
}
status pop(linkStack &s)
{
    linkStack p;
    p=s->top;
    s->top=p->top;
    free(p);
    s.stacksize--;
    return OK;
}
status traverse(linkStack s){
while(s->top!=NULL){
    printf("%d ",s.top->data);
    s.top=s->top;
}
return OK;
}
原创粉丝点击