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) {
}
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

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.
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...
NO NEED TO WRITE CODE,EXPLAIN IN C++ PLEASE. Suppose we have a Stack that can grow indefinitely (for example, the push method has been fixed to double the size of the array when at capacity instead of throwing a StackFullException). We want to create a second Stack data structure, which I promise will always contain only comparable items (e.g., integers, strings). We also want to add a function findMin to the Stack interface that will return the smallest element currently...
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 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...
Suppose I want to implement the public member functions of a Stack (push, pop, top, size, isEmpty). However, instead of the private member data we had in class, I have only a single Queue. You may assume that the Queue has unbounded capacity, but is otherwise as per the one shown in class. Explain at a high level (you do not need to provide C++ code, but someone should be able to understand how you would code it from what...
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 = 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 (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 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 it a simple implementation with just push and pop methods.
How would you implement capacity management in an enterprise network to monitoring throughput and store capacity in DMZ servers?