Alogrithm
Alogrithm
Step 4: Check the stack operation is Push or Pop. Step 5: If operation is push then check the stack status. i. If stack status is over flow we cant push the element in to stack. ii. Other wise we can add the data into stack . iii. Move top to next position.
PROGRAM: //Program for Stack implementation through Array #include <stdio.h> #include<conio.h> #define MAXSIZE 5 int stack[MAXSIZE]; int top=0; //index pointing to the top of stack void main() { void push(); void pop(); void display(); int will=1,i; clrscr(); while(1) { printf("\n\n\nMAIN MENU:\n\n1.PUSH\n2.POP\n3.EXIT\n\nENTER YOUR CHOICE: "); scanf("%d",&will); switch(will) { case 1: push(); display();
break; case 2: pop(); display(); break; case 3: exit(0); break; default: printf("Invalid Choice . "); }
void push() { int num; if(top>=MAXSIZE) { printf("\nSTACK FULL"); return; } else { if(top<0) top=0; printf("\n\nENTER THE STACK ELEMENT : "); scanf("%d",&num); stack[top++]=num; } } void pop() { if(top>=0) top--; } void display() { int i; if(top<=0) printf("\n\nSTACK EMPTY"); else for(i=top-1;i>=0;i--)
RESULT:
ALGORITHM: Step 1: Initialize the queue variables front =0 and rear = -1 Step 2: Read the queue operation type. Step 3: Check the queue operations status. i). If it is Insertion then do the following steps 1. Check rear < queue size is true increment the rear by one and read the queue element and also display queue. otherwise display the queue is full. 2. Go to step2. ii). If it is deletion then do the following steps 1. 2. 3. 4. 5. Check rear< front is true then display the queue is empty. Move the elements to one step forward (i.e. move to previous index ). Decreases the rear value by one (rear=rear-1). Display queue Go to step2.
PROGRAM:
//queue using array #include<stdio.h> #include<conio.h> #define SIZE 5 int i,rear,front,item,s[SIZE]; void insert(int item,int s[]); void del(int s[]); void display(int s[]); void main() { int ch; clrscr(); front=0; rear=-1; do { printf("\n\n 1.INSERTION \n 2.DELETION \n 3.EXIT \n"); printf("\nENTER YOUR CHOICE : "); scanf("%d",&ch);
switch(ch) { case 1: printf("\n\t INSERTION \n"); if(rear>=SIZE-1) { printf("\t\nQUEUE IS FULL\n"); } else { printf("\nENTER AN ELEMENT : "); scanf("%d",&item); insert(item,s); } display(s); break; case 2: printf("\n\t DELETION \n"); if(front>rear) { printf("\t\nQUEUE IS EMPTY\n"); } else { del(s); } display(s); break; } }while(ch!=3); getch(); } void insert(int item,int s[]) { if(rear<SIZE) { rear=rear+1; s[rear]=item; } } void del(int s[]) { int i; item=s[front]; for(i=0;i<=rear;i++) s[i]=s[i+1]; rear--; printf("\n DELETED ELEMENT IS %d\n\n",item);