convert this java code to c++ please: import java.util.*; /* Class Node */ class
ID: 3798396 • Letter: C
Question
convert this java code to c++ please:
import java.util.*;
/* Class Node */
class Node
{
protected int data;
protected Node link;
/* Constructor */
public Node()
{
link = null;
data = 0;
}
/* Constructor */
public Node(int d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public int getData()
{
return data;
}
}
/* Class PriorityStack */
class priorityStack
{
protected Node top ;
protected int size ;
/* Constructor */
public priorityStack()
{
top = null;
size = 0;
}
/* Function to check if stack is empty */
public boolean isEmpty()
{
return top == null;
}
/* Function to get the size of the stack */
public int getSize()
{
return size;
}
/* Function to push an element to the stack */
public void push(int data)
{
Node nptr = new Node (data, null);
if (top == null)
top = nptr;
else
{
nptr.setLink(top);
top = nptr;
}
size++ ;
}
/* Function to pop an element from the stack */
public int pop()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception") ;
Node ptr = top;
top = ptr.getLink();
size-- ;
return ptr.getData();
}
/* Function to check the top element of the stack */
public int peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception") ;
return top.getData();
}
/* Function to display the status of the stack */
public void display()
{
System.out.print(" Stack = ");
if (size == 0)
{
System.out.print("Empty ");
return ;
}
Node ptr = top;
while (ptr != null)
{
System.out.print(ptr.getData()+" ");
ptr = ptr.getLink();
}
System.out.println();
}
}
/* Class LinkedStackImplement */
public class LinkedStackImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class PriorityStack*/
PriorityStack ls = new Prioritystack();
/* Perform Stack Operations */
System.out.println("Linked Stack Test ");
char ch;
do
{
System.out.println(" Linked Stack Operations");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to push");
ls.push( scan.nextInt() );
break;
case 2 :
try
{
System.out.println("Popped Element = "+ ls.pop());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 3 :
try
{
System.out.println("Peek Element = "+ ls.peek());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = "+ ls.isEmpty());
break;
case 5 :
System.out.println("Size = "+ ls.getSize());
break;
case 6 :
System.out.println("Stack = ");
ls.display();
break;
default :
System.out.println("Wrong Entry ");
break;
}
/* display stack */
ls.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
Explanation / Answer
#pragma once
#include <string>
#include <iostream>
#include <stdexcept>
class Node
{
protected:
int data = 0;
Node *link;
public:
Node()
{
delete link;
data = 0;
}
Node(int d, Node *n)
{
data = d;
link = n;
}
virtual void setLink(Node *n)
{
link = n;
}
virtual void setData(int d)
{
data = d;
}
virtual Node *getLink()
{
return link;
}
virtual int getData()
{
return data;
}
};
class priorityStack
{
protected:
Node *top;
int size = 0;
public:
priorityStack()
{
delete top;
size = 0;
}
virtual bool isEmpty()
{
return top == nullptr;
}
virtual int getSize()
{
return size;
}
virtual void push(int data)
{
Node *nptr = new Node(data, nullptr);
if (top == nullptr)
{
top = nptr;
}
else
{
nptr->setLink(top);
top = nptr;
}
size++;
}
virtual int pop()
{
if (isEmpty())
{
throw NoSuchElementException(L"Underflow Exception");
}
Node *ptr = top;
top = ptr->getLink();
size--;
return ptr->getData();
}
virtual int peek()
{
if (isEmpty())
{
throw NoSuchElementException(L"Underflow Exception");
}
return top->getData();
}
virtual void display()
{
std::wcout << std::wstring(L" Stack = ");
if (size == 0)
{
std::wcout << std::wstring(L"Empty ");
return;
}
Node *ptr = top;
while (ptr != nullptr)
{
std::wcout << ptr->getData() << std::wstring(L" ");
ptr = ptr->getLink();
}
std::wcout << std::endl;
}
};
class LinkedStackImplement
{
static void main(std::wstring args[])
{
Scanner *scan = new Scanner(System::in);
PriorityStack *ls = new Prioritystack();
std::wcout << std::wstring(L"Linked Stack Test ") << std::endl;
wchar_t ch;
do
{
std::wcout << std::wstring(L" Linked Stack Operations") << std::endl;
std::wcout << std::wstring(L"1. push") << std::endl;
std::wcout << std::wstring(L"2. pop") << std::endl;
std::wcout << std::wstring(L"3. peek") << std::endl;
std::wcout << std::wstring(L"4. check empty") << std::endl;
std::wcout << std::wstring(L"5. size") << std::endl;
int choice = scan->nextInt();
switch (choice)
{
case 1 :
std::wcout << std::wstring(L"Enter integer element to push") << std::endl;
ls->push(scan->nextInt());
break;
case 2 :
try
{
std::wcout << std::wstring(L"Popped Element = ") << ls->pop() << std::endl;
}
catch (std::exception &e)
{
std::wcout << std::wstring(L"Error : ") << e.what() << std::endl;
}
break;
case 3 :
try
{
std::wcout << std::wstring(L"Peek Element = ") << ls->peek() << std::endl;
}
catch (std::exception &e)
{
std::wcout << std::wstring(L"Error : ") << e.what() << std::endl;
}
break;
case 4 :
std::wcout << std::wstring(L"Empty status = ") << ls->isEmpty() << std::endl;
break;
case 5 :
std::wcout << std::wstring(L"Size = ") << ls->getSize() << std::endl;
break;
case 6 :
std::wcout << std::wstring(L"Stack = ") << std::endl;
ls->display();
break;
default :
std::wcout << std::wstring(L"Wrong Entry ") << std::endl;
break;
}
ls->display();
std::wcout << std::wstring(L" Do you want to continue (Type y or n) ") << std::endl;
ch = scan->next().charAt(0);
}
while (ch == L'Y' || ch == L'y');
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.