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

write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
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.
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
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++ 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 :...
Design and implement a C++ program read in a whole line of characters as the input...
Design and implement a C++ program read in a whole line of characters as the input string; count and display how many times how frequently (among the letters) each (case insensitive) letter appears in the above mentioned input string; Sample program execution: An example of executing such a program is shown below. Note that the user input is in italic font. Please enter a line of characters: This is a really long line of characters! There are 41 characters in...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT