Monday 6 August 2012

In a linked list print odd nodes first and then even nodes and also sort a linked list


#include<iostream.h>
#include<process.h>
#include<conio.h>
class node
{
public:
      node *next;
      int data;
      node()
      {
            next=NULL;
      }
}*start=NULL;
void display()
{
      if(start==NULL)
      {
            cout<<"empty list";
            return;
      }
      node *ptr=start;
      while(ptr!=NULL)
      {
            cout<<"  "<<ptr->data;
            ptr=ptr->next;
      }
}

void insert(int item)
{
      node *newnode = new node;
      newnode->data=item;
      if(start==NULL)
      {
            start=newnode;
            return;
      }
      node *ptr=start;
      while(ptr->next!=NULL)
            ptr=ptr->next;
      ptr->next=newnode;
}

void del(int item)
{
      if(start->data==item)
      {
            start=start->next;
            return;
      }
      node *pre=NULL,*ptr=start;
      while(ptr!=NULL)
      {
            if(ptr->data==item)
            {
                  pre->next=ptr->next;
                  return;
            }
            pre=ptr;
            ptr=ptr->next;
      }
      cout<<"ITEM NOT IN LIST";
}

void sort()
{
      node *min,*parmin;
      for(node *ptr1=start,*parptr1=NULL;ptr1->next!=NULL;)
      {
            min=ptr1;
            for(node *ptr2=ptr1->next,*save=ptr1;ptr2!=NULL;)
            {
                  if(ptr2->data<min->data)
                  {
                        min=ptr2;
                        parmin=save;
                  }
                  save=ptr2;
                  ptr2=ptr2->next;
            }
            if(min!=ptr1)
            {
                  parmin->next=min->next;
                  min->next=ptr1;
                  if(parptr1==NULL)
                  {
                        start=min;
                        parptr1=min;
                  }
                  else
                  {
                        parptr1->next=min;
                        parptr1=min;
                  }

            }
            else
            {
                  parptr1=ptr1;
                  ptr1=ptr1->next;
            }
      }
}
void main()
{
      clrscr();
      cout<<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.ALL ODD FIRST\n";
      cout<<"5.SORT\n6.REVERSE\n7.ALTERNATE REVERSE\n8.Nth ELEMENT FROM LAST\n9.Exit\n";
      int ch,item;
      while(1)
      {
            cout<<"\nchoice? ";
            cin>>ch;
            switch(ch)
            {
                  case 1:     cout<<"ITEM TO INSERT";
                        cin>>item;
                        insert(item);
                        break;
                  case 2:     cout<<"ITEM TO DELETE";
                        cin>>item;
                        del(item);
                        break;
                  case 3:     display();
                        break;
                  case 4:// sort();
                        evenodd();
                        display();
                        break;
                  case 5: sort();
                        display();
                        break;
                  case 6: //node *ptr=reverse(start);
                        //    ptr->next=NULL;
                        i_reverse();
                        break;
                  case 7: kreverse(4);
                        //altreverse();
                        break;
                  case 8: int n;
                        cout<<" ENTER N ";
                        cin>>n;
                        fromlast(n);
                        break;
                  case 9:     exit(0);
            }
      }
}

No comments:

Post a Comment