a) looks like you inserting after given position
b) you should check if new node will be at the head or tail of your list and change head or tail pointers accordingly.
so you mean before i insert i have to check whether it's empty or not?
the tail is just to trace the previous node a.
if like what you want me to do at b)
That does have a head and tail pointer. Since the requirement is singly-linked list, each node should only have a single link. Really, there's no need to even have a tail pointer in this example unless you are also keeping track of size, which you are not.
EDIT:
I guess a tail pointer gives you the option to add at tail in constant time.
because in (ii) Remove the item stored at position p (given by an iterator)
So for the remove isn't i need the tail already ?
to delete previous record
and so.
need i implement correctly for my 2 struct as you said?
am i correct?
Your two structs are correct right now. You don't need a tail pointer ever. It's just for convenience for working with the end like I said. As for the nodes, all you need is a pointer to the next node, which is what your requirements are asking.
i always face problem with my linked list . always not confidence with it,
this is i think with my own without reference to other , i prefer keep do my own only can get understanding..
isn't correct?
The function you just posted appends the new node to the end of the list. Consider changing the name of it to push_back.
In order to add a node before another, you need to make sure that you find that node without loosing the previous node. Something like this:
1 2 3 4
temp = head;
while (temp->next != iterator) // make sure temp->next exists
temp = temp->next;
// temp is the node before the node you want to insert before :)
An important thing with containers like this is that you don't loose nodes when you change their pointers. Note the difference
1 2 3 4 5 6 7 8 9
// The correct way
node *toInsert = new node;
toInsert->next = temp->next;
temp->next = toInsert;
// Incorrect, we loose the pointer
node *toInsert = new node;
temp->next = toInsert;
toInsert->next = // ???
I typically keep paint open whenever I deal with nodes. It helps me to draw the code that is happening.
my question stated like this Suppose that a singly linked-list is implemented with both a header and a tail node. Devise linear-time algorithms to:
(i) Insert item x before position p (given by an iterator)
for my thinking.
iterator = pointer already. so if i using the node * it should be iterator already ?
and your code
1 2 3 4
temp = head;
while (temp->next != iterator) // make sure temp->next exists
temp = temp->next;
// temp is the node before the node you want to insert before :)
Have you tested it? This is where printing out your list comes in handy. Write down on paper the expected output, run the program and output the list after each addition. Then you'll see if it's working. If not, come back and we'll help you.
tested and working fine.
i know how the concept run.
i always use paper to draw it, but not really sure it's fullfill my question or not.
as the question given by the UOW australia are really sucks enough.
said by my lecturer . haha.
so can i ask any senior here?
i really need make sure about it that isn't my question that i implement for the addNode
full fill Insert item x before position p (given by an iterator)? need to make sure..
before position p is mean what..
If you have a list, [0] -> [1] -> [2] -> [3] -> [4]
and you want to insert element A at position 2, you have to make a choice. Does that mean you place it in front of 2, or behind 2. Since it says before, you place it behind, so the new list would be:
here is my full program . but then when i insert data at position middle it's posibble to add.
but if i wan insert the data to the head. it seem like fail. but now my answer is fullfill question requirement already ya?