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

Write a program that opens a text file and reads its contents into a stack of ch

ID: 3693822 • Letter: W

Question

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 fist file. We don’t know the text file length. Directions: 1) Download stack.h from blackboard 2) Finish Stack & Stack::operator=(const Stack & original) { // write code here, refer copy construct } // File name: stack.h #include using namespace std; typedef char StackElement; class Stack { public: Stack(); Stack(const Stack & original); ~Stack(); bool empty() const; void push(const StackElement & value); void display(ostream & out) const; StackElement top() const; void pop(); Stack & operator=(const Stack & original); private: class Node { public: StackElement data; Node *next; Node (StackElement value, Node *link = 0) { data = value; next = link; } }; typedef Node *NodePointer; NodePointer myTop; }; Stack::Stack() { myTop = 0; myTop = NULL; } Stack::Stack(const Stack & original) { myTop = 0; if(!original.empty()) { myTop = new Stack::Node(original.top()); Stack::NodePointer lastPtr = myTop, origPtr = original.myTop->next; while(origPtr != 0) { lastPtr->next = new Stack::Node(origPtr->data); lastPtr = lastPtr->next; origPtr = origPtr->next; } } } Stack::~Stack() { Stack::NodePointer currPtr = myTop, nextPtr; while (currPtr != 0) { nextPtr = currPtr->next; delete currPtr; currPtr = nextPtr; } } bool Stack::empty() const { return (myTop == 0); } void Stack::push(const StackElement & value) { myTop = new Stack::Node(value, myTop); } void Stack::display(ostream & out) const { Stack::NodePointer ptr; for (ptr = myTop; ptr != 0; ptr = ptr->next) out << ptr->data << endl; } StackElement Stack::top() const { if (!empty()) return (myTop->data); else { cerr << "*** Stack is empty " "--- returning garbage value *** "; return *(new StackElement); } } void Stack::pop() { if (!empty()) { Stack::NodePointer ptr = myTop; myTop = myTop->next; delete ptr; } else cerr << "*** Stack is empty -- can't remove a value *** "; } Stack & Stack::operator=(const Stack & original) { // write code here, refer copy construct } #endif

Explanation / Answer

public class MyStack { private int maxSize; private long[] stackArray; private int top; public MyStack(int s) { maxSize = s; stackArray = new long[maxSize]; top = -1; } public void push(long j) { stackArray[++top] = j; } public long pop() { return stackArray[top--]; } public long peek() { return stackArray[top]; } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (top == maxSize - 1); } public static void main(String[] args) { MyStack theStack = new MyStack(10); theStack.push(10); theStack.push(20); theStack.push(30); theStack.push(40); theStack.push(50); while (!theStack.isEmpty()) { long value = theStack.pop(); System.out.print(value); System.out.print(" "); } System.out.println(""); } }

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