Skip to content

Instantly share code, notes, and snippets.

@kriss-u
Created August 29, 2019 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriss-u/7c2834743dad9a26c4f4acbb721e1c95 to your computer and use it in GitHub Desktop.
Save kriss-u/7c2834743dad9a26c4f4acbb721e1c95 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
// Self-referential Structure
struct queueNode {
char data; // A character data in the queue
struct queueNode *nextPtr; // Pointer to next node
}; // end struct queuNode
typedef struct queueNode QueueNode; // alias for the struct
typedef QueueNode *QueueNodePtr; // alias for QueueNode*
// Declarations
void enqueue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr, char value);
char dequeue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr);
int isEmpty(QueueNodePtr headPtr);
void showQueue(QueueNodePtr currentPtr);
void showMenu(void);
int main() {
// Initially, there are no nodes
QueueNodePtr headPtr = NULL;
QueueNodePtr tailPtr = NULL;
// User's choice
unsigned int ch;
// User's entered character
char item;
// Display the menu
showMenu();
printf("? ");
scanf("%u", &ch);
// Loop unless user chooses 3
while(ch != 3) {
switch(ch) {
case 1:
printf("Enter a character: ");
scanf("\n%c", &item);
// insert the item in queue
enqueue(&headPtr, &tailPtr, item);
showQueue(headPtr);
break;
case 2:
// if queue is not empty
if(!isEmpty(headPtr)) {
item = dequeue(&headPtr, &tailPtr);
printf("%c is dequeued.\n", item);
}// end if
showQueue(headPtr);
break;
default:
printf("Invalid choice.\n");
showMenu();
break;
}
showMenu();
printf("? ");
scanf("%u", &ch);
}
printf("Thank You!\n");
return 0;
}
// Display Menu Function
void showMenu(void) {
printf("Enter your choice:\n"
" 1 to enqueue element into the queue.\n"
" 2 to dequeue element from the queue.\n"
" 3 to end" );
}
// insert a new value into the queue
void enqueue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr, char value) {
QueueNodePtr newPtr; // pointer to new node
newPtr = malloc(sizeof(QueueNode)); // Create a node
// if allocated
if (newPtr != NULL) {
newPtr->data = value; // put value in the node
newPtr->nextPtr = NULL;
// if empty, insert node to head
if (isEmpty(*headPtr)) {
*headPtr = newPtr;
}
else {
// assign nextPtr of tail node to the address of newPtr
(*tailPtr)->nextPtr = newPtr;
}
*tailPtr = newPtr; // finally point the tail to the newPtr
}
else {
printf("%c is not inserted. \n", value);
}
}
// dequeue a queue element
char dequeue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr) {
char item; // value of the node
QueueNodePtr tmpPtr; // temporary node pointer
item = (*headPtr)->data;
tmpPtr = *headPtr;
*headPtr = (*headPtr)->nextPtr;
// if queue is empty
if(*headPtr == NULL) {
*tailPtr = NULL;
}
free(tmpPtr);
return item;
}
// return 1 if the queue is empty, otherwise 0
int isEmpty(QueueNodePtr headPtr) {
return headPtr == NULL;
}
// show the queue
void showQueue(QueueNodePtr currentPtr) {
// if queue is empty
if (isEmpty(currentPtr)) {
printf("The queue is empty!\n");
}
else {
printf("The queue is: \n");
// while not the end of list
while(currentPtr != NULL) {
printf("%c ==> ", currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
printf("NULL\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment