temp->data
等价于 (\*temp).data
任意位置插入节点 1 2 3 4 5 6 7 8 9 10 11 Node* head; head = null; temp = new Node(); temp->data = 4 ; temp->link = NULL ; Node* temp1 = A; while (temp1->llink != NULL ) { temp1 = temp1->link; } temp1->link = temp;
头插法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node * next ; }; struct Node * head ; void Insert (int x) { Node* temp = (Node*)malloc (sizeof (struct Node)); temp->data = x; temp->next = head; head = temp; } void Print () { struct Node * temp = head; printf ("列表是:" ); while (temp != NULL ) { printf ("%d" , temp->data); temp = temp->next; } printf ("\n" ); } int main () { int n, x, i; head = NULL ; printf ("请输入你想输入的数字个数:" ); scanf ("%d" , &n); for (i = 0 ; i < n; i++) { printf ("请输入数字:\n" ); scanf ("%d" , &x); Insert(x); Print(); } }
任意位置插入一个节点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node * next ; }; struct Node * head ;void Insert (int x, int n) { Node* temp1 = (Node*)malloc (sizeof (struct Node)); temp1->data = x; temp1->next = NULL ; if (n == 1 ) { temp1->next = head; head = temp1; return ; } Node* temp2 = head; for (int i = 0 ; i < n - 2 ; i++) { temp2 = temp2->next; } temp1->next = temp2->next; temp2->next = temp1; } void Print () { struct Node * temp = head; printf ("列表是:" ); while (temp != NULL ) { printf ("%d" , temp->data); temp = temp->next; } printf ("\n" ); } int main () { head = NULL ; Insert(2 , 1 ); Print(); Insert(3 , 2 ); Print(); Insert(4 , 1 ); Print(); Insert(5 , 2 ); Print(); }
删除指定元素 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 void Delte (int n) { struct Node * temp3 = head; if (n == 1 ) { head = temp3->next; free (temp3); return ; } else { for (int i = 0 ; i < n - 2 ; i++) { temp3 = temp3->next; } struct Node * temp4 = temp3->next; temp3->next = temp4->next; free (temp4); } }
反转一个链表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void Reverse () { struct Node * current , * next , * prev ; current = head; prev = NULL ; while (current != NULL ) { next = current->next; current->next = prev; prev = current; current = next; } head = prev; }
递归打印链表 1 2 3 4 5 6 7 void Print (struct Node* head) { if (head == NULL ) return ; printf ("%d " , head->data); Print(head->next); }
递归反转链表 1 2 3 4 5 6 7 8 9 10 11 12 void Reverse2 (struct Node* current) { if (current->next == NULL ) { head = current; return ; } Reverse2(current->next); struct Node * link = current->next; link->next = current; current->next = NULL ; }
双向链表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node * next ; struct Node * prev ; }; struct Node * head ; struct Node* GetNewNode (int x) { struct Node * newNode = (struct Node*)malloc (sizeof (struct Node)); newNode->next = NULL ; newNode->prev = NULL ; newNode->data = x; return newNode; } void InsertAtHead (int x) { struct Node * temp = GetNewNode(x); if (head == NULL ) { head = temp; return ; } head->prev = temp; temp->next = head; head = temp; } void Print () { struct Node * temp = head; printf ("正序列表是:" ); while (temp != NULL ) { printf ("%d" , temp->data); temp = temp->next; } printf ("\n" ); } void ReversePrint () { struct Node * temp = head; if (temp == NULL ) return ; while (temp->next!= NULL ) { temp = temp->next; } printf ("逆序列表是:" ); while (temp != NULL ) { printf ("%d" , temp->data); temp = temp->prev; } printf ("\n" ); } int main () { head = NULL ; InsertAtHead(2 ); Print(); ReversePrint(); InsertAtHead(3 ); Print(); ReversePrint(); InsertAtHead(4 ); Print(); ReversePrint(); }