DATE : 21/08/2024
EXPERIMENT NO:7
IMPLENTAION OF QUEUE (APPLICATION)
Aim :
To create a C program to implement Queue using linked list
Question :
In an amusement park, there is a popular ride where visitors must queue up to get
on the ride. If a visitor reaches the front of the queue and the ride is full, they can
wait for the ride or choose to leave the queue. However, if they reach the front and
the ride has space,if they wish to ride, they get on the ride else they leave the queue.
Display the visitors who got on the ride and left the queue without a ride.
Note: The maximum length of the queue is 10.
Implement this using Queue Data Structure using Linked List.
ALGORITHM:
Start Program
1. Define Node Structure:
- `name`: String of 10 characters
- `next`: Pointer to Node
2. Initialize:
- `front = NULL`
- `rear = NULL`
3. Function menu():
- Print menu options:
- 1: Enqueue (Add a person to the Ride Queue)
- 2: Dequeue
- 3: Display the queue
- 4: Allow for Ride
- 5: Exit
- Read and return user choice
4. Function enqueue(name):
- Create a new node `nn`
- Set `nn->name = name`
- Set `nn->next = NULL`
- If `front == NULL` and `rear == NULL`:
- Set `front = rear = nn`
- Else:
- Set `rear->next = nn`
- Set `rear = nn`
5. Function dequeue():
- If `front == NULL` and `rear == NULL`:
- Print “Queue is empty”
- Else if `front == rear`:
- Print `front->name`
- Set `front = rear = NULL`
- Else:
- Print `front->name`
- Set `front = front->next`
6. Function display():
- Initialize `temp = front`
- Print “Visitors in the queue are:”
- While `temp != NULL`:
- Print `temp->name`
- Set `temp = temp->next`
7. Function delete(name):
- Initialize `temp = front` and `prev = NULL`
- If `temp->name == name`:
- Set `front = temp->next`
- Else:
- While `temp != NULL`:
- Set `prev = temp`
- Set `temp = temp->next`
- If `temp->name == name`:
- Break the loop
- Set `prev->next = temp->next`
- Free `temp`
8. Function quesize():
- Initialize `ct = 0`
- Set `temp = front`
- While `temp != NULL`:
- Increment `ct`
- Set `temp = temp->next`
- Return `ct`
9. Function ride():
- Initialize `i = 6`
- Set `q = quesize()`
- While `q != 0` and `i != 0`:
- Call `dequeue()`
- Print “Rided!”
- Decrement `q`
- Decrement `i`
- Set `temp = front`
- While `temp != NULL`:
- Print name and ask if the person wants to ride (1 for yes, 0 for no)
- Read `ch`
- If `ch == 1`:
- Print that the person wants to ride and is waiting in the queue
- Else:
- Print that the person doesn’t want to ride and is leaving the queue
- Call `delete(temp->name)`
- Set `temp = temp->next`
10. Main Function:
- Initialize `vis` array with [“P1”, “P2”, “P3”, ..., “P10”]
- While `True`:
- Call `menu()` and store choice in `ch`
- Switch on `ch`:
- Case 1:
- For each `name` in `vis`:
- Call `enqueue(name)`
- Case 2: Call `dequeue()`
- Case 3: Call `display()`
- Case 4: Call `ride()`
- Case 5: Exit the program
End Program
SOURCE CODE :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
Struct node
{
Char name[10];
Struct node *next;
}*front=NULL,*rear=NULL;
Int menu()
{
Int ch;
Printf(“\[Link](Add a person to the Ride Queue) \[Link] \[Link] the
queue \[Link] for Ride \[Link] \nEnter the choice :”);
Scanf(“%d”,&ch);
Return ch;
}
Void enqueue(char name[])
{
Struct node *nn=(struct node *)malloc(sizeof(struct node));
nn->next=NULL;
strcpy(nn->name,name);
if(front==NULL&&rear==NULL)
{
Front=rear=nn;
}
Else
{
Rear->next=nn;
Rear=nn;
}
}
Void dequeue()
{
If(front==NULL&&rear==NULL)
{
Printf(“\nQueue is empty !”);
}
Else if(front==rear)
{
Printf(“ -%s “,front->name);
Front=rear=NULL;
}
Else
{
Printf(“ -%s “,front->name);
Front=front->next;
}
}
Void display()
{
Struct node *temp=front;
Printf(“\nVisitors in the queue are : “);
While(temp!=NULL)
{
Printf(“\n-%s”,temp->name);
Temp=temp->next;
}
}
Void delete(char name[])
{
Struct node *temp=front,*prev;
If(strcmp(temp->name,name)==0)
{
Front=temp->next;
Return;
}
Else
{
While(temp!=NULL)
{
Prev=temp;
Temp=temp->next;
If(strcmp(temp->name,name)==0)
{
Break;
}
}
Prev->next=temp->next;
Free(temp);
}
Return;
}
Int quesize()
{
Int ct=0;
Struct node *temp=front;
While(temp!=NULL)
{
Ct++;
Temp=temp->next;
}
Return ct;
}
Void ride()
{
Int i=6;
Int q=quesize();
While(q!=0&&i!=0)
{
Dequeue();
Printf(“—Rided !\n”);
q--;
i--;
}
Struct node *temp=front;
While(temp!=NULL)
{
Int ch;
Printf(“\n %s : want to ride (1) || Don’t want to ride (0) : “,temp->name);
Scanf(“%d”,&ch);
If(ch==1)
{
Printf(“%s want to ride and %s is waiting in the queue “,temp->name,temp-
>name);
}
Else
{
Printf(“%s don’t want to ride and %s is leaving the queue “,temp->name,temp-
>name);
Delete(temp->name);
}
Temp=temp->next;
}
Int main()
{
Char name[10];
Char *vis[]={“P1”,”P2”,”P3”,”P4”,”P5”,”P6”,”P7”,”P8”,”P9”,”P10”,};
While(1)
{
Switch(menu())
{
Case 1:
{
// printf(“\nEnter the visitor name :”);
// scanf(“%s”,name);
For(int i=0;i<10;i++)
{
Enqueue(vis[i]);
}
Break;
}
Case 2:
{
Dequeue();
Break;
}
Case 3:
{
Display();
Break;
}
Case 4:
{
Ride();
Break;
}
Case 5:
{
Exit(0);
}
}
}
Return 0;
}
SAMPLE OUTPUT :
RESULT :
Thus the C program to implement the queue using linked list is successfully
completed and verified .