Blog Archives

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
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.

T object;
int index = 0;
Node<T> *nextNode;
Node<T> *lastNode;


/* 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
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.

int      size;
Node<T> *headNode;
Node<T> *currentNode;
Node<T> *lastNode;


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