C + + linked list

Source: Internet
Author: User

Understanding lists and nodes for the first time

Two basic properties of the node: node data, pointer to the next node Pnext.

The two basic properties of the linked list are: The number of pointers and nodes of the linked table head.

Linked list is like a line to connect each node, the key is Pnext pointer, linked list is just a logical product.

So the problem is, the node class has a pointer to the next node, and the next node has not yet produced how to point to it. So there is the next node, and then the current node pointer is written in the Pnext pointer of the previous node. The procedure is to add a node pointer to the current (that is, a pointer to the second node) on the pnext of the first node when the second node is added.

So after adding a node, Pnext is not written, only add the next node to go back to the last Pnext write. These are some of the logic.

This function implements a simple three-step:

1) Look for the tail node and return the node pointer;

2) Add a new node, that is, the new node object pointer to the Pnext pointer to the previous node, so that you can find the current node through the previous node, then you are the node of this list;

3) Traversal, is to write the node to visit once.

#include <iostream>

#include <string.h>

using namespace Std;

Class CNode

{

Public

CNode *pnext;

int Date;

CNode (int date)

{

Pnext = NULL;

date = date;

}

~cnode ()

{

Delete Pnext;

Pnext = NULL;

}

};

Class CList

{

Public

CNode *pheader; The head pointer of a linked list is a pointer to a node

int nodesum; Node count

CList ()

{

Pheader = NULL;

nodesum = 0;

}

~clist ()

{

Delete Pheader;

Pheader = NULL;

}

cnode* movetrail ()//move to the tail node and return it

{

CNode *temp;

Temp = Pheader;

for (int i=1; i<nodesum; i++)

{

Temp = temp->pnext;

}

return Temp;

}

void AddNode (cnode* pnode)//Insert node at end of chain

{//The process of inserting a node is to

CNode *ptrail; The node pointer is written to the previous node on the Pnext pointer

if (nodesum = = 0)

{

Pheader = Pnode;

}

Else

{

Ptrail = Movetrail ();

Ptrail->pnext = Pnode;

}

nodesum++;

}

void Displayallnode ()//traversing a node in a list

{

CNode *temp=pheader;

cout<< "all nodes in chronological order" <<endl;

cout<< "1th node:" <<Temp->Date<<endl;

for (int i =1; i<nodesum; i++)

{

Temp = temp->pnext;

cout<< "<<i+1<<" nodes: "<<Temp->Date<<endl;

}

}

};

int main ()

{

CNode *node1 = new CNode (520);

CNode *node2 = new CNode (1314);

CNode *node3 = new CNode (222);

CList List1;

List1. AddNode (Node1);

List1. AddNode (Node2);

List1. AddNode (NODE3);

List1. Displayallnode ();

return 0;

}

C + + linked list

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.