DSA_Module 2
DSA_Module 2
23EC29 23EC28
Data Structure & Algorithm
Module 2: Stack and Queues
✓ Disadvantages of Stacks:
• Limited access: Elements in a stack can only be accessed from the top, making it
difficult to retrieve or modify elements in the middle of the stack.
• Potential for overflow: If more elements are pushed onto a stack than it can hold, an
overflow error will occur, resulting in a loss of data.
• Not suitable for random access: Stacks do not allow for random access to
elements, making them unsuitable for applications where elements need to be
accessed in a specific order.
• Limited capacity: Stacks have a fixed capacity, which can be a limitation if the
number of elements that need to be stored is unknown or highly variable.
Disadvantages of Queue:
1) The operations such as insertion and deletion of elements from the middle are time
consuming.
2) In a classical queue, a new element can only be inserted when the existing elements are
deleted from the queue.
3) Searching an element takes O(N) time.
4) Maximum size of a queue must be defined prior in case of array implementation.
CODE
➢ Queue Array
➢ a#include <stdio.h>
➢ #include <stdlib.h>
➢ #define MAX 8
➢
➢ int queue[MAX];
➢ int front = -1;
➢ int rear = -1;
➢
➢ int enqueue(int data) {
➢ if (rear == MAX - 1) {
➢ printf("Queue overflow\n");
➢ return -1;
➢ } else if (rear == -1 && front == -1) {
➢ rear = front = 0;
➢ queue[rear] = data;
➢ } else {
➢ rear++;
➢ queue[rear] = data;
➢ }
➢
➢ return queue[rear];
➢ }
➢
➢ int dequeue() {
➢ if (rear == -1 && front == -1) {
➢ printf("Queue underflow\n");
➢ return -1;
➢ } else if (rear == front) {
➢ int dequeued = queue[front];
➢ rear = front = -1; // Reset the queue
➢ return dequeued;
➢ } else {
➢ int dequeued = queue[front];
➢ front++;
➢ return dequeued;
➢ }
➢ }
➢
➢ void print() {
➢ if (rear == -1 && front == -1) {
➢ printf("Queue is empty\n");
➢ } else {
➢ for (int i = front; i <= rear; i++) {
➢ printf("%d ", queue[i]);
➢ }
➢ printf("\n");
➢ }
➢ }
➢ int main() {
➢ printf("%d as a data entered in queue \n", enqueue(78));
➢ printf("%d as a data entered in queue \n", enqueue(35));
➢ printf("%d as a data entered in queue \n", enqueue(88));
➢ printf("%d as a data removed from queue \n", dequeue());
➢
➢ print();
➢
➢ return 0;
➢ }
➢ Circular Queue Array
➢ #include <stdio.h>
➢ #include <stdlib.h>
➢ #define MAX 8
➢
➢ int queue[MAX];
➢ int front = -1;
➢ int rear = -1;
➢
➢ void enqueue(int x) {
➢ if (front == -1 && rear == -1) {
➢ front = rear = 0;
➢ queue[rear] = x;
➢ }
➢ else if ((rear + 1) % MAX == front) {
➢ printf("Queue overflow\n");
➢ }
➢ else {
➢ rear = (rear + 1) % MAX;
➢ queue[rear] = x;
➢ }
➢ }
➢
➢ void dequeue() {
➢ if (front == -1 && rear == -1) {
➢ printf("Queue underflow\n");
➢ }
➢ else if (front == rear) {
➢ printf("%d dequeued\n", queue[front]);
➢ front = rear = -1;
➢ }
➢ else {
➢ printf("%d dequeued\n", queue[front]);
➢ front = (front + 1) % MAX;
➢ }
➢ }
➢
➢ void print() {
➢ if (front == -1 && rear == -1) {
➢ printf("Queue is empty\n");
➢ }
➢ else {
➢ int i = front;
➢ printf("Queue: ");
➢ while (i != rear) {
➢ printf("%d ", queue[i]);
➢ i = (i + 1) % MAX;
➢ }
➢ printf("%d\n", queue[rear]);
➢ }
➢ }
➢ int main() {
➢ enqueue(54);
➢ enqueue(77);
➢ enqueue(24);
➢ dequeue();
➢ print();
➢
➢ return 0;
➢ }
➢ Queue Linkedlist
➢ #include <stdio.h>
➢ #include <stdlib.h>
➢
➢ struct node {
➢ int data;
➢ struct node *link;
➢ };
➢
➢ struct node *front = NULL;
➢ struct node *rear = NULL;
➢
➢ void enqueue(int x) {
➢ struct node *temp = malloc(sizeof(struct node));
➢ temp->data = x;
➢ temp->link = NULL;
➢
➢ if (front == NULL && rear == NULL) {
➢ front = rear = temp;
➢ } else {
➢ rear->link = temp;
➢ rear = temp;
➢ }
➢ }
➢
➢ void dequeue() {
➢ if (front == NULL) {
➢ printf("Queue is empty\n");
➢ return;
➢ }
➢
➢ struct node *temp = front;
➢ printf("Dequeued: %d\n", front->data);
➢ front = front->link;
➢ free(temp);
➢
➢ if (front == NULL) {
➢ rear = NULL; // If front becomes NULL, rear should also be NULL
➢ }
➢ }
➢
➢ void peek() {
➢ if (front == NULL) {
➢ printf("Queue is empty\n");
➢ } else {
➢ printf("Front element: %d\n", front->data);
➢ }
➢ }
➢ void print() {
➢ if (front == NULL) {
➢ printf("Queue is empty\n");
➢ return;
➢ }
➢
➢ struct node *temp = front;
➢ printf("Queue elements: ");
➢ while (temp != NULL) {
➢ printf("%d ", temp->data);
➢ temp = temp->link;
➢ }
➢ printf("\n");
➢ }
➢
➢ int main() {
➢ enqueue(40);
➢ enqueue(36);
➢ enqueue(80);
➢ enqueue(66);
➢ dequeue();
➢ peek();
➢ print();
➢ return 0;
➢ }