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);

}

The Date Class

I am using a low level of complexity to show the working of general C++ classes, operator overloading and data hiding/encapsulation concepts. Below is the header file of Date Class. complete implementation will follow. However, Complete code of Date class can be used freely.

Questions/comments welcomed..!

Header File: date.h

#ifndef DATE_H
#define DATE_H

#include <iostream>

class Date
{
private:
short day;
short month;
short year;
short daysOfMonth(Date d);    /**returns the no of days in a month*/
static const short daysInMonth[]; /** array containing the 12 month’s days*/
bool leapYear (short);        /** tells the year is leap year or not*/

public:
/**constructor with default arguments*/
Date(short d=1, short m = 1, short y = 1900);

void display (); /** Display the date on the screen*/
void setDate (short, short, short); /** set the date with given arguments*/
Date operator ++(); /**pre increment operator used as ++date1*/
Date operator +(short); /**plus operator used as date1 + 5 */
Date operator –(); /**pre decrement operator used as –date1 */
Date operator -(short); /**decrement operator used as date1 – 5 */
short operator – (Date); /** return number of days between two dates */
Date operator +=(short); /** add short in left hand operand and return the same. */
Date operator -=(short); /** decrement short in left hand operand and return the same. */
short operator -=(Date); /** decrement Date object in left hand operand and return the same. */
friend Date operator – (short, Date); /**decrement operator used as 5 – date1 */

/**logical comarison operators*/
bool operator < (Date);
bool operator > (Date);
bool operator <=(Date);
bool operator >= (Date);
bool operator == (Date);
bool operator != (Date);

/** Stream insertion operators to get input and output with simple, cin and cout. */
friend std::ostream& operator << (std::ostream&, Date&);
friend std::istream& operator >> (std::istream&, Date&);

};

#endif // DATE_H