You are on page 1of 9

#ifndef LINKEDLIST_H #define LINKEDLIST_H #include

<iostream> using namespace std; //the const //the const size

of the pool of free nodes int LIST_CAPACITY=10; null value is

a value that does not correspond to any index in the list int

NULL_VALUE=-1;

//using a template class template <typename

DataType> class LinkedList { //using a data structure to

identify nodes this data structure contains 2 data members

//one to identify the data part and the other to identify the

link part struct NodeType { DataType data;//variable data of

type DataType(template) int next;//the link part is declared

as an integer that contains the index of the next node };

public: LinkedList();//default constructor bool Full() {

return size==LIST_CAPACITY;} bool Empty() { return size==0;}

void Insert(DataType, int=1);//insert function prototype. The

default value for the position is 1 void Delete(int);//delete

function prototype void print();//print function prototype

private: NodeType listArray[LIST_CAPACITY];//list array of

node type int free;//free is used to identify the first free

node int first;//first is used to link to the first node in

the list int size; int getPreLink(int, char); };//end class

declaration //implementation of class LinkedList //

Constructor: used to create an empty linked list and pool of

free nodes template <typename DataType>//template functions

LinkedList<DataType>::LinkedList() { int i;

first=NULL_VALUE;//initializing first to null(empty list)

free=0; size =0; //intialize pool of free new nodes for (i=0;

i < LIST_CAPACITY -1; i++) listArray[i].next = i+1;


listArray

[i].next = NULL_VALUE; } // end of constructor // This

function traverses the linked list until the predecessor node

is found. // The function takes as input the position of the

node and the type of the operation (i for insert and d for

delete). // In the case of insert a position larger than the

size by one is valid while in the case of delete it is not.

template <typename DataType> int

LinkedList<DataType>::getPreLink(int pos, char oper) { int

plink = first;//an integer to point to first element of list

if (pos == 1) // the new node will be at position 1 (i.e

before the current first node) return NULL_VALUE; // no

predecessor in this case //if operation is insert then

position = size+1 is valid for insertion although the

position does not exist in the list else if (oper == 'i' &&

(pos<1 || pos > size+1)) //if operation is insert and the

position is invalid return -2; // symbol for invalid position

//if operation is delete then position = size+1 is invalid

for deletion else if (oper == 'd' && (pos<1 || pos > size))

//if operation is delete and the position is invalid (does

not exist) return -2; // symbol for invalid position // note

that in the following else the if and its conditions are not

necessary and are written only to make the code // easier to

understand. // when pos== size +1 it means that the position

does not currently exist, and in this case // the new node

will be right after the last node in the case of insertion //

and when pos>1&&pos<=size it means that the position is in

the middle else if (oper == 'i' && (pos == size+1 ||

(pos>1&&pos<=size)) ) for (int i=1; i<pos-1; i++) plink =


listArray[plink].next; //move to next node else if (oper ==

'd' && (pos>1&&pos<=size) ) for (int i=1; i<pos-1; i++) plink

= listArray[plink].next; //move to next node return plink; //

link to predecessor node } // end function getPrelink //

function used to insert a node into a linked list at position

position template <typename DataType> void

LinkedList<DataType>::Insert(DataType val, int position) {

int newlink,link,prelink; if(first==NULL_VALUE) //first

insertion (list had no nodes before) { newlink = free;

listArray[newlink].data = val; //setting the data field to

the value given


first=newlink; //setting first to the inserted

node because it is the first node free = listArray

[free].next; //next free node listArray[first].next =

NULL_VALUE; //setting the link part to null(only one node in

the list) size++; } else//2nd insertion or above (list had

one or more existing nodes) { if(!Full())//if list is not

full { prelink = getPreLink(position,'i'); // prelink links

to the predecessor node link = listArray[prelink].next; //

link links to the node at position "position" newlink = free;

// get a link to a free node if (prelink == -2) // position

does not exist cout<< "\n\aYou have entered an invalid

position. Insert operation failed." << endl; else if (prelink

== NULL_VALUE) // insert at the beginning { listArray

[newlink].data = val;//setting data field to the value given

free = listArray[free].next;//next free node listArray

[newlink].next = first;//setting the link part to the first

node first=newlink;//setting the first to link to the new

node size++; // increment list size } else // insert at the

end or in the middle { listArray[newlink].data =

val;//setting data field to the value given free = listArray

[free].next;//next free node listArray[newlink].next =

link;//setting the link part to the link of the previous node

listArray[prelink].next = newlink;//setting the link of

previous link to point to the new node size++; // increment

list size } } // end if (!Full()) else//if list is full

cerr<<"\n\n\a\aThe list is full. Insert operation

failed."<<endl; } // end else } // end function Insert //

Function used to delete a node from a linked list at position

position template <typename DataType> void


LinkedList<DataType>::Delete(int position) { int

link,prelink; if(!Empty())//if list is not empty {


prelink =

getPreLink(position,'d'); // prelink links to the predecessor

node if (prelink == -2) // position does not exist cout<<

"\n\aYou have entered an invalid position. Insert operation

failed." << endl; else if (prelink == NULL_VALUE) // delete

at the beginning { link = first; // link links to the first

node first = listArray[link].next; // first now links to the

second node listArray[link].next= free; // connect the

deleted node to the first node in the list of free nodes free

= link; // set the deleted node to be the first node in the

list of free nodes size--; // decrement list size } else //

delete at the end or in the middle { link = listArray

[prelink].next; // link links to the node at position

"position" listArray[prelink].next = listArray[link].next; //

predecessor node now links to the successor node of // the

node to be deleted listArray[link].next= free; // connect the

deleted node to the first node in the list of free nodes free

= link; // set the deleted node to be the first node in the

list of free nodes size--; // decrement list size } } // end

if empty else cerr<<"\n\n\a\aThe list is empty. Delete

operation failed."<<endl; }// end of function delete //

Function used to print all elements in the linked list

template <typename DataType> void

LinkedList<DataType>::print() { int link = first;//an integer

to point to first element of list cout<<"\nYour list is now

as follows:"<<endl; while(link!=NULL_VALUE)//traversing the

list to print it { cout<<listArray[link].data<<" "; link =

listArray[link].next;//increment link } cout <<endl<<endl;

cout << "The list has " << size << " element(s) now." <<
endl; } // end of function print #endif

You might also like