Single linked list simple implementation C + +

Source: Internet
Author: User

First define a class, related data, function encapsulation, in the header file

#pragmaOnceclasssinglelist{ Public: typedefstruct_node{intNdata;//data fields_node *pnext;//refers to a node down}node,*Pnode;Private: NODE*M_phandnode; intM_ncount; Public: Singlelist (); ~singlelist (); BOOLInitlist ();//Initialize Operation    BOOLListempty ();//determine if the linked list is empty    BOOLListinsert (intData);//Insert Data (end)    BOOLListinsert (intNPos,intData);//inserting data at a specified location    intGetelem (intNPos);//returns the value of the specified position element    intLocateelem (intData);//find an element equal to a given value    BOOLListdelete (intNPos,int&valuedata);//deletes the element at the specified position and returns its value    intListlength ();//returns the number of elements    BOOLClearlist ();//Empty List};

Implement the specific code in the CPP

#include"stdafx.h"#include"SingleList.h"#include<iostream>singlelist::singlelist () {}singlelist::~singlelist () {}//Initialize the list of linksBOOLsinglelist::initlist () {M_ncount=0;//Linked list length is 0M_phandnode =NewNODE;//allocating space to the head node.memset (M_phandnode,0,sizeof(NODE));//initialization of head node      return true;}//Inserting DataBOOLSinglelist::listinsert (intData) {Pnode Ptemnode=M_phandnode;  while(true){        if(!ptemnode->pnext) {//If the node pointer is empty, insertPnode Pnewnode =NewNODE;//Create a temporary nodePnewnode->ndata = Data;//Temporary node data assignmentPnewnode->pnext = nullptr;//null pointer field under temporary nodePtemnode->pnext= Pnewnode;//Insert the node address to point tom_ncount++;//increase the chain table length by 1            return true; } Ptemnode= ptemnode->pnext;//if not null, point to next node    }}//inserts the data into the specified positionBOOLSinglelist::listinsert (intNPos,intData) {Pnode Ptemnode= M_phandnode;//give the head node A variable for traversing    if(NPos > m_ncount+1){//The insertion position is greater than the number of elementsListinsert (Data);//Insert End        return true; }    Else{pnode Pnewnode=NewNODE;//Create a temporary nodePnewnode->ndata = Data;//Temporary node data assignmentPnewnode->pnext = nullptr;//null pointer field under temporary node         for(inti =0; I < NPos-1; i++) {//traverse to the node where you want to insert the nodePtemnode = ptemnode->Pnext; } Pnewnode->pnext = ptemnode->pnext;//Next to the new node, next to the previous nodePtemnode->pnext = Pnewnode;//give the address of the new node to the next of the previous nodem_ncount++;//linked list length plus 1        return true; }}//non-null decisionBOOLSinglelist::listempty () {if(M_phandnode->pnext) {//the next value of the head node is not null        return false; }    return true;}//returns the value of the specified position elementintSinglelist::getelem (intNPos) {//Incoming coordinatesPnode ptemp =NewNODE;//Create a new node to receive the linked listPtemp =M_phandnode;  for(inti =0; i < NPos; i++) {//traverse to coordinate positionPtemp = ptemp->Pnext; }    returnptemp->ndata;//return coordinate value}//find the specified element, return to positionintSinglelist::locateelem (intData) {       if(Listempty ()) {//if empty, returns-1        return-1; }    Else{        intCount =1;//Count InitializationPnode Temp =NewNODE;//Create a Receive link list pointerTemp = m_phandnode->pnext;//A pointer to the receiving list points to the first element         for(inti =0; i < M_ncount; i++) {//looping through the found element            if((temp->ndata) = = Data) {//find element, return subscript                returncount; } Temp= temp->pnext;//I didn't find it. Point to next elementcount++;//value plus 1        }        }    return false;//not found after traversal, return error}//deletes the specified position elementBOOLSinglelist::listdelete (intNPos,int&valuedata) {    if(Listempty () | | Npos>m_ncount) {//If the linked list is empty, or the specified position is greater than the linked list length        return false; } pnode Pstart=M_phandnode;  for(inti =0; I < NPos-1; i++) {Pstart= pstart->pnext;//The pointer points to the previous position to delete the element} pnode Tempnode=NewNODE;//Create a new nodeTempnode = pstart->pnext;//new node records the node information to be deletedValuedata = tempnode->ndata;//Save the information you want to delete from the outside worldPstart->pnext = tempnode->pnext;//will be deleted after the ends of the link    DeleteTempnode;//Release the space you builtm_ncount--;//reduce the number of linked lists by one    return true;}//returns the chain table lengthintsinglelist::listlength () {returnM_ncount;}//Destroying pointersBOOLsinglelist::clearlist () {Pnode Ptemnode= m_phandnode->pnext;//Save head node pointerPnode Ptemnextnode;  while(Ptemnode)//Head node points to not empty{Ptemnextnode= ptemnode->pnext;//The head node next points to the temporary pointer        DeletePtemnode;//Remove head node temporaryPtemnode = Ptemnextnode;//The temporary head node points to the next} m_ncount=0;//set the list element number 0M_phandnode->pnext = nullptr;//Place the head node next to empty.    ///////////////////////////////////////////    return true;}

Test the main function

1#include"stdafx.h"2#include"SingleList.h"3 int_tmain (intARGC, _tchar*argv[])4 {5 6Singlelist A;//test single-linked list7A.initlist ();//Initialize8A.listinsert ( at);//Insert several data9A.listinsert ( -);TenA.listinsert ( -); OneA.listinsert ( -); AA.listinsert ( -); -A.listinsert ( -); -     intC//define a variable to receive the value to delete theA.listdelete (2, c);//Delete the second element and receive it in C, where C is passed in as a reference -A.getelem (3);//get a third element -A.listinsert (3, the);//Insert in a third position -     intF=a.listlength ();//Get length +     intD=a.locateelem ( -);//get where a value is located -A.clearlist ();//Destroy linked list +     return 0; A}

Single-linked list simple implementation C + +

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.