Question: Modify the stack program to add a roll function. 1) This problem assumes that the pop and make_empty functions for the stack program implemented using
Modify the stack program to add a roll function.
1) This problem assumes that the pop and make_empty functions for the stack program implemented using a linked list.
2) Add a roll function to stack.c. This function will have a parameter of type structnode* and a return type struct node *. When called, it rolls the top three items on the stack. For example, if the stack has 5 elements from top to bottom: 8,3, 9, 4, 7. After calling the roll function, the stack will be 9, 8, 3, 4, 7.You can assume that the stack has three items or more when it calls the roll function. The function returns the head of the linked list.
3) To test the roll function: in the main function of stack_client.c, after all theelements are pushed in stack1 and print_stack function was called, call theroll function on stack1. Then call print_stack again to display stack1 after rolling.
Compile with makefile.
/*****************************************************************************************************************/
stack.c
#include
struct node* push(struct node* top, int i) { struct node *new_node; new_node = malloc(sizeof(struct node)); if(new_node == NULL) { printf("malloc failed"); return top; } new_node->value = i; new_node->next = top; return new_node;
}
struct node *make_empty(struct node *top) { return top; }
struct node *pop(struct node *top, int *i) { return top; }
void print_stack(struct node *top) { struct node *p; if(top != NULL){ for(p = top; p !=NULL; p=p->next) printf("%d ", p->value); printf(" "); } else printf("stack is empty "); }
/*******************************************************************************************************/
stack_cliet.c
#include
int main(void) { int n; struct node *stack1 = NULL; struct node *stack2 = NULL; stack1 = push(stack1, 25); stack1 = push(stack1, 32); stack1 = push(stack1, 8); stack1 = push(stack1, 94); stack2 = push(stack2, 21); printf("stack1: "); print_stack(stack1); printf("stack2: "); print_stack(stack2);
stack1 = pop(stack1, &n); printf("Popped %d from stack1 ", n); printf("stack1: "); print_stack(stack1); stack2 = push(stack2, n); printf("stack2: "); print_stack(stack2); stack1 = pop(stack1, &n); printf("Popped %d from stack1 ", n); printf("stack1: "); print_stack(stack1); stack1 = make_empty(stack1); printf("stack1: "); print_stack(stack1);
stack2 = make_empty(stack2); printf("stack2: "); print_stack(stack2);
return 0;
}
/**************************************************************************************/
stack.h
#ifndef STACK_H #define STACK_H
struct node{
int value; struct node *next; };
struct node *push(struct node *top, int i); struct node *make_empty(struct node *top); struct node *pop(struct node *top, int *i); void print_stack(struct node *top); #endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
