Data structure C language: single-chain table, data structure C language single-chain

Source: Internet
Author: User

Data structure C language: single-chain table, data structure C language single-chain

One-way linked list with table header nodes

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Struct NODE {int data; struct NODE * next;} H, * head, * p, * q, * q1, * s1, * s2, * s3, * s4, * s; int I, j, k, n, t, m; int main () {srand (time (NULL); // fill in the header node data H. data =-1; H. next = NULL; head = & H; // create a single-chain table with 10 nodes, p = head; for (I = 0; I <10; I ++) {q = (struct NODE *) malloc (sizeof (struct NODE); if (NULL = q) return 1; q-> data = rand () % 100; // enter 0 .. random q-> next = NULL for 99; p-> next = q; p = q;} // output the entire single-chain table s = head-> next; while (1) {if (NULL = s) {p Rintf ("\ n"); break;} printf ("% 02d->", s-> data); s = s-> next ;} // insert a node with a value of 5 into the first k node of the single-chain table k = 3; n = 0; p = head; while (1) {if (NULL = p) {break;} n ++; if (k = n) {q = (struct NODE *) malloc (sizeof (struct NODE )); if (NULL = q) return 1; q-> data = 5; q-> next = p-> next; p-> next = q; break ;} p = p-> next;} // output the entire single-chain table s = head-> next; while (1) {if (NULL = s) {printf ("\ n"); break;} printf ("% 02d->", s-> data); s = s-> next ;}// Delete the k-th node k = 5; n = 0; p = head; while (1) {if (NULL = p) {break;} n ++; if (k = n) {q = p-> next; if (q) {p-> next = q-> next; free (q) ;} break ;} p = p-> next;} // output the entire single-chain table s = head-> next; while (1) {if (NULL = s) {printf ("\ n"); break;} printf ("% 02d->", s-> data); s = s-> next ;} // sort from small to large for (p = head; p! = NULL & p-> next! = NULL; p = p-> next) {for (q = p-> next; q! = NULL & q-> next! = NULL; q = q-> next) {if (p-> next-> data> q-> next-> data) {// exchange data // printf ("swap % 02d % 02d \ n", p-> next-> data, q-> next-> data ); // t = p-> next-> data; p-> next-> data = q-> next-> data; q-> next-> data = t; /// // switch next // printf ("swap % 02d % 02d \ n", p-> next-> data, q-> next-> data ); s1 = p-> next; s2 = p-> next; s3 = q-> next; s4 = q-> next; if (s2! = S3) {p-> next = s3; s3-> next = s2; q-> next = s1; s1-> next = s4 ;} else {p-> next = s3; s3-> next = s1; q = s3; s1-> next = s4 ;} // output the entire single-chain table // s = head-> next; // while (1) {// if (NULL = s) {// printf ("\ n"); // break; //} // printf ("% 02d->", s-> data ); // s = s-> next; //} // getchar () ;}}// output the entire single-chain table s = head-> next; while (1) {if (NULL = s) {printf ("\ n"); break;} printf ("% 02d->", s-> data ); s = s-> next;} // reverse the entire linked list if (head-> next! = NULL & head-> next! = NULL) {p = head-> next; q = p-> next; p-> next = NULL; while (1) {q1 = q-> next; q-> next = p; p = q; q = q1; if (NULL = q) break;} head-> next = p ;} // output the entire single-chain table s = head-> next; while (1) {if (NULL = s) {printf ("\ n"); break ;} printf ("% 02d->", s-> data); s = s-> next;} // swap the first m and last n nodes in a single-chain table, m + n is the total length of the linked list 10 m = 4; n = 6; k = 0; p = head; while (1) {if (NULL = p) {break ;} k ++; if (m + 1 = k) {q = p;} s = p; p = p-> next;} s1 = head-> next; head-> next = q-> next; s-> next = s1; q-> next = NULL; // output the entire single-chain table s = head-> next; while (1) {if (NULL = s) {printf ("\ n"); break;} printf ("% 02d->", s-> data ); s = s-> next;} // release all nodes p = head-> next; while (1) {if (NULL = p) {break ;} q = p-> next; free (p); p = q;} return 0 ;} // 84-> 28-> 20-> 23-> 96-> 19-> 59-> 97-> 29-> 41-> // 84-> 28-> 05-> 20-> 23-> 96-> 19-> 59-> 97-> 29-> 41-> // 84-> 28-> 05-> 20-> 96-> 19-> 59-> 97-> 29-> 41-> // 05-> 19-> 20-> 28-> 29-> 41-> 59 -> 84-> 96-> 97-> // 97-> 96-> 84-> 59-> 41-> 29-> 28-> 20-> 19-> 05-> // 41-> 29-> 28-> 20-> 19-> 05-> 97-> 96-> 84-> 59-> //
    
   
  
 

One-way linked list without table header nodes

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
Struct NODE {int data; struct NODE * next;} * head, * p, * q, * s, * p1, * p2, * q1, ** ta; int I, k, n, t, m, v, N = 10; int main () {setlocale (LC_ALL, "chs"); srand (time (NULL); head = NULL; printf ("create a single-chain table of % d nodes:", N); // create a single-chain table of N nodes p = head; for (I = 0; I
      
        Data = rand () % 100; // enter 0 .. random q-> next = NULL for 99; if (NULL = p) {head = q; p = head;} else {p-> next = q; p = q ;}// output the entire single-chain table s = head; while (1) {if (NULL = s) {printf ("\ n"); break ;} printf ("% 02d->", s-> data); s = s-> next;} k = 3; v = 5; printf ("insert the node with the value of % d to the % d node in the single-link table:", v, k ); // insert a node with the value of v to the first n = 0; p = head; while (1) {if (NULL = p) of the k node of the single-chain table) {break;} n ++; if (k = 1) {q = (struct NODE *) malloc (sizeof (struct NODE); if (NUL L = q) exit (1); q-> data = v; q-> next = head; head = q; break;} else {if (k-1 = n) {q = (struct NODE *) malloc (sizeof (struct NODE); if (NULL = q) exit (1); q-> data = v; q-> next = p-> next; p-> next = q; break;} p = p-> next;} // output the entire single-chain table s = head; while (1) {if (NULL = s) {printf ("\ n"); break;} printf ("% 02d->", s-> data ); s = s-> next;} k = 5; printf ("delete node % d:", k); // delete node k n = 0; p = head; while (1) {if (NULL = p) {break;} N ++; if (k = 1) {q = head; head = head-> next; free (q); break ;} else {if (k-1 = n) {q = p-> next; if (q) {p-> next = q-> next; free (q );} break ;}} p = p-> next;} // output the whole single-chain table s = head; while (1) {if (NULL = s) {printf ("\ n"); break;} printf ("% 02d->", s-> data); s = s-> next ;} printf ("Ascending Order:"); // ascending order for (p = head, p1 = NULL; p! = NULL; p1 = p, p = p-> next) {for (q = p-> next, q1 = p; q! = NULL; q1 = q, q = q-> next) {if (p-> data> q-> data) {// exchange data // printf ("swap % 02d % 02d \ n", p-> data, q-> data); // t = p-> data; p-> data = q-> data; q-> data = t; // or // switch next // printf ("swap % 02d % 02d \ n ", p-> data, q-> data); if (p = head) {// p is the header if (p-> next = q) {// pq: head = q; p-> next = q-> next; q-> next = p; q = p; p = head ;} else {// pq is not followed by head = q; p2 = p-> next; p-> next = q-> next; q-> next = p2; q1-> next = p; q = p; p = head ;}} else {// p is not the header if (p-> next = q) {// pq is next to p1-> next = q; p-> next = q-> next; q-> next = p; q = p; p = p1-> next;} else {// pq is not next to p1-> next = q; p2 = p-> next; p-> next = q-> next; q-> next = p2; q1-> next = p; q = p; p = p1-> next ;}// output the entire single-chain table // s = head; // while (1) {// if (NULL = s) {// printf ("\ n"); // break; //} // printf ("% 02d->", s-> data); // s = s-> next; //} // getchar () ;}}// output the entire single-chain table and calculate the chain table length n = 0; s = head; while (1) {if (NULL = s) {printf ("\ n"); break;} printf ("% 02d->", s-> data); n ++; s = s-> next;} printf ("reverse order of the entire linked list:"); // reverse order of the entire linked list if (n> = 2) {p = head; q = p-> next; p-> next = NULL; while (1) {q1 = q-> next; q-> next = p; p = q; q = q1; if (NULL = q) break;} head = p;} // output the entire single-chain table s = head; while (1) {if (NULL = s) {printf ("\ n"); break;} printf ("% 02d->", s-> data ); s = s-> next;} m = 4; n = 6; printf ("swap the first % d nodes and the last % d nodes in the single-linked list:", m, n); // swap the first m nodes and the last n nodes in the single-chain table. m + n is the total length of the chain table k = 0; p = head; while (1) {if (NULL = p) {break;} k ++; if (m = k) {q = p;} s = p; p = p-> next;} q1 = head; head = q-> next; s-> next = q1; q-> next = NULL; // output the whole single-chain table s = head; while (1) {if (NULL = s) {printf ("\ n"); break ;} printf ("% 02d->", s-> data); s = s-> next;} // release all nodes p = head; while (1) {if (NULL = p) {break;} q = p-> next; free (p); p = q;} return 0 ;} // create a single-chain table with 10 nodes: 08-> 74-> 07-> 23-> 03-> 99-> 31-> 56-> 88-> 16-> // insert a node with a value of 5 before the 3rd nodes of a single-chain table: 08-> 74-> 05-> 07-> 23-> 03-> 99-> 31-> 56-> 88-> 16-> // Delete the 5th nodes: 08-> 74-> 05-> 07-> 03-> 99-> 31-> 56-> 88-> 16-> // sort from small to large: 03-> 05-> 07-> 08-> 16-> 31-> 56-> 74-> 88-> 99-> // reverse the entire linked list: 99-> 88-> 74-> 56-> 31-> 16-> 08-> 07-> 05-> 03-> // Add the first four nodes and the last six nodes are exchanged: 31-> 16-> 08-> 07-> 05-> 03-> 99-> 88-> 74-> 56-> //
      
     
    
   
  
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.