C language --- single-chain table creation, length determination, deletion, sorting, and other operations

Source: Internet
Author: User

# Include
 
  
# Include
  
   
// Define the Node struct typedef int ElemType; typedef struct Node {ElemType data; struct Node * next;} LNode, * LinkList; // create a single-chain table void Build (LinkList L) {int n; LinkList p, q; p = L; printf ("Enter n and n numbers: \ n "); scanf (" % d ", & n); while (n --) {q = (LinkList) malloc (sizeof (LNode )); scanf ("% d", & q-> data); q-> next = NULL; p-> next = q; p = q ;}} // calculate the length of a single-chain table void LinkLength (LinkList L) {int num = 0; LinkList p; p = L-> next; while (p) {num ++; printf ("% 3d", p-> data ); P = p-> next;} printf ("Length: % d \ n", num);} // qvoid Find (LinkList L, int x) {LinkList p; p = L; while (p-> next & p-> next-> data! = X) p = p-> next; if (p-> next) printf ("% d: % d \ n", x, p-> data); elseprintf ("not found !! \ N ");} // Delete the node void Delete (LinkList L, int x) {LinkList p, q; p = L; while (p-> next & p-> next-> data! = X) p = p-> next; if (p-> next) {q = p-> next; p-> next = q-> next; free (q ); printf ("deleted successfully !! \ N ", x) ;}// Reverse void Reverse (LinkList L) {LinkList p, s; p = s = L-> next; l-> next = NULL; while (p) {s = s-> next; p-> next = L-> next; L-> next = p; p = s;} printf ("reverse configuration successful !!! \ N ") ;}// sort a single-chain table void sort (LinkList L) {LinkList p, q; int temp; for (p = L; p-> next! = NULL; p = p-> next) {for (q = p-> next; q! = NULL; q = q-> next) {if (p-> data> q-> data) {temp = p-> data; p-> data = q-> data; q-> data = temp ;}} printf ("sorting successful! \ N ") ;}// deletes the same element void Deletesameelem (LinkList L) {LinkList p, q, s; p = L; q = L-> next; while (q-> next) {if (q-> data = q-> next-> data) {p-> next = q-> next; s = q; q = q-> next; free (s) ;}else {p = p-> next; q = q-> next ;}} printf ("deleted successfully !!!! \ N ") ;}void Tips () {printf (" \ n "); printf (" select the appropriate operation based on the buttons: \ n "); printf ("<1> output single-chain table and its length: \ n"); printf ("<2> Find the direct precursor node with the value of x: \ n "); printf ("<3> Delete the node with the value of x: \ n"); printf ("<4> reverse element: \ n "); printf ("<5> sort a single-chain table from small to large: \ n"); printf ("<6> Delete the same element in the Table: \ n "); printf ("<0> Exit: \ n");} void main () {int op, x; LinkList L; L = (LinkList) malloc (sizeof (LNode )); l-> next = NULL; L-> data =-1; Build (L); Tips (); scanf ("% d", & op); while (op) {switch (op) {case 1: LinkLength (L); break; case 2: printf ("Enter the element to be searched x: \ n "); scanf ("% d", & x); Find (L, x); break; case 3: printf ("Enter the element to delete x: \ n "); scanf ("% d", & x); Delete (L, x); break; case 4: Reverse (L); break; case 5: sort (L); break; case 6: Deletesameelem (L); break;} scanf ("% d", & op );}}
  
 

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.