Skip to main content

Queue | Unit-2 | DSA

 https://www.programiz.com/dsa/queue


Array Implementaion of queue:

#include <stdio.h>


#define MAX_SIZE 100 // Maximum size of the queue


int queue[MAX_SIZE]; // Array to store the queue elements

int front = -1;      // Front of the queue

int rear = -1;       // Rear of the queue


// Function to check if the queue is empty

int is_empty() {

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

}


// Function to check if the queue is full

int is_full() {

    return (rear == MAX_SIZE - 1);

}


// Function to insert an element into the queue

void enqueue(int value) {

    if (is_full()) {

        printf("Queue Overflow\n");

        return;

    }

    if (is_empty()) {

        front = rear = 0; // If the queue is empty, initialize front and rear to 0

    } else {

        rear++; // Move rear to the next position

    }

    queue[rear] = value; // Insert the value at the rear position

}


// Function to remove an element from the queue

int dequeue() {

    if (is_empty()) {

        printf("Queue Underflow\n");

        return -1;

    }

    int dequeued_element = queue[front]; // Get the element at the front

    if (front == rear) {

        front = rear = -1; // If the queue has only one element, reset front and rear

    } else {

        front++; // Move front to the next position

    }

    return dequeued_element; // Return the removed element

}


// Function to display the queue elements

void display() {

    if (is_empty()) {

        printf("Queue is empty\n");

        return;

    }

    printf("Queue elements are: ");

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

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

    }

    printf("\n");

}


int main() {

    // Insert elements into the queue

    enqueue(10);

    enqueue(20);

    enqueue(30);


    // Display the queue

    display();


    // Remove an element from the queue

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


    // Display the updated queue

    display();


    return 0;

}


Linked list  Implementaion of Queue:


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

// Node structure for the linked list
struct Node {
    int data;
    struct Node* next;
};

// Structure for the queue
struct Queue {
    struct Node *front, *rear;
};

// Function to create a new node
struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (!newNode) {
        printf("Memory allocation failed.\n");
        exit(1);
    }
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

// Function to initialize an empty queue
struct Queue* createQueue() {
    struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
    if (!queue) {
        printf("Memory allocation failed.\n");
        exit(1);
    }
    queue->front = queue->rear = NULL;
    return queue;
}

// Function to check if the queue is empty
int is_empty(struct Queue* queue) {
    return (queue->front == NULL);
}

// Function to enqueue an element into the queue
void enqueue(struct Queue* queue, int value) {
    struct Node* newNode = createNode(value);
    if (is_empty(queue)) {
        queue->front = queue->rear = newNode;
    } else {
        queue->rear->next = newNode;
        queue->rear = newNode;
    }
}

// Function to dequeue an element from the queue
int dequeue(struct Queue* queue) {
    if (is_empty(queue)) {
        printf("Queue underflow\n");
        exit(1);
    }
    int value = queue->front->data;
    struct Node* temp = queue->front;
    queue->front = queue->front->next;
    free(temp);
    return value;
}

// Function to display the queue elements
void display(struct Queue* queue) {
    if (is_empty(queue)) {
        printf("Queue is empty\n");
        return;
    }
    struct Node* current = queue->front;
    printf("Queue elements are: ");
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

int main() {
    struct Queue* queue = createQueue();

    // Enqueue elements into the queue
    enqueue(queue, 10);
    enqueue(queue, 20);
    enqueue(queue, 30);

    // Display the queue
    display(queue);

    // Dequeue an element from the queue
    printf("Dequeued element: %d\n", dequeue(queue));

    // Display the updated queue
    display(queue);

    return 0;
}


Comments

Popular posts from this blog

linkedlist

  Linked Lists: An Overview Definition: A linked list is a linear data structure consisting of a sequence of elements called nodes. Each node contains data and a reference (or pointer) to the next node in the sequence. Properties: Dynamic Size : Linked lists can grow or shrink in size dynamically as elements are added or removed. Flexibility : They allow for efficient insertion and deletion operations at any position in the list. Non-Contiguous Memory Allocation : Unlike arrays, linked list nodes can be scattered in memory, connected only by pointers. Types of Linked Lists: Singly Linked List: In a singly linked list, each node contains data and a pointer to the next node in the sequence. Doubly Linked List: In a doubly linked list, each node contains data and pointers to both the next and previous nodes, allowing bidirectional traversal. Circular Linked List: In a circular linked list, the last node points back to the first node, forming a circular structure. Advantages of Linked ...

Tools and Methods Used In Cyber Crimes | Proxy Servers and Anonymizers | AKTU B.Tech 2nd year Cyber Security Unit 3 Notes

  Introduction to Tools and Methods in Cybercrime Cybercrime Landscape: The cyber landscape is evolving rapidly, and so are the tools and methods employed by malicious actors. Understanding these elements is essential for defending against cyber threats. Here, we'll delve into three prominent areas of concern. Proxy Servers Proxy Server Basics: Definition: An intermediate server between a user's device and the internet. Manages requests and responses between the user and websites. Functionality: User's request ➔ Proxy server ➔ Website. Website's response ➔ Proxy server ➔ User's device. Benefits of Proxy Servers: Anonymity: Conceals user's identity by presenting the proxy's IP address to websites. Security: Acts as a buffer against malware, viruses, and online attacks. Access Control: Configurable to block or allow specific types of traffic, enhancing control. Types of Proxy Servers: Forward Proxy: Sits between client and internet. Forwards client's requ...