Working with Linked List Data Structures
Posted by mark123
Last Updated: May 04, 2013
  2536

//Node File
#include "Data.h"

data::data()
{
num=0;
next=NULL;
}
data::data(int N, data * nex)
{
num=N;
next=nex;
}
data::~data()
{
std::cout<<this->getNum()<<"has been deleted"<<std::endl;
}
void data::setNum(int d)
{
num=d;
}
int data::getNum()const
{
return num;
}
void data::setNext(data * nex)
{
next=nex;
}
data * data::getNext()const
{
return next;
}
void data::display()
{
std::cout<<"The number is:"<<num<<std::endl;
}

//Linked List File

List::List()
{
head=NULL;
}
List::List(data * h)
{
head=h;
}
List::~List()
{
std::cout<<"List object removed"<<std::endl;
}
bool List::insertfront(int d)
{

data * newNode=new data(d,NULL);
if(head==NULL)
{
head=newNode;
return true;
}
else
{
     newNode->setNext(head);
head=newNode;
return true;
}


}
bool List::insertBack(int d)
{
data * h;
h=head;
data * newNode=new data(d,NULL);
if(head==NULL)
{
head=newNode;
return true;
}
else
{
while(h->getNext()!=NULL)
{
h=h->getNext();
}
h->setNext(newNode);
return true;
}
}
bool List::insertMiddle(int d)
{
data * h;
h=head;
data * newNode=new data(d,NULL);
if(h==NULL)
{
head=newNode;
return true;
}
else
{
while(h->getNext()!=NULL && h->getNum()<=newNode->getNum())
{
h=h->getNext();
}
newNode->setNext(h->getNext());
h->setNext(newNode);
return true;
}

}
void List::display()
{
data * h;
h=head;
if(head==NULL)
{
      std::cout<<"Nothing to display"<<std::endl;
}
else
{
std::cout<<"The list is:---->";
while(h!=NULL)
{
std::cout<<h->getNum();
h=h->getNext();
}

    }
}
bool List::deleteFront()
{
data * tmp=head;
if(head==NULL)
{
std::cout<<"The List is Empty"<<std::endl;
return false;
}
else
{
head=head->getNext();
delete tmp;
return true;
}
}
bool List::deleteBack()
{
data * prev;
data * curr=head;
if(head==NULL)
{
std::cout<<"THe list is Empty"<<std::endl;
return false;
}
else
{
while(curr->getNext()!=NULL)
{
        prev=curr;
curr=curr->getNext();
}
prev->setNext(NULL);
delete curr;
return true;
}



}
bool List::deleteMiddle(int target)
{
data * prev=NULL;
data * curr=head;
if(head==NULL)
{
std::cout<<"The list is Empty"<<std::endl;
return false;
}
else
{
while(curr->getNext()!=NULL&&curr->getNum()!=target)
{
prev=curr;
curr=curr->getNext();
}

if(curr==head)
{
head=head->getNext();
delete curr;
return true;
}
else
{
prev->setNext(curr->getNext());
   delete curr;
    return true;
}
if(curr->getNum()!=target)
{
std::cout<<target<< " could not be found"<<std::endl;
return false;
}

}







}
// Driver File
#include "List.h"

int menu();
using namespace std;
int main()
{
int get;
int num;
List * data=new List();
while((get=menu())!=7)
{
switch(get)
{
case 1:cout<<"Enter a number:";
  cin>>num;
  data->insertfront(num);
  cout<<"showing entry:"<<endl;
      data->display();
  system("pause");
  system("cls");
  break;
case 2: cout<<"Enter a number:";
cin>>num;
data->insertBack(num);
 cout<<"showing entry:"<<endl;
 data->display();

system("pause");
system("cls");
break;
case 3: cout<<"Enter a number:";
  cin>>num;
  data->insertMiddle(num);
  cout<<"showing entry:"<<endl;
      data->display();
  system("pause");
  system("cls");
break;
case 4: data->deleteFront();
cout<<"showing entry:"<<endl;
   data->display();
system("pause");
  system("cls");
  break;

case 5: data->deleteBack();
 cout<<"showing entry:"<<endl;
    data->display();
 system("pause");
  system("cls");
  break;
case 6: cout<<"Enter the number you wish to delete:";
cin>>num;
data->deleteMiddle(num);
cout<<"showing entry:"<<endl;
data->display();
system("pause");
  system("cls");
  break;
case 7: cout<<"Thank you for using the system. Goodbye"<<endl;
    exit(0);
  break;
default: cout<<" Incorrect entry. Please enter a correct entry to continue";
         system("pause");
system("cls");
break;
}

}


system("pause");
return 0;
}
int menu()
{
int opt;
cout<<"Welcome to the data system. Enter option."<<endl;
cout<<"1. Enter at Front"<<endl;
cout<<"2.Enter at Back"<<endl;
cout<<"3. Enter in Middle"<<endl;
cout<<"4. Delete Front"<<endl;
cout<<"5.Delete Back"<<endl;
cout<<"6. Delete Middle"<<endl;
cout<<"7. Exit"<<endl;
cout<<"Enter option:";
cin>>opt;
return opt;
}



   
  
 
 
   

 
 
Forgive me for not commenting the code, i just wrote something quickly out of my head and so i did not comment so i will give a quick run down of the code. Firstly you can think of a linked list as a dynamic array, in that during runtime it can grow and shrink in comparison with an array which is static meaning the data size is fixed. Linked List are very useful as they allow you the luxury of not worrying about space eg. you build an application currently the program stores info on 20 individuals and there is an approximation size for future storage. instead of using an array and wasting space, a linked list would be ideal as it can dynamically shrink and grow in size.
 
Ok now to the code, basically in order to have a linked list you have to have a class known as a self referential class,meaning that the class has a pointer is of the same type as the class, this is a derivation from the self- referential structures where the same ideology holds true. So in the Node File we have a int type to represent the data and a pointer to represent the link.  In the Linked List File. we declare a pointer of the class  data as head. This head pointer will represent the beginning of the list. The operations include addFront(),addBack(),addMiddle(), deleteFront(),deleteBack(),deleteMiddle(), display().
 
This program is going to be really beneficial to me in the future!  #Respect.