C language single linked list common operations summary _c language

Source: Internet
Author: User

C language Single linked list is one of the most commonly used data structures, this article summarizes the common operations of single linked lists, examples are as follows:

#include <stdio.h> #include <stdlib.h>//definition of single linked list structure 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 ("Please 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; 
The length of the single linked list is 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", num); 
}//Find the precursor node void found (linklist l,int x) {linklist p,q; 
P=l; 
while (P->next &&p->next->data!=x) p=p->next; 
if (p->next) printf ("%d pioneer node is%d", x,p->data); 
else printf ("not Found"); 
}//delete element void Delete with node value x (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 ("Delete success!!") 
"); 
///reverse the elements in the table to 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 success!! 
"); 
///Sort a single list of 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 (the sort succeeded!) 
"); 
}//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 (delete succeeded!!) 
"); 
///In ascending list, insert new element, throw ordered void Insert (Linklist l,linklist p) {linklist s; 
S=l; 
while (S->next && s->next->data<p->data) s=s->next; 
p->next=s->next; 
s->next=p; 
The//prompt interface displays void Tips () {printf ("\ n"); printf"Select the appropriate action according to the key: \ n"); 
printf ("<1> output single linked list and its length: \ n"); 
printf ("<2> lookup a direct precursor node of x: \ n"); 
printf ("<3> delete node with x value: \ n"); 
printf ("<4> invert elements in table: \ n"); 
printf ("<5> sort a single list from small to large: \ n"); 
printf ("<6> Delete the same element in the table: \ n"); 
printf ("<7> inserts element x:\n in ascending list)"; 
printf ("<0> exit: \ n"); 
}//Main function void Main () {int op,x; 
Linklist l,p; 
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 ("Please enter the element to find x:\n"); 
scanf ("%d", &x); 
Find (L,X); 
Break 
Case 3:printf ("Please 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 ("Please enter the element to be inserted x:\n"); 
scanf ("%d", &x); 
p= (linklist) malloc (sizeof (Lnode)); 
p->data=x; 
Insert (L,P); printf ("Insert succeeded!!! 
\ n '); 
Break 
} scanf ("%d", &op); } 
}

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.