You are on page 1of 11

Name : - Badal Sarkar, Reg. No.

: - 2021UGEC047, CS Assignment, Due date 07/04/22


17/01/2022

CS ASSIGNMENT 13
[Queues]

NAME: BADAL SARKAR

REG NO. : 2021UGEC047

March 31, 2022


Name : - Badal Sarkar, Reg. No. : - 2021UGEC047, CS Assignment, Due date 07/04/22
17/01/2022

Program1: Write a program using a simple queue which will take 4 choices from the user:
1.Enqueue
2.Dequeue
3.Displaying all elements present in the queue
4.Exit

##CODE ->

#include <stdio.h>
#include <stdlib.h>
typedef struct queue{
int data;
struct queue* next;
}queue;
int main()
{
queue * front = NULL;
int choice = 0,val = 0;
while(choice != 4){
printf("\nEnter 1 for enqueue : ");
printf("Enter 2 for dequeue : ");
printf("Enter 3 for displaying all elements
present in the queue : ");
printf("Enter 4 for exit : ");
scanf("%d",&choice);
switch(choice){
case 1:
queue * ptr =
(queue*)malloc(sizeof(queue));
printf("Enter data : ");
scanf("%d",&val);
ptr->data = val;
ptr->next = front;
front = ptr;
break;
Name : - Badal Sarkar, Reg. No. : - 2021UGEC047, CS Assignment, Due date 07/04/22
17/01/2022

case 2:
if(front == NULL)
printf("Queue underflow \n");
else{
printf("The removed element is
%d",front->data);
front = front->next;

}break;
case 3:
if(front == NULL)
printf("Empty queue\n");
else{
queue* address = front;
printf("The elements are : ");
while(address != NULL){
printf("%d ", address-
>data);
address = address->next;
}
}

}
}
return 0;
}

Program2: Write a program using a circular queue which will take 4 choices from the user:
1.Enqueue
2.Dequeue
3.Displaying all elements present in the queue
4.Exit

##CODE ->
Name : - Badal Sarkar, Reg. No. : - 2021UGEC047, CS Assignment, Due date 07/04/22
17/01/2022

#include <stdio.h>
#include <stdlib.h>
typedef struct c_queue{
int data;
struct c_queue* next;
}c_queue;
int main()
{
c_queue * front = NULL, *rear =
(c_queue*)malloc(sizeof(c_queue));
int choice = 0,val = 0;
while(choice != 4){
printf("\nEnter 1 for enqueue : ");
printf("Enter 2 for dequeue : ");
printf("Enter 3 for displaying all elements
present in the queue : ");
printf("Enter 4 for exit : ");
scanf("%d",&choice);
switch(choice){
case 1:
c_queue * ptr =
(c_queue*)malloc(sizeof(c_queue));
printf("Enter data : ");
scanf("%d",&val);
ptr->data = val;
ptr->next = front;

if(front == NULL){
rear = ptr;
}

front = ptr;
rear->next = front;
Name : - Badal Sarkar, Reg. No. : - 2021UGEC047, CS Assignment, Due date 07/04/22
17/01/2022

break;
case 2:
if(front == NULL)
printf("c_queue underflow \n");
else{
printf("The removed element is
%d",front->data);
front = front->next;
rear = front;
}
break;
case 3:
if(front == NULL)
printf("Empty c_queue\n");
else{
c_queue* address = front;
printf("The elements are : %d
", address->data);
address = address->next;

while(address != front){
printf("%d ", address-
>data);
address = address->next;
}
}

}
}
return 0;
}
Name : - Badal Sarkar, Reg. No. : - 2021UGEC047, CS Assignment, Due date 07/04/22
17/01/2022

Program3: Devise an implementation of queue using two stacks.

##CODE ->

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct stack{
int data;
struct stack* next;
} stack;

int main()
{
stack * top1 = NULL;
stack * top2 = NULL;

int choice = 0;
while(choice!=4){
printf("\nEnter 1 if you want to enqueue :
");
printf("Enter 2 if you want to dequeue :
");
printf("Enter 3 if you want to display :
");
printf("Enter 4 if you want to exit : ");
scanf("%d",&choice);
switch(choice){
case 1:
stack* ptr = (stack*)
malloc(sizeof(stack));
if(ptr == NULL)
Name : - Badal Sarkar, Reg. No. : - 2021UGEC047, CS Assignment, Due date 07/04/22
17/01/2022

printf("Stack Overflow\n");
else{
printf("Enter the value you
want to push : ");
int val; scanf("%d",&val);

ptr->next = top1;
ptr->data = val;
top1 = ptr;
}

break;
case 2:

top2 = NULL;
stack* address = top1;
while(address != NULL){
stack* ptr = (stack*)
malloc(sizeof(stack));
ptr->next = top2;
ptr->data = address->data;
top2 = ptr;
address = address->next;
}

top2 = top2->next;

top1 = NULL;
address = top2;
while(address != NULL){
stack* ptr = (stack*)
malloc(sizeof(stack));
Name : - Badal Sarkar, Reg. No. : - 2021UGEC047, CS Assignment, Due date 07/04/22
17/01/2022

ptr->next = top1;
ptr->data = address->data;
top1 = ptr;
address = address->next;
}
break;
case 3:

top2 = NULL;
address = top1;
while(address != NULL){
stack* ptr = (stack*)
malloc(sizeof(stack));
ptr->next = top2;
ptr->data = address->data;
top2 = ptr;
address = address->next;
}
top1 = NULL;
address = top2;
while(address != NULL){
stack* ptr = (stack*)
malloc(sizeof(stack));
ptr->next = top1;
ptr->data = address->data;
top1 = ptr;
address = address->next;
}

address = top2;
Name : - Badal Sarkar, Reg. No. : - 2021UGEC047, CS Assignment, Due date 07/04/22
17/01/2022

printf("The contents of the stack


are : ");
while(address!= NULL){

printf("%d ",address->data);
address = address->next;
}

break;
case 4:
exit(0);
default:
printf("Invalid Input");
}
}

return 0;
}

Program4: Consider a string of characters, S, consisting of only characters (, ), [, ] and {, }. We say


that S is balanced if it has one of the following forms:
•S = “”, i.e. it is of length zero
•S = “(T)”
•S = “[T]”
•S = “{T}”
•S = “TU”
where both T and U are balanced strings. In other words, for every left parenthesis, bracket or
brace, there is a corresponding right parenthesis, bracket or brace.
Ex: “{()[()]}” is balanced, but “([)]” is not.
Write a program that uses a stack of characters to test whether a given string is balanced.

##CODE ->

#include <stdio.h>
#include <string.h>
Name : - Badal Sarkar, Reg. No. : - 2021UGEC047, CS Assignment, Due date 07/04/22
17/01/2022

#include <stdlib.h>
typedef struct stack{
char data;
struct stack* next;
} stack;

int main()
{
stack * top = NULL;
char str[100];
printf("Enter the string : ");
scanf("%s",&str);

for(int i =0;i<strlen(str);++i){
if(top == NULL){
stack* ptr = (stack*)
malloc(sizeof(stack));
ptr->next = top;
ptr->data = str[i];
top = ptr;
}
else if( (top->data == '(' && str[i]== ')')
||(top->data == '{' && str[i] == '}') ||(top->data
=='[' && str[i]==']')){
top = top->next;
}
else{
stack* ptr = (stack*)
malloc(sizeof(stack));
ptr->next = top;
ptr->data = str[i];
top = ptr;
}
Name : - Badal Sarkar, Reg. No. : - 2021UGEC047, CS Assignment, Due date 07/04/22
17/01/2022

}
if(top == NULL)
printf("%s is balanced string",str);
else
printf("%s is unbalanced string",str);

return 0;
}

You might also like