Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,374 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,000 people online right now. Registration is fast and FREE... Join Now!




c++ stack using linked list style pointers

 
Reply to this topicStart new topic

c++ stack using linked list style pointers, Need some help please.

zoto
25 Oct, 2006 - 05:50 AM
Post #1

New D.I.C Head
*

Joined: 25 Oct, 2006
Posts: 1


My Contributions
I'm writing a program that needs to use a "stack" class to store nodes of a map for reference later. I cannot use STL or arrays.

I'm trying to test if this works, it seems to compile, but the code crashes on execution with the error:

"Run-Time Check Failure #3 - The variable 'a' is being used without being defined." on the line "a->cost = 2;".

How can i fix this and what should i do to make this cleaner/ better code.

The rest of my project is to use the stack and an adjacency list to calculate costs of going from one node to the others based one which nodes are linked to which.

Any help would be appreciated, thanks, Aaron.


My code so far:
CODE

//stackP.cpp
//this is my main, testing code here for now.
#include "searchmain.cpp"


using namespace std;

int main(){
    stackP *stack  = new stackP();
    stackP::stackNode *a;
    a->cost = 2;
    a->L = 'd';
    stack->push(a);
    stack->dumpstack();


    return 0;
}


//stackP.h
#include <iostream>

using namespace std;

class stackP{

public:
    stackP();
    struct stackNode{
        char L;
        int cost;
        stackNode* below;
    };
    bool push(stackNode*);
    bool pop();
    void dumpstack();

private:
    stackNode* top;



};



//stackP.cpp
#include <iostream>
#include "stackP.h"

using namespace std;

stackP::stackP()
{    
    top = NULL;    
}
bool stackP::push(stackP::stackNode *node){
    stackNode *pushtotop = new stackNode;
    top = pushtotop;
    
    pushtotop->cost = node->cost;
    pushtotop->L = node->L;
    pushtotop->below = top;
    return true;
}
    
bool stackP::pop(){
    if(top == NULL){
        return false;
    }
    else{
        stackNode *temp;
        temp = top;
        top = top->below;
        delete temp;
    }

}

void stackP::dumpstack(){
    stackNode *temp = top;
    while(temp != NULL){
        cout << "cost = " << temp->cost << "airport: " << top->L << endl;
        temp = temp->below;
    }


}


Thanks, zoto.
User is offlineProfile CardPM
+Quote Post

dragonlady
RE: C++ Stack Using Linked List Style Pointers
25 Oct, 2006 - 08:53 AM
Post #2

D.I.C Head
**

Joined: 7 Aug, 2005
Posts: 57


My Contributions
So a is a pointer to a variable of type stackNode? I'd say the problem is that in the first bit of code you've posted, where you say you're running tests, you haven't #included your stackP files. So when the compiler sees a reference to something called a stackNode, it doesn't know what that is. That'd be my guess just from looking.
User is offlineProfile CardPM
+Quote Post

Videege
RE: C++ Stack Using Linked List Style Pointers
25 Oct, 2006 - 09:07 AM
Post #3

rêvant.toujours
Group Icon

Joined: 25 Mar, 2003
Posts: 1,406


Dream Kudos: 150
My Contributions
In addition to dragon lady's advice, you need to keep in mind that when you say

CODE

stackP::stackNode *a;


you are declaring a pointer to a stackNode object, not a stackNode object itself. You will need to either assign this pointer the address of an existing stackNode or declare a new one in memory using the new keyword as you did with your Stack object.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 01:37AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month