顺序栈:
代码实现:
mystack.h
#ifndef MYSTACK_H
#define MYSTACK_H
typedef int datatype;
const int maxsize = 10;
class Stack{
private:
datatype *data;
int top;
public:
//初始化
void init(int size);
//判空
bool stack_ifempt();
//判满
bool stack_iffull();
//入栈
int stack_push(datatype e);
//出栈
int stack_pop();
//遍历栈
int stack_show();
//销毁
int stack_destroy();
};
#endif // MYSTACK_H
mysatck.c
#include <iostream>
#include <mystack.h>
using namespace std;
//初始化
void Stack::init(int size){
data = new int[size];
top = -1;
cout << "初始化成功" << endl;
}
//判空
bool Stack::stack_ifempt(){
return top == -1;
}
//判满
bool Stack::stack_iffull(){
return top == maxsize-1;
}
//入栈
int Stack::stack_push(datatype e){
if(stack_iffull()){
cout<< "栈满" << endl;
return -1;
}
top++;
data[top] = e;
cout<<"入栈成功"<<endl;
return 1;
}
//出栈
int Stack::stack_pop(){
if(stack_ifempt()){
cout<<"栈空"<<endl;
return -1;
}
cout << data[top] << "出栈" <<endl;
top--;
return 1;
}
//遍历栈
int Stack::stack_show(){
if(stack_ifempt()){
cout<<"栈空"<<endl;
return -1;
}
cout<<"栈顶开始的元素为 : ";
for(int i=top;i>=0;i--){
cout<<data[i]<<' ';
}
cout << endl;
return 1;
}
//销毁
int Stack::stack_destroy(){
top = -1;
delete []data;
data = nullptr;
cout << "销毁成功" << endl;
return 1;
}
main.c
#include <iostream>
#include <mystack.h>
using namespace std;
int main()
{
Stack s;
s.init(maxsize);
if(s.stack_ifempt())
cout<< "当前栈空" << endl;
cout << "***************************" << endl;
s.stack_push(1);
s.stack_push(3);
s.stack_push(5);
s.stack_push(123);
s.stack_push(7);
s.stack_show();
cout << "***************************" << endl;
s.stack_pop();
s.stack_show();
s.stack_pop();
s.stack_push(33354);
s.stack_show();
cout << "***************************" << endl;
for(int i=0;i<6;i++)
s.stack_push(rand()%100);
s.stack_show();
if(s.stack_iffull())
cout << "当前栈满" << endl;
cout << "***************************" << endl;
s.stack_destroy();
s.stack_show();
cout << "***************************" << endl;
return 0;
}
实现结果:
循环队列:
代码实现:
myqueue.h
#ifndef MYQUEUE_H
#define MYQUEUE_H
typedef int datatype;
const int maxsize = 10;
class Queue{
private:
datatype *data;
int front;
int tail;
public:
//初始化
void init(int size);
//判空
bool Queue_ifempt();
//判满
bool Queue_iffull();
//入队
int Queue_in(datatype e);
//出队
int Queue_out();
//遍历队列
int Queue_show();
//求队长
int Queue_length();
//销毁队列
int Queue_destroy();
};
#endif // MYQUEUE_H
myqueue.c
#include <iostream>
#include <myqueue.h>
using namespace std;
//初始化
void Queue::init(int size){
data = new int[size];
front = 0;
tail = 0;
cout << "初始化成功" << endl;
}
//判空
bool Queue::Queue_ifempt(){
return front==tail;
}
//判满
bool Queue::Queue_iffull(){
return (tail+1)%maxsize==front;
}
//入队
int Queue::Queue_in(datatype e){
if(Queue_iffull()){
cout << "队满" << endl;
return -1;
}
data[tail] = e;
tail=(tail+1)%maxsize;
cout << "入队成功" <<endl;
return 1;
}
//出队
int Queue::Queue_out(){
if(Queue_ifempt()){
cout << "队空" << endl;
return -1;
}
front = (front+1)%maxsize;
cout << "出队成功" <<endl;
return 1;
}
//遍历队列
int Queue::Queue_show(){
if(Queue_ifempt()){
cout << "队空" << endl;
return -1;
}
for(int i=front;i!=tail;i=(i+1)%maxsize){
cout << data[i] << ' ';
}
cout << endl;
return 1;
}
//求队长
int Queue::Queue_length(){
return (tail+maxsize-front)%maxsize;
}
//销毁队列
int Queue::Queue_destroy(){
front = 0;
tail = 0;
delete []data;
data = nullptr;
cout << "销毁成功" << endl;
return 1;
}
main.c
#include <iostream>
#include <myqueue.h>
using namespace std;
int main()
{
Queue q;
q.init(maxsize);
if(q.Queue_ifempt())
cout << "当前队空" << endl;
cout << "***************************" << endl;
q.Queue_in(1);
q.Queue_in(1125);
q.Queue_in(6);
q.Queue_in(3237);
q.Queue_in(2);
q.Queue_show();
cout << "当前队长 : " << q.Queue_length() << endl;
cout << "***************************" << endl;
q.Queue_out();
q.Queue_show();
q.Queue_out();
q.Queue_out();
q.Queue_in(4534);
q.Queue_show();
cout << "当前队长 : " << q.Queue_length() << endl;
cout << "***************************" << endl;
for(int i=0;i<6;i++)
q.Queue_in(rand()%100);
cout << "当前队长 : " << q.Queue_length() << endl;
q.Queue_show();
if(q.Queue_iffull())
cout << "当前队满" << endl;
cout << "***************************" << endl;
q.Queue_destroy();
q.Queue_show();
cout << "***************************" << endl;
return 0;
}
实现结果: