Create a simple queue class and a simple stack class. The queue and stack should
ID: 3865061 • Letter: C
Question
Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list.
Create three functions that utilize these data structures
Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file.
Write a function that opens a text file and reads its contents into a queue of characters. The program should then dequeue each character, convert it to uppercase, and
store it in a second file.
Write a function that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical.
Create a driver program to test the functions
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
struct sNode
{
int data;
struct sNode *next;
};
struct queue
{
struct sNode *stack1;
};
void push(struct sNode** top_ref, int new_data);
int pop(struct sNode** top_ref);
void enQueue(struct queue *q, int x)
{
push(&q->stack1, x);
}
int deQueue(struct queue *q)
{
int x, res;
if(q->stack1 == NULL)
{
printf("Q is empty");
getchar();
exit(0);
}
else if(q->stack1->next == NULL)
{
return pop(&q->stack1);
}
else
{
x = pop(&q->stack1);
res = deQueue(q);
push(&q->stack1, x);
return res;
}
}
void push(struct sNode** top_ref, int new_data)
{
struct sNode* new_node =
(struct sNode*) malloc(sizeof(struct sNode));
if(new_node == NULL)
{
printf("Stack overflow ");
getchar();
exit(0);
}
new_node->data = new_data;
new_node->next = (*top_ref);
(*top_ref) = new_node;
}
int pop(struct sNode** top_ref)
{
int res;
struct sNode *top;
if(*top_ref == NULL)
{
printf("Stack overflow ");
getchar();
exit(0);
}
else
{
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
}
int main()
{
struct queue *q = (struct queue*)malloc(sizeof(struct queue));
q->stack1 = NULL;
enQueue(q, 1);
enQueue(q, 2);
enQueue(q, 3);
printf("%d ", deQueue(q));
printf("%d ", deQueue(q));
printf("%d ", deQueue(q));
getchar();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.