How to realize two-way linked list in C language
This article introduces the knowledge of "how to realize two-way linked list in C language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Basic operation of two-way linked list
1. Establish a two-way linked list by tail interpolation.
2. Traverse the two-way linked list.
3. Delete a specified element from the two-way linked list.
4. Implement the ordered algorithm of inserting element e in the non-decreasing ordered two-way linked list.
5. Determine whether the elements in the two-way linked list are symmetrical or return 0 if the symmetry returns 1.
6. Let the element be a positive integer, and realize the algorithm to arrange all odd numbers before even numbers.
7. Design a simple menu in the main function to debug the above algorithm.
Example code:
/ / when sorting, I didn't sort because I didn't say whether odd and even numbers need to be reordered respectively. I just put odd numbers after even numbers. / / when creating a linked list, because this experiment does not require the length of the output linked list, I entered a linked list of length n. # include#includestruct node {int data; node * pre,*next;} * CreatList () / / create a two-way linked list {int n; node * s node; 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; esigh; e-> next=s; s-> pre=e While (nmurf -) {ethers; scanf ("% d", & s-> data); s = (node *) malloc (sizeof (node)); e-> next=s; s-> pre=e;} s-> next=NULL; end=s; return;} void PrintList () / / output linked list {node * s; slich-> next; printf ("linked list data:"); while (linked end) {printf ("% d", s-> data); swatches-> next;} printf ("\ n"); } void DeletList () / / Delete an element in the linked list {int x; int flag; node * s published; printf ("Please enter the element to be deleted:"); scanf ("% d", & x); next;-> next;; flag=0; while (sliced end) {if (s-> data==x) {e-> next=s- > next; s-> next- > pre=e; free (s); flag=1; break;} ecodes; swatches-> next } if (! flag) printf ("there is no element with value d in the linked list. \ n ", x); / / if there is no x in the linked list, output this sentence. Return;} void InsetList () / / insert an element in the ordered list {int x; node * s quotation; printf ("enter the elements to be inserted:"); scanf ("% d", & x); slighth-> 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) / / put x at the end of the linked list {end= (node *) malloc (sizeof (node)); s-> data=x; end- > pre=s; end- > next=NULL; s-> next=end; break;} swatches-> next;} return;} void JudgeList () / / to determine whether the two-way linked list is symmetrical {node * sdome; int flag=0; swatches-> next; estranged-> pre; while (s-> data==e- > dataholdsendbearable lists) {swatches-> next; elists-> pre } if (s==end&&e==h) printf ("linked list symmetry. \ n "); else printf (" linked list asymmetry. \ n "); return;} void SortList () / / put the odd number in the linked list after the even number {node * s; node * odd; int temp; odd=h- > next; slots-> next; while (sprints end) {if (s-> data% 2 numbers 0) {temp=odd- > data; odd- > data=s- > data; s-> data=temp; odd=odd- > next; slots-> next;} else slots-> next;} return;} int PrintMenu () / print directory {int T Printf ("* directory *\ n"); printf ("create a bidirectional linked list: 1\ n"); printf ("output linked list: 2\ n"); printf ("remove specified elements in the linked list: 3\ n") Printf ("insert elements into the linked list: 4\ n"); printf ("determine whether the linked list is symmetrical: 5\ n"); printf ("arrange the linked list: 6\ n"); printf ("Operation end: 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 ("input error. Please re-enter. \ n ");} return 0;} int main () {int flag; while (1) {flag=PrintMenu (); if (flag) / / Pop out of the break;} printf through the flag control loop (" Thank you for using! \ n "); return 0;}" how to implement two-way linked lists in C language "ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!