Basic operations on a single-chain table (Leading node) in C Language

Source: Internet
Author: User

Basic operations on a single-chain table (Leading node) in C Language

In my previous blog "basic operations for implementing a single-link table (with no leading node) in C Language", I have implemented 11 types of operations for a single-link table with no leading node: such as calculating the length of a linked list, initializing, creating a linked list, and clearing a linked list. However, in actual use, a single-chain table with the leading node is often used more than a single-chain table with no leading node, which is also more convenient to use. Because you do not need to consider the first node separately, the first node and other subsequent nodes are processed in the same way, simplifying the operation. This blog will perform 11 operations on a single-chain table with the leading node.

(1) define the node type of the single-chain table of the leading Node

typedef int elemType;typedef struct NodeList{    int element;    struct NodeList *next;}Node;

(2) initialize the single-chain table of the leading node [This is critical]
// 1. initialize the void InitialList (Node ** pNode) in the single-chain table of the leading Node {// You are advised to determine whether the allocation is successful every time the memory space is allocated by malloc, that is, to determine whether it is empty; // The pNode is a head Node at this time; // After the initialization is successful, it is actually a normal linked list; * pNode = (Node *) malloc (sizeof (Node); if (* pNode = NULL) {printf ("% s function execution, memory allocation failed, failed to initialize a single-chain table ", __function _);} else {(* pNode)-> next = NULL; printf ("% s FUNCTION execution, leading node single-chain table initialization completed \ n ", __function __);}}

(3) create a single-chain table with the lead node by using the end plug method

 

// 2. create a single-chain table void CreateList (Node * pNode) {/*** even if the number entered at the beginning is less than or equal to 0, the single-chain table of the leading Node will be created successfully, the single-chain table is empty, that is, there are no other nodes except the header node. */Node * pInsert; Node * pMove; pInsert = (Node *) malloc (sizeof (Node); // check whether the allocated memory is successfully pInsert = NULL? Memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; scanf ("% d", & (pInsert-> element); pMove = pNode; while (pInsert-> element> 0) {pMove-> next = pInsert; pMove = pInsert; pInsert = (Node *) malloc (sizeof (Node )); // check whether the allocated memory is successfully pInsert = NULL? Memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; scanf ("% d", & (pInsert-> element ));} printf ("% s FUNCTION execution, lead node single-chain table created successfully \ n" ,__ FUNCTION __);}

(4) print the single-chain table of the leading Node

// 3. print the void PrintList (Node * pNode) {/*** of the single-chain table of the leading Node. Note that if the single-chain table is empty (only one header Node exists ), I also asked it to print (print successfully ). There is no element in it, and it is empty, so there is an empty line in the console; */Node * pMove; pMove = pNode-> next; while (pMove! = NULL) {printf ("% d", pMove-> element); pMove = pMove-> next;} printf ("\ n % s function execution, print the single-chain table of the lead node \ n ",__ FUNCTION __);}

(5) Clear the linked list
// 4. clear all elements in a linear table, that is, release all nodes (except the header Node) to make it an empty table void ClearList (Node * pNode) {Node * pMove; pMove = pNode-> next; while (pMove! = NULL) {pNode-> next = pMove-> next; free (pMove); pMove = pNode-> next;} printf ("% s function execution, successfully cleared the linked list of the leading node \ n ",__ FUNCTION __);}

(6) Calculate the length of the linked list
// 5. returns the length int SizeList (Node * pNode) {/*** of the single-chain table of the lead Node. When there is only one header Node, size = 0; the header node is not included in the length of the linked list. */Int I = 0; Node * pMove; pMove = pNode-> next; while (pMove! = NULL) {I ++; pMove = pMove-> next;} printf ("% s function execution, the length of the single-chain table of the leading node is: % d \ n ", __function __, I); return I ;}
(7) judge whether the linked list is empty
// 6. determines whether the single-chain table of the lead Node is empty. If it is null, 1 is returned. Otherwise, 0int IsEmptyList (Node * pNode) {/*** is returned. When there is only one header Node, this linked list is blank */if (pNode-> next = NULL) {printf ("% s function execution, leading node linked list is blank \ n ", __function _); return 1;} printf ("% s FUNCTION execution, leading node linked list non-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-1 is returned, int GetElement (Node * pNode, int pos) {int I = 1; Node * pMove; pMove = pNode-> next; while (pMove! = NULL) {if (I = pos) {printf ("% s FUNCTION execution, pos = % d position value is % d \ n" ,__ FUNCTION __, pos, pMove-> element); return pMove-> element;} I ++; pMove = pMove-> next;} printf ("% s function execution, the value \ n ",__ FUNCTION __, pos); return-1;} is not found at position pos = % d ;}

(9) return 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 * pNode, int x) is returned) {Node * pMove; pMove = pNode-> next; while (pMove! = NULL) {if (pMove-> element = x) {printf ("% s function execution, find that the memory address of x = % d is: 0x % x \ n ",__ FUNCTION __, x, & (pMove-> element); return & (pMove-> element);} pMove = pMove-> next ;} printf ("% s FUNCTION execution, the x = % d value is not found in the single-link table of the lead node, and the memory address \ n" ,__ FUNCTION __, x cannot be obtained ); return NULL ;}

(10) modify the value of a node
// 9. change the value of the pos Node in the single-chain table to the value of x. Node * ModifyElem (Node * pNode, int pos, int x) {int I = 1; Node * pMove; pMove = pNode-> next; while (pMove! = NULL) {if (I = pos) {pMove-> element = x; printf ("% s function execution, change pos = % d to % d success \ n ",__ FUNCTION __, pos, x); return pNode;} I ++; pMove = pMove-> next;} printf ("% s FUNCTION execution. The linked list is empty or the input pos value is invalid. Failed to modify the value \ n" ,__ FUNCTION __); return pNode ;}

(11) insert a node into the header
// 10. insert an element Node * InsertHeadList (Node * pNode, int x) {Node * pInsert; pInsert = (Node *) malloc (sizeof (Node) to the header of a single-chain table )); memset (pInsert, 0, sizeof (Node); pInsert-> element = x; pInsert-> next = pNode-> next; pNode-> next = pInsert; printf ("% s FUNCTION execution, element % d inserted in the header \ n" ,__ FUNCTION __, x); return pNode ;}

(12) insert a node at the end of the table
// 11. add an element Node * InsertTailList (Node * pNode, int x) {Node * pMove; Node * pInsert; pInsert = (Node *) to the end of a single-chain table *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node); pInsert-> element = x; pInsert-> next = NULL; pMove = pNode; while (pMove-> next! = NULL) {pMove = pMove-> next;} pMove-> next = pInsert; printf ("% s function execution, element % d inserted at the end of the table \ n ", __function __, x); return pNode ;}

(13) test functions

 

 

int main(int argc, const char * argv[]) {    Node *pList;    InitialList(&pList);    CreateList(pList);    PrintList(pList);    SizeList(pList);    IsEmptyList(pList);    GetElement(pList, 3);    GetElemAddr(pList, 5);    ModifyElem(pList, 2, 111);    PrintList(pList);    InsertHeadList(pList,1234);    PrintList(pList);    SizeList(pList);    InsertTailList(pList,9999);    PrintList(pList);    SizeList(pList);    ClearList(pList);    PrintList(pList);    return 0;}

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.