Question

C PROGRAMMING -- Please use simple code QUESTION: - Write a charQueue header file containing all...

C PROGRAMMING -- Please use simple code

QUESTION: -

Write a charQueue header file containing all the function headers of following functions:
1- initiateQueue—to initialize a queue

2- enqueue—to add a node at the rear end of the queue

3- dequeue—to remove a node from the front of the queue

4- printQueue—print a queue

and an implementation file implementing these functions for a Queue of chars. Inside your main method, which should be part of your tester file, read a sequence of chars one at a time. Terminate your sequence if user enters the number 0. Remember the difference between a char and an int!


Print the text user entered (that was stored in your Queue as a series of characters) using a while loop. You should loop as long as the Queue is not empty.  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please go through code and output.

CODE:

########################################################

//CharQueue.h

#ifndef CHAR_QUEUE_H
#define CHAR_QUEUE_H
#include <stdio.h>
#include <stdlib.h>
/* structure */
typedef struct Queue
{
   char ch;
   struct Queue * next;
}queue;

queue * initiateQueue(char ch);
void enqueue(char ch,queue ** head);
char dequeue(queue ** head);
void printQueue(queue * head);

#endif

-------------------------------------------------------------------------------------

//charQueue.c

#include <stdio.h>
#include <stdlib.h>
#include "CharQueue.h"
/* functions defination */
queue * initiateQueue(char ch)
{
   queue * temp = (queue *)malloc(sizeof(queue));
   temp->ch = ch;
   temp->next = NULL;
   return temp;
}
void enqueue(char ch,queue ** head)
{
   queue * temp = *head;
   queue * temp1 = (queue *) malloc(sizeof(queue));
   temp1->ch = ch;
   temp1->next = NULL;
   if(*head == NULL)
   {
       temp1->next = (*head)->next;
       *head = temp1;
   }
   else
   {
       while(temp->next != NULL)
       {
           temp = temp->next;
       }
       temp1->next = temp->next;
       temp->next = temp1;

   }

}
char dequeue(queue ** head)
{
   char ch;
   queue * temp = *head;
   if(*head == NULL)
   {
       printf("Queue is in underflow condition\n");
   }
   else
   {
       temp = *head;
       *head = (*head)->next;
       ch = temp->ch;
       free(temp);
   }
   return ch;
}
void printQueue(queue * head)
{
   queue * temp = head;
   while(temp != NULL)
   {
       printf("%c ",temp->ch);
       temp = temp->next;
   }

printf("\n");
}

int main()
{
   queue * head = NULL;
   head = initiateQueue('a');
   enqueue('b',&head);
   enqueue('c',&head);
   enqueue('d',&head);
   printf("dequeue - %c\n",dequeue(&head));
   enqueue('e',&head);
   enqueue('f',&head);
   printQueue(head);
}
############################################################

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
C PROGRAMMING -- Please use simple code QUESTION: - Write a charQueue header file containing all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q.1. Write a C program that determines whether a line entered by a user is a...

    Q.1. Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation demonstrated in the class). Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out). Q.2. Write a charQueue header file containing all the function headers of following functions: 1- initiateQueue—to initialize a queue 2- enqueue—to add a node at the rear end of the...

  • Question1 ) The STL provides similiar classes called stack and queue, both of which gave functions...

    Question1 ) The STL provides similiar classes called stack and queue, both of which gave functions called push and pop. Answer the following questions: 1.1) Does the push perform a different action in the stack class then in the queue class? Explain the answer. 1.2) Does the pop perform a different action in the stack class then in the queue class? Explain the answer. Question 2) 2.1) Suppose you are tasked with implementing a reverse queue in which elements are...

  • Study the "Queue as linked list simple example" posted in this module. Rewrite the code to...

    Study the "Queue as linked list simple example" posted in this module. Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. LListQueue.cpp ///--------------------------------------------------------------- /// File: LListQueue.cpp /// Purpose: Implementation file for a demonstration of a queue /// implemented as an array. Data type: Character /// Programming Language: C++ ///--------------------------------------------------------------- #include "LListQueue.h" ///-------------------------------------------- /// Function: LListQueue() ///...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • Write a C++ program to implement Queue ADT using singly linked structure. The program includes the...

    Write a C++ program to implement Queue ADT using singly linked structure. The program includes the following: Define the Queue class template in the header file QueueLinked.h. // QueueLinked.h #ifndef QUEUE_H #define QUEUE_H #include <iostream> using namespace std; template <typename T> class Queue { public: // Constructor Queue(); //Desctructor ~Queue(); // Makes the queue to the empty state. void make_empty(); // Checks if the queue is empty. bool empty() const; // Inserts item at the end of the queue. void...

  • Create a header file timer.h containing the following functions (you can have more functions, but the...

    Create a header file timer.h containing the following functions (you can have more functions, but the below ones are required. Do not modify the given function signatures. // Initialize the timer with the user-provided input void initTimer(ClockType *clock, int minutes, int seconds); // Run the timer -- print out the time each second void runTimer(); // Clean up memory (as needed) void cleanTimer(ClockType *clock); Create a file timer.c and implement (at least) these three functions and any other functions you...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

  • Recall queueADT structure which had the following queue.h interface: /* queue.h */ #ifndef _queue_h #define _queue_h...

    Recall queueADT structure which had the following queue.h interface: /* queue.h */ #ifndef _queue_h #define _queue_h #include "genlib.h“ typedef void *queueElementT; typedef struct queueCDT *queueADT; queueADT NewQueue(void); void FreeQueue(queueADT queue); void Enqueue(queueADT queue, queueElementT element); queueElementT Dequeue(queueADT queue); bool QueueIsEmpty(queueADT queue); bool QueueIsFull(queueADT queue); int QueueLength(queueADT queue); queueElementT GetQueueElement(queueADT queue, int index); #endif Suppose its implementation is available as queue.o, so you can use all the functions in queue.h, but you cannot change their implementation. Now you are asked to...

  • Please use C programming to write the code to solve the following problem. Also, please use the i...

    Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT