Question

In: Computer Science

Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked...

Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked list. One way means that each node has only one pointer, Q, (not both a rear and a front pointer), so the program must use pointers-to-pointers. Include functions to insert(), remove(), for use in main() where the user is prompted "Enter "i" to insert a new element, "r" to remove an element, "q" to quit:"

Solutions

Expert Solution

code

#include<stdio.h>
#include<stdlib.h>

struct node
{
int info;
struct node *link;
}*rear=NULL;

void insert(int item);
int del();
void display();
int isEmpty();
int peek();

int main()
{
int item;
char choice;
while(1)
{
printf("\n1.Enter i to insert\n");
printf("2.Enter r to Delete\n");
printf("4.Enter d to display \n");
printf("5.Enter q to Quit\n");
printf("\nEnter your choice : ");
scanf("%c",&choice);

switch(choice)
{
case 'i':
printf("\nEnter the element for insertion : ");
scanf("%d",&item);
insert(item);
break;
case 'r':
printf("\nDeleted element is %d\n",del());
break;

case 'd':
display();
break;
case 'q':
exit('q');
break;

}
}
}

void insert(int item)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=item;
if(tmp==NULL)
{
printf("\nMemory not available\n");
return;
}

if( isEmpty() ) /*If queue is empty */
{
rear=tmp;
tmp->link=rear;
}
else
{
tmp->link=rear->link;
rear->link=tmp;
rear=tmp;
}
}

del()
{
int item;
struct node *tmp;
if( isEmpty() )
{
printf("\nQueue underflow\n");
exit(1);
}
if(rear->link==rear)
{
tmp=rear;
rear=NULL;
}
else
{
tmp=rear->link;
rear->link=rear->link->link;
}
item=tmp->info;
free(tmp);
return item;
}

int isEmpty()
{
if( rear == NULL )
return 1;
else
return 0;
}


void display()
{
struct node *p;
if(isEmpty())
{
printf("\nQueue is empty\n");
return;
}
printf("\nQueue is :\n");
p=rear->link;
do
{
printf("%d ",p->info);
p=p->link;
}while(p!=rear->link);
printf("\n");
}
code (SS)

output(SS)


Related Solutions

One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Write a C program to implement the priority queue with the operations insert and extractmax. Sample...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample : ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
C language Write a program in C to implement Queue and its operation (like create, insert,...
C language Write a program in C to implement Queue and its operation (like create, insert, delete, search) using array data structure.
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT