Showing posts with label DS. Show all posts
Showing posts with label DS. Show all posts

Saturday, December 19, 2015

Write A Program to implement operation INSERTION & DELETION of linear queue using array.

Solution:-
 
#include<stdio.h>
#include<conio.h>
#include<process.h>
# define maxsize 5
int queue[maxsize];
int rear=-1;
int front=-1;
void enque();
void deque();
void display();
void main()
{
  int choice;
  clrscr();
  do
  {
            printf("\n 1.ENQUEUW");
            printf("\n 2.DEQUEUE");
            printf("\n 3.DISPLAY");
            printf("\n 4.EXIT");
            printf("\n Enter your choice: \t");
            scanf("%d",&choice);
            switch(choice)
            {
                case 1: enque();
                        break;
                case 2: deque();
                        break;
                case 3: display();
                        break;
                case 4: exit(0);
                        break;
            }
  }
  while(choice!=4);
  getch();
}
void enque()
{
  int item;
  if(rear==maxsize-1)
  printf("\n QUEUE OVERFLOW");
  else
  {
       printf("\n Enter item: \t");
       scanf("%d",&item);
       if(rear==-1)
       {
            rear=0;
            front=0;
       }
       else
       rear=rear+1;
       queue[rear]=item;
       printf("\n %d is inserted successfully",queue[rear]);
  }
}
void deque()
{
  int item;
  if(front==-1)
  printf("\n QUEUE UNDERFLOW");
  else
  {
      item=queue[front];
      if(rear==front)
      {
         front=-1;
         rear=-1;
      }
      else
      front=front+1;
      printf("\n %d is deleted successfully",item);
  }
}
void display()
{
  int i;
  if(rear==-1)
  printf("\n NO ITEM");
  else
  {
     for(i=front;i<=rear;i++)
     printf("%d \t",queue[i]);
  }
}

Write A Program to implement stack operation PUSH & POP using array.

Solution:-
 
#include<stdio.h>
#include<conio.h>
#include<process.h>
# define maxsize 5
int stack[maxsize];
int tos=-1;
void push( );
void pop( );
void display( );
void main( )
{
        int choice;
        clrscr() ;
        do
        {
                  printf("\n 1.PUSH");
                  printf("\n 2.POP");
                  printf("\n 3.DISPLAY");
                  printf("\n 4.EXIT");
                  printf("\n Enter your choice: \t");
                  scanf("%d",&choice);
                  switch(choice)
                  {
                        case 1:push();
                             break;
                        case 2:pop();
                             break;
                        case 3:display();
                             break;
                        case 4:exit(0);
                             break;
                  }
        }
        while(choice!=4);
}
 
void push()
{
        int item;
        if(tos==maxsize-1)
        printf("\n STACK OVERFLOW");
        else
        {
                printf("\n Enter item: \t");
                scanf("%d",&item);
                tos=tos+1;
                stack[tos]=item;
                printf("\n %d is pushed successfully",item);
        }
}
 
void pop()
{
        int item;
        if(tos==-1)
        printf("\n STACK UNDERFLOW");
        else
        {
                item=stack[tos];
                tos=tos-1;
                printf("\n %d is poped successfully",item);
        }
}
 
void display()
{
         int i;
         if(tos==-1)
         printf("\n NO ITEM");
         else
         {
                    for(i=tos;i>=0;i--)
                    printf("\n %d",stack[i]);
         }
}
 

Sunday, September 27, 2015

What is Queue? Write an Algorithm for the INSERTION & DELETION operations.

Solution:-
Introduction of Queue:-
   *   Queue is Linear , Non – primitive data structure.
   *   Accessed from two ends                 
                 i.    Rear -> To insert item
                 ii.   Front -> To delete item
   *   Queue operations are:
               i.    Insertion (enque)
               ii.   Deletion (deque)
   *   Working principles First in First out (FIFO).

Algorithm:-
Algorithm to insert item in to linear queue (enque )

enque ( queue [maxsize] , item , rear , front )

             Step 1:- if (rear == maxsize -1)
                                     print " QUEUE OVERFLOW "                        
                           Other wise
                                 Read item
                                 if ( front = =  rear  = = -1)
                                           Set rear = 0
                                           Set front = 0
                                 Other wise                                                
                                           Set rear = rear + 1                                 
                                 End if
                                 Set queue[rear]=item
                                 print " SUCCESS INSERT ITEM "
                         End if             
             Step 2:- stop.


Algorithm to delete item from linear queue ( deque )
Deque (queue [maxsize ] , item , front , rear )

               Step 1:- if( front == -1 )
                                       print " QUEUE UNDERFLOW "
                             Otherwise                                       
                                       Set item = queue [front]
                                       if( rear == front )
                                                  Set front = -1
                                                  Set rear = -1
                                       Otherwise
                                                  Set front = front +1
                                       End if
                                       print " SUCCESS DELETE ITEM "
                              End if
               Step 2:- stop.


What is STACK ? Write an Algorithm for the PUSH & POP operation.

Solution:-
STACK Introduction:-
*  Stack is non - primitive linear data structure.
*   Accessed from single end called Top Of Stack (TOS).
*   Stack operations are:
        i.  PUSH --: To insert into stack.
        ii.  POP -> To remove item from stack
 *  Using Last in First output (LIFO) principle

Algorithm:-
Algorithm for PUSH operation
PUSH (stack [maxsize] , item , tos)

   Step 1:- if ( tos == maxsize – 1)
                        print " STACK OVERFLOW"
                 Other wise
                        Read item
                        tos = tos + 1
                        set stack [ tos ] = item
                        print " PUSHED SUCCESSFULLY "
                 end if
Step 2: stop

Algorithm for POP operation
POP ( stack [maxsize ] , item , tos )

          Step 1:- if  ( tos  = =  -1 )
                               print " STACK UNDERFLOW "
                        Other wise
                               item = stack [tos ]
                               tos = tos -1
                               print " POPED SUCCESSFULLY "
                        End if
          Step 2:- stop