Shahid Riaz Bhatti

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

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

March 05
by afeef 5. March 2009 15:37

In this post i am going to show you how to implement a simple Linked List data structure in C++ using Visual Studio 2008. However, you can use your favorite IDE to build this example. You can also check the original post at my blog where complete source code of this post is available.

In simplistic view Linked List is a collection of individual List Nodes interconnected with each Node pointing to the Node ahead of it. So to get us started, we need List Nodes first and then we’ll build Linked List data structure using those.

Fire up your favorite IDE and create a new project and add a header file named Node.h. This header file contains the interface of the Node class. Following is the code for this header file.

   1: #pragma once
   2:  
   3: class Node
   4: {
   5:     public:
   6:         Node(void);
   7:         ~Node(void);
   8:  
   9:         int   get();
  10:         void   set(int   object);
  11:  
  12:         Node   * getNext();
  13:         void   setNext(Node   * nextNode);
  14:  
  15:     private:
  16:         int   object;
  17:         Node   * nextNode;
  18: };    

In the code snippet above, first data member is an integer type that will hold the node value and the second data member is a pointer to the next node in the list (Null if this is the last node in the list). Getter and Setter methods for the data members are also defined.

Now create a new class and name it Node.cpp. This will implement the interface defined in the Node.h header file. Following is the code for the Node.cpp.

   1: #include "Node.h"
   2:  
   3:     Node::Node(void){
   4:     }
   5:  
   6:     Node::~Node(void){
   7:     }
   8:     int   Node::get() { 
   9:         return   object; 
  10:     }
  11:  
  12:     void   Node::set(int   object) { 
  13:         this->object   =   object; 
  14:     }
  15:  
  16:     Node   * Node::getNext() { 
  17:         return   nextNode; 
  18:     }
  19:  
  20:     void   Node::setNext(Node   * nextNode) { 
  21:         this->nextNode   =   nextNode; 
  22:     }

To create a linked list of Nodes we have to create a List class. For that create a new header file named List.h and copy the following code snippet into it. Include the Node.h header file created earlier.

   1: #pragma once
   2: #include "Node.h"
   3: class List
   4: {
   5:     public:
   6:         List(void);
   7:         ~List(void);
   8:         void   add (int   addObject);
   9:         int    get();
  10:         bool   next();
  11:         friend void  traverse(List list);
  12:         friend List   addNodes();
  13:         void start();
  14:         void remove();
  15:         int length();
  16:  
  17:     private:
  18:         int      size;
  19:         Node *   headNode;
  20:         Node *   currentNode;
  21:         Node *   lastCurrentNode;
  22: };

In this code snippet there is one data member named size. It will hold the current size of the linked list. A part from that three Node pointer are declared pointing to the Head Node, Current Node and the Last Current Node as their names suggest. Two friend functions are declared namely traverse() and addnodes().In the second part I'll show you the implementation of the List class and i'll finish with testing the Linked List in main method

Be the first to rate this post

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

Tags:

C++

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