C++ 实现栈的基本数据结构和操作
#include <iostream>
using namespace std;
#define MaxSize 50
typedef int Elemtype;
typedef struct
{
Elemtype data[MaxSize];
int top;
} SqStack;
void InitStack(SqStack*& s)
{
s = new SqStack;
s->top = -1;
}
void DestroyStack(SqStack *&s)
{
delete s;
}
bool stackempty(SqStack* s)
{
return s->top == -1;
}
bool push(SqStack*& s, Elemtype e)
{
if (s->top == MaxSize - 1)
return false;
s->top++;
s->data[s->top] = e;
return true;
}
bool pop(SqStack*& s, Elemtype& e)
{
if (s->top == -1)
return false;
e = s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack* s, Elemtype& e)
{
if (s->top == -1)
return false;
e = s->data[s->top];
return true;
}
typedef struct
{
Elemtype data[MaxSize];
int front, rear;
} SqQueue;
void InitQueue(SqQueue*& q)
{
q = new SqQueue;
q->front = q->rear = -1;
}
void destroyqueue(SqQueue*& q)
{
delete q;
}
bool QueueEmpty(SqQueue* q)
{
return q->front == q->rear;
}
bool enQueue(SqQueue*& q, Elemtype e)
{
if (q->rear == MaxSize - 1)
return false;
q->rear++;
q->data[q->rear] = e;
return true;
}
bool deQueue(SqQueue*& q, Elemtype& e)
{
if (q->front == q->rear)
return false;
q->front++;
e = q->data[q->front];
return true;
}
int main()
{
}