C language single linked list common operations summary

  • 2020-04-02 02:21:11
  • OfStack

The single linked list of C language is one of the commonly used data structures. This paper summarizes the common operations of the single linked list, with the following examples:


#include<stdio.h> 
#include<stdlib.h> 
//Defines a single linked list structure
typedef int ElemType; 
typedef struct Node 
{ 
ElemType data; 
struct Node *next; 
}LNode,*LinkList; 
//Create a single linked list
void Build(LinkList L) 
{ 
int n; 
LinkList p,q; 
p=L; 
printf(" Please enter the n and n An element :n"); 
scanf("%d",&n); 
while(n--) 
{ 
q=(LinkList)malloc(sizeof(LNode)); 
scanf("%d",&q->data); 
q->next = NULL; 
p->next=q; 
p=q; 
} 
} 
//Find the length of a single linked list
void LinkLength(LinkList L) 
{ 
int num = 0; 
LinkList p; 
p=L->next; 
while(p) 
{ 
num++; 
printf("%3d",p->data); 
p=p->next; 
} 
printf(" Length is: %d",num); 
} 
//Find the precursor 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 Is the precursor node of %d",x,p->data); 
else 
printf(" Could not find "); 
} 
//Delete the element whose node value is x
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(" Delete successfully!! "); 
} 
//Invert the elements in the table
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(" Inverse success!! "); 
} 
//Sort a single linked list
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(" Delete successfully!! "); 
} 
//In an ascending list, insert new elements and throw them in order
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; 
} 
//Prompt interface display
void Tips() 
{ 
printf("n"); 
printf(" Select the corresponding operation according to the button: n"); 
printf("<1> Output single linked list and its length :n"); 
printf("<2> Find the value of x Direct precursor node of :n"); 
printf("<3> Delete a value of x The node :n"); 
printf("<4> Invert the elements in the table :n"); 
printf("<5> Sort a single linked list from small to large :n"); 
printf("<6> Delete the same element in the table :n"); 
printf("<7> Inserts an element in an ascending list x:n"); 
printf("<0> exit :n"); 
} 
//The 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 you are looking for x:n"); 
scanf("%d",&x); 
Find(L,x); 
break; 
case 3: 
printf(" Please 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; 
case 7: 
printf(" Please enter the element to insert X:n"); 
scanf("%d",&x); 
p=(LinkList)malloc(sizeof(LNode)); 
p->data=x; 
Insert(L,p); 
printf(" Insert the success !!!nn"); 
break; 
} 
scanf("%d",&op); 
} 
}

Related articles: