0% found this document useful (0 votes)
9 views9 pages

CS162 - 3

The document outlines an assessment for a Data Structures Lab course, focusing on implementing a queue using an array, a singly linked list, and a circular queue using an array. It includes student and faculty details, submission date, and specific programming tasks with code examples for each queue implementation. The document concludes with observations on the functionality of each queue type.

Uploaded by

krishajudiya21
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)
9 views9 pages

CS162 - 3

The document outlines an assessment for a Data Structures Lab course, focusing on implementing a queue using an array, a singly linked list, and a circular queue using an array. It includes student and faculty details, submission date, and specific programming tasks with code examples for each queue implementation. The document concludes with observations on the functionality of each queue type.

Uploaded by

krishajudiya21
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/ 9

Semester Course Code Course Title

Data Structures Lab


Winter 2024-25 CS162

Student Details: Student Name: Ajudiya Krish Prafulkumar


Roll/Reg No: 202411005
Email: [email protected]
Mobile: 8799400820
Faculty Details: Faculty Name: Dr. VENKATA PHANIKRISHNA B
Department: Computer Science and Engineering
Email: [email protected]
Assessment No: 3
Assessment Title. Implement Queue using Array and Linked List

Date of Submission 7-Feb-2025

Implement Queue using Array and Linked List


 A3: P1@Write a program to develop a queue using an array.
 A3: P2@Write a program to develop a queue using a singly linked list.
 A3: P3@Write a program to develop a Circular queue using an array.
Question 1 Write a program to develop a queue using an array.

Program #include <stdio.h>


#include <stdlib.h>

struct queue{
int size;
int f;
int b;
int *arr;
};
int n=0;

void display(struct queue *q){


if(q->b != -1){
printf("Queue elements: ");
for(int i=q->f;i<=(q->b);i++){
printf("%d ",q->arr[i]);
}
printf("\n");

printf("Front Index: %d, Front Value: %d\n",q->f,q->arr[q->f]);


printf("Rear Index: %d, Rear Value: %d\n",q->b,q->arr[q->b]);
}else{
printf("Front Index: %d, Front Value: NoVal\n",q->f);
printf("Rear Index: %d, Rear Value: NoVal\n",q->b);
}
}

void push(struct queue *q){

for(int i=0;i<n;i++)
{
scanf("%d",&q->arr[i]);
q->b++;
}
if(q->b > -1) q->f++;
}

void pop(struct queue *q){


if(q->b == -1){
printf("Queue is empty. Cannot dequeue.\n");
printf("Front Index: -1, Front Value: NoVal\n");
printf("Rear Index: -1, Rear Value: NoVal\n");
}else{
printf("Before Dequeue\n");
display(q);
printf("After Dequeue\n");
printf("%d dequeued.\n",q->arr[q->f]);
q->f++;
display(q);

}
}

void empty(struct queue *q){


if(q->b == -1) printf("Queue is empty.\n");
}

void full(struct queue *q){


if(q->b == n-1 && q->b !=q->f) printf("Queue is full.\n");
else printf("Queue is not full.");
}

int main(){

int ch;
scanf("%d",&ch);
struct queue *q=(struct queue *)malloc(sizeof(struct queue));
q->size=n;
q->f= -1;
q->b= -1;
q->arr=(int *)malloc(sizeof(int)*q->size);

while(ch!=6){
if(ch==1) scanf("%d",&n);
switch(ch){
case 1:
push(q);
break;
case 2:
pop(q);
break;
case 3:
display(q);
break;
case 4:
empty(q);
break;
case 5:
full(q);
break;
}

if(ch!=1){
break;
}
scanf("%d",&ch);
}
}
Output:

Your This program implements a queue using a struct, allowing users to enqueue, dequeue, check
Observation if the queue is empty or full, and display its elements.

Question 2 Write a program to develop a queue using a singly linked list.

Program #include <stdio.h>


#include <stdlib.h>
int n;

struct node{
int data;
struct node *next;
};

struct node *head=NULL;

void display(){
struct node *p=head;
int v;
if(head==NULL){
printf("Queue is empty.\n");
printf("Front Value: NULL\n");
printf("Rear Value: NULL\n");
}else{
printf(" Queue elements: ");
while(p !=NULL){
printf("%d ",p->data);
if(p->next==NULL) v=p->data;
p=p->next;
}
printf("\n");
printf("Front Value: %d\n",head->data);
printf("Rear Value: %d\n",v);
}

void push(){
for(int i=0;i<n;i++){
int val;
scanf("%d",&val);
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=NULL;
if(head==NULL){
head=newnode;
}
else{
struct node *p=head;
while(p->next != NULL){
p=p->next;
}
p->next=newnode;
}
}
}

void pop(){
if(head==NULL){
printf(" Enter your choice: 2\n");
printf("Before Dequeue\n");
printf("Queue is empty.\n");
printf("Front Value: NULL\n");
printf("Rear Value: NULL\n");
printf("After Dequeue\n");
printf("Queue is empty. Cannot dequeue.\n");
}else{
printf("Before Dequeue\n");
display();
printf("After Dequeue\n");
printf("%d dequeued.\n",head->data);
struct node *p=head;
head=head->next;
free(p);
display();
}
}

void empty(){
if(head==NULL) printf("Queue is empty.\n");
else printf("Queue is not empty.\n");
}

int main(){
int ch;
scanf("%d",&ch);

while(ch!=5){
switch(ch){
case 1:
scanf("%d",&n);
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
empty();
break;
}

if(ch!=1){
break;
}
scanf("%d",&ch);
}
}
Output:
Your This program implements a queue using a linked list, allowing users to enqueue, dequeue,
Observation check if the queue is empty or full, and display its elements

Question 3 A3: P3@Write a program to develop a Circular queue using an array.

Program #include <stdio.h>


#include <stdlib.h>

struct queue{
int size;
int f;
int b;
int *arr;
};
int n=0;

void display(struct queue *q){


if(q->b == -1 || q->f>q->b){
printf("Queue is empty.\n");
printf("Front Value: NULL\n");
printf("Rear Value: NULL\n");
} else{
printf("Queue elements: ");
for(int i=q->f;i<=(q->b);i++){
printf("%d ",q->arr[i]);
}
printf("\n");

printf("Front Value: %d\n",q->arr[q->f]);


printf("Rear Value: %d\n",q->arr[q->b]);
}
}

void push(struct queue *q){

for(int i=0;i<n;i++)
{
scanf("%d",&q->arr[i]);
q->b++;
}
if(q->b > -1) q->f++;
}

void pop(struct queue *q){


if(q->b == -1){
printf("Before Dequeue\n");
printf("Queue is empty.\n");
printf("Front Value: NULL\n");
printf("Rear Value: NULL\n");
printf("After Dequeue\n");
printf("Queue is empty. Cannot dequeue.\n");
}else{
printf("Before Dequeue\n");
display(q);
printf("After Dequeue\n");
printf("%d dequeued.\n",q->arr[q->f]);
q->f++;
display(q);

}
}

void empty(struct queue *q){


if(q->b == -1) printf("Queue is empty.\n");
else printf("Queue is not empty.\n");
}

void full(struct queue *q){


if(q->b == n-1 && q->b !=q->f) printf("Queue is full.\n");
else printf("Queue is not full.");
}

int main(){

int ch;
scanf("%d",&ch);
struct queue *q=(struct queue *)malloc(sizeof(struct queue));
q->size=n;
q->f= -1;
q->b= -1;
q->arr=(int *)malloc(sizeof(int)*q->size);

while(ch!=6){
if(ch==1) scanf("%d",&n);
switch(ch){
case 1:
push(q);
break;
case 2:
pop(q);
break;
case 3:
display(q);
break;
case 4:
empty(q);
break;
case 5:
full(q);
break;
}

if(ch!=1){
break;
}
scanf("%d",&ch);
}
}
Output:

Your The code implements a circular queue using an array


Observation

You might also like