C + + list

I didn’t care much about linked lists in C++ before, but suddenly I had to write them myself, and I kept making mistakes. No way, decided to make up the knowledge of this aspect, but also for the future of the data structure of the next big good foundation, so I summed up the following points, some places may not be correct, but also hope that everyone is generous to give advice, aimed at common progress.

Conclusion:

1. The basic unit of List is Node, so to facilitate operation, we must lay a good foundation for each step. The basic structure of Node is as follows:

class Node {
public:
    int data;
    Node *next;

    Node(int da = 0, Node *p = NULL) {
        this->data = da;
        this->next = p; }};Copy the code

Node member variables are public and cannot be private because they are operated on. The second argument defaults to NULL, which means that if we create a new Node by specifying only the first argument and not writing the second argument, it defaults to NULL. This is a more flexible way to use Node.

2, the second step is to create our linked list, the same here we first give the linked list code, and then explain one by one.

class List{
private:
  Node *head,*tail;
  int position;
public:
  List(){head=tail=NULL; }; ~List(){delete head; delete tail; };void print();
  void Insert(int da=0);
  void Delete(int da=0);
  void Search(int da=0);
};Copy the code

We have two data types here, one is Node. The other is a member variable that refers to the location of the node (it doesn’t help, but let it go). The use of head and tail names is intended to make the operation more accurate. The most important thing is that in each function we can operate head and tail by default. This simplifies the argument list and makes the function more elegant. Here is my implementation of a single linked list, including creating a linked list, inserting values, deleting specific values, and finding specific values in the list.

#include <iostream>

using namespace std;

class Node {
public:
    int data;
    Node *next;

    Node(int da = 0, Node *p = NULL) {
        this->data = da;
        this->next =p; }}; classList {
private:
    Node *head, *tail;
    int position;
public:
    List() { head = tail = NULL; };

    ~List() {
        delete head;
        delete tail;
    };

    void print();

    void Insert(int da = 0);

    void Delete(int da = 0);

    void Search(int da = 0);

    int getValueAt(int position);

    void setValueAt(int position, int da);
};

int List::getValueAt(int position) {
    Node *p = head;
    if (p = = NULL) {
        cout << "The List is Empty!" << endl;
    } else {
        int posi = 0;
        while (p ! = NULL && posi ! = position) {
            posi++;
            p = p->next;
        }
        if (p = = NULL) {
            cout << "There is no value of this position in this List!" << endl;
        } else {
            cout << "In this Position,the value is" << p->data <<endl; }}return p->data;
}

void List::setValueAt(int position, int da) {
    Node *p = head;
    if (p = = NULL) {
        cout << "The List is Empty!" << endl;
    } else {
        int posi = 0;
        while (p ! = NULL && posi ! = position) {
            posi++;
            p = p->next;
        }
        if (p = = NULL) {
            cout << "There is No Position in this List!" << endl;
        } else {
            p->data = da;
            cout << "The Value in this position has been Updated!" <<endl; }}}void List::Search(int da) {

    Node *p = head;
    if (p = = NULL) {
        cout << "Sorry, The List is Empty!" << endl;
        return;
    }
    int count = 0;
    while (p ! = NULL && p->data ! = da) {
        p = p->next;
        count++;
    }
    cout << "the value you want to search is at position %d" << count << endl;
}

void List::Delete(int da) {
    Node *p = head, *q = head;
    if (p = = NULL) {
        cout << "Sorry, The List is Empty!" << endl;
        return;
    }
    while (p ! = NULL && p->data ! = da) {
        q = p;
        p = p->next;
    }
    q->next = p->next;
    cout << "The Deletion Operation had been finished!" << endl;
}

void List::Insert(int da) {
    if (head = = NULL) {
        head = tail = new Node(da);
        head->next = NULL;
        tail->next = NULL;
    } else {
        Node *p = new Node(da);
        tail->next = p;
        tail = p;
        tail->next = NULL; }}void List::print() {
    Node *p = head;
    while (p ! = NULL) {
        cout << p->data << " \a";
        p = p->next;
    }
    cout << endl;
}

int main() {
    cout << "Hello World!" << endl;
    List l1;
    l1.Insert(1);
    l1.Insert(2);
    l1.Insert(3);
    l1.Insert(4);
    l1.Insert(5);
    l1.Insert(6);
    l1.Insert(7);
    l1.print();
    l1.Search(4);
    l1.Delete(6);
    l1.print();
    l1.getValueAt(3);
    l1.setValueAt(3.9);
    l1.print();
    cout << "The End!" << endl;
    return 0;
}Copy the code

Running results: