Get the App
SLTechnology News&Howtos  ›  Network Security  › 

The method of designing robustness: enter the head node of a linked list and print the linked list in reverse order.

Shulou Source: shulou.com Published: 2022-06-01 09:30:13 10月03日 Update

There have been concepts and basic algorithms for sorting linked lists before. What is more important is to insert, delete, traverse, and create tables (tail insertion, head insertion)

Recall the insertion node at the end of the list:

1 # include 2 using namespace std; 3 4 typedef struct Node {5 int data;// data field 6 Node * next;// pointer field 7} Node, * List; 8 9 / / add a node 10 void addNode (List * head, int value) 11 {12 / / dynamically create node 13 Node * newNode = newNode (); 14 newNode- > data = value;15 newNode- > next = NULL 16 / / determine whether the table is empty, head is the head pointer 17 if (* head = = NULL) {18 * head = newNode;19 cout data next! = NULL) {26 / / p as the mark, move 27 p = p-> next;28} 29 / / sequentially, find the tail node, and insert the new node 30 p-> next = newNode 31 cout next- > data next! = NULL & & p-> next- > data! = value) {20 p = p-> next;21} 22 / / Loop ends, and judge the situation found 23 if (p-> next! = NULL & & p-> next- > data = = value) {24 del = p-> next;25 / / Delete 26 p-> next = p-> next- > next 27} 28 / destroy memory 29 delete del;30 / / eliminate field pointer 31 del = NULL;32} 33 34 void traversal (List head) 35 {36 Node * p = head;37 if (p = = NULL) {38 cout next); 9} 10 / / then print the node 11 cout data

Tags: Nodes pointers reverse order recursion structure that is case output method input that is data complexity from beginning to end from beginning to end code dynamic indication array time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno Shulou Information Shulou Tech Info Apple