Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Stack help C++

  • 17-04-2011 6:53pm
    #1
    Closed Accounts Posts: 2,663 ✭✭✭


    OK, i don't know if this is working or not so could some one look over it for me,, Its giving me no Errors any way so i don't know if it is or not working..

    I'm trying to place my Array into a Stack, and place it back into an Array..


    My Array.
    int x[N_STUDENT];
    
    struct  student
    {
        int student_number;              // Student number of student
        string studentname;             // Name of student
        string Address;                //Address of Studnet
        int CourseCode;               //Course Code 
        string CourseName;           // Name of Course 
    };
    
    student record[N_STUDENT];
    

    Stack.h
    #ifndef STACK_H
    #define STACK_H
    
    
    
    
    namespace StackSavitch
    {
        template<class T>
        class Node
        {
        public:
            Node(T record, Node<T>* theLink) : record(record), link(theLink){}
            Node<T>* getLink( ) const { return link; }
            const T getrecord( ) const { return record; }
            void setrecord(const T& N_STUDENT) { record = record; }
            void setLink(Node<T>* pointer) { link = pointer; }
        private:
            T record;
            Node<T> *link;
        };
        
        template<class T>
        class Stack
        {
        public:
            Stack( );
            //Initializes the object to an empty stack.
            
            Stack(const Stack<T>& aStack);
            
            Stack<T>& operator =(const Stack<T>& rightSide);
            
            virtual ~Stack( );
            
            void push(T stackFrame);
            //Postcondition: stackFrame has been added to the stack.
            
            T pop( );
            //Precondition: The stack is not empty.
            //Returns the top stack frame and removes that top 
            //stack frame from the stack.
            
            bool isEmpty( ) const;
            //Returns true if the stack is empty. Returns false otherwise.
        private:
            Node<T> *top;
        };
        
    }//StackSavitch
    #endif //STACK_H
    

    Stack.cpp
    #include <iostream>
    #include <cstdlib>
    #include <cstddef>
    #include "stack.h"
    using std::cout;
    
    
    namespace StackSavitch
    {
        
        //Uses cstddef:
        template<class T>
        Stack<T>::Stack( ) : top(NULL)
        {
            //Intentionally empty
        }
    
        //Uses cstddef:
        template<class T>
        Stack<T>::Stack(const Stack<T>& aStack)
        {
            if (aStack.isEmpty( ))
                top = NULL;
            else
            {
                Node<T> *temp = aStack.top;//temp moves
                //through the nodes from top to bottom of aStack.
                Node<T> *end;//Points to end of the new stack.
                
                end = new Node<T>(temp->getrecord( ), NULL);
                top = end;
                //First node created and filled with data.
                //New nodes are now added AFTER this first node.
                
                temp = temp->getLink( );//move temp to second node
                //or NULL if there is no second node.
                while (temp != NULL)
                {
                    end->setLink(
                                 new Node<T>(temp->getrecord( ), NULL));
                    temp = temp->getLink( );
                    end = end->getLink( );
                }
                //end->link == NULL;
            }
        }
        
        template<class T>
        Stack<T>& Stack<T>::operator =(const Stack<T>& rightSide)
        {
            if (top == rightSide.top) //if two stacks are the same
                return *this;
            else //send left side back to freestore
            {
                T next;
                while (! isEmpty( ))
                    next = pop( );//remove calls delete.
            }
            
            if (rightSide.isEmpty())
            {
                top = NULL;
                return *this;
            }
            else
            {
                Node<T> *temp = rightSide.top;//temp moves
                //through the nodes from front top to bottom of rightSide.
                
                Node<T> *end;//Points to end of the left side stack.
                end = new Node<T>(temp->getrecord( ), NULL);
                top = end;;
                //First node created and filled with data.
                //New nodes are now added AFTER this first node.
                
                temp = temp->getLink();//Move temp to second node
                //or set to NULL if there is no second node.
                
                while (temp != NULL)
                {
                    end->setLink(new Node<T>(temp->getrecord(), NULL));
                    temp = temp->getLink();
                    end = end->getLink();
                }
                //back->link == NULL;
                
                return *this;
            }
        }
        
        template<class T>
        Stack<T>::~Stack( )
        {
            T next;
            while (! isEmpty( ))
                next = pop( );//pop calls delete.
        }
        
        //Uses cstddef:
        template<class T>
        bool Stack<T>::isEmpty( ) const
        {
            return (top == NULL);
        }
        
        template<class T>
        void Stack<T>::push(T stackFrame)
        {
            top = new Node<T>(stackFrame, top);
        }
        
        //Uses cstdlib and iostream:
        template<class T>
        T Stack<T>::pop( )
        {
            if (isEmpty( ))
            {
                cout << "Error: popping an empty stack.\n";
                exit(1);
            }
            
            T result = top->getrecord( );
            
            Node<T> *discard;
            discard = top;
            top = top->getLink( );
            
            delete discard;
            
            return result;
        }
        
    }//StackSavitch
    

    inside my Main.cpp
     else if ( choice == 5) // Add Array to Stack
                
                
            {
               stack_t();
                
                cout << " Array is Now Placed in Stack";
                
                
            }
    


