Sunday, 2 July 2017

C++ program of linked list

In this program we will insert,delete and print data of linked list
//Program Starts

#include<iostream>
#include<conio.h>
using namespace std;
struct node{
          int data;
          node *next;
};
class linked{
          private:
                    node *ptr,*head,*tail,*cur,*prev;
                    int d,loc;
          public:
                    void create();
                    void insatbeg();
                    void insatend();
                    void insatspec();
                    void delatbeg();
                    void delatend();
                    void delatspecind();
                    void delofspecdat();
                    void show();
};

void linked::create()
{
          head=new node;
          head->data=0;
          head -> next=NULL;
          tail=head;
          for(int i=1;i<10;i++)
          {
                    ptr= new node;
                    ptr->data=i;
                    ptr->next=NULL;
                    tail->next=ptr;
                    tail=ptr;
          }
         
}

void linked::insatbeg()
{
          cout<<"Enter data at begining : ";
          cin>>d;
          ptr= new node;
          ptr->data=d;
          ptr->next=head;
          head=ptr;
}

void linked::insatend()
{
          cout<<"Enter data at end : ";
          cin>>d;
          ptr= new node;
          ptr->data=d;
          ptr->next=NULL;
          cur=head;
         
          while(cur->next!=NULL)
          {
                    cur=cur->next;
          }
          cur->next=ptr;
}

void linked::insatspec()
{
          cout<<"Enter Location : ";
          cin>>loc;
          cout<<"Enter data: ";
          cin>>d;
          ptr= new node;
          ptr->data=d;
          prev=head;
          cur=head->next;
         
          for(int i=1;i<=loc-2;i++)
          {
                    prev= cur;
                    cur=cur->next;
          }
          prev->next=ptr;
          ptr->next=cur;
}

void linked::delatbeg()
{
          cur = head;
          head=head->next;
          delete cur;
}

void linked::delatend()
{
          prev=head;
          cur=head->next;
          while(cur->next!=NULL)
          {
                    prev=cur;
                    cur=cur->next;
          }
          prev->next=NULL;
          delete cur;
}

void linked::delatspecind()
{
          cout<<"Enter location to be deleted : ";
          cin>>loc;
         
          prev=head;
          cur=head->next;
          for(int i=1;i<=loc-2;i++)
          {
                    prev=cur;
                    cur=cur->next;
          }
          prev->next=cur->next;
          delete cur;
}

void linked::delofspecdat()
{
          cout<<"Enter data to be deleted : ";
          cin>>d;
         
          prev=head;
          cur=head->next;
          while(cur->data!=d)
          {
                    prev=cur;
                    cur=cur->next;
          }
          prev->next=cur->next;
          delete cur;
}

void linked::show()
{
          cur=head;
          while(cur!=NULL)
          {
                    cout<<cur->data<<" -> ";
                    cur=cur->next;
          }
}


int main()
{
          linked l1;
          int c;
          l1.create();
          do{
                    cout<<"\n\nPress 1 to insert at beg\nPress 2 to insert at end\nPress 3 to insert at specific location\nPress 4 to show\nPress 5 to delete data from begining\nPress 6 to delete data from End\nPress7 to delete data from specific location\nPress 8 to delete specific data\nPress 9 to exit\n Value==== ";
                    cin>>c;
                   
                    switch(c)
                    {
                              case 1:
                                        l1.insatbeg();
                                        break;
                              case 2:
                                        l1.insatend();
                                        break;
                              case 3:
                                        l1.insatspec();
                                        break;
                              case 4:
                                        l1.show();
                                        break;
                              case 5:
                                        l1.delatbeg();
                                        break;
                              case 6:
                                        l1.delatend();
                                        break;
                              case 7:
                                        l1.delatspecind();
                                        break;
                              case 8:
                                        l1.delofspecdat();
                                        break;
                              case 9:
                                        return 1;
                    }
          }
          while(1);
         

}

OUTPUT:
Share:

C++ program of circular queue

In this program we will see how to insert,delete and show data in Circular Queue.

//Program starts

#include<conio.h>
#include<iostream>
using namespace std;

class cirq{
          private:
                    int d,size,cq[5],counter,front,rear;
          public:
                    cirq();
                    void create();
                    void incq(int d);
                    void delcq();
                    void show();
};

cirq::cirq()
{
          size=5;
          counter=0;
          front=-1;
          rear=-1;
}

void cirq::create()
{
          for(int i=0;i<5;i++)
          {
                    cout<<"Enter data at ["<<i<<"] : ";
                    cin>>cq[i];
                   
          }
          front=0;
          rear=4;
          counter=5;
         
}

void cirq::incq(int d)
{
          if(counter==5)
          {
                    cout<<"Queue is full\n";
          }
          else
          {
                    cout<<"Enter data : ";
                    cin>>d;
                    rear=(rear+1)%size;
                    cq[rear]=d;
                    counter++;
          }
}

void cirq::delcq()
{
          if(counter==-1)
          {
                    cout<<"Queue is empty\n";
          }
          else
          {
                    front=(front+1)%size;
                    counter--;
          }
}

void cirq::show()
{
          for(int i=0;i<counter;i++)
          {
                    cout<<"Data at ["<<(front+i)%5<<"] = "<<cq[(front+i)%5]<<endl;
          }
}


int main()
{
          cirq c; //Creating Object
          int b,d;
          c.create();    
do{
          cout<<"\n Press 1 to show data\n Press 2 to Delete data\n Press 3 to Insert data \n Press 4 to EXIT the program\nValue= ";
          cin>>b;
          switch(b)
          {
                    case 1:
                              c.show();
                              break;
                    case 2:
                              c.delcq();
                              break;
                    case 3:
                              c.incq(d);
                              break;
                    case 4:
                              cout<<"Bye! Bye!";
return 1;
          }
}
while(1);
         
         
}


OUTPUT:
Share:

C++ program of Stack

In this program we will discuss about working of stack and we will see how to insert and delete data from stack.

//Program starts
#include<iostream>
#include<conio.h>
using namespace std;

class stack{
          private:
                    int d,n,a[5],top;
          public:
                    stack();
                    void create();
                    void push();
                    void pop();
                    void show();
};
stack::stack()
{
          n=5;
          top=-1;
}

void stack::create()
{
          for(int i=0;i<n;i++)
          {
                    cout<<"Enter data at ["<<i<<"] : ";
                    cin>>a[i];
          }
         
          top=4;
}

void stack::push()
{
          if(top==4)
          {
                    cout<<"\n Stack is full \n";
          }
          else
          {
                    cout<<"Enter data : ";
                    cin>>d;
                    top++;
                    a[top]=d;
          }
}

void stack::pop()
{
          if(top==-1)
          {
                    cout<<"\n Stack underflow \n";
          }
          else
          {
                    top--;
          }
}

void stack::show()
{
          if(top==-1)
          {
                    cout<<"\nStack is empty\n ";
          }
          else
          {
         
          for(int i=0;i<=top;i++)
          {
                    cout<<"Data at ["<<i<<"] = "<<a[i]<<endl;
          }
}
}

int main()
{
          int c;
          stack s1;
          s1.create();
         
          do{
                    cout<<"\nPress 1 to insert data\nPress 2 to delete data\n Press 3 to show data\nPress 4 to exit\nValue = ";
                    cin>>c;
                   
                    switch(c)
                    {
                              case 1:
                                        s1.push();
                                        break;
                                       
                              case 2:
                                        s1.pop();
                                        break;
                                       
                              case 3:
                                        s1.show();
                                        break;
                                       
                              case 4:
                                        return 0;
                                        break;
                    }       
          }
          while(1);
}

 OUTPUT:




Share:

Saturday, 20 May 2017

C++ program of queue

In this program we will take a look on working of queue by craeting, inserting, deleting and displaying an queue.
program:

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

using namespace std;

class queue{
private:
int front,counter,maxsize,rear,q[5];
public:
queue();
void create();
void enqueue(int d);
void dequeue();
void show();
};

queue::queue()
{
front=-1;
rear=-1;
maxsize=5;
counter=0;
}
/*
void queue::create()
{
for(int i=0;i<5;i++)
{
cout<<"Enter data at ["<<i<<"] : ";
cin>>q[i];
}
front=0;
rear=5;
}
*/
void queue::enqueue(int d)
{
if(rear==4)
{
cout<<"\n\nQueue is full\n";
}
else
{
cout<<"Enter data : ";
cin>>d;

rear++;
q[rear]=d;
if(front==-1)
{
front=rear=0;
}
}
}

