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

DSA Full Queue Assignment

The document provides C code implementations for various types of queues, including a standard queue using arrays, a queue using linked lists, a circular queue using arrays, a deque using arrays, and priority queues using both arrays and linked lists. Each implementation includes functions for enqueueing, dequeueing, displaying elements, and handling overflow and underflow conditions. Sample outputs demonstrate the functionality of each queue type.

Uploaded by

rinonel315
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)
8 views12 pages

DSA Full Queue Assignment

The document provides C code implementations for various types of queues, including a standard queue using arrays, a queue using linked lists, a circular queue using arrays, a deque using arrays, and priority queues using both arrays and linked lists. Each implementation includes functions for enqueueing, dequeueing, displaying elements, and handling overflow and underflow conditions. Sample outputs demonstrate the functionality of each queue type.

Uploaded by

rinonel315
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

1.

Queue using Array

■ C Code:
#include <stdio.h>
#define SIZE 100

int queue[SIZE];
int front = -1, rear = -1;

void enqueue(int value) {


if (rear == SIZE - 1)
printf("Queue Overflow!\n");
else {
if (front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("Inserted %d\n", value);
}
}

void dequeue() {
if (front == -1 || front > rear)
printf("Queue Underflow!\n");
else {
printf("Deleted %d\n", queue[front]);
front++;
}
}

void display() {
if (front == -1 || front > rear)
printf("Queue is Empty\n");
else {
printf("Queue elements: ");
for (int i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}

int main() {
int choice, value;
while (1) {
printf("\n1.Insert 2.Delete 3.Display 4.Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid Choice!\n");
}
}
}

■■ Sample Output:
1.Insert 2.Delete 3.Display 4.Exit
1
Enter value: 10
Inserted 10
1
Enter value: 20
Inserted 20
3
Queue elements: 10 20
2
Deleted 10
3
Queue elements: 20
2. Queue using Linked List

■ C Code:
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node *next;
};
struct node *front = NULL, *rear = NULL;

void enqueue(int value) {


struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = value;
newnode->next = NULL;
if (rear == NULL)
front = rear = newnode;
else {
rear->next = newnode;
rear = newnode;
}
printf("Inserted %d\n", value);
}

void dequeue() {
if (front == NULL)
printf("Queue Underflow!\n");
else {
struct node *temp = front;
printf("Deleted %d\n", temp->data);
front = front->next;
if (front == NULL)
rear = NULL;
free(temp);
}
}

void display() {
if (front == NULL)
printf("Queue is Empty\n");
else {
struct node *temp = front;
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
}

int main() {
int choice, value;
while (1) {
printf("\n1.Insert 2.Delete 3.Display 4.Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid Choice!\n");
}
}
}

■■ Sample Output:
1.Insert 2.Delete 3.Display 4.Exit
1
Enter value: 5
Inserted 5
1
Enter value: 15
Inserted 15
3
Queue elements: 5 15
2
Deleted 5
3. Circular Queue using Array

■ C Code:
#include <stdio.h>
#define SIZE 5

int cq[SIZE];
int front = -1, rear = -1;

void enqueue(int value) {


if ((rear + 1) % SIZE == front)
printf("Queue Overflow!\n");
else {
if (front == -1)
front = rear = 0;
else
rear = (rear + 1) % SIZE;
cq[rear] = value;
printf("Inserted %d\n", value);
}
}

void dequeue() {
if (front == -1)
printf("Queue Underflow!\n");
else {
printf("Deleted %d\n", cq[front]);
if (front == rear)
front = rear = -1;
else
front = (front + 1) % SIZE;
}
}

void display() {
if (front == -1)
printf("Queue Empty\n");
else {
printf("Queue elements: ");
int i = front;
while (1) {
printf("%d ", cq[i]);
if (i == rear)
break;
i = (i + 1) % SIZE;
}
printf("\n");
}
}

int main() {
int choice, value;
while (1) {
printf("\n1.Insert 2.Delete 3.Display 4.Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid Choice!\n");
}
}
}
■■ Sample Output:
1.Insert 2.Delete 3.Display 4.Exit
1
Enter value: 10
Inserted 10
1
Enter value: 20
Inserted 20
3
Queue elements: 10 20
2
Deleted 10
3
Queue elements: 20
4. Dequeue using Array

■ C Code:
#include <stdio.h>
#define SIZE 5

int dq[SIZE];
int front = -1, rear = -1;

void insertFront(int value) {


if ((front == 0 && rear == SIZE - 1) || (front == rear + 1))
printf("Deque Overflow!\n");
else {
if (front == -1)
front = rear = 0;
else if (front == 0)
front = SIZE - 1;
else
front--;
dq[front] = value;
printf("Inserted %d at front\n", value);
}
}

void insertRear(int value) {


if ((front == 0 && rear == SIZE - 1) || (front == rear + 1))
printf("Deque Overflow!\n");
else {
if (front == -1)
front = rear = 0;
else if (rear == SIZE - 1)
rear = 0;
else
rear++;
dq[rear] = value;
printf("Inserted %d at rear\n", value);
}
}

void deleteFront() {
if (front == -1)
printf("Deque Underflow!\n");
else {
printf("Deleted %d from front\n", dq[front]);
if (front == rear)
front = rear = -1;
else if (front == SIZE - 1)
front = 0;
else
front++;
}
}

void deleteRear() {
if (front == -1)
printf("Deque Underflow!\n");
else {
printf("Deleted %d from rear\n", dq[rear]);
if (front == rear)
front = rear = -1;
else if (rear == 0)
rear = SIZE - 1;
else
rear--;
}
}

void display() {
if (front == -1)
printf("Deque is Empty\n");
else {
printf("Deque elements: ");
int i = front;
while (1) {
printf("%d ", dq[i]);
if (i == rear)
break;
i = (i + 1) % SIZE;
}
printf("\n");
}
}

int main() {
int choice, value;
while (1) {
printf("\n1.Insert Front 2.Insert Rear 3.Delete Front 4.Delete Rear 5.Display 6.Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
insertFront(value);
break;
case 2:
printf("Enter value: ");
scanf("%d", &value);
insertRear(value);
break;
case 3:
deleteFront();
break;
case 4:
deleteRear();
break;
case 5:
display();
break;
case 6:
return 0;
default:
printf("Invalid Choice!\n");
}
}
}

■■ Sample Output:
1.Insert Front 2.Insert Rear 3.Delete Front 4.Delete Rear 5.Display 6.Exit
2
Enter value: 10
Inserted 10 at rear
1
Enter value: 5
Inserted 5 at front
5
Deque elements: 5 10
3
Deleted 5 from front
5
Deque elements: 10
5. Priority Queue using Array

■ C Code:
#include <stdio.h>
#define SIZE 100

int pq[SIZE];
int n = 0;

void insert(int value) {


if (n == SIZE)
printf("Queue Overflow!\n");
else {
int i = n - 1;
while (i >= 0 && pq[i] > value) {
pq[i + 1] = pq[i];
i--;
}
pq[i + 1] = value;
n++;
printf("Inserted %d\n", value);
}
}

void delete() {
if (n == 0)
printf("Queue Underflow!\n");
else {
printf("Deleted highest priority element %d\n", pq[0]);
for (int i = 0; i < n - 1; i++)
pq[i] = pq[i + 1];
n--;
}
}

void display() {
if (n == 0)
printf("Queue Empty\n");
else {
printf("Priority Queue: ");
for (int i = 0; i < n; i++)
printf("%d ", pq[i]);
printf("\n");
}
}

int main() {
int choice, value;
while (1) {
printf("\n1.Insert 2.Delete 3.Display 4.Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
insert(value);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid Choice!\n");
}
}
}

■■ Sample Output:
1.Insert 2.Delete 3.Display 4.Exit
1
Enter value: 30
Inserted 30
1
Enter value: 10
Inserted 10
1
Enter value: 20
Inserted 20
3
Priority Queue: 10 20 30
2
Deleted highest priority element 10
6. Priority Queue using Linked List

■ C Code:
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node *next;
};
struct node *front = NULL;

void insert(int value) {


struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = value;
newnode->next = NULL;

if (front == NULL || value < front->data) {


newnode->next = front;
front = newnode;
} else {
struct node *temp = front;
while (temp->next != NULL && temp->next->data <= value)
temp = temp->next;
newnode->next = temp->next;
temp->next = newnode;
}
printf("Inserted %d\n", value);
}

void delete() {
if (front == NULL)
printf("Priority Queue Underflow!\n");
else {
struct node *temp = front;
printf("Deleted highest priority element: %d\n", temp->data);
front = front->next;
free(temp);
}
}

void display() {
if (front == NULL)
printf("Priority Queue Empty\n");
else {
struct node *temp = front;
printf("Priority Queue: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
}

int main() {
int choice, value;
while (1) {
printf("\n1.Insert 2.Delete 3.Display 4.Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
insert(value);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid Choice!\n");
}
}
}

■■ Sample Output:
1.Insert 2.Delete 3.Display 4.Exit
1
Enter value: 5
Inserted 5
1
Enter value: 1
Inserted 1
3
Priority Queue: 1 5
2
Deleted highest priority element: 1

You might also like