Common Operations for a C-language single-chain table

Source: Internet
Author: User

# Include
 
  
# Include
  
   
// Define the single-chain table structure body 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 elements: \ 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 the single-chain table void LinkLength (LinkList L) {int num = 0; LinkList p; p = L-> next; while (p) {num ++; printf ("% 3d", p-> data); p = p-> n Ext;} printf ("Length: % d", num);} // Find the frontend node void Find (LinkList L, int x) {LinkList p, q; p = L; while (p-> next & p-> next-> data! = X) p = p-> next; if (p-> next) printf ("% d's precursor node is % d", x, p-> data ); elseprintf ("not found");} // Delete the element 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 !! ") ;}// Reverse void Reverse (LinkList L) {LinkList p, q; p = q = L-> next; L-> next = NULL; while (p) {q = q-> next; p-> next = L-> next; L-> next = p; p = q ;} printf ("reverse configuration successful !! ") ;}// Sort void sort (LinkList L) {LinkList p, q; int temp; p = L; 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! ") ;}// Delete 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 !! ") ;}// Insert new elements in the ascending linked list, and throw the ordered void Insert (LinkList L, LinkList p) {LinkList s; s = L; while (s-> next & s-> next-> data
   
    
Data) s = s-> next; p-> next = s-> next; s-> next = p;} // The prompt page displays 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 x value: \ n "); printf (" <3> Delete the node with the x value: \ n "); printf (" <4> reverse element in the Table: \ n "); printf (" <5> sort a single-chain table from small to large: \ n "); printf (" <6> Delete the same element in the Table: \ n "); printf (" <7> insert element x in the ascending linked list: \ n "); printf (" <0> Exit: \ n ");} // main function void main () {int op, x; LinkList L, p; L = (LinkList) malloc (sizeof (LNode); L-> next = NULL; L-> da Ta =-1; Build (L); Tips (); scanf ("% d", & op); while (op) {switch (op) {case 1: linkLength (L); break; case 2: printf ("Enter the element x: \ n"); scanf ("% d", & x ); find (L, x); break; case 3: printf ("Enter the element to be deleted x: \ n"); scanf ("% d", & x ); delete (L, x); break; case 4: Reverse (L); break; case 5: sort (L); break; case 6: Deletesameelem (L); break; case 7: printf ("Enter the element X: \ n"); scanf ("% d", & x); p = (LinkList) malloc (sizeof (LNode); p-> data = x; Insert (L, p); printf ("Insert Successful !!! \ N "); 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.