Thread: dynamic stack

  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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 1. A stack is really three separate data items
    So why don't you have
    Code:
    struct stack {
        float   *data;
        int      capacity;
        int      top;
    };
    
    void init ( struct stack *s ) {
    }
    void cleanup ( struct stack *s ) {
    }
    ....
    Your main only has
    Code:
    int main ( ) {
        struct stack mystack;
        init(&mystack);
    
        // a bunch of other method calls, all passing &mystack
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    What input is being used?
    What is the expected output?
    How does the real output differ?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Why is your stack of floats using this code?

    Code:
    stack = malloc(sizeof(int) * cap);
    Why do you think "Stack is not full!!" is a error like condition?
    Code:
      if (top == -1) {
        printf("\nStack is not full!!\n");
        return 0;
      }
    Why do you not wish to use an C structure?
    NOTE: Your code can not work unless use pass by address, global variables, or structures!

    Tim S.
    Last edited by stahta01; 10-27-2019 at 11:01 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    11
    - The input is the first value pushed in because the stack is originally empty with a capacity of one.
    - When the stack gets full the program has to create another stack whose capacity is twice that of the previous one, so in that sense i guess stack is never full
    - the output is a list of choices asking the user to select if the stack is full, empty, less than half full, if the user wants to pop, shrink or display the values.
    - If after popping the stack is less than half full the code should shrink the stack to half its capacity.

    Malloc is used for memory allocation of the stack. And I think i will use pass by address. Thanks!

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    C only has pass by value semantics. If the value passed is an address it's an address. No need to complicate things.

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