Sonic and Sega Retro Message Board: C++ Help With Lists Take 2 - Sonic and Sega Retro Message Board

Jump to content

Hey there, Guest!  (Log In · Register) Help
Page 1 of 1
    Locked
    Locked Forum

C++ Help With Lists Take 2 I made this a separate thread because I feel it is warranted

#1 User is offline Travelsonic 

Posted 31 October 2016 - 08:04 AM

  • Posts: 704
  • Joined: 01-March 05
After talking to my professor, I found out I can do the projects for my operating systems class in C++. What a relief, as now I can work with that which I am more familiar with.

My troubles in debugging my dequeue implementation have not gone away fully, however, and thus I still need help.

I can push and pop off the front no issue. My problem is, I'm not sure if everything is OK. Right now, my biggest problem seems to be an incorrect count - which is making me question if everything is ship shape with my push and pop functions.
(it currently shows a value one more than the number of values I try pushing in my test program). I would appreciate it greatly if someone could check my code, and help me if there are in fact any issues.

EDIT: I realized that in my push and pop functions I might be able to consolidate a few lines of condition checking, see comment in function for issue I face there.
Here is the code I have so far:
EDIT: 7:18PM: Replaced the copypaste of list.cpp with the current version of my code.
node.h: Contains the definition for my node class.
Spoiler

node.cpp: Contains the function implementations for my node class.
Spoiler

list.h: Contains the definition for my list class.
Spoiler

list.cpp: Contains the function implementations for my list class.
Spoiler

main.cpp: The test program for my linked list.
Spoiler

This post has been edited by Travelsonic: 31 October 2016 - 06:18 PM

#2 User is online Black Squirrel 

Posted 31 October 2016 - 05:10 PM

  • wikiman
  • Posts: 3779
  • Joined: 27-December 03
  • Gender:Male
  • Location:Northumberland, England
  • Project:thinking of a better member title
  • Wiki edits:20,569
C++ is very loose with coding standards - you tend to develop your own or are brought into line with an employer's, BUT


Top tip - don't ever write pointer notation like

"gooble * name"

because you will read this as the multiplication operator. I guarantee it.

"gooble* name"


A "gooble pointer" or "pointer to gooble".


Also
if((tmpNode->getData()) == value){result = true;}


if(tmpNode->getData() == value) {
    result = true;
}


The compiler doesn't give two damns about carriage returns or newlines, but the human eye does, and C++ code tends to be read by humans. This is a fairly simple statement so arguably you're not causing too many future problems but

return ((data == compareNode->getData()) && (next == compareNode->getNext()) && (prev == compareNode->getPrev())) ? true : false;

this is not.

I'm personally not a fan of ternary operators in C++ (opinions vary), but here you don't even need it - you're working with boolean logic.


return ((data == compareNode->getData())
     && (next == compareNode->getNext())
     && (prev == compareNode->getPrev()));



Also as it's a learning environment you're not obliged to care about this one right now, but it's very easy to overdose on comments, to the point where they start having a detrimental effect on workflow. Good code should be self documenting - descriptive variable names and functions will make it easy for othe programmers to understand - you don't need to tell them what a class destructor is.


There are other things, but you'll find that the more readable your code is, the easier it will be to discover where the problems are. a.k.a. I don't have the time to compile and run this so I'm avoiding your question and answering my own :eng101:

#3 User is offline Travelsonic 

Posted 31 October 2016 - 06:05 PM

  • Posts: 704
  • Joined: 01-March 05

View PostBlack Squirrel, on 31 October 2016 - 05:10 PM, said:

C++ is very loose with coding standards - you tend to develop your own or are brought into line with an employer's, BUT


Top tip - don't ever write pointer notation like

"gooble * name"

because you will read this as the multiplication operator. I guarantee it.


I haven't yet, but not saying never is a good call - besides, changing it to the way you suggest keeps things clearer for others who need to read my code. :D

Sometimes when my program code is unfinished, and I need another's opinion / help, it's just I feel like someone will inevitably wonder "what the FUCK were you thinking," and suggest something that completely misses the intent of what I am doing, hence my tendency to sometimes overdo it

#4 User is offline BigEvilCorporation 

Posted 01 December 2016 - 11:56 AM

  • Posts: 23
  • Joined: 17-July 16
  • Gender:Male
  • Location:UK
  • Project:Tanglewood
Your bug is in getLength() - you are checking if cursor's next value is the head, you need to check if cursor is the tail. The last element in the list always points back to the head, so it will never be counted.

A few tips:

- 'nullptr' is now built into the language (C++11 and beyond) so you won't need to explicitly #include the file containing the NULL definition
- Why do you have a non-const getLength()? It serves no purpose, it never modifies anything
- The == operator continues searching even after it's determined that the lists are not equal - you can bail out at that point
- Why is -1 a special number? What if you wanted to actually push a -1 to the list?
This post has been edited by BigEvilCorporation: 01 December 2016 - 11:57 AM

#5 User is offline Travelsonic 

Posted 11 December 2016 - 01:15 PM

  • Posts: 704
  • Joined: 01-March 05

View PostBigEvilCorporation, on 01 December 2016 - 11:56 AM, said:

Your bug is in getLength() - you are checking if cursor's next value is the head, you need to check if cursor is the tail.


But what if it is a circular list, where the tail links back to the head? Surely, if you are getting the length of a circular list, you would check for if you are about to go back to the head, to avoid infinitely looping (hence what I attempted to do)?

Made the tweak to the equality check, thanks for catching that. Missing stuff like that... that's what happens when I try to rush to catch up on a project with very little sleep. XD

I had one of those "I don't know what I did, but it works now" moments - and my dequeue started ALMOST working. (way before I remembered that I had this thread, checked to see if I had further replies... XD) - only problem is my counting function is now not working right. 0_0 After popping a node off the list, counting doesn't work - keeps returning 0.

EDIT: Also realized I didn't even need that boolean variable in my overloaded equality operator. Returns false if it catches inequality in list size, and when comparing the nodes,, allowing for the function to simply return true if it gets to the end.
This post has been edited by Travelsonic: 11 December 2016 - 01:33 PM

Page 1 of 1
    Locked
    Locked Forum

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users