0% found this document useful (0 votes)
38 views6 pages

Alogrithm

The document describes an algorithm and program for implementing a stack using an array. The algorithm defines the steps to push and pop elements from the stack, checking for overflow. The program implements the stack with functions for push, pop, and display and uses a menu driven interface to demonstrate pushing and popping elements from the stack.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Alogrithm

The document describes an algorithm and program for implementing a stack using an array. The algorithm defines the steps to push and pop elements from the stack, checking for overflow. The program implements the stack with functions for push, pop, and display and uses a menu driven interface to demonstrate pushing and popping elements from the stack.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ALOGRITHM: Step 1: Define a stack size. Step 2: Read the stack operation. Step 3: Read the stack element.

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 . "); }

} //end of outer while //end of main

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--)

{ printf("\n\n%d ",stack[i]); if(i==(top-1)) printf("---->TOP"); } }

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);

} void display(int s[]) { printf("\n"); for(i=front;i<=rear;i++) { printf(" \t %d",s[i]); } } OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy