Understanding of linked list operations and data structures

Source: Internet
Author: User

Node operations because the linked list is n discrete nodes connected to each other through pointers, the operations related to the linked list are mainly achieved through the header pointer (the address of the header node is stored) operations on the node. 1. How to insert the node pointed to by q to the end of the node pointed to by p? There are two methods: Use the Temporary Variable r = p-> pNext; // use r to save the next node address p points to the node p-> pNext = q; // at this time, the pointer field of p points to the address of the node referred to by q-> pNext = r; Second: Do not use the temporary variable q-pNext = p-> pNext; // Let the pointer fields of p and q point to the same node p-pNext = q; // Let the pointer fields of p point to the q Node 2. how to delete a node (idea: Use a temporary variable r to save the node to be deleted, and then delete the node after p) r = p-> pNext; p-> pNext = p-> pNext; free (r); r = NULL; 3. how to determine whether the linked list is empty (idea: the empty linked list has only one header node, so you only need to set the pointer field of the header node to NULL) related operation examples of the linked list:

# Include <stdio. h> # include <malloc. h> # include <stdlib. h> typedef struct Node {int data; // data field struct Node * pNext; // pointer field} NODE, * PNODE; PNODE CreateList (); // create the linked table void TraverseList (PNODE); // The traversal chain table bool IsEmpty (PNODE); // determine whether the linked list is empty int Length (PNODE ); // calculate the length of the linked list bool InsertNode (PNODE, int, int); // Insert the node bool DeleteNode (PNODE, int, int *); // Delete the node bool QueryNode (PNODE, int); // find the node bool ModifyNode (PNODE, int, int); // modify the node void SortList (PNODE); // sort int main () {PNODE pHead = NULL; pHead = CreateList (); // assign the address of the header pointer for creating the linked list to pHead if (0 = Length (pHead) {printf ("no valid node for the linked list, and exit the program! \ N "); exit (-1);} TraverseList (pHead); if (InsertNode (pHead, 2,100) {printf (" inserted "); traverseList (pHead);} else {printf ("insertion failed! \ N ");} SortList (pHead); printf (" sorted "); TraverseList (pHead); return 0;} PNODE CreateList () {int len; // used to save the number of nodes that need to create the linked list int I; int val; // used to temporarily store the value PNODE pHead = (PNODE) malloc (sizeof (NODE) of the new NODE )); // dynamically allocate a piece of memory, which saves the address of the header node and assigns it to pHead PNODE pTail = pHead; PNODE pNew; pTail-> pNext = NULL; // assign the pointer field of the End Node to NULL if (NULL = pHead) {printf ("the allocation fails and the program stops running! \ N "); exit (-1);} printf (" Enter the number of nodes to create the linked list: len = "); scanf (" % d ", & len); for (I = 0; I <len; I ++) {printf ("Enter the value of node % d:", I + 1 ); scanf ("% d", & val); pNew = (PNODE) malloc (sizeof (NODE); // create a new temporary NODE if (NULL = pNew) {printf ("An error occurred while allocating. The program stops running! \ N "); exit (-1);} pNew-> data = val; pTail-> pNext = pNew; // mount the new node to pNew-> pNext = NULL; // assign the pointer field of the new node to NULL pTail = pNew; // set the new node to the End Node} return pHead;} void TraverseList (PNODE pHead) {PNODE p = pHead-> pNext; // point the pointer field of the header node to the first node (the first valid node of the linked list) and assign it to the pointer Variable p, p points to the address printf ("traverse the entire linked list:"); while (NULL! = P) // when p points to the first node, the address is not NULL (that is, the linked list is not empty ), enter the value of each node in a loop {// when p points to the address of the end node, the value of the end node can be output // but the pointer field of the End Node is NULL at this time, the while LOOP printf ("% d", p-> data); p = p-> pNext; // assign the address of p pointing to the next node to p pointer} printf ("\ n");} bool IsEmpty (PNODE pHead) {if (NULL = pHead-> pNext) return true; else return false;} int Length (PNODE pHead) {PNODE p = pHead-> pNext; // p points to the first node of the linked list (the first valid node) int len = 0; while (NULL! = P) {++ len; p = p-> pNext;} return len;} bool InsertNode (PNODE pHead, int pos, int val) {int I = 0; PNODE p = pHead, q; PNODE pNew; while (NULL! = P & I <pos-1) // aims to point p to the previous node {p = p-> pNext; ++ I; // If the linked list has only two valid nodes and the position point pos to be inserted is 3, I = 2, 2 <3-1 is not true, and the loop ends ,} if (I> pos-1 | NULL = p) // if p is NULL, the previous node of the location node to be inserted does not exist, i> the pos-1 is to prevent the user from entering pos as illegal data such as 0,-1 return false; pNew = (PNODE) malloc (sizeof (NODE )); // allocate memory for the new node if (NULL = pNew) {printf ("dynamic memory allocation failed, Program terminated! \ N "); exit (-1) ;}pnew-> data = val; q = p-> pNext; p-> pNext = pNew; pNew-> pNext = q; return true;} bool DeleteNode (PNODE pHead, int pos, int * pVal) {int I = 0; PNODE p = pHead, q; while (NULL! = P-> pNext & I <pos-1) {p = p-> pNext; + + I;} if (I> pos-1 | NULL = p) return false; q = p-> pNext; * pVal = q-> data; p-> pNext = p-> pNext; free (q); q = NULL; return true;} void SortList (PNODE pHead) {int I, j, t; int len = Length (pHead); // use len to save the Length of PNODE p, q; for (I = 0, p = pHead-> pNext; I <len-1; p = p-> pNext, I ++) // compare the number of rows {for (j = I + 1, q = p-> pNext; j <len; q = q-> pNext, j ++) // compare the logarithm {if (p-> data> q-> data) {t = p-> data; p-> data = q-> da Ta; q-> data = t ;}}} bool QueryNode (PNODE pHead, int pos) {int I = 0; PNODE p = pHead, q; while (NULL! = P-> pNext & I <pos-1) {p = p-> pNext; + + I;} if (I> pos-1 | NULL = p) return false; q = p-> pNext; printf ("the value of the node at LOCATION % d is % d \ n", pos, q-> data); return true ;} bool ModifyNode (PNODE pHead, int pos, int val) {int I = 0; PNODE p = pHead, q; while (NULL! = P-> pNext & I <pos-1) {p = p-> pNext; + + I;} if (I> pos-1 | NULL = p) return false; q = p-> pNext; q-> data = val; printf ("change the node value at LOCATION % d to % d \ n", pos, q-> data); return true ;}# include <stdio. h> # include <malloc. h> # include <stdlib. h> typedef struct Node {int data; // data field struct Node * pNext; // pointer field} NODE, * PNODE; PNODE CreateList (); // create the linked table void TraverseList (PNODE); // The traversal chain table bool IsEmpty (PNODE); // determine whether the linked list is empty int Length (PNODE); // determine the Length of the linked list bool In SertNode (PNODE, int, int); // insert node bool DeleteNode (PNODE, int, int *); // delete node bool QueryNode (PNODE, int ); // find the node bool ModifyNode (PNODE, int, int); // modify the node void SortList (PNODE); // sort int main () {PNODE pHead = NULL; pHead = CreateList (); // assign the header pointer address of the created linked list to pHeadif (0 = Length (pHead) {printf ("the linked list does not have a valid node. Exit the program! \ N "); exit (-1);} TraverseList (pHead); if (InsertNode (pHead, 2,100) {printf (" inserted "); traverseList (pHead);} else {printf ("insertion failed! \ N ");} SortList (pHead); printf (" sorted "); TraverseList (pHead); return 0;} PNODE CreateList () {int len; // used to save the number of nodes that need to create the linked list int I; int val; // used to temporarily store the value PNODE pHead = (PNODE) malloc (sizeof (NODE) of the new NODE )); // dynamically allocate a piece of memory, which saves the address of the header node and assigns it to pHeadPNODE pTail = pHead; PNODE pNew; pTail-> pNext = NULL; // assign the pointer field of the End Node to NULLif (NULL = pHead) {printf ("the allocation fails and the program stops running! \ N "); exit (-1);} printf (" Enter the number of nodes to create the linked list: len = "); scanf (" % d ", & len); for (I = 0; I <len; I ++) {printf ("Enter the value of node % d:", I + 1 ); scanf ("% d", & val); pNew = (PNODE) malloc (sizeof (NODE); // create a new temporary NODE if (NULL = pNew) {printf ("An error occurred while allocating. The program stops running! \ N "); exit (-1);} pNew-> data = val; pTail-> pNext = pNew; // mount the new node to pNew-> pNext = NULL; // assign the pointer field of the new node to NULLpTail = pNew; // set the new node to the End Node} return pHead;} void TraverseList (PNODE pHead) {PNODE p = pHead-> pNext; // point the pointer field of the header node to the first node (the first valid node of the linked list) and assign it to the pointer Variable p, p points to the address printf ("traverse the entire linked list:"); while (NULL! = P) // when p points to the first node, the address is not NULL (that is, the linked list is not empty ), enter the value of each node in a loop {// when p points to the address of the end node, the value of the end node can be output // but the pointer field of the End Node is NULL at this time, the while LOOP printf ("% d", p-> data); p = p-> pNext; // assign the address of p pointing to the next node to p pointer} printf ("\ n");} bool IsEmpty (PNODE pHead) {if (NULL = pHead-> pNext) return true; elsereturn false;} int Length (PNODE pHead) {PNODE p = pHead-> pNext; // p points to the first node of the linked list (the first valid node) int len = 0; while (NULL! = P) {++ len; p = p-> pNext;} return len;} bool InsertNode (PNODE pHead, int pos, int val) {int I = 0; PNODE p = pHead, q; PNODE pNew; while (NULL! = P & I <pos-1) // aims to point p to the previous node {p = p-> pNext; ++ I; // If the linked list has only two valid nodes and the position point pos to be inserted is 3, I = 2, 2 <3-1 is not true, and the loop ends ,} if (I> pos-1 | NULL = p) // if p is NULL, the previous node of the location node to be inserted does not exist, i> the pos-1 is to prevent the user from entering pos as illegal data such as 0,-1 return false; pNew = (PNODE) malloc (sizeof (NODE )); // allocate memory for the new node if (NULL = pNew) {printf ("dynamic memory allocation failed, Program terminated! \ N "); exit (-1) ;}pnew-> data = val; q = p-> pNext; p-> pNext = pNew; pNew-> pNext = q; return true;} bool DeleteNode (PNODE pHead, int pos, int * pVal) {int I = 0; PNODE p = pHead, q; while (NULL! = P-> pNext & I <pos-1) {p = p-> pNext; + + I;} if (I> pos-1 | NULL = p) return false; q = p-> pNext; * pVal = q-> data; p-> pNext = p-> pNext; free (q); q = NULL; return true;} void SortList (PNODE pHead) {int I, j, t; int len = Length (pHead); // use len to save the Length of PNODE p, q; for (I = 0, p = pHead-> pNext; I <len-1; p = p-> pNext, I ++) // compare the number of rows {for (j = I + 1, q = p-> pNext; j <len; q = q-> pNext, j ++) // compare the logarithm {if (p-> data> q-> data) {t = p-> data; p-> data = q-> data; q-> data = t ;}}} bool Que RyNode (PNODE pHead, int pos) {int I = 0; PNODE p = pHead, q; while (NULL! = P-> pNext & I <pos-1) {p = p-> pNext; + + I;} if (I> pos-1 | NULL = p) return false; q = p-> pNext; printf ("the value of the node at LOCATION % d is % d \ n", pos, q-> data); return true ;} bool ModifyNode (PNODE pHead, int pos, int val) {int I = 0; PNODE p = pHead, q; while (NULL! = P-> pNext & I <pos-1) {p = p-> pNext; + + I;} if (I> pos-1 | NULL = p) return false; q = p-> pNext; q-> data = val; printf ("change the node value at LOCATION % d to % d \ n", pos, q-> data); return true ;}

 

Note: 1. In vc ++, the c compiler does not support bool functions, but the c ++ compiler does. So here I use the c ++ compiler. 2. common Errors During c Compilation: illegal use of this type as an expression is because the c language does not allow temporary definition of variables. All Defined variables must start with the function, this is also an important difference between c and c ++. a narrow understanding of data structures: data structures are specialized in data storage (including individual storage and individual relationship storage. Broadly speaking, the data structure includes both data storage and data operations. An algorithm is used to store data. Algorithms: in a narrow sense: algorithms and data storage methods are closely related. in a broad sense, algorithms and data storage methods are irrelevant (this is the idea of generics) Generic: the effects achieved by using a technology are different storage methods, and the operations performed are the same. Generics are mainly implemented through templates, Operator overloading, and pointers, which are often used in c ++. Supplement linear structure: continuous storage [array] advantages: fast access speed disadvantages: insertion and deletion of elements is slow, space is usually limited, you must know the length of the array in advance, in addition, large continuous memory block discrete storage [linked list] is required. Advantages: Fast insertion and deletion of elements with unlimited space; disadvantages: slow access

Related Article

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.