Showing posts with label programs of linklist. Show all posts
Showing posts with label programs of linklist. Show all posts

program to insert an element in the queue

Tuesday, September 1, 2009

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
 clrscr();
 int a[20],front,rare,item,m;
 char c,y,n,str[2];
 cout<<"enter the number of elements(max 20): ";
 cin>>m;
 cout<<"enter the elements"<<endl;
 for(int i=0;i<m;i++)
 cin>>a[i];
 cout<<"enter the element to be inserted: ";
 cin>>item;
 front=0;
 rare=m-1;
 if(front==0&&rare==19)
 { cout<<"over flow";
   exit; }
 else
 { if(rare==19)
   rare=0;
   else
   rare=rare+1;
  }
  a[rare]=item;
  cout<<"\nqueue after insertion the element is"<<endl;
  for(i=front;i<=rare;i++)
  cout<<a[i]<<endl;
  cout<<endl;
  cout<<"Are you want to delete an item"<<endl<<endl;
  cout<<"         if you want to delete one element than press y"<<endl;
  cout<<"         if you want to exit than press n"<<endl<<endl<<endl;
  cout<<"                                      Press(y\\n)";
  dhami:
  c=getch();
  if(c=='n')
  exit;
  if(c=='y')
  { if(front==0&&rare==-1)
    cout<<"\nqueue is already empty";
    else
    front=front+1;
    cout<<"\nqueue after deletion the element is"<<endl;
    for(int i=front;i<=rare;i++)
    cout<<a[i]<<endl;
    getch();
    exit;
   }

   }

program to show inserting and deleting from a queue

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
 clrscr();
 int a[20],front,rare,item,m;
 char c,y,n,str[2];
 cout<<"enter the number of elements(max 20): ";
 cin>>m;
 cout<<"enter the elements"<<endl;
 for(int i=0;i<m;i++)
 cin>>a[i];
 cout<<"enter the element to be inserted: ";
 cin>>item;
 front=0;
 rare=m-1;
 if(front==0&&rare==19)
 { cout<<"over flow";
   exit; }
 else
 { if(rare==19)
   rare=0;
   else
   rare=rare+1;
  }
  a[rare]=item;
  cout<<"queue after insertion the element is"<<endl;
  for(i=front;i<=rare;i++)
  cout<<a[i]<<endl;
  cout<<endl;
  cout<<"Are you want to delete an item"<<endl<<endl;
  cout<<"         if you want to delete one element than press y"<<endl;
  cout<<"         if you want to exit than press n"<<endl<<endl<<endl;
  cout<<"                                      Press yes\\no(y\\n)";
  cin>>c;
  if(c==110)
  exit;
  if(c==121)
  { if(front==0&&rare==-1)
    cout<<"queue is already empty";
    else
    front=front+1;
    cout<<"queue after deletion the element is"<<endl;
    for(int i=front;i<=rare;i++)
    cout<<a[i]<<endl;
    getch();
   }
   }

program to enter the name and email of students using link list

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
 int roll,fee;
 char name[20],email[20];
 public:
 void getdata();
 void show();
};
 void student::getdata()
  {
    cout<<"enter roll no. "<<endl;
    cin>>roll;
    cout<<"enter name  "<<endl;
    gets(name);
    cout<<"Enter email "<<endl;
    gets(email);
    cout<<"Enter fee "<<endl;
    cin>>fee;
  }
 void student::show()
  {
   cout<<"roll num" << roll<<endl;
   cout<<"name is "<<name<<endl;
   cout<<"email "<<email<<endl;
   cout<<"fee "<<fee<<endl;
  }
 struct node
{
 student info;
 node *link;
};

  class linkedlist
   {
    node *start;
    public:
    linkedlist()
    {
     start=NULL;
    }
    void insertbeg(student);
    void display();
   };

     void linkedlist::insertbeg(student a)
     {
       node *p=new node;
       p->info=a;
       p->link=start;
       start=p;
     }
     void linkedlist:: display()
      {
       node *p=start;
       student n;
       while(p!=NULL)
    {
     n=p->info;
     n.show();
     p=p->link;
     cout<<endl<<endl;
    }
      }

 void main()
  {
   clrscr();
   student m1,m2,m3;
   m1.getdata();
   m2.getdata();
   m3.getdata();
   linkedlist l1;
   l1.insertbeg(m1);
   l1.insertbeg(m2);
   l1.insertbeg(m3);
   cout<<endl<<endl<<endl;
   l1.display();
   getch();
  }