void queue::dequeue()
{
if(front==rear||front==-1)
{
cout<<"\n\nQueue is empty\n";
}
else
{
front++;
cout<<"\n\nData is deleted\n";
if(front==4&&rear==4)
{
front=rear=-1;
}
}
}

void queue::show()
{
if(rear==-1&&front==-1)
{
cout<<"\nQueue is empty\n";
}
else
{

for(int i=front;i<=rear;i++)
{
cout<<"Data at ["<<i<<"] == "<<q[i]<<endl;
}

}
}

int main()
{
queue q;
int c,d;

// q.create();

do{
cout<<"\nPress 1 for enqueue\nPress 2 for dequeue\nPress 3 for show\nPress 4 for exit\nValue == ";
cin>>c;

switch(c)
{
case 1:
q.enqueue(d);
break;
case 2:
q.dequeue();
break;
case 3:
q.show();
break;
case 4:
return 1;
}
}
while(1);
}
//Code Ends

Screenshot:


Share:

C++ program of creation, insertion and deletion in array

In this program we will create, print, delete and insert data in array using  methods of data structures.

Program:


#include<iostream>
#include<conio.h>
using namespace std;

class array{
            private:
                        int a[10],size;
                       
            public:
                       
                        array();
                       
                        void create();
                        void print();
                        void insertion(int data,int index);
                        void deletion(int index);

};

array::array()
{
            size=5;
}

void array::create()
{
            for(int i=0;i<size;i++)
            {
                        cout<<"Enter data in index ["<<i<<"] : ";
                        cin>>a[i];
            }
}

void array::print()
{
            cout<<"Your entered data "<<endl;
            for(int i=0;i<size;i++)
            {
                        cout<<"Data at index ["<<i<<"] : "<<a[i]<<endl;
            }
}

void array::insertion(int data,int index)
{          
    
            for(int i=size-1;i>=index;i--)
            {
                        a[i+1]=a[i];
            }
            size++;
           
            a[index]=data;
           
            cout<<"New data after insertion at index["<<index<<"]"<<endl;
            for(int i=0;i<size;i++)
            {
                        cout<<"Index ["<<i<<"] = "<<a[i]<<endl;
            }

}

void array::deletion(int index )
{
            for(int i=index+1;i<=size-1;i++)
            {
                        a[i-1]=a[i];
            }

            size--;
           
            cout<<"After deleting index["<<index<<"]"<<endl;
           
            for(int i=0;i<=size-1;i++)
            {
                        cout<<"Index [        "<<i<<"] = "<<a[i]<<endl;
            }
           
}


int main()
{
            array a;
            int x;
            do{
                       
            cout<<"Enter 1 to enter data : "<<endl;
            cout<<"Enter 2 to show entered data : "<<endl;
            cout<<"Enter 3 to insert data in array : "<<endl;
            cout<<"Enter 4 to delete data in array : "<<endl;
            cout<<"Enter 5 to exit from program : "<<endl;
            cout<<"      Value =     ";
           
                        cin>>x;
            switch (x)
              {
              case 1:
             
                       
                        a.create();
              break;
             
              case 2:
                       
                           a.print();
                                   
                       
                          break;
              case 3:
                        int n,b;
                       
                        cout<<"Enter data : ";
                        cin>>n;
                       
                        cout<<"Enter index : ";
                        cin>>b;
                             a.insertion(n, b);
                         
                          break;
              case 4:
                        int v;
                       
                        cout<<"Enter index to be deleted : ";
                        cin>>v;
                        a.deletion(v);          
                         
                        break;
                       
                        case 5:
                                   
                                                cout<<"Program terminated "<<endl;
                                                return 1;
                                    break;
            }
}
            while(1);

            getch();         

}

//Code Ends

Screenshot:
Share:

Recursive function of exponential

In this program we will see how a function calls itself inside its body of fuction.

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

using namespace std;

int rf(int n,int b)
{
          if(b==1)
          {
                    return n;
                    }
          else 
          {
                    return n*rf(n,b-1);
          }
                   
}

int main()
{
          int m,o;
         
          cout<<"Enter base : ";
          cin>>m;
          cout<<"Enter power: ";
          cin>>o;
         
          cout<<rf(m,o);
          getch();
}


//Code Ends

Screenshot:

Share: