顺序栈:
Stack.h
#pragma once
#define StackSize 100
顺序栈
template <typename T>
class SequentialStack {
public:
SequentialStack();//构造
~SequentialStack() {}//析构
void Push(T x);//压栈
T Pop();//出栈
T GetTop();//得到栈顶
bool Empty();//判断是否为空栈,true为空
private:
T data[StackSize];//开辟存放T类型的数组
int top;//数组指针
};
template <typename T>
SequentialStack<T>::SequentialStack() {
top = -1;
}
template <typename T>
bool SequentialStack<T>::Empty() {
return (top == -1 ? true : false);
}
template <typename T>
void SequentialStack<T>::Push(T x) {
if (top == StackSize - 1)throw"overflow";
data[++top] = x;
}
template <typename T>
T SequentialStack<T>::GetTop() {
if (top == -1)throw"flow";
T x = data[top];
return x;
}
template <typename T>
T SequentialStack<T>::Pop() {
int x = SequentialStack<T>::GetTop();
top--;
return x;
}
main.cpp
#include<iostream>
#include"Stack.h"
using namespace std;
void SeqSta() {
SequentialStack<int> a;
a.Push(20);
cout << a.GetTop() << endl;
a.Push(30);
cout << a.GetTop() << endl;
cout << a.Pop() << endl; cout << a.Pop() << endl;
system("pause");
}
int main() {
SeqSta();
}
链栈
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class linkstack
{
public:
linkstack() { top = NULL; }
~linkstack() {};
void push(int x);
int pop();
void print();
int gettop() { if (top != NULL)return top->data; }
int empty() { return(top == NULL) ? 1 : 0; }
private:
Node *top;
};
void linkstack::push(int x)
{
Node *s;
s = new Node;
s->data = x;
s->next = top;
top = s;
}
int linkstack::pop()
{
if (top == NULL)throw"下溢";
int x = top->data;
Node *p;
p = top;
top = top->next;
delete p;
return x;
}
void linkstack::print()
{
Node *p;
p = top;
while (p != NULL)
{
cout << p->data;
p = p->next;
}
}
int main()
{
linkstack a;
a.push(6);
a.print();
}
顺序队:
#include<iostream>
using namespace std;
const int cd = 10;
class cirqueue
{
public:
cirqueue()
{
front = rear = cd - 1;
}
~cirqueue() {}
void enqueue(int x);
int dequeue();
int get();
void print();
int empty()
{
return(front == rear) ? 1 : 0;
}
private:
int data[cd];
int front, rear;
};
void cirqueue::enqueue(int x)
{
if ((rear + 1) % cd == front)throw"上溢";
rear = (rear + 1) % cd;
data[rear] = x;
}
int cirqueue::dequeue()
{
if (rear == front)throw"下溢";
front = (front + 1) % cd;
return data[front];
}
int cirqueue::get()
{
if (rear == front)throw"下溢";
int i = (front + 1) % cd;
cout << data[i] << endl;
return 0;
}
void cirqueue::print()
{
for (int i = 0; i<cd; i++)
cout << data[i];
}
int main()
{
cirqueue a;
a.enqueue(16);
a.print();
a.enqueue(1);
cout << "\n" << endl;
a.print();
cout << "\n" << endl;
try
{
a.get();
}
catch (char *s)
{
cout << s << endl;
}
}
链队
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class linkqueue
{
public:
linkqueue();
~linkqueue();
void enqueue(int x);
int dequeue();
int get();
void print();
int empty()
{
return(front == rear) ? 1 : 0;
}
private:
Node *front, *rear;
};
linkqueue::linkqueue()
{
Node *s;
s = new Node;
s->next = NULL;
front = rear = s;
}
void linkqueue::enqueue(int x)
{
Node *s;
s = new Node;
s->data = x;
s->next = NULL;
rear->next = s;
rear = s;
}
int linkqueue::dequeue()
{
if (rear == front)"下溢";
Node *p;
p = front->next;
int x;
x = p->data;
front->next = p->next;
if (p->next == NULL)rear = front;
delete p;
return x;
}
linkqueue::~linkqueue()
{
while (front != NULL)
{
Node *q;
q = front;
front = front->next;
delete q;
}
}
int linkqueue::get()
{
if (rear == front)"下溢";
Node *p;
p = front->next;
int x;
x = p->data;
delete p;
cout << x << endl;
return 0;
}
void linkqueue::print()
{
Node *p;
p = front->next;
while (p != NULL)
{
cout << p->data;
p = p->next;
}
}
int main()
{
linkqueue a;
a.enqueue(5);
a.enqueue(6);
a.print();
cout << "\n" << endl;
a.dequeue();
a.print();
cout << "\n" << endl;
a.get();
}