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

Modify the stack program to add a roll function. 1) This problem assumes that th

ID: 3692734 • Letter: M

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 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.

/*****************************************************************************************************************/

#include <>
#include <>
#include "stack.h"
struct node* push(struct node*top, int i)
{
struct node *new_node; new_node =malloc(sizeof(struct node));
if(new_node == NULL) {
printf("mallocfailed");
return top;
}
new_node->value = i;
new_node->next = top;
return new_node;
}

struct node *make_empty(struct node *top)
{
struct node *p;
while(top != NULL)
{
p = top;
top = top->next;
free(p);
}
return NULL;
}

struct node *pop(struct node *top, int *i)
{
struct node *new_top;
if(top != NULL) {
*i = top->value;
new_top = top->next;
free(top);
return new_top;
} else{
printf("stack is empty, can not pop.");
return NULL;
}
}

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
#include "stack.h"

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

Explanation / Answer

struct node * Roll(struct node * head)

{

struct node *temp=head;

temp=temp->next;//second node

struct node *temp1=(temp->next);//third node

temp->next=temp1->next;

temp1->next=head;

return head;

}

without using any temporary list

struct node * Roll(struct node *head)

{

int temp;

temp=head->data;

head->data=(head->next)->data;

(head->next)->data=((head->next)->next)->data;

((head->next)->next)->data=temp;

return head;

}

}

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