c++ - While loop with pointers does not work -
i have problem, trying create list deletes highest value holding number, or numbers same value if value highest in list. thank kind of tips.
// n,n1,head,next - pointers int j = 0; //this number helps put pointer forward 1 place while(n!=0){//should go through every digit of list if(head == 0){ cout << "list empty" << endl; } else{ n = head; n1=0; // n1 , n pointers while(n!=0){ if(n->sk == maxx){//searches maximum digit in list break; } else{ n1=n; n=n->next; } } if(head == n){ head = head->next; } else{ n1->next = n->next; } delete n; // deletes pointer holding highest value } n = head; //problem here or somewhere below j++; for(int i=0; i<j;i++){ // loop should make pointer point first n = n->next; // number, second , on until end of list } // , numbers inside list value } // equals "maxx" should deleted
ok, problem (the of it) code:
while(n!=0){ if(n->sk == maxx){ break; } else{ n1=n; n=n->next; } }
if find maxx
value should delete node , continue searching, don't break
. way don't need code task.
while (n != 0){ if (n->sk == maxx){ node *prev = n->prev; // previous node. node *tmp = n; // assume have class node. // temporaly holds pointer n. prev->next = n->next; // connect previous node following one. n = n->next; // advance n next node in list. delete tmp; // delete node. } }
Comments
Post a Comment