Question: Implement this code in c++ **Post the picture of your output** class StackUsingLinkedLists{ private class Node { int data; Node link; } Node node; StackUsingLinkedLists()
Implement this code in c++
**Post the picture of your output**
class StackUsingLinkedLists{ private class Node { int data; Node link; } Node node; StackUsingLinkedLists() { this.node = null; } public void push(int x) { Node temp = new Node(); if (temp == null) { System.out.println("Stack Overflow"); return; } temp.data = x; temp.link = node; node = temp; } public boolean isEmpty() { return node == null; } public int top(){ if (!isEmpty()) { return node.data; } else { System.out.println("Stack is empty"); return -1; } } int getFrequency(int num){ int count = 0; Node temp = this.node; while(temp != null) { if(temp.data == num) count++; temp = temp.link; } return count; } void clear(){ if(this.node == null) return; this.node = this.node.link; clear(); node = null; return; } public void pop() // remove at the beginning { if (node == null) { System.out.println("Stack Underflow"); return; } node = node.link; } } class Main { public static void main(String[] args){ StackUsingLinkedLists obj = new StackUsingLinkedLists(); obj.push(11); obj.push(22); obj.push(11); obj.push(33); obj.push(44); System.out.println("Is stack empty? " + obj.isEmpty()); System.out.println("Frequency of 11 in the stack is: "+ obj.getFrequency(11)); System.out.println("Top item of the stack before pop: " +obj.top()); obj.pop(); obj.pop(); System.out.println("Top item of the stack after pop: " +obj.top()); System.out.println("Clear Stack"); obj.clear(); System.out.println("Is stack empty? " + obj.isEmpty()); } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
