Basic linked list operations in C Language
The importance of linked lists in data structures and algorithms is self-evident. Here we use C to implement basic operations in the Linked List (single-chain table. For the basic concepts of the linked list, refer to the "linked list of data structures and algorithms" blog.
(1) define the node type of a single-chain table
Typedef int elemType; // defines the Node type of the single-chain table. typedef struct ListNode {elemType element; // data field struct ListNode * next; // address field} Node;
(2) initialize a linear table
// 1. initialize a linear table, that is, set the header pointer of a single-chain table to void initList (Node * pNode) {pNode = NULL; printf ("% s function execution, initialization successful \ n ", __function __);}
After declaring a head node, set the header node to null. That is, set the data and address fields to null to initialize the linked list.
(3) create a linear table
// 2. create a linear table. The input negative value of this function stops reading data Node * creatList (Node * pHead) {Node * p1; // the header Node, always pointing to the header Node * p2; // End Node of the table, always pointing to the last element of the linked list p1 = p2 = (Node *) malloc (sizeof (Node); // apply for a new Node, allocate space if (p1 = NULL | p2 = NULL) {printf ("memory allocation failed \ n"); exit (0);} memset (p1, 0, sizeof (Node); scanf ("% d", & p1-> element); // enter the value of the new Node p1-> next = NULL; // The pointer of the new node is set to null while (p1-> element> 0) {// If the input value is greater than 0, until the input value is negative if (pHead = NULL) {// empty table, access the header pHead = p1; // directly use p1 as the header node, it can also be understood that the pHead header node points to p1} else {p2-> next = p1; // non-empty table, connected to the end of the table} p2 = p1; // After p1 is inserted, p1 is the end Node, so p2 points to the End Node p1 = (Node *) malloc (sizeof (Node )); // re-apply for a node if (p1 = NULL | p2 = NULL) {printf ("memory allocation failed \ n"); exit (0 );} memset (p1, 0, sizeof (Node); scanf ("% d", & p1-> element); p1-> next = NULL ;} printf ("% s FUNCTION execution, linked list created successfully \ n" ,__ FUNCTION _); return pHead; // return the head pointer of the linked list}
Here, I enter an element manually until the input is 0 or negative.
(4) print the linked list
// 3. print the linked list. Traverse void printList (Node * pHead) {if (NULL = pHead) {// The linked list is empty printf ("% s function execution, the linked list is empty \ n ",__ FUNCTION _);} else {while (NULL! = PHead) {printf ("% d", pHead-> element); pHead = pHead-> next;} printf ("\ n ");}}
Use the address field to print the address field in sequence.
(5) Clear the linked list
// 4. clear all elements in a linear table L, that is, release all nodes in a single-chain table L to make it an empty table void clearList (Node * pHead) {Node * pNext; // define a node adjacent to pHead, which is interpreted as the next node of the current node if (pHead = NULL) {printf ("% s function execution, the linked list is empty \ n ", __function _);} while (pHead-> next! = NULL) {pNext = pHead-> next; // Save the pointer of the next node free (pHead); // release the current node pHead = pNext; // point to the next node} printf ("% s FUNCTION execution, the linked list has been cleared \ n" ,__ FUNCTION __);}
If you want to check whether the data has been cleared successfully, you can use the chain table print check in (4.
(6) Calculate the length of the linked list
// 5. Return the length of the single-chain table int sizeList (Node * pHead) {int size = 0; while (pHead! = NULL) {size ++; pHead = pHead-> next;} printf ("% s FUNCTION execution, linked list length % d \ n" ,__ FUNCTION __, size ); return size; // the actual length of the linked list}
That is, to calculate the number of nodes.
(7) judge whether the linked list is empty
// 6. check whether the single linked list is empty. if it is empty, 1 is returned; otherwise, 0int isEmptyList (Node * pHead) {if (pHead = NULL) {printf ("% s function execution, the linked list is empty \ n ",__ FUNCTION _); return 1;} printf (" % s FUNCTION execution, the linked list is not empty \ n ",__ FUNCTION __); return 0 ;}
(8) Searching for elements at a certain position in a linked list
// 7. returns the element of the pos Node in the single-chain table. if the pos is out of the range, the program stops running void getElement (Node * pHead, int pos) {int I = 0; if (pos <1) {printf ("% s FUNCTION execution, invalid pos value \ n" ,__ FUNCTION _);} if (pHead = NULL) {printf ("% s FUNCTION execution, the linked list is empty \ n ",__ FUNCTION _);} while (pHead! = NULL) {I ++; if (I = pos) {break;} pHead = pHead-> next; // move to the next node} if (I <pos) {// The pos value exceeds the length of the linked list printf ("% s FUNCTION execution, and the pos value exceeds the length of the linked list \ n" ,__ FUNCTION _);} printf ("% s FUNCTION execution, the element in LOCATION % d is % d \ n ",__ FUNCTION __, pos, pHead-> element );}
(9) returns the memory address of an element in the linked list.
// 8. find the first element with the given value x from the single-chain table. If the query is successful, the storage address of the data domain of the Node is returned. Otherwise, the NULLelemType * getElemAddr (Node * pHead, elemType x) is returned) {if (NULL = pHead) {printf ("% s FUNCTION execution, linked list is empty \ n" ,__ FUNCTION _); return NULL ;} while (pHead-> element! = X) & (NULL! = PHead-> next) {// determines whether it is at the end of the linked list and whether the element to be searched exists. pHead = pHead-> next;} if (pHead-> element! = X) & (pHead! = NULL) {// when the last node printf ("% s FUNCTION execution arrives, the x value \ n" ,__ FUNCTION _) is not found in the linked list; return NULL ;} if (pHead-> element = x) {printf ("% s FUNCTION execution, element % d address: 0x % x \ n" ,__ FUNCTION __, x, & (pHead-> element);} return & (pHead-> element); // return element address}
(10) modify the value of a node
// 9. modify the value of the pos Node in the single-chain table to the value of x. If the modification is successful, 1 is returned; otherwise, 0 int modifyElem (Node * pNode, int pos, elemType x) is returned) {int I = 0; if (NULL = pNode) {printf ("% s FUNCTION execution, empty linked list \ n" ,__ FUNCTION _); return 0 ;} if (pos <1) {printf ("% s FUNCTION execution, invalid pos value \ n" ,__ FUNCTION _); return 0 ;}while (pNode! = NULL) {I ++; if (I = pos) {break;} pNode = pNode-> next; // move to next node} if (I <pos) {// The pos value is greater than the length of the linked list printf ("% s FUNCTION execution, the pos value exceeds the length of the linked list \ n" ,__ FUNCTION _); return 0 ;} pNode-> element = x; printf ("% s FUNCTION execution \ n" ,__ FUNCTION _); return 1 ;}
(11) insert a node into the header (header node)
// 10. insert an element int insertHeadList (Node ** pNode, elemType insertElem) {Node * pInsert; pInsert = (Node *) malloc (sizeof (Node) to the header of a single-chain table )); memset (pInsert, 0, sizeof (Node); pInsert-> element = insertElem; pInsert-> next = * pNode; * pNode = pInsert; // header node * pNode points to the newly inserted node. Note the order before and after the previous line of code. printf ("% s function execution, element inserted to the header \ n ", __function _); return 1 ;}
(12) insert a node at the end of the table
// 11. add an element int insertLastList (Node ** pNode, elemType insertElem) {Node * pInsert; Node * pHead; pHead = * pNode; pInsert = (Node *) to the end of a single-chain table *) malloc (sizeof (Node); // apply for a new Node memset (pInsert, 0, sizeof (Node); pInsert-> element = insertElem; while (pHead-> next! = NULL) {pHead = pHead-> next;} pHead-> next = pInsert; // point the next node of the node at the end of the linked list to the newly added node printf ("% s FUNCTION execution, successfully inserting the element to the end of the table \ n" ,__ FUNCTION __); return 1 ;}
(13) test functions
Int main (int argc, const char * argv []) {Node * pList; // declare the header Node initList (pList); // The chain table initializes printList (pList ); // print the chain table pList = creatList (pList); // create the chain table printList (pList); sizeList (pList); // The length of the chain table printList (pList ); isEmptyList (pList); // judge whether the linked list is an empty linked list getElement (pList, 3); // obtain the third element. If there are less than three elements, returns 0 printList (pList); getElemAddr (pList, 5); // obtains the memory address modifyElem (pList,) of element 5 ); // change the element on Position 4 in the linked list to 1 printList (pList); insertHeadList (& pList, 5); // Insert the element 5 printList (pList) into the header ); insertLastList (& pList, 10); // insert element 10 at the end of the table printList (pList); clearList (pList); // clear the chain table printList (pList); return 0 ;}