A single-linked list of linear tables C + + implementation

Source: Internet
Author: User

Methods that need to be implemented

#pragmaOnce#include"Node.h"#ifndef List_h#defineList_h#include<iostream>using namespacestd;classlist{ Public: List ();//constructor Function~list ();// Destructors    voidClearlist ();//Empty List    BOOLListempty ();//Linked list Empty    intListlength ();//Linked list length    BOOLGetelem (intI, Node *pnode);//gets the element of the specified index    intLocateelem (Node *pnode);//gets the index of the specified element    BOOLPriorelem (node *pcurrentnode, node *pprenode);//get the precursor node.    BOOLNextelem (node *pcurrentnode, node *pnextnode);//get the successor node.    voidListtraverse ();//Traverse    BOOLListinsert (intI,node *pnode);//inserting elements    BOOLListdelete (intI, Node *pnode);//Delete Element    BOOLListinserthead (Node *pnode);//insert behind the head node    BOOLListinserttail (Node *pnode);//insert to Linked list lastPrivate: Node*m_plist; intm_ilength;};#endif // ! List_h
1. Constructors

Request memory for Head node M_plist in the heap

M_plist data field to 0

Point to address is empty, in fact there is a head node declared here, the head node has no successor node and the data field is empty.

Length to 0

list::list () {    new  Node;    M_plist0;    M_plist->next = NULL;     0 ;}
2. Destructors

Call Empty list method

Delete the head node and place the empty

list::~List () {    clearlist ();     Delete m_plist;     = NULL;}
3. Clear the list

Declares a pointer to a node* type that points to the next node of m_plist, specifically: M_plist is the head node, and M_plist->next is the pointer field of the head node.

Determines whether the currentnode is empty, that is, to determine whether the M_plist pointer field is empty, the empty description CurrentNode is the tail node, the tail node has no successor node

When CurrentNode is not empty, declaring a pointer to a node* type, temp, points to the next node of CurrentNode, or it can be understood as the pointer field value that the temp receives CurrentNode

Delete the current CurrentNode, and then assign the pointer field of the CurrentNode that you saved in the temp back

In turn ...

The loop ends, and then the tail node pointer field is empty.

the difference between emptying the list and the destructor: emptying the list is to empty the nodes after the head node, and the destructor is to empty the head node and the subsequent node.

void list::clearlist () {    *currentnode = m_plist->next;      while (CurrentNode! = NULL)    {        *temp = currentnode->next;         Delete CurrentNode;         = temp;    }    M_plist->next = NULL;}
4. The linked list is empty and gets the length

There's nothing to say.

BOOL List::listempty () {    return0truefalse;} int list::listlength () {    return  m_ilength;}
5. Inserting elements behind the head node

Pnode pointer passed in as a parameter

Temp holds the pointer field of the head node and points to the first node

Request memory for a newnode pointer in the heap

Failed to apply to memory return error

Application to memory, parameter data field assigned to new node data field

The head node points to the new node.

The new node points to the next node of the original head node, so the list is connected.

BOOLList::listinserthead (Node *Pnode) {Node*temp = m_plist->Next; Node*newnode =NewNode; if(NewNode = = NULL)//determine if the requested node memory is empty    {        return false; }    Else{NewNode->data = pnode->data; M_plist->next =NewNode; NewNode->next =temp; M_ilength++; return true; }}
6. Inserting elements behind a tail node

Pnode pointer passed in as a parameter

CurrentNode Receiving head node

Determine if the CurrentNode is a tail node because the trailing node is empty.

Loop to the last node, at which point Currentnode->next=null

Request a newnode memory in the heap

Failed to apply to memory return error

Request to Memory

Pnode data field assigned to NewNode data field

New node pointer field empty

CurrentNode point to new node

The new node points to null, so the list is connected, and the new node becomes the new tail node.

Linked list length + +

Return the correct

BOOLList::listinserttail (Node *Pnode) {Node*currentnode =m_plist;  while(Currentnode->next! =NULL) {CurrentNode= currentnode->Next; } Node*newnode =NewNode; if(NewNode = =NULL) {        return false; }    Else{NewNode->data = pnode->data; NewNode->next =NULL; CurrentNode->next =NewNode; M_ilength++; return true; }}
7. Inserting elements

Judging the legality of parameter I

I cannot be less than 0,i cannot be greater than the linked list length

CurrentNode Saving head node

Statement NewNode

Pnode data field assignment to NewNode data field

The new node points to the next node of the original node.

The original node points to the new node.

BOOLList::listinsert (intI, Node *Pnode) {    if(i<0|| I>m_ilength) {        return false; } Node*currentnode =m_plist;  for(intK =0; K < I; k++) {CurrentNode= currentnode->Next; } Node*newnode =NewNode; if(NewNode = = NULL)//determine if the requested node memory is empty    {        return false; }    Else{NewNode->data = pnode->data; NewNode->next = currentnode->Next; CurrentNode->next =NewNode; return true; }}

A single-linked list of linear tables C + + implementation

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.