1. 程式人生 > >浩先森的棧與佇列

浩先森的棧與佇列

資料結構中學習了棧與佇列 今天來試著做一下簡單的入棧和出棧的基本程式碼

#include <iostream>
#include <cstdalign>
#include <cstdio>
#include <cstdarg>
#include <cstring>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}
#define MAXSIZE 100
typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack &S)
{
    S.base = new  SElemType[MAXSIZE];
    IF(!S.base) exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = MAXSIZE;
    return OK; 
}

Status Push(SqStack &S,SElemType e)
{
    if(S.top-S.base == S.stacksize) return ERROR;
    *S.top++=e;
    return OK;
}

Status Pop(SqStack &S,SElemType &e)
{
    if(S.top == S.base) return ERROR;
    e = *--S.top;
    return OK;
}

SElemType GetTop(SqStack S)
{
    if(S.top! = S.base)
        return *(S.top-1);
}





#include #include #include #include #include

using namespace std;

int main() { cout << “Hello world!” << endl; return 0; } #define MAXSIZE 100 typedef struct { SElemType *base; SElemType *top; int stacksize; }SqStack;

Status InitStack(SqStack &S) { S.base = new SElemType[MAXSIZE]; IF(!S.base) exit(OVERFLOW); S.top = S.base; S.stacksize = MAXSIZE; return OK; }

Status Push(SqStack &S,SElemType e) { if(S.top-S.base == S.stacksize) return ERROR; *S.top++=e; return OK; }

Status Pop(SqStack &S,SElemType &e) { if(S.top == S.base) return ERROR; e = *–S.top; return OK; }

SElemType GetTop(SqStack S) { if(S.top! = S.base) return *(S.top-1); }