Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the provided skeleton code, please write a c program that has the following output / / Small stack implementation using linked list #include #include

Using the provided skeleton code, please write a c program that has the following output//Small stack implementation using linked list
#include
#include
#define EMPTY -1//assuming we will not have -1 in the stack
// Struct used to form a stack of integers.
struct stack {
int data;
struct stack *next;
};
int push(struct stack **front, int num);
int pop(struct stack **front);
int empty(struct stack *front);
int top(struct stack *front);
void init(struct stack **front);
void printStack(struct stack *front);
int main(){
int choice, data;
struct stack *stack1; //stack1 is the root of our stack
int tempval;
init(___________); //call the init function appropriately
printf("Menu: 1. push 2. pop, 3. print 4. exit.
Enter your choice: ");
scanf("%d", &choice);
while(choice !=4)
{
switch(choice)
{
case 1:
printf("Enter data: ");
scanf("%d", &data);
if(!push(&stack1, data))
printf("push failed
");
else
printStack(stack1);
break; //break from case
case 2:
data = pop(&stack1);
if(data != EMPTY)
{
printf("%d popped
", data);
printStack(stack1);
}
else
printf("empty stack
");
break;
case 3:
printStack(stack1);
}
printf("Menu: 1. push 2. pop, 3. print 4. exit.
Enter your choice: ");
scanf("%d", &choice);
}
return 0;
}
void init(struct stack **front){
// write the init functin to initialze the front to NULL
}
void printStack(struct stack *front)
{
if(front == NULL)
printf("empty stack");
else
{
printf("stack: ");
//write the loop to print the stack. Numbers should be printed in space
separated
while(_________)
{
// complete this part
}
}
printf("
");
}
// Pre-condition: front points to the top of the stack.
// Post-condition: a new node storing num will be pushed on top of the
// stack if memory is available. In this case a 1 is
// returned. If no memory is found, no push is executed
// and a 0 is returned.
int push(struct stack **front, int num){
//complete the push function
}
// Pre-condition: front points to the top of a stack
// Post-condition: Return the top (front) most data from the stack and
// remove the current front and upate the front
// return EMPTY if stack is empty.
int pop(struct stack **front){
//complete the pop function
}
// Pre-condition: front points to the top of a stack
// Post-condition: returns true if the stack is empty, false otherwise.
int empty(struct stack *front){
if (front == NULL)
return 1;
else
return 0;
}
// Pre-condition: front points to the top of a stack
// Post-condition: returns the value stored at the top of the stack if the
// stack is non-empty, or -1 otherwise.
int peek(struct stack *front){
if (front != NULL){
return front->data;
}
else
return -1;
}Example input/output (the red color texts are part of the input):
Menu: 1. push 2. pop, 3. print 4. exit.
Enter your choice: 1
Enter data: 50
stack: 50
Menu: 1. push 2. pop, 3. print 4. exit.
Enter your choice: 1
Enter data: 40
stack: 4050
Menu: 1. push 2. pop, 3. print 4. exit.
Enter your choice: 1
Enter data: 60
stack: 604050
Menu: 1. push 2. pop, 3. print 4. exit.
Enter your choice: 2
60 popped
stack: 4050
Menu: 1. push 2. pop, 3. print 4. exit.
Enter your choice: 2
40 popped
stack: 50
Menu: 1. push 2. pop, 3. print 4. exit.
Enter your choice: 2
50 popped
empty stack
Menu: 1. push 2. pop, 3. print 4. exit.
Enter your choice: 2
empty stack
Menu: 1. push 2. pop, 3. print 4. exit.
Enter your choice: 4
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions