0% found this document useful (0 votes)
21 views35 pages

Stack and Queue Operations in C

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)
21 views35 pages

Stack and Queue Operations in C

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

Practical No.

#include <stdio.h>

#include <stdlib.h>

#define MAX 5 // Maximum size of the stack

int stack[MAX];

int top = -1; // Initialize stack as empty

// Function to push an element into the stack

void push(int value) {

if (top == MAX - 1) {

printf("Stack Overflow! Cannot push %d\n", value);

} else {

top++;

stack[top] = value;

printf("%d pushed into stack.\n", value);

// Function to pop an element from the stack

void pop() {

if (top == -1) {

printf("Stack Underflow! Cannot pop.\n");

} else {

printf("%d popped from stack.\n", stack[top]);


top--;

// Function to return the top element without removing it

void peek() {

if (top == -1) {

printf("Stack is empty.\n");

} else {

printf("Top element is %d\n", stack[top]);

// Function to display all elements of the stack

void display() {

if (top == -1) {

printf("Stack is empty.\n");

} else {

printf("Stack elements are:\n");

for (int i = top; i >= 0; i--) {

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

// Main function
int main() {

int choice, value;

while (1) {

printf("\n--- Stack Menu ---\n");

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Peek\n");

printf("4. Display\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to push: ");

scanf("%d", &value);

push(value);

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:
display();

break;

case 5:

exit(0);

default:

printf("Invalid choice! Please try again.\n");

return 0;

Output:--- Stack Menu ---

1. Push
2. Pop
3. Peek
4. Display
5. Exit Enter your choice: 1 Enter value to push: 20 20 pushed into stack.

--- Stack Menu ---

1. Push
2. Pop
3. Peek
4. Display
5. Exit Enter your choice:
Practical No 2:

#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h>

#define MAX 100

// Stack structure struct Stack { int top; int items[MAX]; };

// Function to initialize the stack void init(struct Stack *s) { s->top = -1; }

// Function to check if stack is empty int isEmpty(struct Stack *s) { return s->top == -1; }

// Function to push an element onto the stack void push(struct Stack *s, int value) { if (s-
>top == MAX - 1) { printf("Stack Overflow!\n"); exit(1); } s->items[++(s->top)] = value; }

// Function to pop an element from the stack int pop(struct Stack *s) { if (isEmpty(s))
{ printf("Stack Underflow!\n"); exit(1); } return s->items[(s->top)--]; }

// Function to evaluate a postfix expression int evaluatePostfix(char *exp) { struct Stack s;


init(&s);

for (int i = 0; exp[i] != '\0'; i++) {


char ch = exp[i];

// If character is a digit, push it onto the stack


if (isdigit(ch)) {
push(&s, ch - '0'); // Convert char to int
}
// If operator, pop two elements and apply the operation
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch ==
'%') {
int val2 = pop(&s);
int val1 = pop(&s);
int result;

switch (ch) {
case '+': result = val1 + val2; break;
case '-': result = val1 - val2; break;
case '*': result = val1 * val2; break;
case '/': result = val1 / val2; break;
case '%': result = val1 % val2; break;
}
push(&s, result);
}
}

return pop(&s);

// Main function int main() { char exp[MAX];

printf("Enter a postfix expression (e.g. 23*54*+9-): ");


scanf("%s", exp);

int result = evaluatePostfix(exp);


printf("Result of postfix expression: %d\n", result);

return 0;

Output:

Enter a postfix expression (e.g. 2354+9-): gjgwjf6757&&^%&()

Result of postfix expression: 5

=== Code Execution Successful ===

Practical No 3

#include <stdio.h>

#include <ctype.h>
char stack[100];

int top = -1;

void push(char c) {

stack[++top] = c;

char pop() {

return stack[top--];

int precedence(char c) {

if (c == '^') return 3;

if (c == '*' || c == '/') return 2;

if (c == '+' || c == '-') return 1;

return 0;

int main() {

char infix[100], postfix[100];

int i, k = 0;

char ch;

printf("Enter infix expression: ");

scanf("%s", infix);
for (i = 0; infix[i] != '\0'; i++) {

ch = infix[i];

if (isalnum(ch)) // operand

postfix[k++] = ch;

else if (ch == '(')

push(ch);

else if (ch == ')') {

while (top != -1 && stack[top] != '(')

postfix[k++] = pop();

pop(); // remove '('

else { // operator

while (top != -1 && precedence(stack[top]) >= precedence(ch))

postfix[k++] = pop();

push(ch);

while (top != -1)

postfix[k++] = pop();

postfix[k] = '\0';

printf("Postfix expression: %s\n", postfix);


return 0;

Output:

Enter infix expression: ghf%&&(dsfr)

Postfix expression: ghf%&dsfr&

=== Code Execution Successful ===

Practical No : 4

#include <stdio.h>

#define SIZE 5
int queue[SIZE];

int front = -1, rear = -1;

// Function to check if queue is full

int isFull() {

return (front == (rear + 1) % SIZE);

// Function to check if queue is empty

int isEmpty() {

return (front == -1);

// Insert (enqueue) element

void enqueue(int value) {

if (isFull()) {

printf("Queue is full!\n");

return;

if (isEmpty())

front = 0;

rear = (rear + 1) % SIZE;

queue[rear] = value;

printf("%d inserted.\n", value);

}
// Delete (dequeue) element

void dequeue() {

if (isEmpty()) {

printf("Queue is empty!\n");

return;

printf("%d deleted.\n", queue[front]);

if (front == rear)

front = rear = -1; // Queue becomes empty

else

front = (front + 1) % SIZE;

// Display elements

void display() {

if (isEmpty()) {

printf("Queue is empty!\n");

return;

printf("Queue elements: ");

int i = front;

while (1) {

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

if (i == rear)

break;

i = (i + 1) % SIZE;
}

printf("\n");

// Main function

int main() {

int choice, value;

while (1) {

printf("\n--- Circular Queue Menu ---\n");

printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:

dequeue();

break;

case 3:

display();

break;
case 4:

return 0;

default:

printf("Invalid choice!\n");

Output:

--- Circular Queue Menu ---

1. Enqueue
2. Dequeue
3. Display
4. Exit Enter your choice: 1 Enter value to insert: 229 229 inserted.

--- Circular Queue Menu ---

1. Enqueue
2. Dequeue
3. Display
4. Exit Enter your choice:

Practical No.5.

#include <stdio.h>
#include <stdlib.h>

#define MAX 5 // Define maximum size of the deque


int deque[MAX];
int front = -1;
int rear = -1;

// Function to check if the deque is full


int isFull() {
return ((front == 0 && rear == MAX - 1) || (front == rear + 1));
}

// Function to check if the deque is empty


int isEmpty() {
return (front == -1);
}

// Function to insert an element at the front of the deque


void insertFront(int key) {
if (isFull()) {
printf("Overflow: Unable to insert element at the front. Deque is full.\n");
return;
}

if (front == -1) { // If deque is initially empty


front = 0;
rear = 0;
} else if (front == 0) {
front = MAX - 1; // wrap around
} else {
front = front - 1;
}

deque[front] = key;
printf("Inserted %d at the front.\n", key);
}

// Function to insert an element at the rear of the deque


void insertRear(int key) {
if (isFull()) {
printf("Overflow: Unable to insert element at the rear. Deque is full.\n");
return;
}

if (rear == -1) { // If deque is initially empty


front = 0;
rear = 0;
} else if (rear == MAX - 1) {
rear = 0; // wrap around
} else {
rear = rear + 1;
}

deque[rear] = key;
printf("Inserted %d at the rear.\n", key);
}

// Function to delete an element from the front of the deque


void deleteFront() {
if (isEmpty()) {
printf("Underflow: Unable to delete element from the front. Deque is empty.\n");
return;
}

int removed = deque[front];

if (front == rear) { // Deque has only one element


front = -1;
rear = -1;
} else if (front == MAX - 1) {
front = 0; // wrap around
} else {
front = front + 1;
}

printf("Deleted %d from the front.\n", removed);


}

// Function to delete an element from the rear of the deque


void deleteRear() {
if (isEmpty()) {
printf("Underflow: Unable to delete element from the rear. Deque is empty.\n");
return;
}

int removed = deque[rear];

if (front == rear) { // Deque has only one element


front = -1;
rear = -1;
} else if (rear == 0) {
rear = MAX - 1; // wrap around
} else {
rear = rear - 1;
}

printf("Deleted %d from the rear.\n", removed);


}

// Function to display the deque


void displayDeque() {
if (isEmpty()) {
printf("Deque is empty.\n");
return;
}

printf("Deque elements are: ");


int i = front;
while (1) {
printf("%d ", deque[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
printf("\n");
}
// Main function to test the operations
int main() {
insertRear(5);
displayDeque();

insertFront(15);
displayDeque();

insertRear(25);
displayDeque();

deleteFront();
displayDeque();

deleteRear();
displayDeque();

return 0;
}
Output:

Inserted 5 at the rear.

Deque elements are: 5

Inserted 15 at the front.

Deque elements are: 15 5

Inserted 25 at the rear.

Deque elements are: 15 5 25

Deleted 15 from the front.

Deque elements are: 5 25

Deleted 25 from the rear.

Deque elements are: 5

Practical No. 6
#include <stdio.h>

#include <stdlib.h>

#define MAX 100

struct Queue {

int items[MAX];

int front, rear;

};

// Function to initialize a queue

void initQueue(struct Queue *q) {

q->front = -1;

q->rear = -1;

// Check if queue is empty

int isEmpty(struct Queue *q) {

return q->front == -1;

// Enqueue operation

void enqueue(struct Queue *q, int value) {

if (q->rear == MAX - 1) {

printf("Queue Overflow\n");

return;
}

if (q->front == -1)

q->front = 0;

q->items[++q->rear] = value;

// Dequeue operation

int dequeue(struct Queue *q) {

if (isEmpty(q)) {

printf("Queue Underflow\n");

return -1;

int value = q->items[q->front];

if (q->front == q->rear)

q->front = q->rear = -1;

else

q->front++;

return value;

// Peek operation

int peek(struct Queue *q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return -1;

}
return q->items[q->rear];

// Stack structure using two queues

struct Stack {

struct Queue q1, q2;

};

// Initialize stack

void initStack(struct Stack *s) {

initQueue(&s->q1);

initQueue(&s->q2);

// Push operation - O(1)

void push(struct Stack *s, int value) {

enqueue(&s->q1, value);

printf("%d pushed to stack.\n", value);

// Pop operation - O(n)

int pop(struct Stack *s) {

if (isEmpty(&s->q1)) {

printf("Stack Underflow\n");

return -1;

}
// Move all elements except last from q1 to q2

while (s->[Link] != s->[Link])

enqueue(&s->q2, dequeue(&s->q1));

// Dequeue last element (top of stack)

int popped = dequeue(&s->q1);

// Swap q1 and q2

struct Queue temp = s->q1;

s->q1 = s->q2;

s->q2 = temp;

return popped;

// Display elements of stack

void display(struct Stack *s) {

if (isEmpty(&s->q1)) {

printf("Stack is empty.\n");

return;

printf("Stack elements: ");

for (int i = s->[Link]; i <= s->[Link]; i++)

printf("%d ", s->[Link][i]);

printf("\n");
}

// Main function

int main() {

struct Stack s;

initStack(&s);

push(&s, 10);

push(&s, 20);

push(&s, 30);

display(&s);

printf("Popped element: %d\n", pop(&s));

display(&s);

printf("Popped element: %d\n", pop(&s));

display(&s);

return 0;

Output:

10 pushed to stack.
20 pushed to stack.

30 pushed to stack.

Stack elements: 10 20 30

Popped element: 30

Stack elements: 10 20

Popped element: 20

Stack elements: 10
Practical No 7
#include <stdio.h>

#include <stdlib.h>

#define MAX 100

struct Queue {

int items[MAX];

int front, rear;

};

// Function to initialize queue

void initQueue(struct Queue *q) {

q->front = -1;

q->rear = -1;

// Check if queue is empty

int isEmpty(struct Queue *q) {

return q->front == -1;

// Enqueue operation

void enqueue(struct Queue *q, int value) {

if (q->rear == MAX - 1) {

printf("Queue Overflow\n");

return;
}

if (q->front == -1)

q->front = 0;

q->items[++q->rear] = value;

// Dequeue operation

int dequeue(struct Queue *q) {

if (isEmpty(q)) {

printf("Queue Underflow\n");

return -1;

int value = q->items[q->front];

if (q->front == q->rear)

q->front = q->rear = -1;

else

q->front++;

return value;

// Display elements of queue

void displayQueue(struct Queue *q) {

if (isEmpty(q)) {

printf("Stack is empty.\n");

return;

}
for (int i = q->front; i <= q->rear; i++)

printf("%d ", q->items[i]);

printf("\n");

// Stack structure using two queues

struct Stack {

struct Queue q1, q2;

};

// Initialize stack

void initStack(struct Stack *s) {

initQueue(&s->q1);

initQueue(&s->q2);

// Push operation - O(n)

void push(struct Stack *s, int value) {

// Step 1: Enqueue into q2

enqueue(&s->q2, value);

// Step 2: Move all elements from q1 to q2

while (!isEmpty(&s->q1)) {

enqueue(&s->q2, dequeue(&s->q1));

}
// Step 3: Swap q1 and q2

struct Queue temp = s->q1;

s->q1 = s->q2;

s->q2 = temp;

printf("%d pushed to stack.\n", value);

// Pop operation - O(1)

int pop(struct Stack *s) {

if (isEmpty(&s->q1)) {

printf("Stack Underflow\n");

return -1;

return dequeue(&s->q1);

// Display stack

void display(struct Stack *s) {

printf("Stack elements: ");

displayQueue(&s->q1);

// Main function

int main() {

struct Stack s;
initStack(&s);

push(&s, 10);

push(&s, 20);

push(&s, 30);

display(&s);

printf("Popped element: %d\n", pop(&s));

display(&s);

printf("Popped element: %d\n", pop(&s));

display(&s);

return 0;

Output:

10 pushed to stack.

20 pushed to stack.

30 pushed to stack.

Stack elements: 30 20 10

Popped element: 30

Stack elements: 20 10

Popped element: 20

Stack elements: 10

Practical No 8
#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100

int stack1[MAX_SIZE];

int top1 = -1;

int stack2[MAX_SIZE];

int top2 = -1;

void push1(int data) {

if (top1 == MAX_SIZE - 1) {

printf("Stack1 overflow\n");

return;

stack1[++top1] = data;

int pop1() {

if (top1 == -1) {

printf("Stack1 underflow\n");

return -1;

return stack1[top1--];

}
void push2(int data) {

if (top2 == MAX_SIZE - 1) {

printf("Stack2 overflow\n");

return;

stack2[++top2] = data;

int pop2() {

if (top2 == -1) {

printf("Stack2 underflow\n");

return -1;

return stack2[top2--];

void enqueue(int data) {

while (top2 != -1) {

push1(pop2());

push1(data);

while (top1 != -1) {

push2(pop1());

}
int dequeue() {

if (top2 == -1) {

printf("Queue is empty\n");

return -1;

return pop2();

void display() {

if (top2 == -1) {

printf("Queue is empty\n");

return;

printf("Queue elements: ");

for (int i = top2; i >= 0; i--) {

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

printf("\n");

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

display();
printf("Dequeued element: %d\n", dequeue());

display();

enqueue(40);

display();

printf("Dequeued element: %d\n", dequeue());

printf("Dequeued element: %d\n", dequeue());

printf("Dequeued element: %d\n", dequeue());

display();

return 0;

Output:

Queue elements: 10 20 30

Dequeued element: 10

Queue elements: 20 30

Queue elements: 20 30 40

Dequeued element: 20

Dequeued element: 30

Dequeued element: 40

Queue is empty

You might also like