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

Modify your array-based stack to use a pointer-based stack. All your original fu

ID: 3678705 • Letter: M

Question

Modify your array-based stack to use a pointer-based stack. All your original functions should remain in place, except they will now be using dynamically allocated nodes, as discussed in class. Define your anchor with the name "anchor", so we can quickly see it in your source. The functions below are the same as your original functions, but now must be modified to use a list. The stack can now grow to use up all available memory (you can have a huge number of elements in the stack). Your Node is a struct containing a "next" pointer as discussed in class, and a data item (a character) Push - adds one element to the top of the stack (accepts a character as input), returns an int: -1 if the stack is already full, 0 if the item was successfully added If the stack is empty (use "isFull" to find out) then create the first element of the stack Pop - removes the top element (if it exists) and returns: NULL if there are no elements (stack is empty) The element itself if there was one Top - does NOT remove anything. Returns a char with the value: NULL if there are no elements (stack is empty) The topmost element if there is one d. isFull - does NOT remove anything. Returns an int: 0 if there are no elements (the stack is empty) 1 if the stack is full -1 in all other cases Using the above functions, write a program that: a. Creates the anchor pointer for the stack of characters Uses your functions above to do the following: Accept input characters, 1 at a time until the symbol A is input Push each character onto the stack Output the stack (top to bottom) after all inputs have been received

Explanation / Answer

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

#define MAX 50
int size;

// Defining the stack structure
struct stack {
int arr[MAX];
int top;
};

// Initializing the stack(i.e., top=-1)
void init_stk(struct stack *st) {
st->top = -1;
}

// Entering the elements into stack
void push(struct stack *st, int num) {
if (st->top == size - 1) {
printf(" Stack overflow(i.e., stack full).");
return;
}
st->top++;
st->arr[st->top] = num;
}

//Deleting an element from the stack.
int pop(struct stack *st) {
int num;
if (st->top == -1) {
printf(" Stack underflow(i.e., stack empty).");
return NULL;
}
num = st->arr[st->top];
st->top--;
return num;
}

void display(struct stack *st) {
int i;
for (i = st->top; i >= 0; i--)
printf(" %d", st->arr[i]);
}

int main() {
int element, opt, val;
struct stack ptr;
init_stk(&ptr);
printf(" Enter Stack Size :");
scanf("%d", &size);
while (1) {
printf(" tSTACK PRIMITIVE OPERATIONS");
printf(" 1.PUSH");
printf(" 2.POP");
printf(" 3.DISPLAY");
printf(" 4.QUIT");
printf(" ");
printf(" Enter your option : ");
scanf("%d", &opt);
switch (opt) {
case 1:
printf(" Enter the element into stack:");
scanf("%d", &val);
push(&ptr, val);
break;
case 2:
element = pop(&ptr);
printf(" The element popped from stack is : %d", element);
break;
case 3:
printf(" The current stack elements are:");
display(&ptr);
break;
case 4:
exit(0);
default:
printf(" Enter correct option!Try again.");
}
}
return (0);
}

Hope this will help you out! If not let me know

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote