Problem (10 points). In this problem you need to implement a Stack of Strings. You must use array implementation of the Stack and resize (double the capacity) the Stack when it’s full. Please check...

1 answer below »
I need this code in C language and running in Netbeans.It should follow exactly the requirements and hints given, because professor is very strict.No scanf, sscanf or atoi should be used. No break in if statements, and no compilation warnings.The code should follow same layout and variable naming as Exercise11_1 given.Please remember to free memory and allocate memory space as asked (on stack/heap).Please give output exact as professor's demo.


Problem (10 points). In this problem you need to implement a Stack of Strings. You must use array implementation of the Stack and resize (double the capacity) the Stack when it’s full. Please check Demo version: https://youtu.be/sc8p3rBY5pA Requirements: 1. Please download and use main_template.txt template file from Assignment Dropbox. You must use the template file or you’ll receive zero grade. 2. You must only use stack_t data structure to implement the Stack of strings. You’ll get a zero grade if you use anything else. 3. Do NOT use scanf() , sscanf(), functions anywhere. You'll get -3 points if this requirement not met. 4. You must use fgets() function to get each string from the console. New line must be removed. Assume input won't exceed MAX_LEN – see MAX_LEN macros. 5. You must correctly allocate memory for each string on the HEAP based on the length of the string and push the string to the Stack. 6. You must correctly implement main Stack functions (lines 27- 45 in the template). If needed, you can add your own supplementary functions (See Hint N3). 7. You must implement resizeStack(..) that will double the capacity of the Stack when it’s full. 8. Your program must work similarly to the demo. Namely, the program should have the same input and output, ignore incorrect input, etc. 9. Your solution should have optimal time and space complexity, check for possible error and free allocated memory that no longer needed (there should be no memory leak!) Hints: 1. Check the Stack of integers implementation in Exercise 11.1 ! 2. Stack of strings is essentially a Stack of pointers. 3. You might want to use extra function getStr() to get a string from console, allocate memory for it on the HEAP and return a pointer to memory block that you can then push to the Stack. 4. For resizeStack(..) function you’d need to use realloc() function. 5. Don’t forget to check if any memory allocation was successful. 6. Don’t forget to deallocate memory for each string after pop() function or you’ll have a memory leak. 7. Compilation warnings are considered to be major mistakes. // Final Exam, Winter2020 Template // You can add other libraries if needed #include #include // A structure to represent a stack // YOU MUST USE THIS STRUCTURE for Stack! typedef struct { int top; size_t capacity; char** array; } stack_t; #define FLUSH stdin=freopen(NULL,"r",stdin) // The maximum length of the string in the input #define MAX_LEN 20 // Initial maximum size of the stack. #define STACK_SIZE 2 // function to create a stack of a given capacity. stack_t* createStack(size_t); /* You need to implement the following functions: // Check if Stack is full ... isFull( .... ); // Check if Stack is empty ... isEmpty( .... ); // Function to add an item to stack. ... push( .... ); // Function to remove an item from stack. It decreases top by 1 ... pop( .... ); // Function to return the top from stack without removing it ... peek( .... ); // Function to resize the capacity of the Stack ... resizeStack( .... ); * // You might also declare and implement other functions here! */ int main() { stack_t* stack = createStack(STACK_SIZE); printf("Created a Stack of size %zu.\n", stack->capacity); /* Your code goes below: */ printf("Currently Stack has size %zu.\n", stack->capacity); printf("Popping elements from stack:\n"); /* Your code goes below: */ return 0; } // Exercise 11.1 // C program for array implementation of stack #include #include // A structure to represent a stack typedef struct { int top; size_t capacity; int* array; } stack_t; #define MAX_SIZE 12 // function to create a stack of given capacity. // It initializes size of stack as 0 stack_t* createStack(size_t); // Stack is full when top is equal to the last index int isFull(stack_t*); // Stack is empty when top is equal to -1 int isEmpty(stack_t*); // Function to add an item to stack. It increases top by 1 void push(stack_t*, int); // Function to remove an item from stack. It decreases top by 1 int* pop(stack_t*); // Function to return the top from stack without removing it int* peek(stack_t*); // int main() { int i=0; int array[MAX_SIZE] = { 27, 14, 35, 10, 19, 31, 42, 55, 99 , 39, 36, 40 }; stack_t* stack = createStack(MAX_SIZE); int* item; item = peek(stack); if (item) { printf("Top element of Stack: %d\n", *item); } else { printf("Top element of Stack: empty\n"); } printf("Pushing elements to stack: "); for (i=0; iarray = (int*)malloc(capacity * sizeof(int)); if (stack->array ==NULL) { printf("Cannot allocate memory for %zu elements of the fixed stack!\n", stack->capacity); exit(-1); } stack->capacity = capacity; stack->top = -1; return stack; } // Stack is full when top is equal to the last index int isFull(stack_t* stack) { return stack->top == stack->capacity - 1; } // Stack is empty when top is equal to -1 int isEmpty(stack_t* stack) { return stack->top == -1; } // Function to add an item to stack. It increases top by 1 void push(stack_t* stack, int item) { if (!isFull(stack)) { stack->array[++stack->top] = item; //printf("%d pushed to stack\n", item); } } // Function to remove an item from stack. It decreases top by 1 // We return NULL (stack is empty) or memory address of array element int* pop(stack_t* stack) { return isEmpty(stack) ? NULL: &stack->array[stack->top--]; } // Function to return the top from stack without removing it int* peek(stack_t* stack) { return isEmpty(stack)? NULL : &stack->array[stack->top]; }
Answered Same DayApr 14, 2021

Answer To: Problem (10 points). In this problem you need to implement a Stack of Strings. You must use array...

Aditya answered on Apr 15 2021
152 Votes
// Final Exam, Winter2020 Template
// You can add other libraries if needed
#include
#include
#include
// A st
ructure to represent a stack
// YOU MUST USE THIS STRUCTURE for Stack!
typedef struct {
int top;
size_t capacity;
char** array;
} stack_t;
#define FLUSH stdin=freopen(NULL,"r",stdin)
// The maximum length of the string in the input
#define MAX_LEN 20
// Initial maximum size of the stack.
#define STACK_SIZE 2
// function to create a stack of a given capacity.
stack_t* createStack(size_t);
// Check if Stack is full
int isFull(stack_t*);
// Check if Stack is empty
int isEmpty(stack_t*);
// Function to add an item to stack.
stack_t* push(stack_t*, char*);
// Function to remove an item from stack. It decreases top by 1
char* pop(stack_t*);
// Function to return the top from stack without removing it
char* peek(stack_t*);
// Function to resize the capacity of the Stack
stack_t* resizeStack(stack_t*);
// You might also declare and implement other functions here!
stack_t* create_new(stack_t* );
int main() {
int counter =0;//counter to count the number of times user input
int stop =0;
int x =0;
stack_t* stack = createStack(STACK_SIZE);
printf("Created a Stack of size %d.\n", stack->capacity);
do
{
char buf[MAX_LEN];
printf("Insert A String Upto 20 Characters :");
counter++;
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here