program to remove duplicate nodes from the link list

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insert(int);
  void duplicate();
  void show();
 };

 void linklist::insert(int a)
 {  node *p=new node;
    p->info=a;
    if(start==NULL)
    { start=p;
      p->link=NULL;
    }
     else
     {  node *q=start;
    while(q->link!=NULL)
    { q=q->link;
    }
    q->link=p;
    p->link=NULL;
     }

  }

  void linklist::duplicate()
  {
     node *p1,*p2,*q1;
     p1=start;
     p2=p1->link;
     while(p2!=NULL)
     {  q1=start;
    while(q1!=p2)
    {  if(q1->info==p2->info)
       {   p1->link=p2->link;
           p2->link=NULL;
           delete p2;
           p2=p1->link;
           break;
        }
        q1=q1->link;
    }
       p2=p2->link;
       p1=p1->link;
     }
  }









   void linklist::show()
   {
      node *p=start;
      while(p!=NULL)
      {   cout<<p->info<<endl;
      p=p->link;
      }
    }
  void main()
  {
     clrscr();
     linklist l1;
     l1.insert(10);
     l1.insert(20);
     l1.insert(10);
     l1.insert(40);
     l1.insert(50);
     l1.insert(20);
     l1.show();
     getch();
     l1.duplicate();
     l1.show();
     getch();

   }

program to search a node by value and replace it with the next node

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insert(int);
  void swap(int);
  void show();
 };

 void linklist::insert(int a)
 {  node *p=new node;
    p->info=a;
    if(start==NULL)
    { start=p;
      p->link=NULL;
    }
     else
     {  node *q=start;
    while(q->link!=NULL)
    { q=q->link;
    }
    q->link=p;
    p->link=NULL;
     }

  }

  void linklist::swap(int a)
  {
     node *p1,*p2,*p3;
     p1=start;
     p2=p1->link;
     p3=p2->link;

     if(p1->info==a)
     {  start=p2;
    p2->link=p1;
    p1->link=p3;
    return;
     }

     while(p2!=NULL)
     {  if(p2->info==a)
    {  p1->link=p2->link;
       p2->link=p3->link;
       p3->link=p2;
       return;
    }
    p1=p1->link;
    p2=p2->link;
    p3=p3->link;
     }
     cout<<"there is no node at this value"<<endl;
  }








   void linklist::show()
   {
      node *p=start;
      while(p!=NULL)
      {   cout<<p->info<<endl;
      p=p->link;
      }
    }
  void main()
  {
     clrscr();
     int p;
     linklist l1;
     l1.insert(10);
     l1.insert(20);
     l1.insert(30);
     l1.insert(40);
     l1.insert(50);
     l1.show();
     getch();
     cout<<"\nenter the value which u want to swap with next node: ";
     cin>>p;
     l1.swap(p);
     l1.show();
     getch();
     cout<<"\nenter the value again: ";
     cin>>p;
     l1.swap(p);
     l1.show();
     getch();
   }

program to reversea link list

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insert(int);
  void reverse();
  void show();
 };

 void linklist::insert(int a)
 {  node *p=new node;
    p->info=a;
    if(start==NULL)
    { start=p;
      p->link=NULL;
    }
     else
     {  node *q=start;
    while(q->link!=NULL)
    { q=q->link;
    }
    q->link=p;
    p->link=NULL;
     }

  }

  void linklist::reverse()
  {   if(start==NULL)
      {   cout<<"there is no node";
      return;
      }

      node *p1,*p2,*p3,*p4;
      p1=start;
      p2=p1->link;
      p3=p2->link;

      if(p1->link==NULL)
      {   cout<<"there is only one node";
      return;
      }
      p1->link=NULL;

      while(p2!=NULL)
      {

     if(p2->link==NULL)
     {    p2->link=p1;
          start=p2;
          return;
     }
     p4=p3->link;

     if(p3->link==NULL)
     {   p3->link=p2;
         p2->link=p1;
         start=p3;
         return;
      }

     p3->link=p2;
     p2->link=p1;
     p1=p3;
     p2=p4;
     p3=p4->link;
      }
    }





   void linklist::show()
   {
      node *p=start;
      while(p!=NULL)
      {   cout<<p->info<<endl;
      p=p->link;
      }
    }
  void main()
  {
     clrscr();
     linklist l1;
     l1.insert(10);
     l1.insert(20);
     l1.insert(30);
     l1.insert(40);
     l1.insert(50);
     l1.show();
     getch();
     l1.reverse();
     l1.show();
     getch();
   }

