Shahid Riaz Bhatti

if(my.work == “Interesting” || my.availableTime > my.workHours) { this.blog.Post();}

Creating a simple Linked List Data Structure using C++ – Part II

March 05
by afeef 5. March 2009 15:30

Please read the first part of this topic if you haven't done already. Implement the List class interface by creating a new file named List.cpp. Add the following code to the newly created file. Download the complete source of this example from here.

   1: #include "List.h"
   2: #include <iostream>
   3:  
   4: using namespace std;
   5: List::List(void){
   6:     headNode   =   new Node();
   7:     headNode->setNext(0);
   8:     currentNode   =   0;
   9:     lastCurrentNode  =   0;
  10:     size   =   0;
  11: }
  12:  
  13: List::~List(void)
  14: {
  15: }
  16:  
  17: void   List::add (int  addObject) 
  18: {
  19:     Node *   newNode   =   new   Node();
  20:     newNode->set(addObject);
  21:     if( currentNode   !=   0 )
  22:     {
  23:         newNode->setNext(currentNode->getNext());
  24:         currentNode->setNext( newNode );
  25:         lastCurrentNode   =   currentNode;
  26:         currentNode   =   newNode;
  27:     }
  28:      else
  29:     {
  30:  
  31:         newNode->setNext(0);
  32:         headNode->setNext(newNode);
  33:         lastCurrentNode   =   headNode;
  34:         currentNode   =   newNode;
  35:     }
  36:     size ++;
  37: }
  38:  
  39: /* get() class method */
  40: int   List::get() 
  41: { 
  42:     if (currentNode  !=  0) 
  43:      return   currentNode->get(); 
  44: }
  45:  
  46: /* next() class method */
  47: bool   List::next() 
  48: {
  49:     if (currentNode  ==  0)   
  50:         return  false;
  51:  
  52:     lastCurrentNode  =  currentNode;
  53:     currentNode  =  currentNode->getNext();
  54:     if (currentNode == 0 || size == 0) 
  55:         return  false;
  56:     else
  57:         return  true;
  58: }
  59:  
  60: /* Friend function to traverse linked list */
  61: void traverse(List list)
  62: {
  63:     Node* savedCurrentNode  =  list.currentNode;
  64:     list.currentNode  =  list.headNode;
  65:     
  66:     for(int i = 1; list.next(); i++)
  67:     {
  68:         cout << "\n Element " << i << "   " << list.get();
  69:     }
  70:     list.currentNode  =  savedCurrentNode;
  71: }
  72:  
  73: /* Friend function to add Nodes into the list */ 
  74: List addNodes()
  75: {
  76:     List   list;
  77:     list.add(2);
  78:     list.add(6);
  79:     list.add(8); 
  80:     list.add(7);   
  81:     list.add(1);
  82:     cout  <<  "\n List size = "  <<  list.size  <<'\n';         
  83:     return  list;
  84: }
  85:  
  86: void List::start() {
  87:     lastCurrentNode = headNode;
  88:     currentNode = headNode;
  89: }
  90:  
  91: void List::remove() {
  92:      if( currentNode != NULL && currentNode != headNode) {
  93:          lastCurrentNode->setNext(currentNode->getNext());
  94:          delete currentNode;
  95:          currentNode = lastCurrentNode->getNext();
  96:          size--;
  97:      }
  98: }
  99:  
 100: int List::length() 
 101: { 
 102:     return size; 
 103: }

The traverse() friend function traverses the list from start to finish and output the elements contained in the list. The addnodes() friend fuction adds the Nodes to the List. the start() method sets the currentNode pointer to the start of the List. remove() method removes the currentNode from the list. The length() method returns the current size of the Linked list.

Create a main.cpp file to test the Linked List data structure we have created.

   1: #include<iostream>
   2: #include "List.h"
   3:  
   4: using namespace std;
   5:  
   6: int main(){
   7:  
   8:     List list;  // creating a list object
   9:     
  10:     // adding values to the list
  11:     list.add(5); 
  12:     list.add(13); 
  13:     list.add(4);
  14:     list.add(8); 
  15:     list.add(24); 
  16:     list.add(48); 
  17:     list.add(12);
  18:  
  19:     // calling the start method of the list
  20:     list.start();
  21:  
  22:     // printing all the elements of the list
  23:     while (list.next()) 
  24:         cout << "List Element: "<< list.get()<<endl;
  25:  
  26:     return 0;
  27:  
  28: }

Happy programming …!!!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C++

RecentComments

Comment RSS

Most comments

supplynflshop supplynflshop
51 comments
tiffany-bracelets tiffany-bracelets
39 comments
AVI to iPad AVI to iPad
36 comments