顺序栈基本操作
文件声明
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
typedef struct{
int data[MaxSize];
int top;
} SqStack;
typedef struct Linknode{
int data;
struct Linknode *next;
}*LiStack;
栈的初始化
void InitStack(SqStack &S){
S.top = -1;
}
判断栈是否为空
bool StackEmpty(SqStack S){
if(S.top == -1){
return true;
}
else return false;
}
进栈
bool Push(SqStack &S, int x){
if(S.top = MaxSize - 1){
return true;
}
else{
S.data[++S.top] = x;
}
}
出栈
bool Pop(SqStack &S, int &x){
if(S.top == -1){
return false;
}
else{
x = S.data[S.top--];
return true;
}
}
读栈顶元素
bool GetTop(SqStack S, int &x){
if(S.top == -1){
return false;
}
else{
x = S.data[S.top];
}
}
循环队列基本操作
前面文件声明
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
typedef struct
{
int data[MaxSize];
int front, rear;
}SeQueue;
判断队空,队满
初始化
void InitQueue(SeQueue &Q){
Q.rear = Q.front = 0;
}
判断队是否为空
bool isEmpty(SeQueue Q){
if(Q.rear == Q.front) return true;
else return false;
}
入队
bool EnQueue(SeQueue &Q, int x){
if((Q.rear+1)%MaxSize == Q.front){
return false;
}
Q.data[Q.rear] = x;
Q.rear = (Q.rear+1)%MaxSize;
return true;
}
出队
bool Deqeue(SeQueue &Q,int &x){
if(Q.rear == Q.front) return false;
x = Q.data[Q.front];
Q.front = (Q.front+1)%MaxSize;
return true;
}
链栈基本操作
前面文件声明
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
typedef struct LinkNode{
int data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front,*rear;
}LinkQueue;
链队的初始化
void InitQueue(LinkQueue &Q){
Q.front=Q.rear=(LinkNode *)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
判断队是否为空
bool IsEmpty(LinkQueue Q){
if(Q.front == Q.rear) return true;
else return false;
}
入队
void EnQueue(LinkQueue &Q, int x){
LinkNode *s = (LinkNode *)malloc(sizeof(LinkNode));
s->data = x;
s -> next = Q.rear->next;
Q.rear -> next = s;
Q.rear = s;
}
出队
bool DeQueue(LinkQueue &Q, int &x){
if(Q.front == Q.rear) return false;
LinkNode *p = Q.front->next;
x = p->data;
Q.front ->next = p->next;
if(Q.rear == p){
Q.rear = Q.front;
}
free(p);
return true;
}