# 栈

## 2.顺序栈

``````#include <bits/stdc++.h>
using namespace std;
#define maxSize 10

//入栈
void push(int* a,int &top,int elem){
a[++top]=elem;
}

//出栈
void pop(int *a,int &top){
if(top==-1){
cout<<"空栈"<<endl;
top=-1;
}
cout<<"pop:"<<a[top--]<<endl;
}

int main(){
int arr[maxSize];
int top=-1;
push(arr,top,5);
push(arr,top,4);
push(arr,top,3);
pop(arr,top);
pop(arr,top);
return 0;
}

## 3.链栈

``````#include <bits/stdc++.h>
using namespace std;

typedef struct LNode{
int data;
struct LNode* next;
}LNode;

void push(LNode *&head,int elem){
LNode *top=new LNode();
top->data=elem;
}

cout<<"pop:"<<p->data<<endl;
free(p);
}else{
cout<<"栈空"<<endl;
}
}

int main(){
return 0;
}

# 队列

## 2.第一种顺序队列

``````#include <bits/stdc++.h>
using namespace std;
#define maxSize 100

void push(int *a,int &rear,int data){
a[rear++]=data;
}

void pop(int *a,int &front,int rear){
if(front!=rear){
cout<<"pop:"<<a[front++]<<endl;
}else{
cout<<"队空"<<endl;
}
}

int main(){
int queue[maxSize];
int front=0,rear=0;
push(queue,rear,5);
push(queue,rear,4);
push(queue,rear,3);
pop(queue,front,rear);
pop(queue,front,rear);
pop(queue,front,rear);
return 0;
}

## 3.第二种顺序队列

`````` rear=(rear+1)%maxSize;
a[rear]=x;

``````front=(front+1)%maxSize;
cout<<"pop:"<<a[front]<<endl;

``````#include <bits/stdc++.h>
using namespace std;
#define maxSize 4

void push(int *a,int front,int &rear,int x){
if(front!=(rear+1)%maxSize){
rear=(rear+1)%maxSize;
a[rear]=x;
}else
cout<<"FULL"<<endl;
}

void pop(int *a,int &front,int rear){
if(front!=rear){
front=(front+1)%maxSize;
cout<<"pop:"<<a[front]<<endl;
}else{
cout<<"NULL"<<endl;
}
}

int main(){
int queue[maxSize];
int front=0,rear=0;
push(queue,front,rear,4);
push(queue,front,rear,3);
push(queue,front,rear,2);
pop(queue,front,rear);
pop(queue,front,rear);
pop(queue,front,rear);
return 0;
}

## 4.链队

``````#include <bits/stdc++.h>
using namespace std;
typedef struct LNode{
int data;
struct LNode* next;
}LNode;

void push(LNode *&rear,int a){
LNode *top=new LNode();
top->data=a;
top->next=NULL;
rear->next=top;
rear=top;
}

void pop(LNode *&front,LNode *&rear){
if(front->next==NULL){
cout<<"NULL"<<endl;
}
LNode *p=front->next;
cout<<p->data<<endl;
front->next=p->next;
if(rear==p){
rear=front;
}
free(p);
}

int main(){
LNode *front,*rear;
rear=new LNode();
rear->next=NULL;
front=rear;
push(rear,4);
push(rear,3);
push(rear,2);
pop(front,rear);
pop(front,rear);
pop(front,rear);
pop(front,rear);
return 0;
}