栈是数据结构的一种存储结构,栈的实现是一种后进先出策略
#include<iostream>
using namespace std;
typedef struct stack {
int a[10];
int top;
}s;//定义一个栈
int main()
{
s p;
p.top = 0;
//压栈
while (p.top != 10)
{
int a;
cin >> a;
p.a[p.top] = a;
p.top++;
}
//弹出
while (p.top-1 >=0)
{
cout << p.a[p.top-1] << endl;
p.top--;
}
}
队列是数据结构的一种存储结构,队列的实现是一种先进先出策略
#include<iostream>
using namespace std;
typedef struct queue {
int a[10];
int head;
int tail;
}s;
int main()
{
s p;
p.head = p.tail = 0;
//入队
while (p.tail != 10)
{
int a;
cin >> a;
p.a[p.tail] = a;
p.tail++;
}
//出队
while (p.head<p.tail)
{
cout << p.a[p.head] << endl;
p.head++;
}
}