Linked lists questions

This is a discussion on Linked lists questions within the C++ Programming forums, part of the General Programming Boards category; tmp = tmp->next means "move tmp to the next node", which is exactly what you need to do to traverse ...

tmp = tmp->next means "move tmp to the next node", which is exactly what you need to do to traverse the list. You are not, in any way shape or form, modifying the content of the list. tmp must of course be set to something useful at the start of the function, and you need to check in every iteration that tmp is not NULL.

I have no idea what you are actually doing in your code, but I would suggest that you start by separating your input loop from your insertion, and moving the "sort" code to a different function. But before you even start sorting, make sure you can just insert things.

Write a separate function that prints/traverses the list, so that you can check what the list looks like at any given time [before and after insert for example].

Oh, and your sort code is incomplete - it walks through the list once, but it needs to walk as many times as necessary to get the smallest item at one end and the largest at the other.

getting the sorting function to repeat until sorted

I took some time to try to understand linked list. I split up the functions to do separate things. But now whatever i do, i cannot get m sorting function to sort correctly. can anyone see how to repeat it until its sorted?

i thought i was taking a headptr in. HeadPtr is the first node that is empty. and it inserts a number when it is in the insertion function?
since i dont know how to do it recursively, i at least want to learn how to do it normally.

my assignment says

2. Write recursive functions that perform insertion and retrieval operations on a pointer-based sorted linked list of integers. The insertion function should be void, taking a head pointer and the item to be inserted as parameters. The retrieval function should take a head pointer and the item to be retrieved as parameters, and should return the item's position, or -1 if the item is not found in the list. Use these procedures to write a program that inputs a series of integers, inserting them into a list until 0 is entered. Then a second series of integers is input, until 0 is entered, and the position of each integer in the list is displayed. Example:
Enter numbers to be inserted (0 to end): 34 23 1 45 7 0
The list is: 1 7 23 34 45
Enter numbers to be retrieved (0 to end): 23 8 45 0
23 is at position 3
8 is not in the list
45 is at position 5
Note that the program should be able to handle requests to retrieve items that are not in the list.

Use these procedures to write a program that inputs a series of integers, inserting them into a list until 0 is entered. Then a second series of integers is input, until 0 is entered, and the position of each integer in the list is displayed. Example:
Enter numbers to be inserted (0 to end): 34 23 1 45 7 0

Use these procedures to write a program that inputs a series of integers, inserting them into a list until 0 is entered. Then a second series of integers is input, until 0 is entered, and the position of each integer in the list is displayed. Example:
Enter numbers to be inserted (0 to end): 34 23 1 45 7 0

is what i am doing incorrect?

Yes, but car factories do not provide petrol for your car either, do they? You should have a loop, yes.

In fact, your function does not insert the number passed into the function - it uses it to read in a new value.

so i would insert the first number in the head outside of the insertion function. Then in the insertion function it would take that head in, then add more numbers to the list until 0 is entered. is this the order?

so i would insert the first number in the head outside of the insertion function. Then in the insertion function it would take that head in, then add more numbers to the list until 0 is entered. is this the order?

No, you would have a loop in your MAIN, that calls your insert function once for each number entered, and the loop ends when 0 is entered.

As to sorting, you need an outer loop and an inner loop. In the outer loop, you just set up the pointer to the start of the list. In the inner loop, you swap items that are out of order and move to the next item. The inner loop ends when you have gone through the list completely. The outer loop ends when you haven't swapped anything [which means that all items are in order].

I know this is getting repetitive. Pretty ridiculous, i need a Live breathing teacher, If anyone is out there PLEASE pm me. I am willing to pay you for your time. Me paying you will save me money actually. I am not sure on the rules of the website about asking for help, im very desperateactually. have 2 assignments and a final in about 3 weeks. and still dont see everything the book is trying to teach, and the online teacher doesnt clarify.

anyway,

My program is falling apart. I'm talking very bad. i scrapped the whole thing because i had to change the insertion.
The goal of what i am looking to do is one thing, then i can move on to something else .
Insert a number in if the HeadPtr is empty or NULL.