Java question Write a display method to go inside of the Lin

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!

Solution

#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); } }
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 elemen

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site