58 DS Project
58 DS Project
ON
BACHELOR OF TECHNOLOGY IN
COMPUTER SCIENCE AND ENGINEERING (DATA SCIENCE)
By
L.ROHINI
22W91A6734
Mrs T.Prashanthi
Assistant Professor
HYDERABAD
2023-2024
1
MALLA REDDY INSTITUTE OF ENGINEERING & TECHNOLOGY
(Autonomous)
Department of IT&ET
CERTIFICATE
2
ACKNOWLEDGEMENT
L.ROHINI
22W91A6734
3
CONTENTS
4
ABSTRACT
5
INTRODUCTION
7
OBJECTIVES/THEORY
1. Array-based Implementation:
Understand queue as a linear data structure.
Implement enqueue and dequeue using a fixed-size
array. Analyze advantages and limitations.
2. Pointer-based Implementation:
Explore linked list representation for queues.
Implement dynamic enqueue and dequeue using
pointers. Evaluate benefits and drawbacks.
3. Comparative Analysis:
Compare time and space complexity. Assess
efficiency and ease of implementation.
4. Dynamic Memory Management:
Implement dynamic memory allocation for
pointer-based queues. Avoid overflow and
underflow conditions.
5. Real-world Applications:
Apply queues to practical scenarios. Choose
implementation based on application needs.
8
ALGORITHM USING ARRAY
1.INSERTION:
9
ALGORITHM USING POINTER
1.INSERTION:
2.DELETION:
10
address in which the last element to be deleted
is sort
3.DISPLAY:
11
HARDWARE REQUIREMENTS
1. Memory: Sufficient memory should be available
to store the array elements and to handle pointer
operations.
2. Processor: A standard processor capable of
handling array manipulation and pointer operations
efficiently.
SOFTWARE REQUIREMENTS
1. Programming Language: You would need a
programming language that supports pointer
manipulation, such as C or C++.
2. Compiler: An appropriate compiler for the
chosen programming language to convert the
source code into machine code executable by the
processor.
3. Operating System: A compatible operating
system that supports the chosen programming
language and its associated compiler.
12
CODE USING ARRAY
/*Array Implementation Queue
ADT*/
#include <stdio.h>
#define MAX_SIZE 10
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
void enqueue(int data) {
if (rear == MAX_SIZE - 1) {
printf("Queue is full. Cannot enqueue.\n");
return;
}
if (front == -1) {
front = 0;
}
rear++;
13
queue[rear] = data;
printf("%d enqueued to the queue.\n", data);
}
int dequeue() {
if (front == -1) {
printf("Queue is empty. Cannot dequeue.\n");
return -1; // Return a special value indicating
an error
}
int data = queue[front];
front++;
if (front > rear) {
front = rear = -1; // Reset front and rear when
the last element is dequeued
}
printf("%d dequeued from the queue.\n", data);
return data;
}
void displayQueue() {
if (front == -1) {
14
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
displayQueue();
dequeue();
displayQueue();
return 0;
}
15
CODE USING POINTER
/*Linked List Implementation
of Queue*/
#include <stdio.h>
#include <stdlib.h>
struct QueueNode {
int data;
struct QueueNode* next;
};
struct Queue {
struct QueueNode* front;
struct QueueNode* rear;
};
void initializeQueue(struct Queue* queue) {
queue->front = NULL;
queue->rear = NULL;
}
void enqueue(struct Queue* queue, int data) {
struct QueueNode* newNode = (struct
QueueNode*)malloc(sizeof(struct QueueNode));
16
newNode->data = data;
newNode->next = NULL;
if (queue->front == NULL) {
queue->front = newNode;
queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
printf("%d enqueued to the queue.\n", data);
}
int dequeue(struct Queue* queue) {
if (queue->front == NULL) {
printf("Queue is empty. Cannot dequeue.\n");
return -1; // Return a special value indicating
an error
}
struct QueueNode* temp = queue->front;
int data = temp->data;
if (queue->front == queue->rear) {
17
queue->front = NULL;
queue->rear = NULL;
} else {
queue->front = queue->front->next;
}
free(temp);
printf("%d dequeued from the queue.\n", data);
return data;
}
void displayQueue(struct Queue* queue) {
if (queue->front == NULL) {
printf("Queue is empty.\n");
return;
}
struct QueueNode* current = queue->front;
printf("Queue elements: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
18
printf("\n");
}
int main() {
struct Queue queue;
initializeQueue(&queue);
enqueue(&queue, 10);
enqueue(&queue, 20);
enqueue(&queue, 30);
displayQueue(&queue);
dequeue(&queue);
displayQueue(&queue);
return 0;
}
19
OUTPUT USING ARRAY
20
CONCLUSION
21
REFERENCE
22