C++ Linked List Watch

Usar
Badges: 8
Rep:
?
#1
Report Thread starter 9 years ago
#1
Would someone be nice enough to look through my implementation of a doubly-linked list that I did with C++ to hold a list of numbers? I'm worried that my pointer use may be inefficient. There are five files in all (3 .cpps and 2 .hs) which are easy to understand. Please reply if you want to help!
0
reply
laser
Badges: 0
Rep:
?
#2
Report 9 years ago
#2
(Original post by Usar)
Would someone be nice enough to look through my implementation of a doubly-linked list that I did with C++ to hold a list of numbers? I'm worried that my pointer use may be inefficient. There are five files in all (3 .cpps and 2 .hs) which are easy to understand. Please reply if you want to help!
Pastebin it somewhere? Although 5 files does sound like a lot.
0
reply
Usar
Badges: 8
Rep:
?
#3
Report Thread starter 9 years ago
#3
I pastebinned it.

Here are the headers:
http://pastebin.com/m63d844e7

Here are the .cpps:
http://pastebin.com/m26822dcd

I think it's ok, but I'd appreciate pointing out of any bad habits/coding/usage. Thanks.
0
reply
nowheree
Badges: 0
Rep:
?
#4
Report 9 years ago
#4
(Original post by Usar)
I pastebinned it.

Here are the headers:
http://pastebin.com/m63d844e7

Here are the .cpps:
http://pastebin.com/m26822dcd

I think it's ok, but I'd appreciate pointing out of any bad habits/coding/usage. Thanks.
i only skimmed a bit of it but why are you using a doubly linked list just to find highest/smallest numbers? I don't see you freeing any of the pointers either

using NULL instead of zero is a bit clearer, also it could do with a few brief comments
0
reply
INTit
Badges: 17
Rep:
?
#5
Report 9 years ago
#5
Code:
char *a = new char[3];
        while (*a != 'q') {
               
                cout << "Enter a number to add to the list (q to stop): ";
                cin >> a;
                if (*a != 'q') {
                        l.insert(atoi(a));
                }
                else {
                        break;
                }
        }
Is buggy as hell why are you doing it like that ? You got no bounds checking or input validation.

Regarding the list its got no memory deallocation so will be leaking memory. I remember I tried to code a double linked list out of boredom once but gave up when It came to freeing the memory.

I would stick to the STL data structures in a real project but this is good pointer experiance.
0
reply
DFranklin
Badges: 18
Rep:
?
#6
Report 9 years ago
#6
Unless I'm very much mistaken, ::remove crashes if you try to remove an element that doesn't exist.

As others have said, no deallocation, either on removing elements, or in the destructor for the list as a whole.
0
reply
Psyk
Badges: 14
Rep:
?
#7
Report 9 years ago
#7
(Original post by INTit)
I would stick to the STL data structures in a real project but this is good pointer experiance.
For my job I've been specifically told not to use the STL, so it depends on the nature of the project.
0
reply
Usar
Badges: 8
Rep:
?
#8
Report Thread starter 9 years ago
#8
Thanks for the posts.

Yeah I know some parts of it aren't foolproof (i.e. removing a non-existent element) but I didn't bother with that since I just wanted to get good at pointers.

Regarding the pointers memory deallocation: do you just mean that I need to do delete nameOfNode when the time's right? I didn't think this was necessary as I was told that you only need to free memory in that way when you use something like "Node node = new Node(200)" with the keyword new. So do I just need to delete all my nodes in ~Node? (Or ~List).
0
reply
Usar
Badges: 8
Rep:
?
#9
Report Thread starter 9 years ago
#9
(Original post by nowheree)
i only skimmed a bit of it but why are you using a doubly linked list just to find highest/smallest numbers? I don't see you freeing any of the pointers either

using NULL instead of zero is a bit clearer, also it could do with a few brief comments
I used a doubly linked list 'cause I just wanted to code it. Finding the min/max numbers isn't really important.. just wanted to do it.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Have you registered to vote?

Yes! (260)
38.92%
No - but I will (45)
6.74%
No - I don't want to (49)
7.34%
No - I can't vote (<18, not in UK, etc) (314)
47.01%

Watched Threads

View All