使用语言:c++
栈
1.什么是栈?
栈的特点:先进后出
。
栈是一种只能在一端
进行插入或删除操作的线性表
。
线性表:栈的逻辑结构属于线性表,只不过在操作上加了一些约束。
一端:可以插入或者删除元素的一端叫栈顶
,另一端叫栈底
。
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;
//入栈,head返回为栈顶元素
void push(LNode *&head,int elem){
LNode *top=new LNode();
top->data=elem;
top->next=head;
head=top;
}
//出栈,对head栈顶元素进行出栈处理
void pop(LNode *&head){
if(head){
LNode *p=head;
head=head->next;
cout<<"pop:"<<p->data<<endl;
free(p);
}else{
cout<<"栈空"<<endl;
}
}
int main(){
LNode *head=new LNode();
head->next=NULL;
push(head,5);
push(head,4);
push(head,3);
pop(head);
pop(head);
pop(head);
return 0;
}
结果:
队列
1.什么是队列?
队的特点:先进先出
。
队列是一种插入元素只能在一端
能进,删除元素只能在另一端
进行的线性表
。
线性表:栈的逻辑结构属于线性表,只不过在操作上加了一些约束。
一端:可以插入
元素的一端叫队尾
。
另一端:可以删除
元素的一段叫队头
。
2.第一种顺序队列
思路: 通过rear来实现数据的入队,每次入队就+1,通过front来实现数据的出队,每次出队都+1,当front等于rear时,队列为空。
#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;
当front!=(rear+1)%maxSize显示为栈满,front!=rear显示为栈空,这种条件只能存取maxSize-1个元素
。
#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;
}
结果:
参考原文博客:c.biancheng.net/view/3351.h…