C-language single-link list

Source: Internet
Author: User

learned that the sequential table in the linear table knows that the data in the sequential tables is adjacent to the physical memory, so when we want to access the next element in the sequential table, we can go directly to it, just like an array. But the single-linked list is different, the location of the data storage of the single-linked list is dynamically allocated, that is, the storage of the single-linked list is not adjacent to the physical memory, so we can only use the pointer this way to string up the single-linked list, through the pointer to access the next node.

Single-Link Table nodes

The nodes of a single-linked list have data fields and pointer fields, which are used to store data, and the pointer field is the domain used to store direct successor nodes.
typedef struct NODE{INT data;//data domain struct Node *next;//pointer field}linklist,*link;
Create a single linked list here I create a single-linked list of head nodes, which do not store the data you enter, but can store some data such as the length of the table, while the next pointer of the head node points to the next first node, which is the first node to start storing the data, and the single-linked list structure such as:
When we create the head node, we use a pointer to point to the head node, which is the head pointer, which is very important for the whole list, because every time we want to traverse the data in the linked list, we have to find the head pointer before we can find all the data one by one. So we can't lose the head pointer of the single-linked list. There is also the last node of the list of pointers to point to null, can also point to the head node, point to the head node after the linked list is also a circular list, when we put the last node pointer to NULL, we can judge the null value is not the last node. Create the code for the linked list:
Create a linked list void creastelist (link h) {//here h is a struct pointer, and when I declare the struct, link is the * number, so here is link Link*link p,newnode;//p is a pointer to the head node, Because I also said above the head pointer cannot move, so we create a copy of the head pointer P, through p to traverse all the nodes int i = 1,s;//s is the data we entered, I is the length of the linked list P = h;//will point p to the head node, that is, create a copy of the head pointer printf (" Please enter%d elements (-1 end): ", I); scanf ("%d ", &s), while (s!=-1) {//input-1 o'clock end input newnode = (Link) malloc (sizeof (linklist));// Allocate space for the new node Newnode->data = s;//The input s into the new node NewNode p->next = newnode;//The pointer of p at this point points to the newly created node P = newnode;// This is to point p from the previous node to the newly created node, which means that the last node of the linked list i++;//the length of the linked list by 1printf ("Please enter%d elements (-1 end):", I); scanf ("%d", &s);} Listlength = i;//The length of the list p->next = NULL; After the last end of the input p points to the last node, pointing to the last node next to null}.
Print single-linked list print single-linked list all we need to do is to have the copy of the head pointer pointing to the next node, traversing the entire list, and the end condition is that the next of the tail node is null.

Print List void printlist (link H) {Link p=h;//p points to the head node while (p->next) {//To determine the end of the last node is null when ending P = p->next;// Constantly pointing to the single-linked list next node printf ("%d", P->data);} printf ("\ n");}
Single-linked list delete operation to delete an element in a single-linked list, you don't need to move the trailing data of the deleted position forward one position in the same way as the sequential table, just change the pointer point.

Therefore, the time complexity of the delete operation of the single-linked list is much lower than the sequential table. The code is as follows:
Delete the linked list void deletelist (link h,int i) {//h as the head pointer of the linked list, I for the position you want to remove link P = H; Link delep;//the pointer to be removed int j = 0;//j is a tag variable, indicating that this is the first node if (i<=0| | i>listlength) printf ("The location you want to delete does not exist!") \///When the value of I is less than 0 or greater than the linked list length, the deleted node does not exist when Else{while (p->next&&j<i-1) {//j<i-1 That is, when P points to the first node of the I node that needs to be deleted, the end loop P = p->next;//continuously points to the next node, j + +;} Delep = p->next;//The p at this point points to the previous node of the node that needs to be deleted, Delep points to the node that needs to be deleted P->next = delep->next;//points The next point of P to the node of the Delep next, For example, point 2 of the next pointer to 4free (DELEP);p rintf ("Delete succeeded! \ n "); listlength--;//list length minus 1} after successful deletion
Single-linked list insert operation the same single-linked list insert operation is much lower than the time complexity of the sequential table, and the single-linked list does not need to move all the elements back when it is inserted, just to change the pointer's direction.
The code is as follows:
Insert list void insertlist (link h,int i) {//i for insertion position link P = h;//p point to head node int j=0;if (i<=0| | i>listlength+1) printf ("You have inserted the wrong position!") ");//When the insertion position is less than = 0 and greater than the linked list length plus 1 o'clock, the insertion is incorrect, the list length plus 1 is because the link can be added to the tail of the list else{link cnew = (link) malloc (sizeof (linklist)); Allocate space for the nodes that need to be inserted printf ("Please enter data you want to insert: \ n"); scanf ("%d",& (Cnew->data)); while (p->next&&j<i-1) {//the same j<i-1 execution ends when p points to the previous node where the insertion is required p = p->next;j++;} Cnew->next = p->next;//The new node's next point to P->next, i.e. point p next to 4p->next = cnew;//and then point P's next to the new node printf ("Insert succeeded!"). "); listlength++;//Insert after successful link list length plus 1}}
A single-linked list query implements a query that simply pairs the data of the query with each data in the linked list, returning the data location at the time of the pairing success.
Query element int findlist (link h,int s) {//s for query data Link P = H;int i=0;while (p->next&&p->data!=s) {// The end of the decision for the next null and the data pairing is successful, when the pairing is successful, the P data at this time is paired with S to successfully node i++; P = P->next;} if (i>listlength| | P->data!=s) return 0;//If the value of I is greater than the length of the list or p->data is not s at this time, then there is no node else in the linked list that has a successful pairing with S, and the return i;//pairing successfully returns the location of the node at this point}

All code
#include <stdio.h> #include <malloc.h>int listlength = 0;//list length typedef struct NODE{INT data;struct Node *next ;} Linklist,*link;void creastelist (link H);//create linked list void Insertlist (link h,int i);//Insert list, I is insertion position void deletelist (link h,int i) ;//delete, I for the deletion of the position int findlist (link h,int s);//query, I for the query data, query the linked list whether there is this data void printlist (link H);//Print List int getlistlength (link H);//Get the list length void Sortinsert (link h,int s);//Insert int main () {link H; h = (Link) malloc (sizeof (linklist));//Generate Head node creastelist (h); Printlist (H); Sortlist (H); Printlist (H); return 0;} Create a linked list void creastelist (link H) {link P,newnode;int i = 1,s; P = h;printf ("Please enter%d elements (-1 end):", I); scanf ("%d", &s), while (s!=-1) {NewNode = (Link) malloc (sizeof (linklist)); Newnode->data = s; P->next = NewNode; P = newnode;i++;p rintf ("Please enter%d elements (-1 end):", I); scanf ("%d", &s);} Listlength = i; P->next = NULL; }//Print List void printlist (link H) {link p=h;while (p->next) {P = p->next;printf ("%d", P->data);} printf ("\ n");} Get the list length int getlistlength (link H) {Link P = H;intLength = 0;while (p->next) {length++; P=p->next;} return length;} Delete list void DeleteList (link h,int i) {link P = H; Link Delep;int j = 0;if (i<=0| | i>listlength) printf ("The location you want to delete does not exist!") \ n "); Else{while (p->next&&j<i-1) {P = p->next;j++;} Delep = p->next; P->next = Delep->next;free (delep);p rintf ("Delete succeeded! \ n "); listlength--;}} Orderly insert void Sorteninsert (link h,int s) {link P = H; Link NewNode = (link) malloc (sizeof (linklist)); newnode->data = S;while (p->next&&p->next->data <s) {P = P->next;} if (p->next==null) {p->next = Newnode;newnode->next = NULL;} Else{newnode->next = p->next; P->next = NewNode; }listlength++;} Insert list void insertlist (link h,int i) {link P = H;int j=0;if (i<=0| | i>listlength+1) printf ("You have inserted the wrong position!") Else{link cnew = (Link) malloc (sizeof (linklist));p rintf ("Please enter the data you want to insert: \ n"); scanf ("%d",& (Cnew->data)); while (p->next&&j<i-1) {P = p->next;j++;} Cnew->next = p->next; P->next = cnew;printf ("Insert succeeded! "); Listlength++;}} Query element int findlist (link h,int s) {link P = h;int i=0;while (p->next&&p->data!=s) {i++; P = P->next;} if (i>listlength| | P->data!=s) return 0;else return i;}




C-language single-link list

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.