Learning C++

Discussion on C, C++ and C++11 at the COJ. This is the place to clear your doubts about these languages, and to share with the community the new things you learn about them.
User avatar
Phantom
Posts: 58
Joined: Thu Nov 17, 2011 11:21 am
Location: Cuba
Gender: None specified

Learning C++

Postby Phantom » Thu Jun 28, 2012 3:51 am

People!! I can say I am re-starting in C++. My friend Robbin has been orientating me. But... I can not molest him more... The problem is the next. I has been doing some exercises were I have to use a queue. Now... I has been receiving Runtime Error in these exercises. I supouse the problem is in my queue. Pleace, if you can do any appoint about any suggestion or error, please, do it!! My queue with the Robbin´s help is the next:

Code: Select all

template <class T>
class Node
{
public:
    T Value;
    Node(T);
    Node<T>* Next;
};

template <class T>
Node<T>::Node(T val)
{
    Value = val;
    Next = NULL;
}

template <class T>
class Queue
{
    Node<T>* act;
    Node<T>* last;
public:
    Queue();
    ~Queue();
    void Enqueue(T);
    bool Dequeue(T&);
};

template <class T>
Queue<T>::Queue()
{
    act = NULL;
    last = NULL;
}

template <class T>
Queue<T>::~Queue()
{
    Node<T> * nodito=act;
    while(act!=NULL)
    {
        act=act->Next;
        nodito->Next=NULL;
        delete nodito;
   nodito=act;
    }
    delete act;
    act=NULL;
    last=NULL;
}

template <class T>
void Queue<T>::Enqueue(T val)
{
    if(act!=NULL)
    {
        last->Next = new Node<T>(val);
        last = last->Next;
    }
    else
    {
        last = new Node<T>(val);
        act = last;
    }
}

template <class T>
bool Queue<T>::Dequeue(T &result)
{
    if(act == NULL)return false;
    result = act->Value;
    Node<T>* temp = act;
    act = act->Next;
    temp = NULL;
    delete temp;
    if(act == NULL)delete last;
    return true;
}



User avatar
Robbin
Posts: 38
Joined: Mon Nov 21, 2011 11:42 am
Location: Santiago de Cuba
Gender: None specified

Re: Learning C++

Postby Robbin » Thu Jun 28, 2012 8:14 pm

Hello again!!
I told you not to worry for anything..
just tell me if you need something..
Now I finally finished seeing your code.. and I think there's another litle problem on your code.. in the dequeue function..
This is your function:
template <class T>
bool Queue<T>::Dequeue(T &result)
{
if(act == NULL)return false;
result = act->Value;
Node<T>* temp = act;
act = act->Next;
temp = NULL;
delete temp;
if(act == NULL)delete last;
return true;
}

your'e leaving trash here..
before you say temp=NULL; you have to write temp->Next=NULL;
then you can erase the step when you say temp=NULL;
if you just write temp->Next=NULL; delete temp; that's right..
and may be you can quit the senctence delete last.. and put last=NULL;

User avatar
Robbin
Posts: 38
Joined: Mon Nov 21, 2011 11:42 am
Location: Santiago de Cuba
Gender: None specified

Re: Learning C++

Postby Robbin » Thu Jun 28, 2012 8:19 pm

Question, phantom:
how do you invoke the functions??

User avatar
Phantom
Posts: 58
Joined: Thu Nov 17, 2011 11:21 am
Location: Cuba
Gender: None specified

Re: Learning C++

Postby Phantom » Fri Jun 29, 2012 1:09 pm

Wait a moment; my problem is that I don’t see yet the difference between:
temp = NULL; and delete temp;
Can you explain me that very well?

I invoke the functions normally, as I do it in all the languages:
jose = pepe.Friend();

User avatar
Robbin
Posts: 38
Joined: Mon Nov 21, 2011 11:42 am
Location: Santiago de Cuba
Gender: None specified

Re: Learning C++

Postby Robbin » Fri Jun 29, 2012 1:28 pm

The problem is that temp has a field named Next, and another fields, but, Next is an apointer..
when you say delete temp you restore all the occuped memory for temp, otherwise you just say that temp is a null field, but the old value of temp still in memory.. you don´t erase it..
please send me back your e-mail and I will send you an image for explain you that..

PD: I can send you the conference about aponters in c++, and linked lists and so.. if you want it..

User avatar
Robbin
Posts: 38
Joined: Mon Nov 21, 2011 11:42 am
Location: Santiago de Cuba
Gender: None specified

Re: Learning C++

Postby Robbin » Fri Jun 29, 2012 1:29 pm

The problem is that temp has a field named Next, and another fields, but, Next is an apointer..
when you say delete temp you restore all the occuped memory for temp, otherwise you just say that temp is a null field, but the old value of temp still in memory.. you don´t erase it..
please send me back your e-mail and I will send you an image for explain you that..

PD: I can send you the conference about aponters in c++, and linked lists and so.. if you want it..

User avatar
Phantom
Posts: 58
Joined: Thu Nov 17, 2011 11:21 am
Location: Cuba
Gender: None specified

Re: Learning C++

Postby Phantom » Tue Jul 03, 2012 5:22 pm

Yes!! I want it!! But… now we have problems with the e-mail in my faculty. Send me that to my internet e-mail. I´ll try to check it. My internet e-mail is in my profile, but I´ll give you now… Write me to: alejandrobez@gmail.com
I’ll be grateful with you.

User avatar
Robbin
Posts: 38
Joined: Mon Nov 21, 2011 11:42 am
Location: Santiago de Cuba
Gender: None specified

Re: Learning C++

Postby Robbin » Thu Jul 05, 2012 3:32 pm

sorry, phantom, but I can't arrive to an a-mail if it's out of Cuba, or if it's international, I have no rights

User avatar
Robbin
Posts: 38
Joined: Mon Nov 21, 2011 11:42 am
Location: Santiago de Cuba
Gender: None specified

Re: Learning C++

Postby Robbin » Thu Jul 05, 2012 3:34 pm

If you could enter on my faculty, may be you can obtain those things..
try ftp://10.30.150.4

User avatar
Phantom
Posts: 58
Joined: Thu Nov 17, 2011 11:21 am
Location: Cuba
Gender: None specified

Re: Learning C++

Postby Phantom » Wed Jul 11, 2012 1:00 pm

:( I couldn't enter... Is a shame that you can't arrive to an a-mail if it's out of Cuba... I have not way to exchange programs and informaton with you... :?


Return to “C/C++/C++11”

Who is online

Users browsing this forum: No registered users and 1 guest