The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Wednesday, May 9, 2012

Ordered List


#include<iostream.h>
#include<conio.h>

struct list
{
 int node;
 struct list *nxt;
};

class linked_list
{
 public:
  struct list *start;
  int nol;
   linked_list();
  ~linked_list();
  void insert_node();
  void delete_node();
  void display_list();
};

void linked_list :: linked_list()
{
  nol = 0;
  start = NULL;
}

void linked_list :: ~linked_list()
{
  nol = 0;
  struct list *nptr;
  nptr = start;
  start = NULL;
  delete nptr;
}

void linked_list :: insert_node()
{
 struct list *nptr;
 nptr = new list;
 cout<<"\n Enter the New Node to be Inserted : ";
 cin>>nptr->node;
 nptr->nxt = NULL;

 if(start == NULL)
  start = nptr;
 else
 {
  nptr->nxt = start;
  start = nptr;
 }
 nol += 1;
 cout<<"\n The Node has been Inserted into the List ";
 getch();
}


void linked_list :: delete_node()
{
 if(nol == 0)
  cout<<"\n WARNING !! No more Nodes in the List \n";
 else
 {
 struct list *loc,*locp,*nptr;
 int info;
 cout<<"\n Enter the Node to be Deleted : ";
 cin>>info;
 locp = NULL;
 loc = start;
 while(loc->node != info && loc != NULL)
 {
   locp = loc;
   loc = loc->nxt;
 }
 if(loc == NULL)
  cout<<"\n The Node is not present in the List ";
 else
 {
  nptr = new list;
  if(loc == start)
  {
   nptr = start;
   start = start->nxt;
  }
  else
  {
   nptr = loc;
   locp->nxt = loc->nxt;
  }
  nol -= 1;
  delete nptr;
  cout<<"\n The Node has been Deleted from the List ";
 }
 }
 getch();
}


void linked_list :: display_list()
{
 struct list *ptr;
 cout<<"\n The Linked List : \n\t";
 ptr = start;
 while(ptr != NULL)
 {
  cout<<ptr->node<<" -> ";
  ptr = ptr->nxt;
 }
 cout<<"NULL\n";
 cout<<"\n The Numer of Nodes in this List : "<<nol;
 getch();
}

class ord_linked_list : public linked_list
{
 public:
  ord_linked_list();
  ~ord_linked_list();
  void insert_node();
  void delete_node();
  void display_list();
};

void ord_linked_list :: ord_linked_list()
{
  linked_list::linked_list();
}

void ord_linked_list :: ~ord_linked_list()
{
  linked_list::~linked_list();
}

void ord_linked_list :: insert_node()
{
 struct list *nptr;
 nptr = new list;
 cout<<"\n Enter the New Node to be Inserted : ";
 cin>>nptr->node;
 nptr->nxt = NULL;

 if(start == NULL)
  start = nptr;
 else
 {
  struct list *loc,*locp;
  locp = NULL;
  loc = start;
  while(loc != NULL && loc->node <= nptr->node)
  {
   locp = loc;
   loc = loc->nxt;
  }
  if(loc == NULL)
   locp->nxt = nptr;
  else if(loc == start)
  {
   nptr->nxt = start;
   start = nptr;
  }
  else
  {
   locp->nxt = nptr;
   nptr->nxt = loc;
  }
 }
 nol += 1;
 cout<<"\n The Node has been Inserted into the List ";
 getch();
}


void ord_linked_list :: delete_node()
{
 linked_list::delete_node();
}


void ord_linked_list :: display_list()
{
 linked_list :: display_list();
}

void main()
{
 int cho;
 linked_list ll;
 linked_list();
 do
 {
 clrscr();
 cout<<"\n.. UNORDERED LINKED LIST MENU ..\n";
 cout<<"\n 1. Insert a node";
 cout<<"\n 2. Delete a node";
 cout<<"\n\n  Enter an option : ";
 cin>>cho;
 switch(cho)
 {
  case 1 :  ll.insert_node();
   break;
  case 2 :  ll.delete_node();
   break;
  default:  break;
 }
 ll.display_list();
 }while(cho == 1 || cho == 2);
 getch();


 clrscr();
 ord_linked_list oll;
 ord_linked_list();
 do
 {
 clrscr();
 cout<<"\n.. ORDERED LINKED LIST MENU ..\n";
 cout<<"\n 1. Insert a node";
 cout<<"\n 2. Delete a node";
 cout<<"\n\n  Enter an option : ";
 cin>>cho;
 switch(cho)
 {
  case 1 :  oll.insert_node();
   break;
  case 2 :  oll.delete_node();
   break;
  default:  break;
 }
 oll.display_list();
 }while(cho == 1 || cho == 2);
 getch();
}

No comments:

Post a Comment