C++单向链表的创建、插入、删除、打印、销毁

#include <iostream>
#include <stack>
#include <string>
using namespace std;

struct node {
    int value;
    node *next;
};


//add a node at the end of the list
node *add_node(node *head, int value) {
    node *new_node = new node();
    new_node -> value = value;
    new_node -> next = NULL;
    node *p_node = new node();
    p_node = head;
    if(head == NULL)
        head = new_node;
    else {
        while(p_node -> next != NULL) {
            p_node = p_node -> next;
        }
        p_node -> next = new_node;
    }
    return head;
}

// remove a node at side temp;
node *remove_node_num(node *head, int temp) {
    node * delete_node = NULL;
    node * p_node = head;
    int temps = 0;
    if(head == NULL) {
        cout << "the list is empty!" << endl;
        return head;
    }
    else if (temp == 0) {
        delete_node = head;
        head = head -> next;
    }
    else {
        while(p_node -> next != NULL)
        {
            if(temps == temp - 1) {
                if(p_node -> next == NULL)
                    cout << "already find the end, the side is too large!" <<endl;
                    //already at end
                else
                {
                    delete_node = p_node -> next;
                    p_node -> next = p_node -> next -> next;
                }
                break;    
            }
            //comsider temp = 1;
            p_node = p_node -> next;
            temps ++;
        }
        if(temps < temp - 1)
            cout << "already find the end, the side is too large!" << endl;
        if(delete_node != NULL)
            delete delete_node;
    }
    return head;
}

// remove a node at a value;
node *remove_node_value(node *head, int values) {
    node * delete_node = NULL;
    node * p_node = head;
    if(head == NULL) {
        cout << "the list is empty!" << endl;
        return head;
    }
    else if (head -> value == values) {
        delete_node = head;
        head = head -> next;
    }
    else {
        while(p_node -> next != NULL)
        {
            p_node = p_node -> next;
            if(p_node -> next -> value == values) {
                delete_node = p_node -> next;
                p_node -> next = p_node -> next -> next;
                break;    
            }
        }

        if(delete_node != NULL)
            delete delete_node;
        else
            cout << "already find the end, the value is not find!" << endl;
    }
    return head;
}

// travel list
void travellist(node *head)
{
    if(head == NULL) {
        cout << "the list is empty!" << endl;
        return ;
    }
    node *p_node = new node();
    p_node = head;
    cout << p_node -> value << ' ';
    while(p_node -> next != NULL) {
        p_node = p_node -> next;
        cout << p_node -> value << ' ';
    }
}

//travel list reverse
void travellist_reverse(node *head)
{
    stack <node *> stack_list;
    if(head == NULL)
        return ;
    node *p_node = new node();
    p_node = head;
    
    stack_list.push(p_node);
    while(p_node -> next != NULL) {
        p_node = p_node -> next;
        stack_list.push(p_node);
    }

    while(!stack_list.empty())
    {
        p_node = stack_list.top();
        cout << p_node -> value << ' ';
        stack_list.pop();
    }
}

// destroy the list
void destroy_node(node *head)
{
    if(head == NULL)
        return ;
    while(head != NULL) {
        node *p_node = head;
        head = head -> next;
        delete p_node;
    }
}

int main() {
    node *head = NULL;
    cout << "the linked list is created!" << endl;
    cout << "please select the method! add remove1 remove2 travel1 travel2 exit" << endl;
    string A; int num;
    while(cin >> A)
    {
        if(A == "add") {
            cout << "please input the num to add!" << endl;
            cin >> num;
            head = add_node(head, num);
        }
        else if(A == "remove1") {
            cout << "please input the temp to find! (staet with 0!)" << endl;
            cin >> num;
            head = remove_node_num(head, num);
        }
        else if(A == "remove2") {
            cout << "please input the temp to find!" << endl;
            cin >> num;
            head = remove_node_value(head, num);
        }
        else if(A == "travel1") {
            travellist(head);
            cout << endl;
        }
        else if(A == "travel2") {
            travellist_reverse(head);
            cout << endl;
        }
        else if(A == "exit") {
            destroy_node(head);
            return 0;
        }
        else {
            cout << "please try again!" << endl;
        }
        cout << "please select the method! add remove1 remove2 travel1 travel2 exit" << endl;

    }
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Cris_7/article/details/82941060