Sign in to follow this  

Linked List Problem

This topic is 4198 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am new to c++ and i wana make a pathfinding program anyway i want to save positions of the map on a linked list. So i wrote this: typedef class anlist//ALPHANUMERIC LIST { public: anlist() { pointer = 0; _next = 0; x = y = 0; } void add() { if(_next == 0) { _next = new anlist; } else { cout << "?"; } } ~anlist() { if(pointer != 0) { delete [] pointer; } if(_next != 0) { delete [] _next; } } unsigned short x,y; private: char *pointer; anlist *_next; }ANLIST; The problem is that when i try to add() a bode to the list it says Debig Assseretion Failed! and an expression _BLOCK_TYPE_IS_VALID(pHEAD->nBlockinuse). But that just doesent make sense !? Any reply would be apriciated

Share this post


Link to post
Share on other sites
delete & delete[] both check for null-ness, you're just adding one more check like this:

if (p != NULL) {
// delete {
if (p != NULL) {
...
} // } delete
}




EDIT: The A.P. meant that you should be using std::list:

#include <list>
using std::list;

int main() {
std::list<int> l;

l.push_back(1);
l.push_front(0);
l.push_front(123);
l.pop_front();

// should be:
// 0 1
}

Share this post


Link to post
Share on other sites
Well i got that about delete but about the stl ?
My list is a cust one i am not using list.h.
The std::list<int> l;
Doesent mean that list is a class template ?
and i have wrote using namespace std then the std:: would not be necesary,would it ?

PS:How do you put the source code in a box ?

Share this post


Link to post
Share on other sites
Code in a box in source /source tags, each in square brackets.

They mean you "should" use std::list in the sense that it is better to use the tried and tested, writted by experts list class rather than write you own.

Writing your own is a very good learning technique though. Just don't do it in any critical, team-based or life-critical applications!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by DigitalBeing
My list is a cust one i am not using list.h.
The std::list<int> l;
Doesent mean that list is a class template ?


Yes, it's a templated class. Hence, you can use it for any type you want, including your own one.

Quote:
Original post by DigitalBeing
and i have wrote using namespace std then the std:: would not be necesary,would it ?


You are correct.

Share this post


Link to post
Share on other sites

This topic is 4198 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this