Advertisement
gha890826

linked_list - stack

Jun 4th, 2019
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. // linking_list.cpp : 此檔案包含 'main' 函式。程式會於該處開始執行及結束執行。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. class node
  9. {
  10. public:
  11.     int num;
  12.     node *next = NULL;
  13. };
  14.  
  15. class list
  16. {
  17. public:
  18.     void push(int);
  19.     void pop();
  20. private:
  21.     node *head = new(node);
  22. };
  23.  
  24. void list::push(int num)
  25. {
  26.     //cout << "\ncall push\n";
  27.     node* pos = list::head;
  28.     while (pos->next != NULL)
  29.     {
  30.         //cout << "***pos->next= " << pos->next << endl;
  31.         pos = pos->next;
  32.     }
  33.     //cout << "***pos= " << pos << endl;
  34.     pos = pos->next = new(node);
  35.     pos->num = num;
  36.     //cout << "push end\n";
  37. }
  38.  
  39. void list::pop()
  40. {
  41.     if (head->next != NULL)
  42.     {
  43.         //cout << "\nstart pop\n";
  44.         node *pos = list::head,*pre=list::head;
  45.         while (pos->next != NULL)
  46.         {
  47.             pre = pos;
  48.             //cout << "***pos->next= " << pos->next << endl;
  49.             pos = pos->next;
  50.         }
  51.         pre->next = NULL;
  52.         //cout << "**head= " << head << endl;
  53.         cout << pos->num << endl;
  54.         delete(pos);
  55.     }
  56.     else
  57.     {
  58.         cout << "list is empty!\n";
  59.     }
  60. }
  61.  
  62. int main()
  63. {
  64.     list list1;
  65.     cout << "list1 creat\n";
  66.     list1.push(1);
  67.     list1.push(2);
  68.     list1.push(3);
  69.     list1.push(4);
  70.     cout << "all push end\n";
  71.     list1.pop();
  72.     list1.pop();
  73.     list1.pop();
  74.     list1.pop();
  75.     list1.pop();
  76.     list1.pop();
  77.     list1.pop();
  78.     cout << "all pop end\n";
  79. }
  80.  
  81. // 執行程式: Ctrl + F5 或 [偵錯] > [啟動但不偵錯] 功能表
  82. // 偵錯程式: F5 或 [偵錯] > [啟動偵錯] 功能表
  83.  
  84. // 開始使用的秘訣:
  85. //   1. 使用 [方案總管] 視窗,新增/管理檔案
  86. //   2. 使用 [Team Explorer] 視窗,連線到原始檔控制
  87. //   3. 使用 [輸出] 視窗,參閱組建輸出與其他訊息
  88. //   4. 使用 [錯誤清單] 視窗,檢視錯誤
  89. //   5. 前往 [專案] > [新增項目],建立新的程式碼檔案,或是前往 [專案] > [新增現有項目],將現有程式碼檔案新增至專案
  90. //   6. 之後要再次開啟此專案時,請前往 [檔案] > [開啟] > [專案],然後選取 .sln 檔案
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement