c++ - Need help making a linked list program -



c++ - Need help making a linked list program -

so have assigment consist of making programme allows me come in integer 10 on linked list after finding value 10 on list 3 times. 10 have insert needs go after 3rd 10. code made programme following:

linkedlist<int>*prt=new linkedlist<int>; bool found = false; int count; while(ptr==null & !found) { if (ptr -> info == 10) count ++; if(count == 3) found = true; if(!found) ptr = ptr -> next; node newnode = new node; if(count == 3) newnode -> info = 10; newnode -> next = ptr -> next; ptr -> next = newnode; }

i think code made correct, however, kind of lost on how turn piece of code running program. help appreciated.

you have multiple errors in program. one, variable prt should ptr. also, while status should using && boolean operator instead of & bitwise operator. description, appears lastly 2 lines of loop:

newnode -> next = ptr -> next; ptr -> next = newnode;

should invoked if count == 3. however, in it's current form beingness executed on every loop iteration. likewise, should create newnode when plan insertion.

you're checking status count == 3 multiple times including indirectly through found variable. each of these conditions collapsed single if so:

if (count == 3) { found = true; node newnode = new node; newnode->info = 10; newnode->next = ptr->next; ptr->next = newnode; } else { ptr = ptr->next; }

you should think loop write , loop invariants want maintain through loop thing going alter on every loop iteration can have deterministic end loop.

also, whenever dealing new type of info construction have never worked before, recommend drawing out little diagram boxes , arrows. help figure out process need follow want.

i start initial diagram , final diagram showing image want. draw marker corresponds pointer iterates across list. move marker 1 time through loop on each step. seek implement steps took marker in code form.

here's illustration starting diagrams drew using google docs:

assume dotted arrows represent null pointers. gives 3 potential starting cases linked list.

c++ linked-list

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -