# A simple example of a bidirectional linked list of C language data structures

• 2020-05-12 03:00:24
• OfStack

Basic operation of bidirectional linked list

1. Using the tail insertion method to establish a two-way linked list.

2. Traversing a bidirectional list.

3. Implements the removal of a specified element from a bidirectional linked list.

4. Implement the insertion element e still - ordered algorithm in non - decreasing order bidirectional linked list.

5. Determines whether an element in a bidirectional list is symmetric if it returns 1 or 0.

6. If the element is a positive integer, the algorithm will arrange all odd Numbers before even Numbers.

7. Design a simple menu in the main function to debug the algorithm.

Example code:

// I'm not sorting because I didn't tell you whether the odd and even Numbers need to be sorted separately, so I'm not sorting, I'm just putting the odd Numbers after the even Numbers.
// When I created the list, because the experiment didn't require the length of the list to be output, I typed it 1 A length of n The linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *pre,*next;
}*h,*end;
void CreatList()// create 1 Two two-way linked lists
{
int n;
node *s,*e;
printf(" Please enter the list length:     ");
scanf("%d",&n);
printf(" Please enter data:       ");
h=(node *)malloc(sizeof(node));
s=(node *)malloc(sizeof(node));
h->pre=NULL;
e=h;
e->next=s;
s->pre=e;
while(n--)
{
e=s;
scanf("%d",&s->data);
s=(node *)malloc(sizeof(node));
e->next=s;
s->pre=e;
}
s->next=NULL;
end=s;
return ;
}
void PrintList()// The output list
{
node *s;
s=h->next;
printf(" Linked list data:        ");
while(s!=end)
{
printf("%d ",s->data);
s=s->next;
}
printf("\n");
return ;
}
void DeletList()// Deletes an element from a linked list
{
int x;
int flag;
node *s,*e;
printf(" Please enter the element to be deleted:    ");
scanf("%d",&x);
s=h->next;
e=h;
flag=0;
while(s!=end)
{
if(s->data==x)
{
e->next=s->next;
s->next->pre=e;
free(s);
flag=1;
break;
}
e=s;
s=e->next;
}
if(!flag)
printf(" There is no value of %d The element. \n",x);// If it's not in the list x , output this sentence.
return ;
}
void InsetList()// Insert an element into an ordered list
{
int x;
node *s,*e;
printf(" Input the element to be inserted:   ");
scanf("%d",&x);
s=h->next;
while(1)
{
if(s->data>=x)
{
e=(node *)malloc(sizeof(node));
e->data=x;
e->next=s;
e->pre=s->pre;
s->pre->next=e;
s->pre=e;
break;
}
else if(s==end)// will x Put it at the end of the list
{
end=(node *)malloc(sizeof(node));
s->data=x;
end->pre=s;
end->next=NULL;
s->next=end;
break;
}
s=s->next;
}
return ;
}
void JudgeList()// Determine if the bidirectional list is symmetric
{
node *s,*e;
int flag=0;
s=h->next;
e=end->pre;
while(s->data==e->data&&s!=end&&e!=h)
{
s=s->next;
e=e->pre;
}
if(s==end&&e==h)
printf(" The list is symmetric. \n");
else
printf(" Lists are asymmetric. \n");
return ;
}
void SortList()// Place the odd Numbers in the list after the even Numbers
{
node *s;
node *odd;
int temp;
odd=h->next;
s=h->next;
while(s!=end)
{
if(s->data%2!=0)
{
temp=odd->data;
odd->data=s->data;
s->data=temp;
odd=odd->next;
s=s->next;
}
else
s=s->next;
}
return ;
}
int PrintMenu()// Printed catalogue
{
int T;
printf("****************** directory ******************\n");
printf(" create 1 Two two-way linked lists:            1\n");
printf(" Output linked list:                2\n");
printf(" Delete the specified element in the linked list:          3\n");
printf(" Insert elements into the list:            4\n");
printf(" Determine whether the linked list is symmetric:            5\n");
printf(" Arrange the linked list:                6\n");
printf(" End of operation:                0\n");
printf(" Input operation instruction:      ");
scanf("%d",&T);
switch(T)
{
case 1:CreatList();break;
case 2:PrintList();break;
case 3:DeletList();break;
case 4:InsetList();break;
case 5:JudgeList();break;
case 6:SortList();break;
case 0:return 1;
default:printf(" Typing error. Please enter again. \n");
}
return 0;
}
int main()
{
int flag;
while(1)
{