hey, i'm designing a program that uses a linked list. the user 'pushes' values onto the linked list, or 'pops' values off of the list. the program 'pushes' successfuly, but when the user pops values off, it pops values from the wrong end. so for example, if the user pushes 1,2,3,4. then pops a value, its pops off 1, then 2, then 3 etc. i need it to pop starting at the other end of the list. ie, 4,3,2,1. does anyone know how i could possibly 'pop' values off from the opposite end of the list?
main() is self explanitory and didn't need to be included.
thanks alot!
CODE
class STACK
{
private:
struct NODE
{ double num;
NODE *next; // Pointer to next node
};
NODE *temp, *temp2; // Temporary pointers
NODE *head;
public:
STACK(); //Constructor
//~STACK(); //Destructor
void push(double temp);
double pop();
/*bool isEmpty() //if top is empty, return true, and do not execute 'pop'
{
if(temp==NULL){
return true;
}else{
return false;
}
}*/
double current ()
{
return temp->num;
}
};
//Default Constructor
STACK::STACK()
{
head=NULL;
}
//'Push' member method
void STACK::push (double x)
{
// Reserve space for new NODE and fill it with data
temp = new NODE;
temp->num=x;
temp->next = NULL;
// Set up link to this NODE
if (head == NULL)
head = temp;
else
{ temp2 = head;
// We know this is not NULL - list not empty!
while (temp2->next != NULL)
{ temp2 = temp2->next;
// Move to next link in chain
}
temp2->next = temp;
}
}
//'Pop' member method
double STACK::pop ()
{
temp = head;
head = head->next;
return temp->num;
}
This post has been edited by Dark_Nexus: 6 Apr, 2006 - 05:55 PM