To find the penultimate node of a linked list

Source: Internet
Author: User

Recently read a book on the list of the last nth node, a simple implementation of the list, the implementation of the scheme is as follows

1, not through the chain table length sequence to traverse the reciprocal nth node Getreserven is so implemented.

2, of course, if the link table records the length of the node can also be directly sequence traversal of the first LENTH-N node is the reciprocal node.

Template<class t> class Linkedlist{public:operator T () {return m_data;}    Virtual ~linkedlist () {if (m_size!=0) Clear ();        } void AddData (T data) {if (m_size==0) {m_data=data;            } else {linkedlist<t>* ptem=new linkedlist<t>;            Ptem->m_data=data;            Ptem->m_pprev=this->m_ptail;            ptem->m_pnext=0;            this->m_ptail->m_pnext=ptem;        this->m_ptail=ptem;    } m_size++;    } void SetData (T data) {m_data=data;        } void Clear () {linkedlist<t>*ptem=this->next ();        linkedlist<t>*pdel;        for (; Ptem;)            {Pdel=ptem;            Ptem=ptem->next ();        Delete Pdel;        } m_ptail=this;        M_pprev=this;    m_size=0;    } int Size () {return m_size;    } T GetData () {return m_data; } LinkeDlist<t>*next () {return m_pnext;    } linkedlist<t>*gettail () {return m_ptail;    } linkedlist<t>*getprevious () {return m_pprev; } linkedlist<t>* Getreserven (int n) {if (n>m_size| |        N<=0) return 0;        linkedlist<t>*pfront=this;        linkedlist<t>*pbehand=this;        for (int i=1; i<=n; i++) {pbehand=pbehand->next ();            } while (true) {if (pbehand==0) return pfront;            Pbehand=pbehand->next ();        Pfront=pfront->next ();    }} static linkedlist<t>* CreateList () {return new linkedlist<t>;        };p rivate:linkedlist (): M_pnext (0), M_pprev (0), M_ptail (0), m_size (0) {m_ptail=this;    M_pprev=this;    } linkedlist<t>*m_pnext;    linkedlist<t>*m_ptail;    linkedlist<t>*m_pprev;    int m_size; T m_data;};
The test code is as follows

#include <iostream> #include <string> #include "LinkedList.h" using namespace Std;int Main () {    Linkedlist<string> *ptem=linkedlist<string>::createlist ();    Ptem->adddata ("xxx");    Ptem->adddata ("Dsdf");    Ptem->adddata ("DFSD");    Ptem->adddata ("dseee");    Ptem->adddata ("xxxx");    Ptem->adddata ("DSFDSF");    Ptem->adddata ("FDSFD");    Linkedlist<string>*p=ptem;    for (; p;)    {        cout<<p->getdata () << ",";        P=p->next ();    }    cout<<endl;    Linkedlist<string>*pdata=ptem->getreserven (5);    if (pData)      cout<< "reserve N:" <<pdata->getdata () <<endl;    else{      cout<< "Reserver not Found!" <<endl;    }    return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Count the nth nodes of the 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.