栈(c++)

[复制链接]
发表于 2025-9-22 23:47:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本日介绍两种在c++中写“栈”方法

1.
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class mystack {
  4. private:
  5.         int a[1000];
  6.         int curr = -1;
  7. public:
  8.         void push(int);
  9.         void pop();
  10.         int top();
  11.         bool empyt();
  12.         int size();
  13. };
  14. int main()
  15. {
  16.         mystack n;
  17.         while(true)
  18.         {
  19.                 int a;
  20.                 cout<<"1.加入元素\n2.删除元素\n3.显示顶端的元素\n4.栈是否为空(1是,0否)\n5.栈的大小\n6.结束"<<endl;
  21.                 cin>>a;
  22.                 if(a==1)
  23.                 {
  24.                         int sh;
  25.                         cin>>sh;
  26.                         n.push(sh);
  27.                 }
  28.                 else if(a==2) n.pop();
  29.                 else if(a==3) cout<<n.top()<<endl;
  30.                 else if(a==4) cout<<n.empyt()<<endl;
  31.                 else if(a==5) cout<<n.size()<<endl;
  32.                 else if(a==6) break;
  33.                 system("pause");
  34.                 system("cls");
  35.         }
  36.        
  37.         return 0;
  38. }
  39. void mystack::push(int v)
  40. {
  41.         if(curr<1000)
  42.         {
  43.                 a[++curr] = v;
  44.         }
  45.         else cout<<"栈已满\n";
  46.         return;
  47. }
  48. void mystack::pop()
  49. {
  50.         if(curr>-1)
  51.         {
  52.                 curr--;
  53.         }
  54.         else cout<<"栈为空\n";
  55.         return;
  56. }
  57. int mystack::top()
  58. {
  59.         if(curr>-1) return a[curr];
  60.         else cout<<"栈为空\n";
  61.         return -1;
  62. }
  63. bool mystack::empyt()
  64. {
  65.         if(curr==-1) return true;
  66.         else return false;
  67. }
  68. int mystack::size()
  69. {
  70.         return curr+1;
  71. }
复制代码

2.
  1. ​
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. struct node
  5. {
  6.         int value;
  7.         node* next;
  8.         node* prev;
  9.         node(){}
  10.         node(int v)
  11.         {
  12.                 value = v;
  13.                 next = NULL;
  14.                 prev = NULL;
  15.         }
  16. };
  17. class mystack {
  18. private:
  19.         node* root = NULL;
  20.         node* ptop = NULL;
  21.         int n = 0;
  22. public:
  23.         void push(int);
  24.         void pop();
  25.         int top();
  26.         bool empyt();
  27.         int size();
  28. };
  29. int main()
  30. {
  31.         mystack aaaaa;
  32.        
  33.         while(true)
  34.         {
  35.                 int a;
  36.                 cout<<"1.加入元素\n2.删除元素\n3.显示顶端的元素\n4.栈是否为空(1是,0否)\n5.栈的大小\n6.结束"<<endl;
  37.                 cin>>a;
  38.                 if(a==1)
  39.                 {
  40.                         int sh;
  41.                         cin>>sh;
  42.                         aaaaa.push(sh);
  43.                 }
  44.                 else if(a==2) aaaaa.pop();
  45.                 else if(a==3) cout<<aaaaa.top()<<endl;
  46.                 else if(a==4) cout<<aaaaa.empyt()<<endl;
  47.                 else if(a==5) cout<<aaaaa.size()<<endl;
  48.                 else if(a==6) break;
  49.                 system("pause");
  50.                 system("cls");
  51.         }
  52.        
  53.         return 0;
  54. }
  55. void mystack::push(int v)
  56. {
  57.         node* curr = new node(v);
  58.         if(root==NULL)
  59.         {
  60.                 root = curr;
  61.                 ptop = curr;
  62.                 return;
  63.         }
  64.         ptop->next = curr;
  65.         curr->prev = ptop;
  66.         ptop = curr;
  67.         n++;
  68. }
  69. void mystack::pop()
  70. {
  71.         if(root!=ptop)
  72.         {
  73.                 node* curr = ptop;
  74.                 ptop = ptop->prev;
  75.                 ptop->next = NULL;
  76.                 delete curr;
  77.                 n--;
  78.         }
  79.         else if(root==ptop&&root!=NULL)
  80.         {
  81.                 node* curr = ptop;
  82.                 delete curr;
  83.                 n--;
  84.                 root = NULL;
  85.                 ptop = NULL;
  86.         }
  87.         else cout<<"栈为空"<<endl;
  88. }
  89. int mystack::top()
  90. {
  91.         return ptop->value;
  92. }
  93. bool mystack::empyt()
  94. {
  95.         if(root==NULL)return true;
  96.         else return false;
  97. }
  98. int mystack::size()
  99. {
  100.         return n;
  101. }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表