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

Java question. Write a display method to go inside of the LinkedStack class. Dis

ID: 3936860 • Letter: J

Question

Java question. Write a display method to go inside of the LinkedStack class. Display the contents like this (including the BOTTOM and TOP labels):

BOTTOM element3 element2 element1 TOP

The method header is:

public void display()

Notes:

This method goes inside of the LinkedStack class, so you can directly access the instance data variables of that class.

For full credit, your methods should be efficient- O(n ).

Take advantage of being able to directly access the chain of linked nodes.

And think recursively!

Explanation / Answer

#include #include struct Node { int data; struct Node *next; }*top = NULL; void push(int); void pop(); void display(); void main() { int choice, value; clrscr(); while(1){ printf(" ****** MENU ****** "); printf("1. Push 2.Pop 3.Display. 4.Exit "); printf("Enter your choice: "); scanf("%d",&choice); switch(choice){ case 1: printf("Enter the value to be insert: "); scanf("%d", &value); push(value); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf(" Wrong selection..Please try again.."); } } } void push(int value) { struct Node *newNode; newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; if(top == NULL) newNode->next = NULL; else newNode->next = top; top = newNode; printf(" Insertion is Success!!! "); } void pop() { if(top == NULL) printf(" Stack is Empty!!! "); else{ struct Node *temp = top; printf(" Deleted element: %d", temp->data); top = temp->next; free(temp); } } void display() { if(top == NULL) printf(" Stack is Empty!!! "); else{ struct Node *temp = top; while(temp->next != NULL){ printf("%d--->",temp->data); temp = temp -> next; } printf("%d--->NULL",temp->data); } }
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