完整的Linked List程式


#include<bits/stdc++.h>
using namespace std;

struct Node
{
    int data;
    Node * next;
};

int main()
{

    Node *head= NULL, *tail, *ptr, *temp, *prev, *next;
    int selection, value, key, found, index, i;

    while (1) //重複執行選單畫面
    {
        system("cls"); // 清空畫面
        cout << "==== Function List ====" << endl;
        cout << "[1] Create linked list"<<endl;
        cout << "[2] Push the Node after last Node"<<endl;
        cout << "[3] Delete the last Node"<<endl;
        cout << "[4] Display whole linked list" << endl;
        cout << "[5] Search Node"<<endl;
        cout << "[6] Insert a Node before given position"<<endl;
        cout << "[7] Delete a Node at a given position"<<endl;
        cout << "[8] Reverse linked list" << endl;
        cout << "==================" << endl << endl;
        cout << "Enter function number:";

        cin >> selection; //選擇要使用第幾個功能

        if (selection !=1 & head == NULL)
        {
            cout << "You haven't create a linked list yet" << endl;
            system("pause");
            continue;
        }

        switch (selection)
        {

        case 1: // Create linked list

            cout << "Enter value of new Node:";
            cin >> value;
            if (head == NULL)
            {
                head = new Node;
                head ->next=NULL;
                head ->data=value;
                tail = head;
                cout << "Create a Linked list and new Node is " << value << endl;
            }
            else
            {
                cout << "You already created linked list" << endl;
            }

            system("pause");
            break;

        case 2: // Push the Node after last Node

            cout << "Enter the new Node value:";
            cin >> value;
            cout << "You enter " << value << " into linked list" << endl;

            ptr = new Node;
            ptr ->data=value;
            ptr ->next=NULL;
            tail ->next=ptr;
            tail = ptr;

            ptr = head;
            while (ptr != NULL)
            {
                cout << ptr ->data << " ";
                ptr = ptr ->next;
            }

            system("pause");
            break;

        case 4: // Display whole linked list

            ptr = head;
            while (ptr != NULL)
            {
                cout << ptr ->data << " ";
                ptr = ptr ->next;
            }

            system("pause");
            break;


        case 3: // Delete the last Node

            if (head == tail)
            {
                ptr = head;
                head = NULL;
                delete ptr;
                cout << "Clear linked list" << endl;
                system("pause");
                break;
            }

            ptr = head;
            while (ptr ->next != tail || head == tail)
            {
                ptr = ptr ->next;
            }

            ptr ->next=NULL;
            delete tail;
            tail = ptr;

            ptr = head;
            while (ptr != NULL)
            {
                cout << ptr ->data << " ";
                ptr = ptr ->next;
            }

            system("pause");
            break;

        case 5: // Search Node

            cout << "Which value you want to search:";
            cin >> key;

            found = false;
            ptr = head;
            while (ptr != NULL)
            {
                if (ptr ->data == key)
                {
                    found = true;
                    break;
                }
                ptr = ptr ->next;
            }

            if (found)
                cout << key << " is found" << endl;
            else
                cout << key << " is not found" << endl;

            ptr = head;
            while (ptr != NULL)
            {
                cout << ptr ->data << " ";
                ptr = ptr ->next;
            }

            system("pause");
            break;


        case 6: // Insert a Node before given position

            i = 0;
            ptr = head;
            while (ptr != NULL)
            {
                cout << "[" << i++ << "]:" << ptr ->data << " ";
                ptr = ptr ->next;
            }
            cout << "Enter the index of Node you want to insert before:";
            cin >> index;

            cout << "Enter the value of new Node:";
            cin >> value;

            if(index==0)
            {
                ptr = new Node; //創立一個全新的Node
                ptr->data=value;
                ptr->next=head;
                head=ptr;
            }
            else
            {
                i = 0;
                ptr = head;
                while (ptr != NULL)
                {
                    if (i+1 == index)
                    {
                        temp = new Node;
                        temp ->data=value;
                        temp ->next = ptr ->next;
                        ptr ->next = temp;
                        break;
                    }
                    ptr = ptr ->next;
                    i++;
                }
            }

            ptr = head;
            while (ptr != NULL)
            {
                cout << ptr ->data << " ";
                ptr = ptr ->next;
            }

            system("pause");
            break;


        case 7: // Delete a Node at a given position

            i = 0;
            ptr = head;
            while (ptr != NULL) // 先列出整個linked list
            {
                cout << "[" << i++ << "]:" << ptr ->data << " ";
                ptr = ptr ->next;
            }

            cout << "Enter the index of Node to delete:";
            cin >> index;

            if (index == 0)
            {
                ptr = head;
                head = head ->next;
                delete ptr;
            }
            else
            {

                i = 0;
                ptr = head;
                while (ptr != NULL)
                {
                    if ((i + 1) == index)//如果下一個Node是我們要刪除的
                    {
                        if (ptr ->next == tail)//如果我們要刪的是最後一個Node
                        {
                            tail = ptr; // 因為原本的tail要被刪掉了,所以要換位置
                        }
                        temp = ptr ->next; // temp是我們要刪除的Node
                        ptr ->next=ptr ->next ->next;
                        delete temp;
                        break;
                    }
                    ptr = ptr ->next;
                    i++;
                }
            }

            ptr = head;
            while (ptr != NULL)
            {
                cout << ptr ->data << " ";
                ptr = ptr ->next;
            }

            system("pause");
            break;

        case 8: // Reverse linked list

            prev = NULL;
            next = NULL;

            // 更新tail
            tail = ptr = head;
            while (ptr != NULL)
            {
                next = ptr ->next;
                ptr ->next = prev;
                prev = ptr;
                ptr = next;
            }

            // 更新head
            head = prev;
            ptr = head;
            while (ptr != NULL)
            {
                cout << ptr ->data << " ";
                ptr = ptr ->next;
            }

            system("pause");
            break;

        default:
            break;

        }


    }
}

results matching ""

    No results matching ""