Comments

  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    What is stack_t() supposed to do?


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    thats what i'm hoping to find out

    What do i need to put into my main.cpp from stack.cpp so that Stack will work. but do i have the Stack.h and stack.ccp right ?


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Where is stack_t() declared? If I made a guess from naming convention, it's just the constructor of some stack<T> typedef.

    Are you using an IDE like visual studio? Can you right click on stack_t(), choose "go to definition" and post that code?

    Anyway, looking at the header for your stack, you'd need to call push to put something on to the stack.


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    I am using Xcode.. to place array in Stack, and place it back into an Array.. what Code do i need to place inside my Main.cpp


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    You can go to definition in XCode too. I'm just curious about where this stack_t came from!

    Anyway, something like this might work...
    typedef StackSavitch::Stack<student*> StudentStack;
    
    StudentStack stack;
    stack.push(record);
    
    student* pRecords = stack.pop();
    


  • Advertisement
  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    I place this code
    typedef StackSavitch::Stack<student*> StudentStack; StudentStack stack; stack.push(record); student* pRecords = stack.pop();
    inside the main.cpp ?


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    Thanks for that,

    its giving me that pRecords Unused variable..


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    That's because the variable is unused! It was just an example of how to pop a pointer to an array off the stack.

    Can you describe at a higher level what you're trying to do?


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    I am trying to do the Following..

    1. Place the Array in a Stack.
    2. Once in the Stack Empty the Stack.
    3. Place Items in Stack into a New Array.

    that is what i am trying to do


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Is this an assignment? If so, it makes more sense to push students from your array individually onto the stack, then pop them off individually into another array.

    So you'd declare your stack the same way.
    StackSavitch::Stack<student> stack;
    

    Then loop over your array pushing each item into the stack. Do the same again with pop to put things into a new array element by element.


  • Advertisement
  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    OK, i kind of get it, take all the Elements of the Array and push it with a loop
    some thing like this ?


    for (int n = 0; n < N_STUDENT;n++)


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    Cork24 wrote: »
    OK, i kind of get it, take all the Elements of the Array and push it with a loop
    some thing like this ?


    for (int n = 0; n < N_STUDENT;n++)

    Pretty much.

    1. Loop through array (using N_STUDENT) and push each student on to array.

    2 & 3. When you want to empty stack and fill array, while stack !isEmpty(), keep popping and adding onto end of array of size N_STUDENT (depending on how you want to maintain order).

    However, if students are added/removed to the stack between 1 and 3, your N_STUDENT variable won't be the correct count of students (and hence an array declared at this size will overflow or be surplus to requirements).

    Could this be the case?


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    Ok, I think i got it all working if i sent a Zip file could some one debug the program as i.. can't seem to find nothing Wrong... but a new Set of Eyes might find something wrong that i'm after over looking...


Advertisement