0% found this document useful (0 votes)
7 views

Middel Element

Uploaded by

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

Middel Element

Uploaded by

oldxmonk49
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/ 5

/* SY CSE 2024-25 Name: Akshay Rajendra

Sonwane Roll No: 65 Batch: S4

Program 12= To delete the middle element of the stack

#include <stdio.h>

#include <conio.h>

#define MAX 10 // Maximum size of the stack

int stack[MAX];

int top = -1;

intmiddleIndex;

inttempStack[MAX];

inttempTop,i;

// Function to push an element onto the stack

void push(int value) {

if (top >= MAX - 1) {

printf("Stack overflow\n");

} else {

stack[++top] = value;

// Function to pop an element from the stack

int pop() {

if (top == -1) {
printf("Stack underflow\n");

return -1;

} else {

return stack[top--];

// Function to delete the middle element of the stack

voiddeleteMiddle() {

if (top == -1) {

printf("Stack is empty\n");

return;

middleIndex = top / 2; // Find the middle index

//tempStack[MAX];

tempTop = -1;

// Remove elements above the middle element and store them in a temporary stack

for ( i = 0; i <middleIndex; i++) {

tempStack[++tempTop] = pop();

// Pop the middle element (do not store it, effectively deleting it)
pop();

// Push back the elements from the temporary stack to the original stack

while (tempTop != -1) {

push(tempStack[tempTop--]);

// Function to display the stack elements

void display() {

if (top == -1) {

printf("Stack is empty\n");

return;

printf("Stack elements: ");

for ( i = 0; i <= top; i++) {

printf("%d ", stack[i]);

printf("\n");

void main() {

// Adding elements to the stack

push(1);
push(2);

push(3);

push(4);

push(5);

printf("Original Stack:\n");

display();

// Deleting the middle element

deleteMiddle();

printf("Stack after deleting the middle element:\n");

display();

getch();

}
Output=

You might also like