0% found this document useful (0 votes)
23 views

Data Structures Using C

The document discusses implementing queue data structures in C. It includes code to create, add, remove, and print elements from both a standard queue and a circular queue. Functions are provided to check if the queues are empty or full and to handle overflow and underflow cases.

Uploaded by

dehel55536
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)
23 views

Data Structures Using C

The document discusses implementing queue data structures in C. It includes code to create, add, remove, and print elements from both a standard queue and a circular queue. Functions are provided to check if the queues are empty or full and to handle overflow and underflow cases.

Uploaded by

dehel55536
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/ 6

#Week5

Exp1: Write a program to implement operations on queue structure.

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
// Queue structure
typedef struct {
int data[MAX_SIZE];
int front;
int rear;
} Queue;
// Function to create a new queue
Queue* createQueue() {
Queue* queue = (Queue*) malloc(sizeof(Queue));
queue->front = -1;
queue->rear = -1;
return queue;
}
// Function to check if queue is empty
int isEmpty(Queue* queue) {
return queue->front == -1;
}
// Function to check if queue is full
int isFull(Queue* queue) {
return queue->rear == MAX_SIZE - 1;
}
// Function to add an element to the queue
void enqueue(Queue* queue, int element) {
if (isFull(queue)) {
printf("Queue is full.\n");
} else {
if (isEmpty(queue)) {
queue->front = 0;
}
queue->rear++;
queue->data[queue->rear] = element;
}
}
// Function to remove an element from the queue
int dequeue(Queue* queue) {
int element;
if (isEmpty(queue)) {
printf("Queue is empty.\n");
element = -1;
} else {
element = queue->data[queue->front];
if (queue->front == queue->rear) {
queue->front = -1;
queue->rear = -1;
} else {
queue->front++;
}
}
return element;
}
// Function to print the elements in the queue
void printQueue(Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
} else {
printf("Queue: ");
for (int i = queue->front; i <= queue->rear; i++) {
printf("%d ", queue->data[i]);
}
printf("\n");
}
}
// Main function
int main() {
Queue* queue = createQueue();
// Enqueue elements
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
// Print the queue
printQueue(queue);
// Dequeue an element
int element = dequeue(queue);
printf("Dequeued element: %d\n", element);
// Print the queue again
printQueue(queue);
return 0;
}

Exp2: Write a program to implement operations on circular queue.

#include <stdio.h>
# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.
}
}

// function to delete the element from the queue


int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x; // variables declaration
while(choice<4 && choice!=0) // while loop
{
printf("\n Press 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter the element which is to be inserted");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}
}
return 0;
}

You might also like