Sunday, 27 January 2013

Write a program to Insert and Delete an element in the beginning of Singular circular linked List.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void insert_element(void);
int Delete(void);
void display(void);
struct node
{
int data;
struct node *link;
}*f=NULL,*r=NULL;

void main()
{
               int choice,x;
               clrscr();
               cout<<"---------QUEUE ----"<<endl;
               cout<<"press 1. insert the element in the begning"<<endl;
               cout<<"press 2. delete the element from the begning"<<endl;
               cout<<"press 3. display queue"<<endl;
               cout<<"press 4. EXIT"<<endl;
               while(1)
               {
                               cout<<"enter your choice:-";
                               cin>>choice;
                               switch(choice)
                               {
               case 1:

                               insert_element();
                               break;
               case 2:
                               x=Delete();
                               cout<<" deleted number is:-"<<x<<endl;

                               break;
               case 3:
                               cout<<"QUEUE IS"<<endl;
                              display();
                               break;
               case 4:
                               exit(0);
                               break;
               default:
                               cout<<"plz  enter correct choice";
                               break;
               }//end of switch case

               getch();
               }//end of while

               }//end of main

               void insert_element()
               {
               char ch;
               do
               {
                               struct node *temp;
                               temp=new node;
                               cout<<"enter data:-";    //insert a node in the circular queue
                               cin>>temp->data;
                               if(r==NULL && f==NULL)
                               {
                                              temp->link=temp;
                                              r=temp;
                                              f=temp;
                               }
                               else
                               {
                                              temp->link=f;
                                              r->link=temp;

                                              f=temp;
                               }
                               cout<<"do you want to insert more node(y/n):-";
                               cin>>ch;
                      }while(ch=='y');

               }//end of function

               int Delete()
               {
                               struct node *temp;
                               int x;

                               if(f==NULL && r==NULL)
                               {
                               cout<<" no element for deletion";
                               }
                               else               //delete a node from the circular queue
                               {
                               x=f->data;
                               temp=f;
                               f=f->link;
                               r->link=f;
                               delete(temp);
                               }
                               return(x);


               }//end of function

               void display()
               {
                     struct node  *ptr;
                     ptr=f;                          //display a queue
                     while(ptr->link!=f)
                     {
                     cout<<" "<<ptr->data;
                     ptr=ptr->link;
                     }
                     cout<<" "<<ptr->data;
                     cout<<endl;
                }//end of function

Wednesday, 16 January 2013

Double Linked list with double data element

//double link list with double data

#include<conio.h>
#include<iostream.h>
struct node
{
struct node *plink;
int data1;
int data2;
struct node *flink;
};
void main()
{
clrscr();
 int i,n,d1,d2;
struct node *temp,*start=NULL;


temp=new node;
temp->plink=NULL;
temp->data1=100;
temp->data2=200;
temp->flink=NULL;
start=temp;
cout<<"----------------------------------------"<<endl;
cout<<"How Many element do you want to Insert |"<<endl;
cout<<"----------------------------------------"<<endl;
cin>>n;
    for(i=1;i<=n;i++)
    {
        cout<<"Enter data 1st in node" <<endl;
        cin>>d1;
        cout<<"Enter data 2nd in node" <<endl;
        cin>>d2;
        temp=new node;
        temp->plink=NULL;
        temp->data1=d1;
        temp->data2=d2;
        temp->flink=start;
        start->plink=temp;
        start=temp;
    }

                 cout<<"-----------"<<endl;
                cout<<"Data in link|"<<endl;
                cout<<"------------"<<endl;
        for(temp=start;temp!=NULL;temp=temp->flink)
                    {

                                cout<<"---------"<<endl;
                                cout<<"!"<<temp->data1<<"!"<<temp->data2<<"!"<<endl;
                                cout<<"---------"<<endl;
                    }



}

inserting kth possition in single linked list (except first postion and last postion).....

#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *link;
};
void main()
{
clrscr();
struct node *start=NULL,*temp,*ptr,*new1,*temp1;
int num,n,count=0,k,j;
temp=new node;
start=temp;
start->link=NULL;
start->data=10;
cout<<"\n how many number u want to enter";
cin>>n;
for(int i=0;i<n-1;i++)
{
cout<<"\n enter number";
cin>>num;
temp=new node;
temp->data=num;
temp->link=start;
start=temp;
}
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<"\n element is="<<"\t"<<temp->data;
count++;
}
cout<<"\n total elements are="<<count;
cout<<"\n enter location where u want to insert";
cin>>k;
temp1=start;
for(j=1;j<k;j++,temp1=temp1->link)
{
ptr=temp1;
}
cout<<"enter data part";
cin>>num;
new1=new node;
new1->data=num;
new1->link=temp1;
ptr->link=new1;
count=0;
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<"\n element is="<<"\t"<<temp->data;
count++;
}
cout<<"\n total elements are now="<<count;
getch();
}

PUSH, POP and Peek operation in stack using array


#include<iostream.h>
#include<conio.h>
#include<process.h>
#define max 20
int top;
class stack
{
               int a[max],n,item;
               public:
               stack()
                {
                top=0;
                }
               void initial_stack();
               void push(int);
               int pop();
               void display();
};
               void stack::initial_stack()
               {
               char c;
               cout<<"\nDo you want initialy elements inserted in the stack(y/n):-";
               cin>>c;
                               if(c=='y')
                               {
                               cout<<"How many elements you want initialy in the stack"<<endl;
                               cin>>n;
                               cout<<"enter elements\n";
                                              for(int i=1;i<=n;i++)
                                              {
                                              top=top+1;
                                              cin>>item;
                                              a[top]=item;
                                              }
                               }
                }

               void stack::push(int m)
                {
                               if(top==max)
                               {
                               cout<<"\nCONDITION OF OVERFLOW";
                               getch();
                               exit(1);
                               }
                               else
                               {
                               top=top+1;
                               a[top]=m;
                               }
                }
               int stack::pop()
                {
                                if(top==0)
                                {
                                cout<<"\nCONDITION OF UNDERFLOW";
                                getch();
                                exit(1);
                                }
                               else
                                {
                                item=a[top];
                                top=top-1;
                                }
                               return item;
                }
               void stack::display()
                {
                               cout<<"\nTop ="<<top;
                               cout<<"\nstack["<<top<<"]="<<a[top];
                }
void main()
{
clrscr();
               stack obj;
               char ch='y';
               int choice,element;
               obj.initial_stack();
               while(ch=='y')
                {
               cout<<"What you want to do :-Enter ur choice"<<endl;
               cout<<"1.Push in stack\n2.Pop from stack\n";
               cout<<"3.Display(peek) the TOP of stack\n4.exit";
               cin>>choice;
               switch(choice)
               {
               case 1:
                               cout<<"enter the element to push"<<endl;
                               cin>>element;
                               obj.push(element);
                               cout<<"After push:-"<<endl;
                               obj.display();
               break;
               case 2:
                               element=obj.pop();
                               cout<<"After pop of"<<element<<"from"<<top+1<<"location:-"<<endl;
                               obj.display();
               break;
               case 3:
                               obj.display();
               break;
               default:
                               exit(1);

               }
cout<<endl<<"Do  you  want to continue(y/n)";
cin>>ch;
                }
getch();

}