Working with Linked List Data Structures
Posted by mark123
Last Updated: May 04, 2013
//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;
}