Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in C++ , in a new c++ file Consider the file Stack.h. This file contains the Stack data structure. Your task is to write a

in C++, in a new c++ file

Consider the file Stack.h. This file contains the Stack data structure. Your task is to write a program that declares an instance of Stack, fills it up with double numbers and then prints out the numbers. You have to use the Stack member functions to complete this exercise (except for printing).You need to read a file called input.txt, which contains floating points numbers at each line. Read in these values, store them in your Stack object and print out the values in the reverse order without deleting them from the stack (for printing you can directly access member variables). After the doublevalues have been printed out, free the Stack by removing all elements with the pop() function. When the stack is empty, call the cleanup() function.

Input: input is in input.txt

Input.txt

------------------------------------------------------

16.0

2.3

-4.5

22.0

- 0.4

87

34.9

- 40.1

- 10.5

8.8

101.4

2.56

- 3.14

9.78

12.3

- 3.1

1.2

----------------------------------------

Output: 1.2 -3.1 12.3 9.78 -3.14 2.56 101.4 8.8 -10.5 -40.1 34.9 87 -0.4 22 -4.5 2.3 16

----------------------------------------------------------------------------------

Stack.h

#ifndef STACK_H #define STACK_H

#include

struct Stack { struct Link { void* data; Link* next; void initialize(void* dat, Link* nxt){ data = dat; next = nxt; } }* head; void initialize(){ head = 0; } void push(void* dat){ Link* newLink = new Link; newLink->initialize(dat, head); head = newLink; } void* peek(){ if (head == 0){ std::cout << "Stack is empty"; } return head->data; } void* pop(){ if(head == 0) return 0; void* result = head->data; Link* oldHead = head; head = head->next; delete oldHead; return result; } void cleanup(){ if (head == 0){ std::cout << "Stack is empty"; } else { std::cout << "Stack is not empty"; } } }; #endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions