-4

So I have an assigment that consist of making a program that allows me to enter the integer 10 on a linked list after finding the value 10 on the list three times. The 10 that I have to insert needs to go after the third 10. The code that I made for this program is the 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 the code I made is correct, however, I am kind of lost on how to turn this piece of code into a running program. Any help would be greatly appreciated.

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
  • This might help. http://www.tutorialspoint.com/cplusplus/index.htm – R Sahu Nov 10 '14 at 02:08
  • why do you think your code is correct? what is your issue? – Kick Buttowski Nov 10 '14 at 02:09
  • You need to delete your list or you have a memory leak. Even better would be simply `Linkedlist mylist;` Now you don't need to remember to delete it. – Neil Kirk Nov 10 '14 at 02:11
  • Is `prt` and `ptr` the same variable and a typo? `prt` will not be NULL in your while condition. – Neil Kirk Nov 10 '14 at 02:13
  • @KickButtowski, I said I think is correct, cause I think it would do what I need it to do. However my issue is that I need to be able to run this code on a program and get it to display the list. But I do not know how to do that part – Adda Fuentes Nov 10 '14 at 02:33
  • @NeilKirk thank for the advice, I did not notice that I forgot to delete the list. Thank you for the suggestion. – Adda Fuentes Nov 10 '14 at 02:34
  • Try `int main() { CODE HERE }` – Neil Kirk Nov 10 '14 at 02:35
  • Possible duplicate of [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Gabriel M Oct 06 '18 at 11:48

1 Answers1

1

You have multiple errors in your program. For one, the variable prt should probably be ptr. Also, your while condition should be using the && boolean operator instead of the & bitwise operator. From your description, it appears that the last two lines of your loop:

NewNode -> next = ptr -> next;
ptr -> next = NewNode;

Should only be invoked if count == 3. However, in it's current form they are being executed on every loop iteration. Likewise, you should only create a NewNode when you actually plan to do the insertion.

You're also checking the condition count == 3 multiple times including indirectly through the found variable. Each of these conditions could be collapsed into a single if like 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 strongly think about any loop that you write and what loop invariants you want to maintain through the loop as well as what thing is going to change on every loop iteration so that you can have a deterministic end to your loop.

Also, whenever you are dealing with a new type of data structure that you have never worked with before, I recommend drawing out a little diagram with boxes and arrows. This will help you figure out the process which you need to follow to get what you want.

I would start with an initial diagram and then a final diagram showing the picture you want. Then draw a marker that corresponds to the pointer that iterates across your list. Move the marker one time through the loop on each step. Then try to implement the steps you took with the marker in code form.

Here's some example starting diagrams that I drew using Google Docs:

enter image description here

Assume the dotted arrows represent NULL pointers. This gives you three potential starting cases for your linked list.

b4hand
  • 9,550
  • 4
  • 44
  • 49