CS162 - 3
CS162 - 3
struct queue{
int size;
int f;
int b;
int *arr;
};
int n=0;
for(int i=0;i<n;i++)
{
scanf("%d",&q->arr[i]);
q->b++;
}
if(q->b > -1) q->f++;
}
}
}
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.
struct node{
int data;
struct node *next;
};
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
struct queue{
int size;
int f;
int b;
int *arr;
};
int n=0;
for(int i=0;i<n;i++)
{
scanf("%d",&q->arr[i]);
q->b++;
}
if(q->b > -1) q->f++;
}
}
}
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: