Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have seen many question related to <em>Linked Lists</em>, so I decide to share my Linked List version, so it can be adapted and/or improved. This version is written in C++, some methods were omitted for saving space:</p> <pre><code>class Node { friend class LinkedList; public: Node(); Node(const node&amp; krNode); explicit Node(const int kiData); virtual ~Node(); Node&amp; operator=(const Node&amp; krNode); int GetData(void) const {return _idata; } private: int _iData; Node* _pNetxNode; } Node::Node(): _iData(0), _pNextNode(0) {} Node::Node(const Node&amp; krnode): _iData(krNode._iData), _pNextNode(0) {} Node::~Node() {} Node&amp; Node::operator=(const Node&amp; krNode) {_iData = krNode._iData; return *this; } class LinkedList { public: LinkedList(); virtual ~LinkedList(); int GethLenght(void) {return _iSize;} void Append(Node* pNode); void Insert(const int kiIndex, node* pNode); Node* GetItem(const int kiIndex); int IndexOf(Node* pNode); void Remove(const int kiInde); void Swap(constconst int kiIndexA, const int kiIndexB); void Clear(void); void Print(void); protected: LinkedList(const LinkedList&amp; krLinkedList); LinkedList&amp; operator=(const LinkedList&amp; krLinkedList); private: Node* Detach(const int kiIndex); Node* _pFirstNode; Node* _pLastNode; int _iSize; } LinkedList::LinkedList() : _pFirstNode(0), _pLastNode(0), _iSize(0) {} LinkedList::~LinkedList() { Clear(); } void LinkedList::Append(Node* pnode) { if(0==_iSize{_pFistrNode = pNode; } else { _pLastNode-&gt;_pNextNode = pnode; } _pLastNode = pNode; _iSize++; } void LinkedList::Insert(const int kiIndex, node* pNode) { Node* pNext = 0; Node* pPrevious = 0; if(0==_iSize) { Append(pNode); } else { if(0==kiIndex){ pNode-&gt;_pNextNode = _pFirstNode; _pFirstNode = pNode; } else { pPrevious = Getitem(kiIndex-1); pNext=pPrevoius-&gt;_pNextnode; pNode-&gt;_pNextNode=pNext; pPrevious-&gt;_pNextnode=pNode;} } _iSize++; } Node* LinkedList::GetItem(const int kiIndex){ Node* pNode = _pFirstNode; int iCounter = 0; while(icounter++ != kiIndex) { pNode = pNode-&gt;_pNextnode; } return pNode; } int LinkedList::IndexOf(Node* pSearchNode){ node* pNode = _pFirstnode; int iIndex=0; while(o != pNode) { if(pnode==pSearchNode) { break;} iIdex++; pNode=pnode-&gt;_pnextnode; } if(iIndex ==_iSize) {iIndex = -1;} return iIndex; } void LinkedList::Remove(const int kiIndex){ delete Detach(kiIndex); } void LinkedList::Swap(const int kiIndexA, const int kiIndexB){ int iLowIndex = 0; int iHighIndex = 0; if(kiIndex&gt;==kiIndexB) {return;} iLowIndex = (kiIndexA &lt; kiIndexB) ? kiIndexA : kiIndexB; iHighIndex = (kiIndexA &gt; kiIndexB) ? kiIndexA : kiIndexB; Insert(iHighIndex, Detach(iLowIndex)); Insert(iLowIndex, Detach(iHighIndex-1)); } void LinkedList::Clear(void) { Node* pNode=_pFirstNode; while(0!=pnode){delete pNode; pNode=pNode-_pNextNode;} _pFirstnode=0; _pLastnode=0; } void LinkedList::Print(void) { Node* pNode = _pFirstNode; while(0 != pnode) {printf("%d ", pNode_iData); pNode = pNode-&gt;_pNextNode;} } Node* LinkedList::Detach(const int kiindex){ Node* pnode = _pFirstnode; Node* pToDetachNode = 0; if(kiIndex==0){ pToDetachNode = _pFirstNode; _pFirstNode=pnode-&gt;_pNextNode; if(1==_iSize){_pLastNode=0;} } else{ Node* pPreviousNode = GetItem(kiIndex-1); pToDetachNode = pPreviousNode-&gt;_pNextNode; if(kiIndex&lt;_iSize){pPreviousNode-&gt;_pNextNode=pPreviousNode-&gt;_pNextnode; } else {pPreviousNode-&gt;_pNextnode=0; _pLastNode=pPrevoiusNode;} }_iSize--; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload