CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 1999
    Location
    wallingford, pennsylvania
    Posts
    70

    basic C++ pointer stuff

    i wrote this stack program a while ago, and have been unable to make the listWrite and pop functions work. please help me.

    CODE:
    //program stack- linked list data structure

    #include <iostream.h>
    #include <stddef.h>

    struct nodeType;
    typedef nodeType* nodePtrType;
    typedef int itemType;



    //--------------Definition of struct nodeType---------------
    struct nodeType
    {
    itemType items;
    nodePtrType nextPtr;
    };
    //----------------------------------------------------------


    itemType item;



    //---------------Definition of class stack------------------
    class stack {
    public:
    initStack();
    //initializes stack

    void listWrite();
    //prints out entire stack

    void push(int&);
    //puts an inputed integer on top of stack

    void pop(int&);
    //removes top item from stack

    int topItem();
    //returns top item of stack

    private:
    nodePtrType S;
    };
    //----------------------------------------------------------



    //____________________-MEMBER FUNCTIONS-_____________________
    /***********************************************************/
    stack::initStack()
    //pre: S is an empty stack
    //post: S is initialized to null
    {
    S = NULL;
    return 0;
    }

    /***********************************************************/
    void stack::listWrite()
    //pre: S is not empty
    //post: list is printed
    {
    nodePtrType currPtr;
    currPtr = S;


    while(currPtr != NULL)
    {
    cout << currPtr->items << "\n";
    currPtr = currPtr->nextPtr;
    }


    }

    /************************************************************/
    void stack:ush(int& item)
    //pre: stack is valid and initialized
    //post: entered integer is put on top of stack
    {
    nodePtrType temp;

    temp->items = item;
    temp->nextPtr = S;
    S = temp;
    }

    /*************************************************************/
    void stack:op(int& item)
    //pre: stack has at least one element
    //post: top element is removed from stack
    {
    item = S->items;
    S = S->nextPtr;
    }

    /*************************************************************/
    int stack::topItem()
    //pre: stack has at least one item
    //post: top item of stack is returned
    {
    item = S->items;
    return item;
    }



    //__________________________-MAIN-_____________________________
    void main ()
    {
    stack s;

    s.initStack();

    cout << "Enter an integer (999 to quit): ";
    cin >> item;



    while(item != 999)
    {
    s.push(item);
    cout << "Enter an integer (999 to quit): ";
    cin >> item;
    }

    cout << "\n\nTop item is " << s.topItem() << "\n";


    }

    END CODE

    additionally, the book i'm using recommends breaking any class program into 3 files, but it provides no examples on how to do this. please show me.


  2. #2
    Join Date
    Apr 1999
    Location
    California USA
    Posts
    15

    Re: basic C++ pointer stuff

    When you push one item into stack, actually, you need to allocate a piece of memory and its size is struct nodeType. In your push(), you only decalre a nodePtrType, which is a pointer address not a entire struct of nodeType.
    void stack:ush(int& item)
    {
    nodePtrType *temp; // pointer

    temp = new (struct nodeType); // allocate a new node from heap
    temp->items = item;
    temp->nextPtr = S;

    S = temp;
    }

    After you pop out one item, you need to free the memory. There are one problem: if there are no more item in the stack, what are you gonna do?
    /*************************************************************/
    void stack:op(int& item)
    //pre: stack has at least one element
    //post: top element is removed from stack
    {
    nodePtrType temp;

    if(S == NULL){
    cout << "No more item in stack!!\n";
    return;
    }
    temp = S;
    item = temp->items;
    S = temp->nextPtr;
    delete temp; // free memory
    }

    Also, you need to test the S before accessing it to avoid crashing.
    int stack::topItem()
    //pre: stack has at least one item
    //post: top item of stack is returned
    {
    if(S == NULL){
    cout <<"Stack is empty!!\n";
    return 0;
    }
    item = S->items;
    return item;
    }


    /////////////////////////////////////////////////////////////////
    Hope this will help. Good luck.

    Allen


  3. #3
    Join Date
    May 1999
    Location
    wallingford, pennsylvania
    Posts
    70

    Re: basic C++ pointer stuff

    thanx. this was a great help. but one questions remains; how do code the class definition and its member functions to make it usable as a header file?


  4. #4
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: basic C++ pointer stuff


    File stack.h
    =============
    //stack- definition of linked list data structure

    struct nodeType;
    typedef nodeType* nodePtrType;
    typedef int itemType;

    struct nodeType
    {
    itemType items;
    nodePtrType nextPtr;
    };

    //---------------Definition of class stack------------------
    class stack {
    public:
    initStack(); //initializes stack
    void listWrite(); //prints out entire stack
    void push(int&); //puts an inputed integer on top of stack
    void pop(int&); //removes top item from stack
    int topItem(); //returns top item of stack

    private:
    nodePtrType S;
    };

    //end of header file


    File stack.cpp

    #include "stack.h"

    and the rest of your source code changed as per Allen's specifications

    sally


  5. #5
    Guest

    Re: basic C++ pointer stuff

    I hope this is the correct answer, for the question you asked is incredibly simple...
    All you have to do is put the class definition and member functions in a seperate file with the extension *.h, then just include that where you want to use the declarations


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured