【数据结构】---栈和队列

 1 #include<iostream>
 2 using namespace std;
 3 
 4 #define Maxsize 100  //预先分配空间,这个数值根据实际需要预估确定;
 5 
 6 typedef struct SqStack {
 7     int *base; //栈底指针
 8     int *top; //栈顶指针
 9 }SqStack;
10 
11 bool InitStack(SqStack &S) //构造一个空栈S
12 {
13     S.base = new int[Maxsize];//为顺序栈分配一个最大容量为Maxsize的空间
14     if (!S.base)    //空间分配失败
15         return false;
16     S.top=S.base;  //top初始为base,空栈
17     return true;
18 }
19 
20 bool Push(SqStack &S, int e) // 插入元素e为新的栈顶元素
21 {
22     if (S.top-S.base == Maxsize) //栈满
23         return false;
24     *(S.top++) = e; //元素e压入栈顶,然后栈顶指针加1,等价于*S.top=e; S.top++;
25     return true;
26 }
27 
28 bool Pop(SqStack &S, int &e) //删除S的栈顶元素,暂存在变量e中
29 {
30     if (S.base == S.top) //栈空
31         return false;
32     e = *(--S.top); //栈顶指针减1,将栈顶元素赋给e
33     return true;
34 }
35 
36 int GetTop(SqStack S) //返回S的栈顶元素,栈顶指针不变
37 {
38     if (S.top != S.base)  //栈非空
39         return *(S.top - 1); //返回栈顶元素的值,栈顶指针不变
40     else
41         return -1;
42 }
43 
44 int main()
45 {
46     int n,x;
47     SqStack S;
48     InitStack(S);//初始化一个顺序栈S
49     cout <<"请输入元素个数n:" <<endl;
50     cin>>n;
51     cout <<"请依次输入n个元素,依次入栈:" <<endl;
52     while(n--)
53     {
54         cin>>x; //输入元素
55         Push(S, x);
56     }
57     cout <<"元素依次出栈:" <<endl;
58     while(S.top!=S.base)//如果栈不空,则依次出栈
59     {
60         cout<<GetTop(S)<<"	";//输出栈顶元素
61         Pop(S, x);   //栈顶元素出栈
62     }
63     return 0;
64 }
 1 #include <iostream>
 2 using namespace std;
 3 #define Maxsize 100
 4 
 5 typedef  struct SqQueue{
 6   int *base; //基地址
 7   int front,rear; //头指针,尾指针
 8 }SqQueue;
 9 
10 //循环队列的初始化
11 bool InitQueue(SqQueue &Q)//注意使用引用参数,否则出了函数,其改变无效
12 {
13     Q.base=new int[Maxsize];//分配空间
14     if(!Q.base) return false;
15     Q.front=Q.rear=0; //头指针和尾指针置为零,队列为空
16     return true;
17 }
18 
19 //循环队列的入队
20 bool EnQueue(SqQueue &Q,int e)//将元素e放入Q的队尾
21 {
22     if((Q.rear+1)%Maxsize==Q.front) //尾指针后移一位等于头指针,表明队满
23         return false;
24     Q.base[Q.rear]=e; //新元素插入队尾
25     Q.rear=(Q.rear+1)%Maxsize; //队尾指针加1
26     return true;
27 }
28 
29 //循环队列的出队
30 bool DeQueue(SqQueue &Q, int &e) //删除Q的队头元素,用e返回其值
31 {
32     if (Q.front==Q.rear)
33         return false; //队空
34     e=Q.base[Q.front]; //保存队头元素
35     Q.front=(Q.front+1)%Maxsize; //队头指针加1
36     return true;
37 }
38 
39 //取循环队列的队头元素
40 int GetHead(SqQueue Q)//返回Q的队头元素,不修改队头指针
41 {
42     if (Q.front!=Q.rear) //队列非空
43         return Q.base[Q.front];
44     return -1;
45 }
46 //循环队列的长度
47 int QueueLength(SqQueue Q)
48 {
49     return (Q.rear-Q.front+Maxsize)%Maxsize;
50 }
51 
52 int main()
53 {
54     SqQueue Q;
55     int n,x;
56     InitQueue(Q);//初始化队列(一定要初始化,否则后面存储出错)
57     cout <<"请输入元素个数n:" <<endl;
58     cin>>n;
59     cout <<"请依次输入n个整型数,依次入队:" <<endl;
60     while(n--)
61     {
62            cin>>x;
63         EnQueue(Q,x);//入队
64     }
65     cout<<endl;
66     cout <<"队列内元素个数,即长度:"<<QueueLength(Q)<<endl;
67     cout <<"队头元素:" <<GetHead(Q)<<endl;
68     cout <<"元素依次出队:" <<endl;
69     while(true)//如果栈不空,则依次出栈
70     {
71         if(DeQueue(Q,x))
72             cout<<x<<"	";//出队元素
73         else
74             break;
75     }
76     cout <<endl;
77     cout <<"队列内元素个数,即长度:"<<QueueLength(Q)<<endl;
78     return 0;
79 }
原文地址:https://www.cnblogs.com/alec7015/p/12511627.html