Corey Posted June 30, 2012 Share Posted June 30, 2012 (edited) I'm supposed to redo a program I did earlier in the semester when I used Arrays, but replace everything with a Link List. I'm having a really hard time especially with a function below. Here is the description of the function "InsertAfter - inserts a new element after the current position" Heres the code http://nopaste.me/paste/11391501464fee6bffaf998 the Insert_before function seems to work... but the insert_after crashes every time it gets to here temp = NewNode(); temp->num = v; temp->linky = c; p->linky = temp; this is the function im trying to call for (int i=1;i<=20;i++) { a.insert_after(i); } Really tired of looking at it. Thinking about dropping this class cause I fell behind while I as on vacation and not sure if i will be able to rebound Edited June 30, 2012 by Corey Quote Link to comment Share on other sites More sharing options...
rolf Posted June 30, 2012 Share Posted June 30, 2012 In the code you used, you have for(int i = 0;i> 0;i++) { p = c; c = c->linky; } temp = NewNode(); temp->num = v; temp->linky = c; p->linky = temp; Did you fix the loop? I have to go now, but I can help you later. Also, don't drop the course, C++ is one of the most beautiful programming languages and once you see what you're doing you'll love it Quote Link to comment Share on other sites More sharing options...
Clan Friend SunLight Posted June 30, 2012 Clan Friend Share Posted June 30, 2012 this one makes no sense... for (int i = 0; i > 0; i++) btw, it's never executed, and when you get to: p->linky = temp; it crashes because you are accessing a pointer which hasn't been initialized Quote Link to comment Share on other sites More sharing options...
PiNoY Posted June 30, 2012 Share Posted June 30, 2012 you might want to initialize the variable i to a higher value other than 0 , so that for (int i = 0; i > 0; i++) the above and the body of for loop can be executed. never analyzed the entire code though Quote Link to comment Share on other sites More sharing options...
Corey Posted June 30, 2012 Author Share Posted June 30, 2012 (edited) sorry I forgot to correct the loop after I was messing around with it. It is supposed to run until it comes to the LinkPosition for(int i = 0;i <= LinkPosition ;i++) { p = c; c = c->linky; } temp = NewNode(); temp->num = v; p->linky = temp; LinkPosition++; I also had the wrong symbol it was supposed to be <= not >= It works for that specific function I was trying to run so i guess thats a good start thanks for the help guys I just needed a good nights rest to think straight. Edited June 30, 2012 by Corey Quote Link to comment Share on other sites More sharing options...
Clan Friend SunLight Posted June 30, 2012 Clan Friend Share Posted June 30, 2012 I don't know if you were forced to use a pointer only to the head and to the next element in the list, I think having a pointer to both previous and next element is easier, but it would be a double linked list then. I would also suggest you to indent your code better, readability is important when you have to read it after a while, now I discovered a nice little thingy on Linux called indent, that I had always overlooked $ indent -linux testcode.cpp and there you go Anyway, I see a series of problems there in your code, for example that LinkPosition variable, what is it for? It is never changed after you set it to 0, I don't see any place where it is changed from 0 or I'm blind You have to insert after the current position, right? By current you mean the he last one you inserted? Or the last one in the list? Quote Link to comment Share on other sites More sharing options...
Corey Posted June 30, 2012 Author Share Posted June 30, 2012 (edited) I have LinkPosition++; there at the end after it adds a node. My teacher gave us the main part of the program which will move the position around so that if "insert_after" is called and position is set somewhere in the middle of my linklist then a new node will be added after that position. So i made the loop run until it gets to the specified position. that specific function I was calling is just going to add numbers 1 after another so position will just move up by 1. I know the indents and tabs are sloppy lol I keep pasting and cutting code trying to fix stuff so it gets all messed up and I have not learned anything about a double linked list. Edited June 30, 2012 by Corey Quote Link to comment Share on other sites More sharing options...
Clan Friend SunLight Posted July 1, 2012 Clan Friend Share Posted July 1, 2012 lol I was really blind sorry, I was still looking at the original code and not your edit by the way, which compiler are you using? do you use a debugger? Quote Link to comment Share on other sites More sharing options...
Corey Posted July 1, 2012 Author Share Posted July 1, 2012 (edited) Umm I use codeblocks which I think uses the GNU GCC compiler (least thats the one I pick ) I have a Microsoft Visual 2008 and its compiler as well, but my teacher wanted us to get used to a different program. And no I do not use the Debug part. I might have been told what it does, but I forgot by now. All I do is build it and run it. I turned in that homework last night on time which makes me happy just hope I get a good grade Now I have more assignments to do *facepalm* Now on to recursion... wish me luck! Edited July 1, 2012 by Corey Quote Link to comment Share on other sites More sharing options...
rolf Posted July 1, 2012 Share Posted July 1, 2012 Recursion is fun Just remember two things. 1. If you come at an 'end position', do the right things. For example, you can search in a linked list with recursion, and an end position is both 'found the number' as well as 'end of list'. 2. Make sure you always get at an 'end position', do not go on for infinity. Quote Link to comment Share on other sites More sharing options...
parrot Posted July 1, 2012 Share Posted July 1, 2012 very cool tip on that "indent" command. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.