Data structure and algorithm learning the basic functions of the 1th season 02 linked list C + + implementation

Source: Internet
Author: User

2015 Study Plan Arrangement:

Http://www.cnblogs.com/cyrus-ho/p/4182275.html

Try to implement the bidirectional linked list class linklist with C + +, the basic function is to insert the node in position I and delete the node of position I.

The first is the node class, where each node has data, a pointer to the previous node front, and a pointer to the last node next.

class node{public:    int  data;    Node* next;    Node* front;  Public :    Node ();    Node (int  data);    Node (int data, node* nextptr, node* frontptr);};

Implementation of the Node class:

#include"stdafx.h"#include"Node.h"Node::node () { This->data =0;  This->next =NULL;  This->front =NULL;} Node::node (intdata) {     This->data =data;  This->next =NULL;  This->front =NULL;} Node::node (intData, node* nextptr, node*frontptr) {     This->data =data;  This->next =nextptr;  This->front =frontptr;}

Implementation of doubly linked list class

#include"Node.h"classlinklist{ Public:    intlength;    Node Headnode; Node Rearnode; Public: Linklist (); BOOLIslinklistempty (); voidDisplay (intmode); voidInsertnode (intIintdata); voidDeletenode (inti);};

Initialize the empty list, and note that Headnode and Rearnode call the node class's parameterless construction method separately.

linklist::linklist () {    this0;     This->headnode.next = & (rearnode);     This->rearnode.front = & (headnode);}

If you want to display a parameterized constructor that calls the node class, you need to initialize the Headnode and Rearnode in the form of an initialization list.

Linklist::linklist (): Headnode (ten), Rearnode (ten) {    this0;     This->headnode.next = & (rearnode);     This->rearnode.front = & (headnode);}

Of course, the head node and the tail node data is meaningless, in fact, we only need to use the head node of next and tail node of the front, but in order to unify the operation, a simple pointer is expanded to a node.

The number of nodes in a doubly linked list is length and does not include the kinsoku nodes.

Determines whether the linked list is empty.

BOOL Linklist::islinklistempty () {    if(This->length==0)    {         returntrue;    }     return false ;}

In the console output list, you can select the reverse output.

voidlinklist::D isplay (intmode) {    if(Mode = =0) {Node* ptr = This-Headnode.next;  for(inti =0; I < This->length; i++) {Std::cout<<ptr->data<<Std::endl; PTR= ptr->Next; }    }     Else{Node* ptr = This-Rearnode.front;  for(inti =0; I < This->length; i++) {Std::cout<<ptr->data<<Std::endl; PTR= ptr->Front; }    }    return;}

The rule is that PTR starts out as a pointer field for the head node, so I

PTR = ptr->next;

The node in the list that PTR points to is I (note i is calculated from 0, one does not perform naturally at the point of the node 0---of course it may also point to the tail node, if the list is empty)

In position I insert a node, that is, the inserted node in the position of the linked list is I.

voidLinklist::insertnode (intIintdata) {    if((i > This->length) | | (I <0) ) {Std::cout<<"Error!!!!!!!!!"<<Std::endl; }     Else{Node* Temp =NewNode (data); Node* ptr = This-Headnode.next;  for(intK =0; K < I; k++) {ptr= ptr->Next; } temp->next =ptr; Temp->front = ptr->Front; PTR->front =temp; Temp->front->next =temp;  This->length++; }    return;}

First judge whether position I is legal. For a linked list of length lengths, the position of each node (without the Kinsoku node) is 0,1,...,length-1, and the legal insertion position is 0,1,...,length.

Next, you find a next pointer in the list that points to the node I in the original list. That is Ptr:node i-1, node I, when i=0, node i-1 is the head node. When i=length, node i is the tail node.

This->Headnode.next;
for (int0; k < i; k++) { = ptr->next;}

The code above implements this function. In particular, when i=0, the loop body is not executed at once, ptr:head node---node 0, when I=length, the loop body executes length+1 times, ptr:node I-Rear node.

Then, a new node is generated with the pointer (pointing to it) Temp:

New Node (data);

This node is the node to be inserted, so its position should be I, so its next should point to node I in the original list, and the address of node I in the original list is PTR:

Temp->next = ptr;

And this new node front should point to the original linked list of nodes i-1, and the original list of nodes I-1 address, is Ptr->front. (PTR now points to the node I of the original list, and the front of the node I of the original list points to the node of the original list i-1):

Temp->front = ptr->front;

Then, the front of the node I of the original list, should point to this new node, the address of this new node is temp. (PTR now points to the node I of the original list, and the front of that node should be i-1 to the new node by the node that points to the original list):

Ptr->front = temp;

Next, the node of the original linked list i-1 Next, should also point to this new node. (Temp's front now points to the node i-1 of the original list, changing the node's next from node I to the original list to point to the new node):

Temp->front->next = temp;

Finally, length+1.

I made 3 mistakes in the process of completing this function:

1. At first I did not track the pointer field, but kept the copy node itself, get the original node i-1 and the copy of node I, and then do pointer transformation on the top, so I actually create 3 nodes (new node Temp, node i-1 Copy and node I copy), And then connect the 3 nodes together, which has no effect on the original list, because I'm dealing with node i-1 and node I copy!

2.track pointer field, the last step in doing a pointer break/swap operation (next, the node of the original linked list i-1 Next, should also point to this new node), wrote a

ptr = temp;

Indeed, the node of the original list i-1 next, and the value of PTR is the same (all points to the original linked list of nodes I), but the above code is changed PTR, instead of the original linked list of nodes i-1 next!

3. At the beginning of the new node creation, I used the following methods:

Node temp (data);

There is no mistake in executing the result within the function (of course node* to node), but the function returns and does not get the correct result in the main program. I presume that the function of the local variable will be destroyed automatically at the end of the function, so the linked list obtained in the main program will get garbage value when it reads the new node.

In the experience of inserting nodes, it is much easier to implement delete nodes:

voidlinklist::D Eletenode (inti) {    if((i > This->length-1) || (I <0) ) {Std::cout<<"Error!!!!!!!!!"<<Std::endl; }     Else{Node* ptr = This-Headnode.next;  for(intK =0; K < I; k++) {ptr= ptr->Next; } Node* Temp =ptr; PTR->next->front = ptr->Front; PTR->front->next = ptr->Next;  This->length--;        Delete temp; Temp=NULL; }    return;}

Data structure and algorithm learning the basic functions of the 1th season 02 linked list C + + implementation

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.