Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS2123 Data Structures Summer 2017 Assignment 2: Stacks and Recursive Programs D

ID: 3852091 • Letter: C

Question

CS2123 Data Structures Summer 2017 Assignment 2: Stacks and Recursive Programs Due 6/26/17 by 11:59pm 1. Stacks in C (15 points) Implement a stack to store and remove strings that will 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 complet ing the push 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 # elernents after popping: 0 Stack capacity has groum from 10 elements to 20 elements string popped: Be string popped: su string popped: to re Continued on the back

Explanation / Answer

//Please see the updated code below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
    char** data;    /* Array of strings representing the stack */
    int top;        /* Index of the top of the stack. top is -1 if the stack is empty. */
    int size;       /* Number of elements that the stack can currently hold */
} Stack;

typedef enum { FALSE, TRUE } boolean;

/* function prototypes */
Stack* create();
void deleteStack( Stack* ps );
void push( Stack* ps, char* str );
char* pop( Stack* ps );
bool empty( Stack *s );
bool full( Stack *s );

int main(int argc, char *argv[])
{
    FILE *in_file = fopen("data_a2.txt", "r");
    Stack *s;

    printf("CS 2123 Assignment 2 ");

    if (in_file == NULL)
    {
            printf("File %s not found. ", "data_a2.txt");
            return -1;
    }

    /* Place your code here */

  
   char strBuffer[256];
    s=create();

    while(fscanf(in_file, "%s", strBuffer)!=EOF) {
        printf("%s ", strBuffer);
        if(strcmp(strBuffer, "pop") == 0)
       {
            pop(s);
        }
       else
       {
                push(s, strBuffer);
        }
       }

    /* Uncomment next line one you get create working */
    //deleteStack( &s );
    fclose(in_file);

}

/* create: returns a new empty stack. */
Stack * create(){
     /* Place your code here */

      Stack *s;
      s = (Stack*)malloc(sizeof(Stack));
      s->top = -1;
      s->size = 10;
      s->data = (char**)malloc(s->size * sizeof(char*));
      return s;
}

/* deleteStack: deletes the memory associated with the given stack. */
void deleteStack( Stack* ps ){
    while( ps->top>=0 ){
        free( ps->data[ps->top] );
        ps->top--;
    }

    free( ps->data );
}

/*
* 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 push.
*/
void push( Stack* s, char* str ){
    /* Place your code here */
    if(full(s)){
       printf("stack capacity is grown from %d elements to %d",s->size,s->size+10);
       s->size = s->size+10 ;
       s->data = (char**)realloc(s->data, s->size);
   }
    s->data[++s->top]=(char *) malloc(sizeof(char)*strlen(str));
   strcpy(s->data[s->top] ,str);
  
}

/* pop: returns the string that was removed from the stack. */
char* pop( Stack* s ){
    /* Place your code here */
    if(empty(s)) {
        return NULL;
    }
   printf("# elements after popping: %d, string popped: %s ",s->top, s->data[s->top]);
   return s->data[s->top--];
}

/* empty: returns TRUE if the stack has no elements, otherwise FALSE. */
bool empty( Stack *s ){
    /* Place your code here */
   if(s->top == -1) {
    return TRUE;
   }
    return FALSE;
}

/* full returns TRUE if the stack does not have any room left, otherwise FALSE. */
bool full( Stack *s ){
    /* Place your code here */
   if (s->top == s->size - 1)
   {
        return TRUE;
   }
    return FALSE;
}