0% found this document useful (0 votes)
19 views12 pages

Queue & Stack

The document discusses implementing queue and stack data structures using both arrays and linked lists. It includes functions for initialization, checking for empty/full, insertion, removal and peeking at elements for both queue and stack implementations.

Uploaded by

armanmund27
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)
19 views12 pages

Queue & Stack

The document discusses implementing queue and stack data structures using both arrays and linked lists. It includes functions for initialization, checking for empty/full, insertion, removal and peeking at elements for both queue and stack implementations.

Uploaded by

armanmund27
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/ 12

Queue in array

#include <stdio.h>

#define MAX_SIZE 100

// Structure to represent queue

struct Queue {

int arr[MAX_SIZE];

int front, rear;

};

// Function to initialize queue

void initialize(struct Queue *q) {

q->front = -1;

q->rear = -1;

// Function to check if queue is empty

int isEmpty(struct Queue *q) {

return (q->front == -1 && q->rear == -1);

// Function to check if queue is full

int isFull(struct Queue *q) {

return (q->rear == MAX_SIZE - 1);

// Function to enqueue element into queue

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

if (isFull(q)) {

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

if (isEmpty(q)) {

q->front = q->rear = 0;

} else {

q->rear++;

q->arr[q->rear] = data;

// Function to dequeue element from queue

int dequeue(struct Queue *q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return -1;

int dequeuedData = q->arr[q->front];

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

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

} else {

q->front++;

return dequeuedData;

// Function to peek at the front element of queue

int peek(struct Queue *q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return -1;

}
return q->arr[q->front];

void printQueue(struct Queue *q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return;

int i = q->front;

printf("Queue: ");

while (i != q->rear) {

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

i = (i + 1) % MAX_SIZE;

printf("%d\n", q->arr[i]);

int main() {

struct Queue q;

initialize(&q);

enqueue(&q, 10);

enqueue(&q, 20);

enqueue(&q, 30);

printf("Front element: %d\n", peek(&q));

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

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

printf("Front element: %d\n", peek(&q));

printQueue(&q);
return 0;

Queue using linked list


#include <stdio.h>

#include <stdlib.h>

// Structure to represent a node in the linked list

struct Node {

int data;

struct Node* next;

};

// Structure to represent queue

struct Queue {

struct Node* front;

struct Node* rear;

};

// Function to initialize queue

void initialize(struct Queue* q) {

q->front = NULL;

q->rear = NULL;

// Function to check if queue is empty

int isEmpty(struct Queue* q) {

return (q->front == NULL && q->rear == NULL);

// Function to enqueue element into queue


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

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (newNode == NULL) {

printf("Memory allocation failed\n");

return;

newNode->data = data;

newNode->next = NULL;

if (isEmpty(q)) {

q->front = q->rear = newNode;

} else {

q->rear->next = newNode;

q->rear = newNode;

// Function to dequeue element from queue

int dequeue(struct Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return -1;

struct Node* temp = q->front;

int dequeuedData = temp->data;

q->front = temp->next;

free(temp);

if (q->front == NULL) {

q->rear = NULL;

return dequeuedData;

}
// Function to peek at the front element of queue

int peek(struct Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return -1;

return q->front->data;

void printQueue(struct Queue* q) {

struct Node* temp = q->front;

if (temp == NULL) {

printf("Queue is empty\n");

return;

printf("Queue: ");

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

int main() {

struct Queue q;

initialize(&q);

enqueue(&q, 10);

enqueue(&q, 20);

enqueue(&q, 30);
printf("Front element: %d\n", peek(&q));

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

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

printf("Front element: %d\n", peek(&q));

printQueue(&q);

return 0;

Stack using array


#include <stdio.h>

#define MAX_SIZE 100

// Structure to represent stack

struct Stack {

int arr[MAX_SIZE];

int top;

};

// Function to initialize stack

void initialize(struct Stack *s) {

s->top = -1;

// Function to check if stack is empty

int isEmpty(struct Stack *s) {

return (s->top == -1);

// Function to check if stack is full


int isFull(struct Stack *s) {

return (s->top == MAX_SIZE - 1);

// Function to push element onto stack

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

if (isFull(s)) {

printf("Stack Overflow\n");

return;

s->arr[++s->top] = data;

// Function to pop element from stack

int pop(struct Stack *s) {

if (isEmpty(s)) {

printf("Stack Underflow\n");

return -1;

return s->arr[s->top--];

// Function to peek at the top element of stack

int peek(struct Stack *s) {

if (isEmpty(s)) {

printf("Stack is empty\n");

return -1;

return s->arr[s->top];

void print_stack(Stack *stack) {


if (isEmpty(stack)) {

printf("Stack is empty\n");

return;

printf("Stack: ");

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

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

printf("\n");

int main() {

struct Stack s;

initialize(&s);

push(&s, 10);

push(&s, 20);

push(&s, 30);

printf("Top element: %d\n", peek(&s));

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

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

printf("Top element: %d\n", peek(&s));

print_stack(&s);

return 0;

Stack using linked list


#include <stdio.h>

#include <stdlib.h>
// Structure to represent a node in the linked list

struct Node {

int data;

struct Node* next;

};

// Structure to represent stack

struct Stack {

struct Node* top;

};

// Function to initialize stack

void initialize(struct Stack* s) {

s->top = NULL;

// Function to check if stack is empty

int isEmpty(struct Stack* s) {

return (s->top == NULL);

// Function to push element onto stack

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

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (newNode == NULL) {

printf("Memory allocation failed\n");

return;

newNode->data = data;

newNode->next = s->top;
s->top = newNode;

// Function to pop element from stack

int pop(struct Stack* s) {

if (isEmpty(s)) {

printf("Stack Underflow\n");

return -1;

struct Node* temp = s->top;

int poppedData = temp->data;

s->top = temp->next;

free(temp);

return poppedData;

// Function to peek at the top element of stack

int peek(struct Stack* s) {

if (isEmpty(s)) {

printf("Stack is empty\n");

return -1;

return s->top->data;

void printStack(struct Stack* s) {

struct Node* current = s->top;

printf("Stack: ");

while (current != NULL) {

printf("%d -> ", current->data);

current = current->next;

}
printf("NULL\n");

int main() {

struct Stack s;

initialize(&s);

push(&s, 10);

push(&s, 20);

push(&s, 30);

printf("Top element: %d\n", peek(&s));

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

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

printf("Top element: %d\n", peek(&s));

printStack(&s);

return 0;

You might also like