目录
栈:
包括建栈、进栈、获取栈顶数据、出栈,获取栈中的元素个数和栈的总容量基本操作;期末数据结构的同学可以看一看自己写一写;
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#define stackSize 4
#define stackIncrement 10
using namespace std;
typedef struct {
int *base; //栈的基地址
int *top; //栈的顶指针
int size; //栈的总长度
int len; //栈中元素的个数
}stackk;
int Init(stackk &L) //申请栈,初始化
{
L.base = (int *)malloc(stackSize * sizeof(int));
if(!L.base) return 0;
L.size = 0;
L.size += stackSize;
L.top = L.base;
L.len = 0;
return 1;
}
int push(stackk &L, int e) //进栈
{
if(L.top - L.base >= L.size)
{
int *newbase = (int *)realloc(L.base, (L.size + stackIncrement)*sizeof(int));
if(!newbase) return 0;
L.base = newbase;
L.size += stackIncrement;
}
*L.top++ = e;
L.len++;
return 1;
}
int pop(stackk &L) //出栈
{
if(L.top == L.base) return 0;
L.top--;
L.len--;
return 1;
}
int get(stackk &L, int &e) 获取栈顶元素
{
if(L.top == L.base) return 0;
else e = *(L.top-1);
return 1;
}
int getlen(stackk &L) { //获取栈的元素个数
return L.len;
}
int getsize(stackk &L){ ///获取栈的容量
return L.size;
}
int main()
{
printf("请你输入6个你想推入栈中的数据\n");
int i = 6;
stackk S; if(!Init(S)) printf("创建栈失败!!\n");
while(i--)
{
int y; cin >> y;
if(!push(S,y)) printf("插入失败!!\n");
};
cout << "获取到栈中的元素有" << getlen(S) << "个"<< endl;
cout << "栈的总长度为" << getsize(S) << endl;
i = 6;
while(i--)
{
int h;
get(S, h);cout << h << " ";
if(!pop(S)) printf("出栈失败!!\n");
}
return 0;
}
队列:
普通队列:
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#define stackSize 10
#define stackIncrement 10
using namespace std;
typedef struct {
int *base; //队列的基地址
int *top; //队列的尾指针
int *head; //队列的头指针
int size; //队列的容量
int len; ///队列里的元素个数
}stackk;
int Init(stackk &L) //创建队列,初始化
{
L.base = (int *)malloc(stackSize * sizeof(int));
if(!L.base) return 0;
L.size = 0;
L.size += stackSize;
L.top = L.head = L.base;
L.len = 0;
return 1;
}
int push(stackk &L, int e) //入队
{
if(L.top - L.base >= L.size)
{
cout << "队列已满!!" << endl;
return 0;
}
*L.top++ = e;
L.len++;
return 1;
}
int pop(stackk &L) //出队列
{
if(L.top == L.head) return 0;
L.head++;
L.len--;
return 1;
}
int get(stackk &L, int &e) //获取队头元素
{
if(L.top == L.head) return 0;
else e = *(L.head);
//else e = *(L.top-1); 获取队尾元素
return 1;
}
int getlen(stackk &L) { //获取队里元素的个数
return L.len;
}
int getsize(stackk &L){ //获取队列的容量
return L.size;
}
int main()
{
printf("请你输入6个你想推入队列中的数据\n");
int i = 6;
stackk S; if(!Init(S)) printf("创建队列失败!!\n");
while(i--)
{
int y; cin >> y;
if(!push(S,y)) printf("插入失败!!\n");
};
cout << "获取到队列中的元素有" << getlen(S) << "个"<< endl;
cout << "队列的总长度为" << getsize(S) << endl;
i = 6;
while(i--)
{
int h;
get(S, h);cout << h << " ";
if(!pop(S)) printf("出队列失败!!\n");
}
return 0;
}
循环队列:
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#define queSize 10
#define queIncrement 10
using namespace std;
typedef struct {
int *base; //队列的基地址
int top; //队列的尾指针
int head; //队列的头指针
int size; //队列的容量
int len; ///队列里的元素个数
}que;
int Init(que &L) //创建队列,初始化
{
L.base = (int *)malloc(queSize * sizeof(int));
if(!L.base) return 0;
L.size = 0;
L.size += queSize;
L.top = L.head = 0;
L.len = 0;
return 1;
}
int push(que &L, int e) //入队
{
if((L.top + 1) % queSize == L.head){
cout << "队列已满!!" << endl;
return 0;
}
else {
L.base[L.top] = e;
L.top = (L.top + 1) %queSize;
L.len++;
}
return 1;
}
int pop(que &L) //出队列
{
if(L.top == L.head) return 0;
L.head = (L.head + 1) % queSize;
L.len--;
return 1;
}
int get(que &L, int &e) //获取队头元素
{
if(L.top == L.head) return 0;
else e = L.base[L.head];
//else e = L.base[L.top - 1]; 获取队尾元素
return 1;
}
int getlen(que &L) { //获取队里元素的个数
return L.len;
}
int getsize(que &L){ //获取队列的容量
return L.size;
}
int main()
{
printf("请你输入6个你想推入队列中的数据\n");
int i = 6;
que S; if(!Init(S)) printf("创建队列失败!!\n");
while(i--)
{
int y; cin >> y;
if(!push(S,y)) printf("插入失败!!\n");
};
cout << "获取到队列中的元素有" << getlen(S) << "个"<< endl;
cout << "队列的总长度为" << getsize(S) << endl;
i = 6;
while(i--)
{
int h;
get(S, h);cout << h << " ";
if(!pop(S)) printf("出队列失败!!\n");
}
return 0;
}