Write a program for Stack using Linked-List, and implement the following functio
ID: 3762666 • Letter: W
Question
Write a program for Stack using Linked-List, and implement the following functionalities:
1. Push 2. Pop 3. IsEmpty 4. MakeEmpty
Write a main program (C++) which reads the attached file containing all presidents of the US.
1. Your program should push the president names into the stack as you read from the file.
2. Display the names
3. Insert your name as the last president into the list.
4. Display the names
5. Then remove your name from the list.
6. Display the names
7. Empty all the list
8. Verify that your list becomes empty
presidents.txt - Notepad++ File Edit Search View Encoding LanquageSettings Macro Run Plugins Window? presidcnts.bt 1 George Washington 2 John Adams 4 James Madison 5 James Monroe 6 John quincy Adams 7 Andrew Jackson 8 Martin Van Buren 9 William Henry Hairison 10 John Tyler 11 James K. Polk 12 Zachary Taylor 13 Millard Fillmore 14 Franklin Pierce 15 James Buchanan 16 Abraham Lincoln 17 Andrew Johnson 18 Ulysses S. GrdlL 19 Rutherford B. IIayes 20 James Garfield 1 Chest.er A. Arthur 22 Grover Cleveland 23 Benjamin Harrison 24 Grover Cleveland 25 William McKinley 26 Theodore Roosevet 20 Woodrow wilson 29 30 Calvin coolidge 31 Herbert Hoover 32 Franklin D. Roosevelt 33 Harry S. Truman 34 Dwight D. Eisenhowe:r 3 John F. Kennedy 36 Lyndon B. JOhnso 37 Richard M. Nixon 38 Gerald R. Ford 3 Tames Carter 40 Ronald Reaqan 41 George H. W. Bush 42 William J. Clinton 43 George W. Bush 44 Barack Obama Warren G. Harding Normal text file length : 763 lines :47 Ln:46 Col:1 Sel: 010 UNIX UTF-8 INSExplanation / Answer
#include <iostream>
#include <fstream>
#include <cstddef>
using namespace std;
using std::cout;
using std::endl;
/* definition of the list node class */
class node
{
friend class linked_list;
private:
string _value;
node *_next_node;
public:
node(void) : _next_node(NULL){ }
node(string val) : _value(val), _next_node(NULL){ }
node(string val, node* next) : _value(val), _next_node(next) { }
/* accessors */
string get_value() { return _value; }
node * getNext(){ return _next_node; }
};
/* definition of the linked list class */
class linked_list
{
private:
node *_head;
node *_tail;
public:
linked_list();
linked_list(string val);
void print();
void push(node * n);
string pop();
bool isEmpty();
void makeEmpty();
};
linked_list::linked_list()
{
_head = _tail = NULL;
}
linked_list::linked_list(string val)
{
_head = new node(val);
_tail = _head;
}
void linked_list::print()
{
node * n = _head;
// Check to see if empty
if (_head == NULL)
{
cout << "The stack is empty" << endl;
return;
}
cout << "Elements in stack are: "<<endl;
while (n != NULL)
{
cout << n->_value;
n = n->_next_node;
cout << endl;
}
cout << endl;
}
bool linked_list::isEmpty()
{
node *p;
p = _head;
if(p == NULL)
return true;
return false;
}
void linked_list::makeEmpty()
{
while(!this->isEmpty())
{
this->pop();
}
}
void linked_list::push(node * n)
{
//set new node to point to current head
n->_next_node = _head;
//make our new node the head
_head = n;
}
string linked_list::pop(){
string val;
node * p = NULL;
p = _head;
if(p == NULL)
return "";
_head = p->_next_node;
val = p->_value;
delete p;
return val;
}
int main(int argc, const char * argv[])
{
int choice;
string ele;
node *n;
linked_list list;
ifstream ipFile;
ipFile.open("US_Presidents.txt");
while(!ipFile.eof())
{
getline(ipFile, ele);
n = new node(ele);
list.push(n);
}
cout<<"The names from the file: "<<endl;
list.print();
cout<<"Enter your name: ";
string yourName;
getline(cin, yourName);
n = new node(yourName);
list.push(n);
cout<<"The stack elements after inserting my name: "<<endl;
list.print();
list.pop();
cout<<"The stack elements again after removing my name: "<<endl;
list.print();
list.makeEmpty();
if(list.isEmpty())
cout<<"The list is empty now..."<<endl;
else
cout<<"The list is still not empty..."<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.