Single-chain table [Table creation, output, insertion, deletion, and reverse order]
During the interview, I was always asked about the Single-linked list. simply summarize it.
This article mainly summarizes the creation, output, reverse order, insertion and deletion of a single-chain table.
Reverse schematic diagram:
Source code:
# Include
Using namespace std; typedef int ElemType; // data type template typedef struct Node // Node {ElemType data; struct Node * next;} Node; typedef struct Node * LinkList; //////// create the table Node * creat_Link (Node * head) {int x; Node * p, * q; head = (Node *) malloc (sizeof (Node); head-> next = NULL; q = head; cin> x; while (x! = 999) {p = (Node *) malloc (sizeof (Node); p-> data = x; p-> next = NULL; q-> next = p; q = p; cin> x;} return head; // return the header Node after the table is created} // output void output_Link (Node * head) {if (head = NULL) {cout <"Empty linked list! "<
Next; while (q! = NULL) {cout <
Data <"; q = q-> next ;}///// // reverse Node * reverse_Link (Node * head) {Node * current, * p; if (head = NULL) {cout <"Empty linked list! "<
Next; while (current-> next! = NULL) {p = current-> next; current-> next = p-> next; p-> next = head-> next; head-> next = p ;} return head;} // insert Node * insert_Link (Node * head, int I, ElemType e) // insert e {Node * q, * iq; q = head-> next; for (int j = 1; j
Next;} Node * s; // insert S Node in iq // s = (LinkList) malloc (sizeof (Node )); // generate new Node s = (Node *) malloc (sizeof (Node); s-> data = e; s-> next = iq-> next; iq-> next = s; return head;} // delete Node * delete_Link (Node * head, int I) // Delete location I {if (I = 1) head = head-> next; else {Node * p, * q; q = head-> next; for (int j = 1; j
Next; // locate the deleted q location} p-> next = q-> next; delete q; // free (q);} return head;} int main () {Node * head = NULL; // creat_Link (head); Node * m = creat_Link (head); // create a table cout <"Initial linked list:" <
Test results: