Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please go through how I have implemented linked list in c++ . I believe it will be simple to understand</p> <p>This is <strong>Node.h</strong></p> <pre><code>#ifndef _NODE_ #define _NODE_ class Node{ private: char data; Node* next; void allocate(); public: Node(); char getData(void); Node*getNext(); void setData(char data); void setNext(Node * next); ~Node(); }; #endif </code></pre> <p>This is <strong>Node.cpp</strong></p> <pre><code>#include&lt;iostream&gt; #include"Node.h" Node::Node(): data('0'), next(NULL){ std::cout&lt;&lt;"Node created"&lt;&lt;std::endl; } void Node::setData(char data){ this-&gt;data=data; } void Node::setNext(Node *next){ this-&gt;next=next; } char Node::getData(void){ return data; } Node* Node:: getNext(void){ return next; } Node::~Node(){ std::cout&lt;&lt;"Node deleted"&lt;&lt;std::endl; } </code></pre> <p>This is <strong>linkedList.h</strong></p> <pre><code>#include"Node.h" #ifndef Linked_ #define Linked_ class LinkedList{ private: Node* head; Node* createNode(); Node* getToLastNode(); int getLength(); char getUserData(); public: LinkedList(); void addNodeAtTheBeginning(); void addNodeAtAGivenLocation(int); void addNodeAtTheEnd(); void deleteFirstNode(); void deleteNodeAtAGivenLocation(int); void deleteLastNode(); void display(); void deleteLinkedList(); ~LinkedList(); }; #endif </code></pre> <p>This is <strong>LinkedList.cpp</strong></p> <pre><code>#include"Node.h" #include"LinkedList.h" #include&lt;iostream&gt; Node* LinkedList::createNode(){ Node *tempNode; tempNode = new Node(); tempNode-&gt;setNext(NULL); tempNode-&gt;setData(getUserData()); return tempNode; } int LinkedList::getLength(){ Node *tempNode=head; int count=1; if(NULL==head){ return 0; }else{ while(NULL!=tempNode-&gt;getNext()){ tempNode=tempNode-&gt;getNext(); count++; } return count; } } LinkedList::LinkedList(){ head=NULL; // if(NULL==head){ // head=createNode(); // } } void LinkedList::addNodeAtTheBeginning(){ Node *tempNode; tempNode=createNode(); if(NULL!=head){ tempNode-&gt;setNext(head); head=tempNode; }else{ head=tempNode; } } void LinkedList::addNodeAtAGivenLocation(int position){ Node *tempNode; Node *tempNode2; if(getLength()&lt;position){ std::cout&lt;&lt;"No node can be inserted at this poition "&lt;&lt;std::endl; }else{ tempNode=createNode(); tempNode2=head; for(int i=1;i&lt;position;i++){ tempNode2=tempNode2-&gt;getNext(); } tempNode-&gt;setNext(tempNode2-&gt;getNext()); tempNode2-&gt;setNext(tempNode); } } void LinkedList::addNodeAtTheEnd(){ if(NULL==head){ head=createNode(); }else{ Node *tempNode=head; while(NULL!=tempNode-&gt;getNext()){ tempNode=tempNode-&gt;getNext(); } tempNode-&gt;setNext(createNode()); } } void LinkedList::deleteFirstNode(){ Node *tempNode; if(NULL==head){ std::cout&lt;&lt;"No node available for deletion"&lt;&lt;std::endl; }else{ tempNode=head; head=head-&gt;getNext(); delete tempNode; } } void LinkedList::deleteNodeAtAGivenLocation(int position){ Node *tempNode; if(getLength()&lt;=position){ std::cout&lt;&lt;"No node can be deleted as no node exist at this poition "&lt;&lt;std::endl; }else{ for(int i=1;i&lt;position;i++){ tempNode=head; tempNode=tempNode-&gt;getNext(); } tempNode-&gt;setNext(tempNode-&gt;getNext()-&gt;getNext()); delete tempNode-&gt;getNext(); } } void LinkedList::deleteLastNode(){ Node *tempNode; Node *tempNode2; if(NULL==head){ std::cout&lt;&lt;"No node available for deletion"&lt;&lt;std::endl; }else{ tempNode=head; while(NULL!=tempNode-&gt;getNext()){ tempNode=tempNode-&gt;getNext(); tempNode2=tempNode; } tempNode=tempNode-&gt;getNext(); tempNode2-&gt;setNext(NULL); delete tempNode; } } LinkedList::~LinkedList(){ Node *tempNode=NULL; if(NULL==head){ std::cout&lt;&lt;"No nodes in the Linked List available for Deletion"&lt;&lt;std::endl; }else{ tempNode =head; while(NULL!=head-&gt;getNext()){ tempNode=head; head=head-&gt;getNext(); delete tempNode; } delete head; } std::cout&lt;&lt;"Linked List Deleted"&lt;&lt;std::endl; head=NULL; } void LinkedList::display(void){ Node *tempNode; tempNode=head; if(NULL==head){ std::cout&lt;&lt;"head--&gt;X"; }else{ std::cout&lt;&lt;"head--&gt;"; while(NULL!=tempNode-&gt;getNext()){ std::cout&lt;&lt;"["&lt;&lt;tempNode-&gt;getData()&lt;&lt;"]--&gt;"; tempNode=tempNode-&gt;getNext(); } std::cout&lt;&lt;"["&lt;&lt;tempNode-&gt;getData()&lt;&lt;"]--&gt;X"&lt;&lt;std::endl; } } void LinkedList::deleteLinkedList(){ delete this; head=NULL; } char LinkedList::getUserData(){ char data; std::cout&lt;&lt;"Enter Data"&lt;&lt;std::endl; std::cin&gt;&gt;data; return data; } </code></pre> <p>And Finally the <strong>main.cpp</strong></p> <pre><code>#include &lt;cstdlib&gt; #include &lt;iostream&gt; #include"LinkedList.h" #include"Node.h" #include&lt;stdlib.h&gt; void printMenu(); int getUserSelection(); void performOperation(int); LinkedList lk; int main(){ int option=0; while(9!=option){ printMenu(); option=getUserSelection(); performOperation(option); } } void printMenu(void){ std::cout&lt;&lt; ""&lt;&lt;std::endl; std::cout&lt;&lt; "1) Add Node At The Beginning"&lt;&lt;std::endl; std::cout&lt;&lt; "2) Add Node At A Given Location"&lt;&lt;std::endl; std::cout&lt;&lt; "3) Add Node At The End"&lt;&lt;std::endl; std::cout&lt;&lt; "4) Delete First Node"&lt;&lt;std::endl; std::cout&lt;&lt; "5) Delete Node At A Given Location"&lt;&lt;std::endl; std::cout&lt;&lt; "6) Delete Last Node"&lt;&lt;std::endl; std::cout&lt;&lt; "7) Display "&lt;&lt;std::endl; std::cout&lt;&lt; "8) Delete LinkedList"&lt;&lt;std::endl; std::cout&lt;&lt; "9) Exit"&lt;&lt;std::endl; } int getUserSelection(){ int option=0; std::cout&lt;&lt;"Select an option: "&lt;&lt;std::endl; std::cin&gt;&gt;option; return option; } void performOperation(int option){ switch (option){ case 1: lk.addNodeAtTheBeginning(); break; case 2:{ int location=0; std::cout&lt;&lt;"Enter a location:"&lt;&lt;std::endl; std::cin&gt;&gt;location; lk.addNodeAtAGivenLocation(location); } break; case 3: lk.addNodeAtTheEnd(); break; case 4: lk. deleteFirstNode(); break; case 5:{ int location=0; std::cout&lt;&lt;"Enter a location:"&lt;&lt;std::endl; std::cin&gt;&gt;location; lk.deleteNodeAtAGivenLocation(location); } break; case 6: lk.deleteLastNode(); break; case 7: lk.display(); break; case 8: lk.deleteLinkedList(); break; case 9: exit(0); } } </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