Question

How would you implement a stack in C with some initial capacity, but it can grow in size depending on growF. Likewise, how would you implement a stack with some initial capacity that it can grow and s...

How would you implement a stack in C with some initial capacity, but it can grow in size depending on growF. Likewise, how would you implement a stack with some initial capacity that it can grow and shrink in size with growF and shirnkF?

Stack* makeStackG(int cap, float growF){

}

Stack* makeStackGnS(int cap, float growF, float shrinkF) {

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the C code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

#include<stdio.h>
struct stack
{
    int cap,top,*arr;
};
void push(struct stack*);
void pop(struct stack*);
int getCapacity(struct stack*);
struct stack* createstack(int);
void main()
{
    int choice;
    int temp;
    struct stack *stk;
    stk=createstack(3);
    printf("1.push\n2.pop\n3.capacity\n4.exit\n");
    while(1)
    {
        printf("\nEnter choice: ");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
            push(stk);
            break;
        case 2:
            pop(stk);
            break;
        case 3:
            printf("Stack capacity is %d",getCapacity(stk));
            break;
        case 4:
            exit(0);
        }
    }
}
struct stack* createstack(int cap)
{
    struct stack *stk = (struct stack*)malloc(sizeof(struct stack));
    stk->cap = cap;
    stk->top = -1;
    stk->arr=(int *)malloc(sizeof(int)*stk->cap);
   return(stk);
};
int getCapacity(struct stack *stk)
{
    return stk->cap;
}
void push(struct stack *stk)
{
    int data, i;
    int *temp;
    printf("enter data\t");
    scanf("%d",&data);
    if(stk->top==stk->cap-1)    //stack is full
    {
        stk->cap*=2;    //double the capacity
        temp = (int *)malloc(sizeof(int)*stk->cap);
        for(i=0;i<stk->cap/2;i++)
            temp[i] = stk->arr[stk->top];
        stk->arr = temp;
    }
    stk->top++;
    stk->arr[stk->top]=data;
}
void pop(struct stack *stk)
{
    if(stk->top==-1)
        printf("stack is empty\n");
    else
        stk->top--;
}

Below is the screenshot of output

CAUsers Prakher 1Documentslcltest.exe 1.push 2.pop 3.capacity 4.exit Enter choice: 1 enter data 10 Enter choice: 1 enter da

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.

Add a comment
Know the answer?
Add Answer to:
How would you implement a stack in C with some initial capacity, but it can grow in size depending on growF. Likewise, how would you implement a stack with some initial capacity that it can grow and s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C, Implement each of the functions to create a working stack 6 II-Implement each of...

    In C, Implement each of the functions to create a working stack 6 II-Implement each of the functions to create a working stack. 7 II -Do not change any of the function declarations I-(i.e. stack t* create stack) should not have additional arguments) II-You should not have any 'printf' statements in your stack functions 10 II(You may consider using these printf statements to debug, but they should be removed from your final version) 12 #ifndefMYSTACK_A 13 #define MYSTACKH 14 15...

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • (b) an int top index indicator (c) an int capacity (physical size of the array) - note this must ...

    Program must be in C We were unable to transcribe this image(b) an int top index indicator (c) an int capacity (physical size of the array) - note this must be a variable this time since it will change over time 2. The effective size of the stack (number of items in the stack) can be deduced from the top index indicator. (b) an int top index indicator (c) an int capacity (physical size of the array) - note this...

  • How to use C to output this: indigo1 376 % lab9 Capacity = 4 Capacity =...

    How to use C to output this: indigo1 376 % lab9 Capacity = 4 Capacity = 8 0 5 10 15 20 Capacity = 16 0 5 10 15 20 25 30 35 40 Capacity = 32 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125...

  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

  • How do you implement a stack using a singly linked list in Java? Can you make...

    How do you implement a stack using a singly linked list in Java? Can you make it a simple implementation with just push and pop methods.

  • Describe, in pseudocode, how you can implement all the functions of the stack ADT using two...

    Describe, in pseudocode, how you can implement all the functions of the stack ADT using two queues. What is the running time of the push and pop functions in this case?

  • HI CAN YOU PLEASE PROGRAM THIS ASSIGNMENT USING C++ Use a stack to reverse a word. A user enters a word and the reverse...

    HI CAN YOU PLEASE PROGRAM THIS ASSIGNMENT USING C++ Use a stack to reverse a word. A user enters a word and the reverse of the word is printed out. The characters are reversed using a stack. Hint: you can use the stackArray implementation we did in class today, but instead of an int arr[SIZE], you can use a char arr[SIZE]: class Stack char arr[SIZE]; // array to store Stack elements Use a stack to reverse a word. A user...

  • Implement the EasyStack interface with the MyStack class. You can use either a linked list or...

    Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure. A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack. The class should be able to work with the following code: EasyStack stack = new MyStack(); NB: You cannot import anything from the standard library for this task. The data structure must...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT