queue on linkedlists

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 printNode(ListNode * headerNode);
};

class Stack
{
private:
ListNode * head;
public:
Stack();
ListNode * getHead();
void setHead(ListNode * newHead);

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;
}

newNode->setNext(this->getHead());
newNode->setData(newNumber);
this->setHead(newNode);
}


//////////////////////////////////////////////////////////////
// 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;
}

currentNode = getHead();
this->setHead(currentNode->getNext());
poppedValue = currentNode->getData();
delete currentNode;

return poppedValue;
}



//////////////////////////////////////////////////////////////
// Print a single node. If this is the header node, print "Head"
//////////////////////////////////////////////////////////////

void ListNode::printNode(ListNode * headerNode)
{
if (this != headerNode) cout << "-> ";
cout << this->getData() << " ";
}



//////////////////////////////////////////////////////////////
// Print out an entire singly linked list
//////////////////////////////////////////////////////////////

void Stack::printStack()
{
ListNode * currentNode;

if (this == NULL) return;

cout << endl;

if (this->getHead() == NULL)
{
cout << "NULL" << endl;
return;
}

currentNode = this->getHead();

while(currentNode != NULL)
{
currentNode->printNode(this->getHead());
currentNode = currentNode->getNext();
}

cout << "-> NULL" << endl;
}


//////////////////////////////////////////////////////////////
// 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
//////////////////////////////////////////////////////////////

int getnumber()
{
char str[256];

cout << "Enter a number:";
cin >> str;
return atoi(str);
}


ListNode::ListNode(int newNumber)
{
data = newNumber;
next = NULL;
}

int ListNode::getData() { return data; }
void ListNode::setData(int newNumber) { data = newNumber; }
ListNode * ListNode::getNext() { return next; }
void ListNode::setNext(ListNode * newNext) { next = newNext; }

Stack::Stack()
{
head = NULL;
}

ListNode * Stack::getHead() { return head; }
void Stack::setHead(ListNode * newHead) { head = newHead; }



//////////////////////////////////////////////////////////////
// main function
//////////////////////////////////////////////////////////////

int main()
{

Stack * s = NULL;
char command;
int number;

while (1)
{
s->printStack();

command = getmenucommand(s);

// Create a new stack
if (command == 'C')
{
s = new Stack;
}

// Push an element into the stack
if (command == 'P')
{
number = getnumber();
s->push(number);
}

// Pop a node from the stack
if (command == 'O')
{
number = s->pop();
cout << endl << number << " popped" << endl;
}

}

return 0;
}



Topic archived. No new replies allowed.