Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // linking_list.cpp : 此檔案包含 'main' 函式。程式會於該處開始執行及結束執行。
- //
- #include "pch.h"
- #include <iostream>
- using namespace std;
- class node
- {
- public:
- int num;
- node *next=NULL;
- };
- class list
- {
- public:
- void push(int);
- void pop();
- private:
- node *head = new(node);
- };
- void list::push(int num)
- {
- //cout << "\ncall push\n";
- node* pos=list::head;
- while (pos->next != NULL)
- {
- //cout << "***pos->next= " << pos->next << endl;
- pos = pos->next;
- }
- //cout << "***pos= " << pos << endl;
- pos = pos->next = new(node);
- pos->num = num;
- //cout << "push end\n";
- }
- void list::pop()
- {
- if (head != NULL)
- {
- //cout << "\nstart pop\n";
- //cout << "**head= " << head << endl;
- //cout << "**head->next= " << head->next << endl;
- node *del = head;
- //cout << "***change head\n";
- head = head->next;
- //cout << "**head= " << head << endl;
- cout << del->num << endl;
- delete(del);
- }
- else
- {
- cout << "list is empty!\n";
- }
- }
- int main()
- {
- list list1;
- cout << "list1 creat\n";
- list1.push(1);
- list1.push(2);
- list1.push(3);
- list1.push(4);
- cout << "all push end\n";
- list1.pop();
- list1.pop();
- list1.pop();
- list1.pop();
- list1.pop();
- list1.pop();
- list1.pop();
- cout << "all pop end\n";
- }
- // 執行程式: Ctrl + F5 或 [偵錯] > [啟動但不偵錯] 功能表
- // 偵錯程式: F5 或 [偵錯] > [啟動偵錯] 功能表
- // 開始使用的秘訣:
- // 1. 使用 [方案總管] 視窗,新增/管理檔案
- // 2. 使用 [Team Explorer] 視窗,連線到原始檔控制
- // 3. 使用 [輸出] 視窗,參閱組建輸出與其他訊息
- // 4. 使用 [錯誤清單] 視窗,檢視錯誤
- // 5. 前往 [專案] > [新增項目],建立新的程式碼檔案,或是前往 [專案] > [新增現有項目],將現有程式碼檔案新增至專案
- // 6. 之後要再次開啟此專案時,請前往 [檔案] > [開啟] > [專案],然後選取 .sln 檔案
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement