0

I have a problem, I am trying to create a list that deletes a highest value holding number, or all numbers with the same value if the value is highest in the list. Thank you for any kind of tips.

// n,n1,head,next - are pointers
int j = 0; //this number helps to put pointer forward by one place
while(n!=0){//should go through every digit of the list
    if(head == 0){
        cout << "list is empty" << endl;
    }
    else{
        n = head;
        n1=0; // n1 and n are pointers
        while(n!=0){
            if(n->sk == maxx){//searches for maximum digit in the list
                break;
            }
            else{
                n1=n;
                n=n->next;
            }
        }
        if(head == n){
            head = head->next;
        }
        else{
            n1->next = n->next;
        }
        delete n; // deletes the pointer holding the highest value
    }
    n = head; //problem is here or somewhere below
    j++;
    for(int i=0; i<j;i++){ // this loop should make the pointer point to the first
        n = n->next;       // number, then the second and so on until the end of list
    }                      // and all the numbers inside the list with the value that
}                      // equals "maxx" should be deleted

3 Answers 3

2

You should dereference the pointers. Right now, you're pointing to their addresses. See if that helps resolve your problem.

Sign up to request clarification or add additional context in comments.

Comments

0

Ok, the problem (the most of it) is the code:

   while(n!=0){
        if(n->sk == maxx){
            break;
        }
        else{
            n1=n;
            n=n->next;
        }
    }

If you find the maxx value you should delete that node and continue to searching, don't break. This way you don't need so much code for this task.

while (n != 0){
    if (n->sk == maxx){
        node *prev = n->prev; // The previous node.
        node *tmp = n;        // this assume you have a class node.
                              // temporaly holds the pointer to n.
        prev->next = n->next; // Connect the previous node with the following one.
        n = n->next;          // advance n to the next node in the list.
        delete tmp;           // delete the node.
    }
}

4 Comments

Would your answer still be the same if the list is two-sided?
By two-sided you mean circular?
i mean double linked list if my english is right where you can move forward and backward with pointers
Yes my answer still working for double linked lists. You can still move over the entire list from the head to the end. the main issue with my answer is that don't take in to account that the list has to remain conected, I will fix that right now!
0

If I understand correctly what you want, you can just iterate over your list and save the pointer for deletion:

it = head;
pos = nullptr;
while (it != nullptr) {
    if(it -> sk == maxx) {
        pos = it; // save ptr
        it = it -> next;
        delete pos; // delete saved ptr
        pos = nullptr;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.