Monday, 21 August 2017

STACK AS A LINKED LIST

#include<iostream.h>
#include<conio.h>
struct node { int roll;
            char name[20];
            float total;
            node *next; };
class stack
{  
     node *top;
public :      
      stack()
      { top=NULL;}  
      void push();     
      void pop();     
     void display();
};
void stack::push()
{      node *temp;      
     temp=new node;    
    cout<<“Enter roll :”;  
     cin>>temp->roll;
     cout<<“Enter name :”;    
      cin>>temp->name;     
      cout<<“Enter total :”;     
       cin>>temp->total;   
      temp->next=top;    
       top=temp; }
void stack::pop()
{       if(top!=NULL)  
    { 
        node *temp=top;     
        top=top->next;
       cout<<temp->roll<<temp->name<<temp->total<<“deleted”;
       delete temp;        
        }      
else       
      cout<<“Stack empty”; }
void stack::display()
{     
  node *temp=top;    
   while(temp!=NULL)   
    {      
  cout<<temp->roll<<temp->name<<temp->total<<” “;
          temp=temp->next;    
   }   }
void main()
    stack st;
      char ch;  
     do  
     { 
      cout<<“stack  options\nP push \nO  Pop \nD  Display \nQ quit”;
      cin>>ch;            
    switch(ch)        
     {   case ‘P’:  st.push();break;  
           case ‘O’:  st.pop();break;    
           case ‘D’: st.display();break;   
        }    
   }while(ch!=’Q’);
}

STACK AS AN ARRAY

#include<iostream.h>
#include<process.h>
const int size=5
class stack
{           int arr[size];
            int top;             //top will point to the last item pushed onto the stack
    public:
            stack()           //constructor to create an empty stack
           { top=-1; }      // top=-1 indicate that no item is present in the array
            void push(int item)
            {           if(top==size-1)
                        cout<<”stack is full, given item cannot be added”;
                        else
                        {           top++;
                                    arr[top]=item;
                        }
           }
           void  pop()
           { if (top== -1)
                 cout<<”Stack is empty “;
             else
                 { cout<<arr[top];
                 top – -; }
           }
           void display()
           {
            for(int i=top;i>=0;i–)
                        cout<<arr[top];
           }
};
void main()
{           stack s1;
            int ch,data;
            while(1)
            {
           cout<<”1.push”<<”\n 2.pop”<<”\n3.display”<<”\n 4.exit”;
            cout<<”Enter choice :”;
            cin>>ch;
            if (ch==1)
            {           cout<<”enter item to be pushed in the stack”;
                        cin>>data;
                        s1.push(data);
            }
            else if (ch == 2)
                        {  s1.pop();  }
            else if (ch==3)
                        {  s1.display();  }
            else
                        exit(-1);
            } }

STACK AS AN ARRAY

#include<iostream.h>
#include<process.h>
const int size=5
class stack
{           int arr[size];
            int top;             //top will point to the last item pushed onto the stack
    public:
            stack()           //constructor to create an empty stack
           { top=-1; }      // top=-1 indicate that no item is present in the array
            void push(int item)
            {           if(top==size-1)
                        cout<<”stack is full, given item cannot be added”;
                        else
                        {           top++;
                                    arr[top]=item;
                        }
           }
           void  pop()
           { if (top== -1)
                 cout<<”Stack is empty “;
             else
                 { cout<<arr[top];
                 top – -; }
           }
           void display()
           {
            for(int i=top;i>=0;i–)
                        cout<<arr[top];
           }
};
void main()
{           stack s1;
            int ch,data;
            while(1)
            {
           cout<<”1.push”<<”\n 2.pop”<<”\n3.display”<<”\n 4.exit”;
            cout<<”Enter choice :”;
            cin>>ch;
            if (ch==1)
            {           cout<<”enter item to be pushed in the stack”;
                        cin>>data;
                        s1.push(data);
            }
            else if (ch == 2)
                        {  s1.pop();  }
            else if (ch==3)
                        {  s1.display();  }
            else
                        exit(-1);
            } }

INSERTION SORT

void InsertionSort(int a[],int n)
{
       int i,j,temp;
      for(i=1;i<n;i++)
      {
                  temp=a[i];
                  j=i-1;
                  while(temp<a[j])&&j>=0)
                  {           a[j+1]=a[j];
                              j – -;
                  }
      a[j+1]=temp;
      cout<,”\n After Pass “<<i ;
      for(k=0;k<n;k++)
                  cout<<a[k];
} }
 

BUBBLE SORT

void BubbleSort(int a[],int n)
{           int temp;
            for(int i=0;i<n;i++)
            {                         for(int j=0;j<n-1;j++)
                        {  if(a[j]>a[j+1])
                                    {temp=a[j];
                                    a[j]=a[j+1];
                                    a[j+1]=temp;
                        }
            }
            cout<,”\n After Pass “<<i+1 ;
            for(int k=0;k<n;k++)
                        cout<<a[k];
}

SELECTION SORT

void SelectionSort(int a[],int n)
{              int i,small,pos,temp;
            for(i=0;i<n;i++)
            {           small=a[i];
                         pos=i;
                        for(j=i+1;j<n;j++)
                        {  if(a[j]<small)
                                    {small=a[j];
                                    pos=j;}
                        }
            temp=a[i];
            a[i]=a[pos];
            a[pos]=temp;
            cout<,”\n After Pass “<<i+1 ;
            for(j=0;j<n;j++)
                        cout<<a[j];
} }

CONSTRUCTORS

class student
 {
  private:
             int rollno;
            char name[20];
            float marks;
 public:
            student( )                          // Default Constructor
            { rollno=0; marks=0.0; }
            student(int roll, char nam[20], float mar)   // Parametrized Constructor
            { rollno=roll;
            strcpy(name, nam);
            marks =mar;
            }          

student( student & s)   // Copy Constructor
            { rollno= s.rollno;
            strcpy(name, s.name);
            marks =s.marks;
            }
}
void main()
{
student s1; //statement1
student s2( 1, “RAJESH”, 450); // statemet2
student s3(s2);    //statement3
student s4(2, “MOHIT”,320); //statement4
Student s5=s4;      //statement5
}

Explanation:
Statement1: default constructor is called
Statement2: parametrized constructor is called
Statement 3: copy constructor is called
statement4: Parametrized constructor is called
statement5: Copy constructor is called

RETURNING OBJECTS

#include <iostream.h>
#include <conio.h>
class time
{
        int hour;
        int  min;
public :
        void GetTime ();
        time AddTime(time t1);
        void ShowTime();
};
void GetTime()
{         cin<< hour;
           cin<<  min;
}
void ShowTime()
{
          cout<< “ Hours=“<< hour;
           cout<<“Minutes=“<< min;
}
time  AddTime(time t1 )
{          time  t3;
           t3.hour = t1.hour +hour;
           t3.min = t1.min + min;
           if (t3.hour>=60)
           h1= t3.hour – 60;
           t3.min= t3.min + h1;
}
}
void main()
{
         time x1, x2, x3;
         x1.GetTime();
         x2.GetTime();
         x3= x2.AddTime(x1);
         x1.ShowTime();
          x2.ShowTime();
          x3.ShowTime();
}