Implement a stack to store and remove strings that wil be read from a file. The
ID: 3852038 • Letter: I
Question
Implement a stack to store and remove strings that wil be read from a file. The data file contains a series of strings, one per line. Each string will contain 255 or fewer characters Whenever you read the string "pop", this is a signal to pop your stack. Any other string should be pushed onto your stack The format of the data file is pop pop To create the initial stack, use malloc to allocate enough space to store 10 strings. Keep track of how many elements are in your stack. When you stack reaches capacity, your push method needs to allocate more space to your stack before pushing the next element (add space for another 10 strings). You can use realloc, or something else like malloc/copy/swap You do not ever need to shrink your stacks capacity You are required to implement the following stack functions: push, pop, empty, and full · create returns a new empty stack » push takes a string parameter which is the value it pushes onto the stack. It may also need to call realloc to expand the size of the stack before completing the puslh » pop returns the string that was removed from the stack » empty returns TRUE if the stack has no elements, otherwise FALSE full returns TRUE if the stack does not have any room left, otherwise FALSE. Your program must print the assignment 2 and your name. Additionally, each time you read "pop" (i.e., each time you receive a signal to pop the stack) you should print the # of elements in the stack after popping and also print the string that is popped off the stack You should also print a message every time your stack grows. For example, the program might print the following Assignment 2 Problem 1 by # elements after popping: 2 # elements after popping: 1 # elements after popping: 0 Stack capacity has grown from 10 elements to 20 elements string popped: Be string popped: sure string popped: toExplanation / Answer
--------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define SIZE 10
int array[SIZE];
int top1 = -1;
int top2 = SIZE;
//push data to function
void push_stack1 (int data)
{
if (top1 < top2 - 1)
{
array[++top1] = data;
}
else
{
printf (" here Stack Full! so Cannot Push data ");
}
}
void push_stack2 (int data)
{
if (top1 < top2 - 1)
{
array[--top2] = data;
}
else
{
printf (" here Stack Full!so Cannot Push data ");
}
}
//Functions for pop data
void pop_stack1 ()
{
if (top1 >= 0)
{
int popped_value = array[top1--];
printf ("%d is being popped from Stack 1 ", popped_value);
}
else
{
printf ("Stack Empty! Cannot Pop ");
}
}
void pop_stack2 ()
{
if (top2 < SIZE)
{
int popped_value = array[top2++];
printf ("%d is being popped from Stack 2 ", popped_value);
}
else
{
printf ("Stack Empty! Cannot Pop ");
}
}
//Functions for Print Stack 1 and Stack 2
void print_stack1 ()
{
int i;
for (i = top1; i >= 0; --i)
{
printf ("%d ", array[i]);
}
printf (" ");
}
void print_stack2 ()
{
int i;
for (i = top2; i < SIZE; ++i)
{
printf ("%d ", array[i]);
}
printf (" ");
}
int main()
{
int array[SIZE];
int i;
int num_of_ele;
printf ("We can push a total of 10 values ");
for (i = 1; i <= 6; ++i)
{
push_stack1 (i);
printf ("Value Pushed in Stack 1 is %d ", i);
}
for (i = 1; i <= 4; ++i)
{
push_stack2 (i);
printf ("Value Pushed in Stack 2 is %d ", i);
}
//Print Both Stacks
print_stack1 ();
print_stack2 ();
//Pushing on Stack Full
printf ("Pushing Value in Stack 1 is %d ", 11);
push_stack1 (11);
//Popping All Elements From Stack 1
num_of_ele = top1 + 1;
while (num_of_ele)
{
pop_stack1 ();
--num_of_ele;
}
//Trying to Pop From Empty Stack
pop_stack1 ();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.