Addition and deletion of linked list nodes

Source: Internet
Author: User
Struct listnode {int m_nvalue; listnode * m_pnext;}; // Add node/* The first parameter is a pointer to the pointer. When we insert a node into an empty linked list, the newly inserted node is the head pointer of the linked list. Because the header pointer is changed at this time, you must set the phead parameter to the pointer */void addtotail (listnode ** phead, int value) {listnode * pnew = new listnode (); pnew-> m_pnext = NULL; pnew-> m_nvalue = value; If (* phead = NULL) {* phead = pnew ;} else {listnode * pnode = * phead; // use a temporary node to traverse the table to find the last base point while (pnode-> m_pnext) pnode = pnode-> m_pnext; pnode-> m_pnext = pnew;} void removenode (listnode ** phead, int value) {If (* phead = NULL | phead = NULL) return; listn Ode * ptobedelete = NULL; // put the node to be deleted into ptobedeleteif (* phead)-> m_nvalue = value) // If the header node is to be deleted {ptobedelete = * phead; * phead = (* phead)-> m_pnext; // change the header node} else {listnode * pnode = * phead; while (pnode-> m_pnext! = NULL & pnode-> m_pnext-> m_nvalue! = Value) {pnode = pnode-> m_pnext; // find the previous node of the deleted node} // find the node if (pnode-> m_pnext! = NULL & pnode-> m_pnext-> m_nvalue! = Value) {ptobedelete = pnode-> m_pnext; pnode-> m_pnext = pnode-> m_pnext; // adjust the node} If (ptobedelete! = NULL) {Delete ptobedelete; // delete node }}

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.