Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Stack Queue Using Array

Stack Queue Using Array

Uploaded by

stdesai1005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Stack Queue Using Array

Stack Queue Using Array

Uploaded by

stdesai1005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Aim: Queue Implementation using Array

#include <stdio.h>

#include <stdlib.h>

#define SIZE 100

int queue[SIZE];

int front = -1;

int rear = -1;

void enqueue(int value) {

if (rear == SIZE - 1)

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

else {

if (front == -1)

front = 0;

rear++;

queue[rear] = value;

printf("Inserted %d\n", value);

void dequeue() {

if (front == -1)

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

else {

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

front++;
if (front > rear)

front = rear = -1;

void display() {

if (front == -1)

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

else {

printf("Queue elements are: ");

for (int i = front; i <= rear; i++)

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

printf("\n");

int main() {

int choice, value;

while (1) {

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

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value to be inserted: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:
dequeue();

break;

case 3:

display();

break;

case 4:

exit(0);

default:

printf("Invalid choice!\n");

return 0;

Aim: stack Implementation using Array


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

#define MAX 10

int stack[MAX];
int top = -1;

void push(int value) {


if (top == MAX - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = value;
printf("Inserted %d\n", value);
}
}

int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
} else {
return stack[top--];
}
}

int peek() {
if (top == -1) {
printf("Stack is empty\n");
return -1;
} else {
return stack[top];
}
}
int isEmpty() {
return top == -1;
}

int isFull() {
return top == MAX - 1;
}

int main() {
push(10);
push(20);
push(30);
printf("Top element is %d\n", peek());
printf("Stack full: %s\n", isFull() ? "true" : "false");
printf("Stack empty: %s\n", isEmpty() ? "true" : "false");
printf("Popped element is %d\n", pop());
printf("Top element is %d\n", peek());
return 0;
}

You might also like