# Blog Archives

## Smart Array (Linked List template implementation using C++)

As we have discussed about the Smart Array (Linked List) in our previous post and showed the Node class as well as the header file for the linked list. Below is the actual implementation of Linked List template in C++.

Any questions welcomed!

#include “list.h”

template <class T>

// in constructor we add a head node in the class as well as the cursor which we called current node.

// head node and current node set to null as there are no other nodes to point to.

List<T>::List()

{

headNode = new Node<T>();

headNode->setNext(NULL);

headNode->setLast(NULL);

currentNode = NULL;

size = 0;

}

//in destructor we do memory management as we did made nodes dynamically.

template <class T>

List<T>::~List ()

{

while (size != 0)

{

remove_node();

}

delete currentNode;

delete headNode;

}

// this function simply call remove_node function until all nodes are deleted.

template <class T>

bool List<T>::clear_list ()

{

while (size != 0)

{

remove_node ();

}

delete currentNode;

}

// this is the main addition of a node function.

template <class T>

void List<T>::add (T addObject)

{

Node<T> *newNode = new Node<T>();

newNode->set(addObject);

size ++;

if(currentNode != NULL) // in case we already have other nodes in the list

{

lastNode = currentNode;

newNode->setNext(currentNode->getNext()); // pointer will be adjusted for the new node.

if (newNode->getNext()!= NULL) // if the node is not at the end of the list.

{

currentNode = currentNode->getNext();

currentNode->setLast(newNode);

}

newNode->setLast(lastNode);

currentNode = newNode;

lastNode->setNext(newNode);

newNode->setIndex(size);

}

else // if this the very first node to be added in the list. this else part will be executed once in the life span of list.

{

newNode->setNext(NULL);

headNode->setNext(newNode);

newNode->setLast(headNode);

currentNode = newNode;

newNode->setIndex(size);

}

}

// this function return the current node value.

template <class T>

T List<T>::get()

{

if (currentNode != NULL)

{

return currentNode->get();

}if (currentNode == headNode) // if cursor is pointing towards head node. we cannot return anything as head node doesn’t

// have any value.

{

return false;

}

}

// move the cursor to next node only if the cursor is not reached on the last node.

template <class T>

bool List<T>::move_next()

{

if (currentNode->getNext() == NULL) return false ;

currentNode = currentNode->getNext();

}

//move the cursor to backwards only if the cursor is not reached till head node.

template <class T>

bool List<T>::move_back ()

{

if (currentNode->getLast() == headNode) return false;

currentNode = currentNode->getLast();

}

//move the cursor to very first node after head node, if there are any nodes in the list.

template <class T>

bool List<T>::move_first()

{

if (headNode->getNext()!=NULL)

currentNode = headNode->getNext();

}

//move the cursor to last node.

template <class T>

bool List<T>::move_last()

{

while (currentNode->getNext() != NULL)

{

currentNode= currentNode->getNext();

}

return currentNode;

}

//delete current node where cursor pointing to.

template <class T>

bool List<T>::remove_node()

{

Node<T> *temp = currentNode;

if (currentNode != NULL) // if cursor is actually pointing to a valid node

{

if (currentNode->getLast() == headNode) // if cursor is on the very first node after head node.

{

currentNode = headNode;

currentNode->setNext(NULL);

currentNode->setLast(NULL);

delete temp;

size–;

return true;

}else if (currentNode->getNext() == NULL) //if cursor is pointing to the very last node of the list.

{

move_back();

currentNode->setNext(NULL);

delete temp;

size–;

return true;

}else // if the cursor is not at the start or end of the list but somewhere in the middle.

{

move_back();

currentNode->setNext(temp->getNext());

move_next();

currentNode->setLast(temp->getLast());

delete temp;

size–;

return true;

}

}

}

//move the cursor to next node.

template <class T>

Node<T> List<T>::operator++()

{

move_next();

return *currentNode;

}

//move theh cursor to last node.

template <class T>

Node<T> List<T>::operator–()

{

move_back();

return *currentNode;

}

// add one node and assign rvalue to it. the name of list instance is used as lvalue.

template <class T>

Node<T> List<T>::operator=(T i)

{

add(i);

}

//cout object can be used to display list values.

template <class T>

std::ostream& operator << (std::ostream& leftOpr, List<T>& rhs)

{

leftOpr << rhs.get();

return leftOpr;

}

//cin can be used to add one node and assign it a value directing getting input from user.

template <class T>

std::istream& operator >> (std::istream& leftOpr, List<T>& rhs)

