0% found this document useful (0 votes)
58 views7 pages

Exp-1-2-Discrete Structure

The document describes implementing a stack and queue using arrays in C. It defines functions to push, pop, and display elements in the stack. It also defines functions to insert, delete, and display elements in the queue. The main function runs an infinite loop menu to test the stack and queue functions by pushing, popping, and displaying elements and inserting, deleting, and displaying elements.

Uploaded by

Akash Akm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views7 pages

Exp-1-2-Discrete Structure

The document describes implementing a stack and queue using arrays in C. It defines functions to push, pop, and display elements in the stack. It also defines functions to insert, delete, and display elements in the queue. The main function runs an infinite loop menu to test the stack and queue functions by pushing, popping, and displaying elements and inserting, deleting, and displaying elements.

Uploaded by

Akash Akm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

* IMPLEMENT STACK USING ARRAY

#include<stdio.h>
#include<stdlib.h>
#define MAX 5 //Maximum number of elements that can be stored
int top=-1,stack[MAX];
void push();
void pop();
void display();

void main()
{
int ch;

while(1) //infinite loop, will end when choice will be 4


{
printf("\n*** Stack Menu ***");
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);

default: printf("\nWrong Choice!!");


}
}
}

void push()
{
int val;
if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}

void display()
{
int i;

if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
OUTPUT:-

*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):1

Enter element to push:23

*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):1

Enter element to push:45

*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):1

Enter element to push:78

*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):3

Stack is...
78
45
23

*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):2

Deleted element is 78
*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):3

Stack is...
45
23

*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):4


*IMPLEMENT QUEUE USING ARRAY

#include<stdio.h>
#include<stdlib.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\nQueue is Full");
else
{
printf("\nEnter the element:");
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\nQueue is empty");
}
else
{
printf("\nDeleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
if(front==rear)
printf("\nQueue is Empty");
else
{
printf("\nQueue Elements are:\n");
for(i=front;i<rear; i++)
{
printf("%d\n",queue[i]);
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}

OUTPUT:-

Queue using Array


1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1

Enter the element:34

Enter the Choice:1

Enter the element:56

Enter the Choice:1

Enter the element:89

Enter the Choice:3

Queue Elements are:


34
56
89

Enter the Choice:2

Deleted Element is 34
Enter the Choice:3

Queue Elements are:


56
89

Enter the Choice:4

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