program to remove alternate nodesin the link list

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insert(int);
  void remove();
  void show();
 };

 void linklist::insert(int a)
 {  node *p=new node;
    p->info=a;
    if(start==NULL)
    { start=p;
      p->link=NULL;
    }
     else
     {  node *q=start;
    while(q->link!=NULL)
    { q=q->link;
    }
    q->link=p;
    p->link=NULL;
     }

  }

  void linklist::remove()
  { node *p1=start;
    node *p2=start->link;
    while(p1->link!=NULL)
    {  p1->link=p2->link;
       p1=p1->link;
       p1=p2->link;
       p2=p1->link;
    }




  }
   void linklist::show()
   {
      node *p=start;
      while(p!=NULL)
      {   cout<<p->info<<endl;
      p=p->link;
      }
    }
  void main()
  {
     clrscr();
     linklist l1;
     l1.insert(10);
     l1.insert(20);
     l1.insert(30);
     l1.insert(40);
     l1.insert(50);
     l1.insert(60);
     l1.insert(70);
     l1.show();
     getch();
     l1.remove();
     l1.show();
     getch();
   }

program which domonstrate the use of tree

#include<i-add.h>
class node
{       public:
    int left;
    int right;
    int data;
    node()
    {    data=left=right=NULL;
    }
}tree[21];
void input(int &index,int &item)
{    tree[index].data=item;
    tree[index].left=2*index;
    tree[index].right=2*index+1;
}
void push()
{       clrscr();
    int index=1,temp;
    cout<<"\n\n\n\tEnter the element: ";
    cin>>temp;
    if(tree[1].data==NULL)
    {    input(1,temp);
        return;
    }
    while(index<=20)
    {       if(tree[index].data!=NULL)
        {    if(temp<tree[index].data)
            {    index=tree[index].left;
                continue;
            }
            if(temp>tree[index].data)
            {    index=tree[index].right;
                continue;
            }
            if(temp==tree[index].data)
            {    cout<<"\n\n Sorry this element already exists";
                bioskey(0);
                return;
            }
        }
        else
        {    input(index,temp);
            break;
        }
        cout<<" Index="<<index<<"\n";
    }
}
void show()
{       clrscr();
    cout<<"Index\tLeft\tData\tRight\n";
    for(int i=1;i<=20;i++)
    {    cout<<i<<"\t  "<<tree[i].left<<"\t  "<<tree[i].data<<"\t  "<<tree[i].right<<endl;
    }
    bioskey(0);
}
void main()
{    int choice=0;
    while(choice!=4)
    {       clrscr();
        gotoxy(35,10);
        cout<<"1. Insert";
        gotoxy(35,11);
        cout<<"2. Delete";
        gotoxy(35,12);
        cout<<"3. Show";
        gotoxy(35,13);
        cout<<"4. EXIT";
        gotoxy(35,15);
        cout<<"Your choice: ";
        cin>>choice;

        switch(choice)
        {    case 1:    push();
                break;
            case 2:    //pop();
                break;
            case 3:    show();
                break;
        }
    }
}

program to search a node by value and remove it

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insert(int);
  void searchmov(int);
  void show();
 };

 void linklist::insert(int a)
 {  node *p=new node;
    p->info=a;
    if(start==NULL)
    { start=p;
      p->link=NULL;
    }
     else
     {  node *q=start;
    while(q->link!=NULL)
    { q=q->link;
    }
    q->link=p;
    p->link=NULL;
     }

  }

  void linklist::searchmov(int a)
  { int count,count1;
    count=0;
    node *p=start;
    while(p!=NULL)
    {
       if(p->info==a)
       {  node *g=p;
      p=g->info;
      g->info=NULL;
      delete g;
      count++;
       }
    p=p->link;
    }
    if(count==0)
     cout<<"There is no node at this value"<<endl;


  }
   void linklist::show()
   {
      node *p=start;
      while(p!=NULL)
      {   cout<<p->info<<endl;
      p=p->link;
      }
    }
  void main()
  {
     clrscr();
     int f;
     cout<<"enter the value of node which u want 2 remove"<<endl;
     cin>>f;
     linklist l1;
     l1.insert(10);
     l1.insert(20);
     l1.insert(30);
     l1.searchmov(f);
     l1.show();
     getch();
   }

program to search a node by value and place a node after it

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insert(int);
  void searchsrt(int,int);
  void show();
 };

 void linklist::insert(int a)
 {  node *p=new node;
    p->info=a;
    if(start==NULL)
    { start=p;
      p->link=NULL;
    }
     else
     {  node *q=start;
    while(q->link!=NULL)
    { q=q->link;
    }
    q->link=p;
    p->link=NULL;
     }

  }

  void linklist::searchsrt(int a,int b)
  { int count,count1;
    count=0;
    node *p=start;
    while(p!=NULL)
    {
       if(p->info==a)
       {  node *q=new node;
      q->info=b;
      q->link=p->link;
      p->link=q;
      count++;
       }
    p=p->link;
    }
    if(count==0)
     cout<<"There is no node at this value"<<endl;


  }
   void linklist::show()
   {
      node *p=start;
      while(p!=NULL)
      {   cout<<p->info<<endl;
      p=p->link;
      }
    }
  void main()
  {
     clrscr();
     int f,g;
     cout<<"enter the value of node and info part of naw node respectively"<<endl;
     cin>>f>>g;
     linklist l1;
     l1.insert(10);
     l1.insert(20);
     l1.insert(30);
     l1.searchsrt(f,g);
     l1.show();
     getch();
   }

program to search a node by value

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insert(int);
  void search(int);
 };

 void linklist::insert(int a)
 {  node *p=new node;
    p->info=a;
    if(start==NULL)
    { start=p;
      p->link=NULL;
    }
     else
     {  node *q=start;
    while(q->link!=NULL)
    { q=q->link;
    }
    q->link=p;
    p->link=NULL;
     }
      p=start;
      while(p!=NULL)
      { cout<<p->info<<endl;
    p=p->link;
      }
  }

  void linklist::search(int a)
  { int count,count1;
    count=count1=0;
    node *p=start;
    while(p!=NULL)
    {  count1++;
       if(p->info==a)
       {  count++;
      cout<<"this value occured at "<<count1<<"th node"<<endl;
       }
    p=p->link;
    }
    if(count==0)
    cout<<"There is no node at this value"<<endl;

  }
  void main()
  {
     clrscr();
     linklist l1;
     l1.insert(5);
     l1.insert(20);
     l1.insert(30);
     getch();
     l1.search(40);
     getch();
     l1.search(20);
     getch();
   }

program to count odd values of node in linklist

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insert(int);
  void count();
 };

 void linklist::insert(int a)
 {  node *p=new node;
    p->info=a;
    if(start==NULL)
    { start=p;
      p->link=NULL;
      p=NULL;
     }
     else
     {  node *q=start;
    while(q->link!=NULL)
    { q=q->link;
    }
    q->link=p;
    p->link=NULL;
    p=NULL;
      }
  }

  void linklist::count()
  { int count=0;
    node *p=start;
    while(p!=NULL)
    {  if(p->info%2!=0)
       count++;
       p=p->link;

    }
    if(count==0)
    cout<<"there is no node";
    else
    cout<<"number of odd values nodes are: "<<count;
  }
  void main()
  {
     clrscr();
     linklist l1;
     l1.insert(5);
     l1.insert(20);
     l1.insert(30);
     l1.count();
     getch();
   }

program to count number of nodes in the link list

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insert(int);
  void count();
 };

 void linklist::insert(int a)
 {  node *p=new node;
    p->info=a;
    if(start==NULL)
    { start=p;
      p->link=NULL;
      p=NULL;
     }
     else
     {  node *q=start;
    while(q->link!=NULL)
    { q=q->link;
    }
    q->link=p;
    p->link=NULL;
    p=NULL;
      }
  }

  void linklist::count()
  { int count=0;
    node *p=start;
    while(p!=NULL)
    {  p=p->link;
       count++;
    }
    if(count==0)
    cout<<"there is no node";
    else
    cout<<"number of nodes are: "<<count;
  }
  void main()
  {
     clrscr();
     linklist l1;
     l1.insert(10);
     l1.insert(20);
     l1.insert(30);
     l1.count();
     getch();
   }

program to show basics of linklist

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insertbeg(int);
  void show();
  void delfirst();
  void insertend(int);
  void dellast();
};
void linklist::insertbeg(int a)
{ node *p=new node;
  p->info=a;
  p->link=start;
  start=p;
}

 void linklist::show()
 { node *p=start;
   while(p!=NULL)
   { cout<<p->info<<endl;
     p=p->link;
   }
 }

 void linklist::delfirst()
 {   if(start==NULL)
     { cout<<"there is no node in the linklist";
     }
     else
     {  node *p=start;
    start=p->link;
    p->link=NULL;
    delete p;
     }
 }
 void linklist::insertend(int b)
 { node *p=new node;
   p->info=b;
   p->link=NULL;
   if(start==NULL)
   start=p;
   else
   {
     node *q=start;
     while(q->link!=NULL)
     { q=q->link;
     }
     q->link=p;
   }
 }