473,493 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem using delete (Linked Lists)

Hi everyone, I'm trying to code a linked list example and everything
was working fine. Then I started to code my clear() (to clear my list)
but it won't work. Here is the code:

class list {
protected:
struct node {
char* data;
node* prev;
node* post;
node(): prev(NULL),post(NULL) {};
node(char* u): data(u),prev(NULL),post(NULL) {};
};
node* head;
node* tail;
int QS,qtd;
public:
list(int size,char* u);
void go(char* u);
void show();
void clear();
};

void list::clear()
{
node *L,*D;
int I;
for (L=head;L!=tail;L=D) { D=L->post; delete L; };
cout << "clear(): " <<endl;
};

I didn't post all the code here but I can send the rest of it if anyone
needs (wishes).
Well, I can compile it and even run it, but "clear()" won't work.
I wrote a main.cpp just to populate the list, then show its nodes,
clear the list, then show its nodes again (it should be 0 nodes, after
the clear()), but after the clear, its nodes are the very same before
the clear.

---main.cpp----
#include stuff
int main() {
list L(20,"teste");
L.go("papa01");
L.go("papa02");
L.go("papa03");
L.go("papa04");
L.show();
L.clear();
L.show();
};
----------------

the output is:
----
papa01
papa02
papa03
papa04
clear():
papa01
papa02
papa03
papa04
-----

If anyone can help just saying what is wrong, I would appreciate it.
Thanks, Jonas.

ps: Sorry about the poor english.

Jan 6 '07 #1
4 1613
* Jonas Ferreira:
Hi everyone, I'm trying to code a linked list example and everything
was working fine. Then I started to code my clear() (to clear my list)
but it won't work. Here is the code:

class list {
protected:
struct node {
char* data;
node* prev;
node* post;
node(): prev(NULL),post(NULL) {};
node(char* u): data(u),prev(NULL),post(NULL) {};
};
node* head;
node* tail;
int QS,qtd;
public:
list(int size,char* u);
void go(char* u);
void show();
void clear();
};

void list::clear()
{
node *L,*D;
int I;
for (L=head;L!=tail;L=D) { D=L->post; delete L; };
cout << "clear(): " <<endl;
};
Avoid non-idiomatic single char names. Don't use all uppercase
identifiers except for macros. Remember to delete the tail node and
null out 'prev' and 'post'.

I didn't post all the code here but I can send the rest of it if anyone
needs (wishes).
No need.

Well, I can compile it and even run it, but "clear()" won't work.
I wrote a main.cpp just to populate the list, then show its nodes,
clear the list, then show its nodes again (it should be 0 nodes, after
the clear()), but after the clear, its nodes are the very same before
the clear.
That's just by happenchance: the memory for each deleted node has been
made available for reuse, but (with this C++ implementation) the
contents haven't been changed.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 6 '07 #2
* Alf P. Steinbach:
null out 'prev' and 'post'.
'head' and 'tail', I meant.

Those names are confusing.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 6 '07 #3
Sorry about the names, i had to change them to post here (they were all
in portuguese).
I setted the head and tail pointers to NULL and now everything is
working fine.

You said "with this C++ implementation" the contents remain unchanged.
Is there a way to "nullify" these content?? Is it really necessary?
Because I was taught that you don't need a destructor, because most
compilers already give you one that destroys the builtin types (does
this apply to char* type?).

Really, thanks for the help!

Alf P. Steinbach escreveu:
* Alf P. Steinbach:
null out 'prev' and 'post'.

'head' and 'tail', I meant.

Those names are confusing.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 6 '07 #4

Jonas Ferreira wrote:

1.
node(): prev(NULL),post(NULL) {};
node(char* u): data(u),prev(NULL),post(NULL) {};
much better do like this:

node(char* u=0): data(u),prev(NULL),post(NULL) {};

2.
List must have ctors and dtor

public:
list()throw():head(0),tail(0),QS(?),qtd(?){}
~list()throw(){clear();}

//at least disable copy&assign
private:
list(const list&)throw():head(0),tail(0),QS(?),qtd(?){exit(1) ;}
void operator= (const list&)throw(){exit(1);}

3.
void list::clear()
{
node *L,*D;
int I;
for (L=head;L!=tail;L=D) { D=L->post; delete L; };
cout << "clear(): " <<endl;
};
Do not use uninitialized vars (*L,*D; I).
What the reason of D?

Learn C/C++ function structure. It is something like this:

chech incoming (bounds etc)
do actions
chech result

After function have returned, class must remain in defined correct
state.
What do with list after clear()?

Learn RAII resource control idiom.

http://www.hackcraft.net/raii

This is nothing wrong with naked pointers using, but It is often no
reasons to do it in new classes, because RAII wrapper often gives
better and clear structure of classes.

To use RAII you need define some types of "holders" with different
behaviour, (something like auto_ptr<>), keeping each naked resource
separatedly.

Note (if you are using unwrapped resources) that the C++ does not
allow automatic call of destructor if ctor throw.

Jan 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
12818
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
57
4228
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
17
5798
by: Yuri CHUANG | last post by:
Hi, I'm the beginner of the CPL.I write a program about the problem of Ring of Josephus,using DoubleLinkList data structure. I'm very confused that I think there is really no error in my...
3
1863
by: Rohini Ramamurthy | last post by:
Hi All, I have taken up this problem to learn a bit in using linked lists. I am trying a variant of it what it is supposed to so is delete the nth person clockwise and counter clockwise...
5
9903
by: Neil | last post by:
I am getting time-out errors when I try to perform a simple delete on a linked server. The command is: Delete From MyTable Where PKID=12345 I have tried executing this command directly from...
11
3726
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
8
3925
by: tonywinslow1986 | last post by:
I'm reading MIT's book "Introduction to Algorithms". The following is one of the excercises from it: < 10.2-8 Explain how to implement doubly linked lists using only one pointer value np per...
10
6552
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
36
2860
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
0
7119
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6873
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7367
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
285
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.