Thread: dynamic stack

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    11

    dynamic stack

    Hello Please i need help with this code. I am not sure of what i did wrong but it does not generate the required result, and I am trying to do it without using a struct. Can someone help me figure this out? Thanks
    Here are the instructions:

    The difference with a dynamic stack is that adds never fail. If the stack is full when you attempt toadd a new item, you create a new array with twice the capacity of the current array, copy all the existingitems to the new array and then the new item, free the memory of the old array, set the stack pointer tothe new array and adjust the top indicator as always. Similarly, if after a remove from the stack, the stackis less than half full, then you shrink the array in half.
    write a C program which satisfies the following:
    • Your program must dynamically allocate the memory for a dynamic stack - using the appropriatefunction listed below.
    • Your program must dynamically free this memory before termination.
    • Your C program must declare and define functions to:– Create an empty stack (of float values) with a capacity of 1– Test to see if the stack is empty– Test to see if a stack is less than half full– Test to see if the stack is full– Push (add) an item (a float value) onto the stack– Pop (remove) an item (a float value) from the stack - if not already empty
    • Your C program must define a main function to fully test all of the above functionality.
    Notes:
    1. A stack is really three separate data itemsa) a float pointer to the array(b) an int top index indicator(c) an int capacity (physical size of the array) - note this must be a variable this time - since itwill change over time
    2. The effective size of the stack (number of items in the stack) can be deduced from the top indexindicator.

    And here is my code so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    float *stack;
    int top = -1;
    int full(int top, int cap)
    {
      return top == cap - 1;
    }
    
    int less_half_full(float *stack, int top, int cap)
    {
      if (*stack == cap / 2)
        return top;
    }
    
    int empty(int top)
    {
      return top == -1;
    }
    
    void push(float *stack, int top, int cap, int value)
    {
      if (top >= cap - 1) {
        printf("\nStack is full\n");
        return;
      }
      top = top + 1;
      stack[top] = value;
      printf("\nPushed %f into the stack!!\n", stack[top]);
    }
    
    int pop(float *stack, int top)
    {
      if (top == -1) {
        printf("\nStack is not full!!\n");
        return 0;
      }
      printf("\nPopped %f from the stack!!\n", stack[top]);
      stack[top] = 0;
      top--;
      return 0;
    }
    
    void display(float *stack, int top)
    {
      printf("\nThe values in the stack are:\n");
      int i;
      for (i = 0; i <= top; i++) {
        printf("%f\n", stack);
      }
    }
    
    int main()
    {
      int top = -1, cap = 4, value, choice, f = 0, f1 = 0, f2 = 0;
      /*printf("\nEnter the size of the stack:"); fflush(stdout);
         scanf("%d", &cap); */
      stack = malloc(sizeof(int) * cap);
      while (1) {
        printf
            ("\n1. Is stack empty?\n2. Is stack full?\n3. Is stack half full?\n4. Push\n");
        fflush(stdout);
        printf("5. Pop\n6. Display\n7. Exit\n Enter your choice:");
        fflush(stdout);
        scanf("%d", &choice);
        switch (choice) {
        case 1:
          f = empty(top);
          if (f == 1)
            printf("\nStack is Empty\n");
          else
            printf("\nStack is Not Empty\n");
          break;
        case 2:
          f1 = full(top, cap);
          if (f1 == 1)
            printf("\nStack is Full\n");
          else
            printf("\nStack is Not Full\n");
          break;
        default:
          printf("\nWrong Option!!\n");
          break;
        case 3:
          f2 = less_half_full(stack, top, cap);
          if (f2 == 1)
            printf("\nStack is less than half full\n");
          else
            printf("\nStack is not less than half full\n");
          break;
        case 4:
          printf("\nEnter value to push:");
          fflush(stdout);
          scanf("%d", &value);
          push(stack, cap, top, value);
          break;
        case 5:
          pop(stack, top);
          break;
        case 6:
          display(stack, top);
          break;
        case 7:
          exit(0);
          break;
          free(stack);
        }
      }
      return 0;
    }
    Last edited by Salem; 10-27-2019 at 10:43 PM. Reason: Removed crayola

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making Stack via Dynamic Array
    By Linell Bonnette in forum C++ Programming
    Replies: 10
    Last Post: 10-24-2011, 07:36 PM
  2. Help with dynamic stack linked list code
    By mihir.khatwani in forum C Programming
    Replies: 5
    Last Post: 11-10-2010, 05:45 PM
  3. Dynamic Stack Error
    By tarheelfan_08 in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2010, 12:16 AM
  4. Dynamic array in Stack ?
    By janaka in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 12:19 AM
  5. Dynamic stack?
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2007, 01:31 PM

Tags for this Thread