Hi. I'm trying to modify the following code to be able to eliminate the tail node instead of the head node in the pop operation.
How can I do that? Please help me.
// StackCPP.cpp
// This program performs various operations on singly linked lists
#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
// A node in a singly linked list
class ListNode
{
private:
int data;
ListNode * next;
public:
ListNode(int newNumber = 0);
int getData();
void setData(int NewNumber);
ListNode * getNext();
void setNext(ListNode * newNext);
void push(int newNumber);
int pop();
void printStack();
};
//////////////////////////////////////////////////////////////
// This function inserts the number at the head of the stack
//////////////////////////////////////////////////////////////
void Stack::push(int newNumber)
{
ListNode * newNode = new ListNode;
if (this == NULL){
cout << "Error in push" << endl;
return;
}
//////////////////////////////////////////////////////////////
// This function deletes the node from the head of the stack
//////////////////////////////////////////////////////////////
int Stack::pop()
{
int poppedValue;
ListNode * currentNode = new ListNode;
if (this->getHead() == NULL){
cout << "Error in pop" << endl;
return 0;
}
//////////////////////////////////////////////////////////////
// Print a single node. If this is the header node, print "Head"
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// Print out an entire singly linked list
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// Print the menu and get a command from the user
//////////////////////////////////////////////////////////////
char getmenucommand(Stack * s)
{
char str[256];
while(1)
{
cout << "\n";
if (s == NULL) cout << "(C)reate a new stack" << endl;
if (s != NULL) cout << "(P)ush" << endl;
if (s != NULL) cout << "(O)Pop" << endl;
cout << "(Q)uit" << endl;
cin >> str;
if (s == NULL && toupper(str[0]) == 'C') return 'C';
if (s != NULL && toupper(str[0]) == 'P') return 'P';
if (s != NULL && toupper(str[0]) == 'O') return 'O';
if (toupper(str[0]) == 'Q') exit(0);
}
}
//////////////////////////////////////////////////////////////
// Get a number from the user and return it
//////////////////////////////////////////////////////////////