{

int i = 0;

leftOpr >> i;

rhs.add(i);

return leftOpr;

}

//return size of the list. number of nodes in the list.

template <class T>

int List<T>::get_size()

{

return size;

}

//return node value by index.

template <class T>

Node<T> List<T>::return_node(int i)

{

move_first();

while (currentNode->getNext() != NULL)

{

if (currentNode->getIndex() == i)

return *currentNode;

else

move_next();

}

}

//brackets can be used to access list elements by giving index as used in simple array.

template <class T>

Node<T> List<T>::operator[](int i)

{

return_node(i);

}

###### Related articles

- Data Structures implemantation in C++ (alikhuram.wordpress.com)
- Intersecting Linked Lists Faster (twistedoakstudios.com)
- doublelinkedBag (daniweb.com)

## Data Structures implemantation in C++

I have implemented data structures in C++ with some tweaks. These are custom made for some specific needs which I have. These might be interesting for anyone who is interested in data structures and learn how to implement them. There can be many methods which I can think of and can provide with these data structures but as I have said earlier, I wrote these for some specific needs. However, if someone is interested to know or want some added functionality, I can help!

Any questions are welcomed!

/* The Node class

A node is basic building block of many user define data structures.

Below node class is used to implement double linked list, stack and Queue data structures.

As this node class will become a part of a larger data structures, it doesn’t contain utility functions and operators.

this is a smart node, which also keeps track index of itself. as well as the address of last node

and the next node.

the class is implemented as template so can be instantiated with any data type.

Author: Khuram Ali

*/

template <class T>

class Node

{

public:

Node<T> (){object;} //a node constructor can be called with a value which needs to be stored.

T get() { return object; }; //getter function for out putting value stored in a node.

void set(T object) { this->object = object; }; //setting value of a node.

Node<T> *getNext() { return nextNode; }; // return a pointer to next node.

Node<T> *getLast(){return lastNode;}; // return a pointer to last node.

void setNext(Node<T> *nextNode) { this->nextNode = nextNode; }; // set a pointer to next node.

void setLast (Node<T> *lastNode) {this->lastNode = lastNode;};//set a pointer to last node.

void setIndex (int num) {this->index = num;}; // set index of node.

int getIndex () {return index;}; // return the index of node.

private:

T object;

int index = 0;

Node<T> *nextNode;

Node<T> *lastNode;

};

ifndef LIST_H_INCLUDED

#define LIST_H_INCLUDED

/* Smart Array header file:

this is a linked list implementation and I called it a smart array as you can do all, what you used to do with arrays,

with the flexibility of linked list. it is a double linked list so you can move forward and back easily. you can use

arithmetic operators as well as [] to access any node by its index. you can use iostream objects to print the elements on

the screen as you can do with normal array. though you can access any index of the list but if the list is big enough there is

a give n take on efficiency with ease of use. accessing a list element through index in a very large array is less efficient, if

the speed is what you want.

it is a template class so you can create a list of any data type.

Author Khuram Ali

*/

#include <iostream>

#include “node.cpp”

template <class T>

class List

{

public:

List();

~List();

void add (T); // function used to add an element (node) in the list.

T get(); //return current node value.

int get_size(); // return size of the list (number of nodes in a list)

bool move_next(); // move to next node and can go forward.

bool move_back (); // move to last node and can go backward.

bool move_first (); // move the cursor to first node.

bool move_last (); // move the cursor to last node.

bool remove_node (); // delete current node.

bool clear_list (); // delete all nodes in the list.

Node<T> return_node (int); // return node value by index.

Node<T> operator ++(); // move the cursor to next node.

Node<T> operator –(); // move the cursor to last node.

Node operator = (T); // add a new node and assign the rvalue to it.

Node<T> operator [] (int); // return node value by index.

template <class W>

friend std::ostream& operator << (std::ostream&, List<W>&); // printing node value on screen using cout.

template <class S>

friend std::istream& operator >> (std::istream&, List<S>&); // getting value from user from cin and add a new node

// with that value.

private:

int size;

Node<T> *headNode;

Node<T> *currentNode;

Node<T> *lastNode;

};

#endif // LIST_H_INCLUDED

/** implementation of List data structure will be added shortly.

###### Related articles

- Top 15 Data Structures and Algorithm Interview Questions for Java programmer – Answers (javarevisited.blogspot.com)
- Data Warehouse Dilemma | Solving Real Problems | Domo | Blog (domo.com)
- basic applications of data structure (stackoverflow.com)
- Help with implementing a linked list as an array in JAVA (daniweb.com)
- Why do we need pointers/references? (mortoray.com)