![]() |


|
|||||||
| Hardware & Software Discussion Hardware and Software discussion and troubleshooting. Tweakers and Overclockers welcome! |
![]() |
|
|
Thread Tools |
|
|
#1 (permalink) |
![]() ![]() Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
|
C++ Gurus
I took a few intensive courses on C++ and Data structures my freshmen year of college. 5 years later I'm a bit rusty, but have a few requests to critique my logic:
Simple function to remove a node in a list of type list, I think I've got the right idea but I'm so damn rusty on pointers it's killing me: Code:
//This isn't right, but just shows what I'm working with
struct list() {
list *next;
list *prev;
list *data;
}
//This is the function I wrote
void remove(list node) {
list *nPrev = node->prev;
list *nNext = node->next;
nPrev->next = nNext;
nNext->prev = nPrev;
delete node;
return 1;
}
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop. TGNS Admin | NS Forum Moderator Digz | NS Play Tester Natural Selection | TGNS Primer | Communication Awareness! Army Info: Deployment or Bust - June 2009 |
|
|
|
|
|
#2 (permalink) |
|
Join Date: May 2003
Location: Ottawa, Canada
Posts: 4,600
|
Re: C++ Gurus
You probably want to do some kind of comparison with node and the current node you're looking at. If they are the same, delete it.
This part looks funny... Code:
nPrev->next = nNext; Code:
nNext = node->next; nPrev = node->prev; |
|
|
|
| Sponsored links | |
|
|
|
|
|
#3 (permalink) |
![]() ![]() Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
|
Re: C++ Gurus
Hm, I thought that was what I was doing? I thought I was taking the node, selecting its next pointer as a node, and then changing the "next" nodes previous to the original nodes previous. And doing the same with the nodes previous pointer.
Or am I just using them wrong?
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop. TGNS Admin | NS Forum Moderator Digz | NS Play Tester Natural Selection | TGNS Primer | Communication Awareness! Army Info: Deployment or Bust - June 2009 |
|
|
|
|
|
#4 (permalink) |
|
Join Date: May 2003
Location: Ottawa, Canada
Posts: 4,600
|
Re: C++ Gurus
Ah, I see... I got all mixed up when I first looked at it.
What happens when you run it? The only other thing that might cause a problem is the function signature. If the function's argument is pass by value, it might not delete when you want it to. You'll have to pass the node argument by reference since the parameter calls for the list struct and not a pointer to a list struct. Change the function's parameter to Code:
void remove (list *node) Code:
remove(&node); |
|
|
|
|
|
#5 (permalink) |
![]() ![]() Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
|
Re: C++ Gurus
Understood. Right now I haven't been able to run any code, my PC is currently dead.
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop. TGNS Admin | NS Forum Moderator Digz | NS Play Tester Natural Selection | TGNS Primer | Communication Awareness! Army Info: Deployment or Bust - June 2009 |
|
|
|
|
|
#6 (permalink) |
![]() ![]() Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
|
Re: C++ Gurus
Ok, what about this:
Code:
void remove() {
list *nNext = this->next;
list *nPrev = this->prev;
nNext ->prev = nPrev ;
nPrev ->next = nNext ;
delete this->data;
delete this;
}
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop. TGNS Admin | NS Forum Moderator Digz | NS Play Tester Natural Selection | TGNS Primer | Communication Awareness! Army Info: Deployment or Bust - June 2009 |
|
|
|
| Sponsored links | |
|
|
|
|
|
#8 (permalink) |
![]() ![]() Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
|
Re: C++ Gurus
Yeah that's what I did (seems more logical, plus I won't have to check if the node is of type list). Great, thanks for the guidance.
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop. TGNS Admin | NS Forum Moderator Digz | NS Play Tester Natural Selection | TGNS Primer | Communication Awareness! Army Info: Deployment or Bust - June 2009 |
|
|
|
|
|
#9 (permalink) |
![]() Join Date: Aug 2005
Location: By the PC
Age: 35
Posts: 2,259
|
Re: C++ Gurus
Perhaps off topic, but if you are going to do programming these days you might want to look at either Java or .NET. This way you can safely ignore the pesky *'s.
Pointer arithmetics will be a thing of your past.
__________________
-- VI VI VI - the number of the beast |
|
|
|
|
|
#10 (permalink) | |
|
Join Date: May 2003
Location: Ottawa, Canada
Posts: 4,600
|
Re: C++ Gurus
Quote:
Managed code would be good for this unless it's a lesson in using pointers. I think it's still good to understand what pointers are and how to use them. Java uses pointers but not in the way you'd think because you never have to think about memory references. This can lead to problems when you accidentally pass a reference to a method instead of a copy of the object or vice versa. You also still need to understand the concept of pointers for linked lists! ![]() |
|
|
|
|
| Sponsored links | |
|
|
|
|
|
#11 (permalink) |
![]() ![]() Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
|
Re: C++ Gurus
This was entirely for a refresher with pointers and linked lists. I've done this all before, but I just needed to wrap my head around it again.
Plus I enjoy the control I get with pointers Now if I just remembered how to write in C instead of C++ I'd be in line to create some quick peices of code.Now Assembly language is where the real speed is at
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop. TGNS Admin | NS Forum Moderator Digz | NS Play Tester Natural Selection | TGNS Primer | Communication Awareness! Army Info: Deployment or Bust - June 2009 |
|
|
|
|
|
#12 (permalink) |
![]() ![]() ![]() ![]() Join Date: Oct 2005
Location: Gillette Stadium, Section 309, Row 12, Seat 24
Age: 33
Posts: 8,468
|
Re: C++ Gurus
Whenever I'm using linked lists, I use a 'list *pCurrent' pointer and use that as my main reference point. (the notion of a 'current' pointer gets used a lot in real-time code as well)
|
|
|
|
| Sponsored links | |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|

