Using C++: 1. Stack-like behavior. A stack is a first in last out (FILO) structu
ID: 3759485 • Letter: U
Question
Using C++:
1. Stack-like behavior. A stack is a first in last out (FILO) structure. So the top of the stack will be the last item entered. You will create a singly linked Stacknode. You will need to add an element and to take off an element. You should have functions:
void add(Type_of_Data Value)
Type_of_Data remove()
Type_of_Data is whatever data type you want to use; int, float, char, string, or even a class. You are only required to use one data type! So using int, add() will take an integer value, create a node, set your value to the data part of the node, and adjust pointers as necessary. The remove() function will return the data in the top node. It will then remove the node and adjust pointers.
Explanation / Answer
Here is the code for stack implementation, using Singly Linked List. If you have any difficulty in understanding the code, just revert to me.
#include <iostream>
#include <cstddef>
using namespace std;
using std::cout;
using std::endl;
/* definition of the list node class */
class node
{
friend class linked_list;
private:
int _value;
node *_next_node;
public:
node(void) : _next_node(NULL){ }
node(int val) : _value(val), _next_node(NULL){ }
node(int val, node* next) : _value(val), _next_node(next) { }
/* accessors */
int 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(int val);
void print();
void add(node * n);
int remove();
};
linked_list::linked_list()
{
_head = _tail = NULL;
}
linked_list::linked_list(int 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: ";
while (n != NULL)
{
cout << n->_value;
n = n->_next_node;
cout << " ";
}
cout << endl;
}
void linked_list::add(node * n)
{
//set new node to point to current head
n->_next_node = _head;
//make our new node the head
_head = n;
}
int linked_list::remove(){
int val;
node * p = NULL;
p = _head;
if(p == NULL)
return -1;
_head = p->_next_node;
val = p->_value;
delete p;
return val;
}
int main(int argc, const char * argv[])
{
int choice, ele;
node *n;
linked_list list;
while(1)
{
cout<<"Stack Operations"<<endl;
cout<<"1. PUSH."<<endl;
cout<<"2. POP."<<endl;
cout<<"3. DISPLAY."<<endl;
cout<<"4.EXIT."<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the element to PUSH: ";
cin>>ele;
n = new node(ele);
list.add(n);
break;
case 2:
ele = list.remove();
if(ele == -1)
cout<<"No element to POP."<<endl;
else
cout<<"The popped element is: "<<ele;
break;
case 3:
list.print();
break;
case 4:
exit(0);
default: cout<<"Invalid MENU option. Please choose from the menu."<<endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.