声明:大部分内容来自 - 《2019天勤数据结构高分笔记》
1. 栈
栈:只能在一端进行插入 和 删除的 线性表
该端 叫 栈顶(由栈顶指针表示,可变),另一端 是 栈底(固定的)
插入 叫 入栈,删除 叫 出栈
特点:先进后出 或 后进先出
存储结构分类:顺序栈 和 链式栈
n个元素入栈顺序排列数 =
C
2
n
n
n
+
1
\frac{C^n_{2n}}{n + 1}
n+1C2nn
2. 队列
队尾:可插入,进队
对头:可删除,出队
特点:先进先出
存储结果分类:顺序队 和 链队
3. 代码定义
3.1 栈
3.1.1 顺序栈
定义 + 初始化 + 判空 + 进栈 + 出栈 + 遍历
完整代码:
#include<bits/stdc++.h>
#define num 100
using namespace std;
typedef struct //顺序栈
{
int data[num];
int top;
}Sqtack;
void init(Sqtack &st)//1.初始化
{
st.top = -1;
cout << "初始化成功!" << endl;
}
void push(Sqtack &st, int x)
{
if(st.top == (num-1))
{
cout << "栈已满!" << endl;
}
else
{
st.top++;
st.data[st.top] = x;// 两语句等价于 st.data[++st.top] = x
printf("元素%-5d已进栈\n",x);
}
}
void pop(Sqtack &st, int &x)
{
if(st.top == -1)
cout << "栈为空,不可进行出栈操作!" << endl;
else
{
x = st.data[st.top];
st.top--; // 两语句等价于 x = st.data[st.top--];
cout << "成功!" << endl;
}
}
void isempty(Sqtack st)
{
if(st.top == -1)
cout << "为空栈" << endl;
else
cout << "栈非空" << endl;
}
void creat(Sqtack &st)
{
int length = rand()%num;
int i, x;
while(length >= num)
{
length = rand()%num;
}
cout << endl << "length = " << length << endl;
for(i = 0; i < length; i++)
{
x = rand()%(num*100);
push(st, x);
}
cout << "创建成功!" << endl;
}
void trace(Sqtack st)
{
int i;
for(i = 0; i <= st.top; i++)
cout << st.data[i] << " " ;
}
int main()
{
Sqtack st;
int x;
srand(time(NULL));
cout << "栈st" ;
init(st);
cout <<endl;
cout << "栈st" ;
creat(st);
cout <<endl;
cout << "栈st元素" << "(" << st.top+1 << "个元素)" << endl;
trace(st);
cout << endl << endl;
cout << "栈st" ;
isempty(st);
cout << endl;
cout << "栈st 出栈操作" ;
pop(st, x) ;
cout << "x = " << x << endl;
cout << "栈st元素" << "(" << st.top+1 << "个元素)" << endl;
trace(st);
cout << endl << endl;
return 0;
}
3.1.2 链式栈
定义 + 初始化 + 判空 + 进栈 + 出栈 + 遍历
完整代码:
#include<bits/stdc++.h>
#define num 100
using namespace std;
typedef struct LNode //链式栈
{
int data;
struct LNode *next;
}*Link, LNode;
void init(Link &L)
{
L = (Link)malloc(sizeof(LNode));
L->next = NULL;
cout << "初始化成功!" << endl;
}
void push(Link &L, int x) //头插法
{
Link p = L->next, q;
q = (Link)malloc(sizeof(LNode));
q->data = x;
q->next = p;
L->next = q;
printf("元素%-5d已进栈\n", x);
}
void pop(Link &L, int &x)
{
Link p = L->next;
if(p)
{
x = p->data;
L->next = p->next;
free(p);
cout << "成功!" << endl;
}
else
cout << "栈空!非法操作!" << endl;
}
void creat(Link &L)
{
int length = rand()%num;
int i, x;
cout << "length = " << length << endl;
cout << "随机生成:" << endl;
for(i = 0; i < length; i++)
{
x = rand();
push(L, x);
}
cout << "创建成功!" <<endl;
}
int getlength(Link L)
{
Link p = L->next;
int i = 0;
while(p)
{
i++;
p = p->next;
}
return i;
}
void trace(Link L)
{
Link p = L->next;
int length = getlength(L);
cout << "(" << length << "个元素)" << endl;
while(p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main()
{
Link L;
int x;
srand(time(NULL));
cout << "栈L" ;
init(L);
cout << endl;
cout << "栈L" ;
creat(L);
cout << endl;
cout << "栈L元素:" << endl ;
trace(L);
cout << endl;
cout << "栈L出栈操作" << endl ;
pop(L, x);
cout << "x = " << x << endl << endl;
cout << "栈L元素:" << endl ;
trace(L);
cout << endl;
return 0;
}
3.2 队列
3.2.1 顺序队
顺序队列定义 + 初始化 + 创建 + 入队 + 出队 + 判空 + 遍历
遍历时注意判断队列是否为空!
完整代码:
#include<bits/stdc++.h>
#define num 100
using namespace std;
typedef struct //顺序队
{
int data[num];
int front; //队首指针
int rear; //队尾指针
}Squeue;
void init(Squeue &L)
{
L.front = L.rear = 0;
cout << "初始化成功!" << endl;
cout << "大小 num = " << num << endl;
}
void enQueue(Squeue &L, int x)
{
if((L.rear + 1)%num == L.front)
cout << "队满,无法入队!" << endl;
else
{
L.rear = (L.rear + 1)%num;
L.data[L.rear] = x;
printf("元素 %-6d入队成功!\n", x);
}
}
void deQueue(Squeue &L, int &x)
{
if(L.front == L.rear)
cout << "顺序队列为空,无法出队!" << endl;
else
{
L.front = (L.front + 1)%num;
x = L.data[L.front];
}
}
void creat(Squeue &L)
{
int length, i, x;
length = rand();
while(length >= num )
{
length = rand();
}
cout << "length = " << length << endl;
for(i = 0; i < length; i++)
{
x = rand();
enQueue(L, x);
}
cout << "创建成功!" << endl;
}
void isEmpty(Squeue L)
{
if(L.front == L.rear)
cout << "队空!" << endl;
else
cout << "队非空!" << endl;
}
int getlength(Squeue L)
{
if(L.front < L.rear)
return L.rear - L.front;
else
{
return num - L.front + L.rear;
}
}
void trace(Squeue L)
{
int i;
int length = getlength(L);
cout << "对头 -> 队尾" << "(" <<length << "个元素):" << endl;
if(L.front != L.rear)
{
for(i = L.front + 1; i != L.rear;)
{
cout << L.data[i] << " " ;
i = (i + 1)%num;
}
cout << L.data[i] << endl;
}
else
{
cout << "队空!" << endl;
}
}
int main()
{
srand(time(NULL));
Squeue L;
int x;
cout << "顺序队L" << endl;
init(L);
cout << endl;
isEmpty(L);
cout << endl;
cout << "顺序队L" << endl;
creat(L);
cout << endl;
cout << "顺序队L元素:" << endl;
trace(L) ;
cout <<endl;
cout << "顺序队L,对首元素出队" << endl;
deQueue(L, x) ;
cout << "x = " << x << endl << endl;
cout << "顺序队L元素:" << endl;
trace(L) ;
cout <<endl;
isEmpty(L);
cout << endl;
return 0;
}
3.2.2 链式队
链式队列定义 + 初始化 + 创建 + 入队 + 出队 + 判空 + 遍历
完整代码:
#include<bits/stdc++.h>
#define num 100
using namespace std;
typedef struct QNode //结点
{
int data;
struct QNode *next;
}*Node, QNode;
typedef struct //链式队
{
QNode *front;
QNode *rear;
}*Link, LQNode;
void init(Link &L)
{
L = (Link)malloc(sizeof(QNode));
L->front = L->rear = NULL;
cout << "初始化成功!" << endl << endl;
}
void enQueue(Link &L, int x)
{
Node p ;
p = (Node)malloc(sizeof(QNode));
p->data = x;
p->next = NULL;
if(L->rear == NULL) //队空
{
L->front = L->rear = p;
printf("元素 %-6d 已入队\n",x);
}
else
{
L->rear->next = p;
L->rear = p;
printf("元素 %-6d 已入队\n",x);
}
}
void creat(Link &L)
{
int length = rand()%num;
int i, x;
cout << "length = " << length << endl;
cout << "随机生成:" << endl;
for(i = 0; i < length; i++)
{
x = rand();
enQueue(L, x);
}
cout << endl;
}
void isEmpty(Link &L)
{
if(L->front == NULL || L->rear == NULL)
cout << "队空!" << endl << endl;
else
cout << "队非空!" << endl << endl;
}
int getlength(Link L)
{
Node p = L->front;
int c = 0;
while(p != L->rear)
{
c++;
p = p->next;
}
return c+1;
}
void trace(Link L)
{
Node p = L->front;
int length = getlength(L);
cout << "(" << length << "个元素)" << endl;
while(p != L->rear )
{
cout << p->data << " " ;
p = p->next;
}
cout << p->data << endl << endl;
}
void deQueue(Link &L)
{
Node p ;
int x;
if(L->rear == NULL)
cout << "队为空,出队操作非法!" << endl << endl;
else if(L->front == L->rear)
{
p = L->front;
x = p->data;
L->front = L->rear = NULL;
cout << "出队成功!" << endl << endl;
printf("队首元素 x = %-6d\n\n", x);
}
else
{
p = L->front;
x = p->data;
L->front = L->front->next;
free(p);
cout << "出队成功!" << endl << endl;
printf("队首元素 x = %-6d\n\n", x);
}
}
int main()
{
Link L;
int x;
srand(time(NULL));
cout << "链式队" << endl;
init(L);
cout << "链式队 " ;
isEmpty(L);
cout << "链式队" << endl;
creat(L);
cout << "链式队元素 " ;
trace(L);
cout << "链式队 " ;
isEmpty(L);
cout << "链式队,对首元素出队 " ;
deQueue(L);
cout << "链式队元素 " ;
trace(L);
return 0;
}