Jump to content

Link List c++


Corey

Recommended Posts

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 by Corey
Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

  • Clan Friend

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Corey
Link to comment
Share on other sites

  • Clan Friend

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 :P

 

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?

Link to comment
Share on other sites

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 by Corey
Link to comment
Share on other sites

  • Clan Friend

lol I was really blind sorry, I was still looking at the original code and not your edit :P

 

by the way, which compiler are you using? do you use a debugger?

Link to comment
Share on other sites

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 :P Now I have more assignments to do *facepalm*

 

Now on to recursion... wish me luck! ;)

Edited by Corey
Link to comment
Share on other sites

Recursion is fun :P 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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.