USING C++: Part 1: Reverse the word We can create a stack using linked lists if
ID: 3699772 • Letter: U
Question
USING C++:
Part 1: Reverse the word
We can create a stack using linked lists if we force ourselves to insert and remove nodes only at the top of the list. One use of a stack is when you want to write a word backward. In that case, you will read the letters of the word one-by-one and as you read them will push them onto a stack. Once all letters are pushed onto the stack, then pop them back one-by-one. This will produce the letters of the word in reverse order.
Give the definition of the member function push of the class Stack.
Given the definition of the copy constructor for the class Stack.
Write a program that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file.
Part 2: Reverse Polish Notation (RPN)
Polish notation/expression is also known as ‘prefix notation’ where the numbers are preceded by its operator (placed in the front). In RPN, the operator comes after the operands instead of the normal format in which the operator is between the operations (this is called infix notation).
Starting with an empty stack, a RPN calculator can be implemented with the following rules:
If a number is input, push it o the stack.
If “+” is input then pop value1, pop value2, then push value2-value1 on the stack.
If “-“ is input, then pop the last two operands off the stack, multiply them, and push the result on the stack.
If “/” is input, then pop value1, pop value2, then push value2/value1on the stack.
If “q” is input, then stop inputting values, print out the top of the stack, and exit the program.
Modify the Stack class given in the class to store integers instead of characters. Use the modified stack to implement a RPN calculator. Output an appropriate error message if there are not two operands on the stack when given an operator.
Here is a sample input and output that is equivalent to ( (10 – ( 2 + 3 ) * 2 ) ) / 5:
10
2
3
+
-
2
*
5
/
q
The top of the stack is: 2
Explanation / Answer
Hi friend, Question is long.
I have answered Part I and using command line input.
//Code to Copy
// StackOperations.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
//node the structure
struct node {
//data is the variable type of integer
int data;
//next is the variable type of structure node
struct node *next;
};
class LinkedStack {
//pointer varibles declaration
struct node *top;
struct node *temp;
public:
//constructor declaration
LinkedStack() {
top = NULL;
}
//push the letters into stack
void push(char letter) {
struct node *ptr;
if (top == NULL) {
//memory allocation using malloc
top = (struct node *)malloc(1 * sizeof(struct node));
top->next = NULL;
top->data = letter;
}
else {
//memory allocation using malloc
temp = (struct node *)malloc(1 * sizeof(struct node));
temp->next = top;
temp->data = letter;
top = temp;
}
}
//pop the characters from linked stack
char pop() {
struct node *temp;
char letter;
//top is NULL ,it will return stack empty message
if (top == NULL) {
cout << "stack is empty";
}
temp = top;
top = top->next;
letter = temp->data;
//delete the temp
delete temp;
return letter;
}
};
int main()
{
//object creation of the LinkedStack
LinkedStack stackobject;
char letter;
//count declaration
int count = 0, i;
//Access the Elements from user
cout << "Enter a word letter-by-letter(0 to stop) into stack using Linked List = " << endl;
cin >> letter;
//push the characters until 0 enter into stack
while (letter != '0') {
stackobject.push(letter);
count++;
cin >> letter;
}
char reverseWord[50];
//store the characters reverse
for (i = 0;i < count;i++) {
reverseWord[i] = stackobject.pop();
}
reverseWord[count] = '';
//Display the statements
cout << endl;
cout << "The reverse of the word from stack using Linked List = " << reverseWord;
cout << endl;
cout << endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.