Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,630 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,037 people online right now. Registration is fast and FREE... Join Now!




Linked List Problem

 
Reply to this topicStart new topic

Linked List Problem

Quiksilver
post 6 Apr, 2006 - 05:44 PM
Post #1


New D.I.C Head

Group Icon
Joined: 12 Mar, 2005
Posts: 45



Dream Kudos: 25
My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

Nova Dragoon
post 6 Apr, 2006 - 05:50 PM
Post #2


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,128



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


pop from the tail, not the head
User is offlineProfile CardPM

Go to the top of the page

Quiksilver
post 6 Apr, 2006 - 05:56 PM
Post #3


New D.I.C Head

Group Icon
Joined: 12 Mar, 2005
Posts: 45



Dream Kudos: 25
My Contributions


yes, i know tongue.gif
but how?
User is offlineProfile CardPM

Go to the top of the page

Nova Dragoon
post 6 Apr, 2006 - 07:01 PM
Post #4


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,128



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


where you do:

CODE

double STACK::pop ()
{
   temp = head;
   head = head->next;
   return temp->num;
}


traverse the list till the end, that last node is the tail. Pull it off, and set the previous node's next to null.
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 7 Apr, 2006 - 08:41 AM
Post #5


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,970



Thanked 15 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


it would be easier and more efficient, to switch your push so that it adds at the head:
CODE

Node* newNode;
newNode->num = x;
newNode->next = head;
head = newNode;
//clean up memory leak
newNode = NULL; //clear Node address (already pointed to by head)
delete newNode; //delete Node memory (destroy any copy)

then both push and pop would be running in O(1) time, rather than both in O(N).
It is a common mistake to assume that the link must go at the end.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month