0

This is what I have so far, but it's not working. Basically skips to else if(cnode == preposition).

void LinkedList::Delete(Node *PrePosition) {

    Node *cnode = head;
    Node *pnode = NULL;
    while (cnode != NULL) {
        if (cnode->value != NULL) {
            if (pnode == NULL) {
                // if there is not previous node
                head = cnode->next;
            }
            else if (cnode == PrePosition) {
                // if there is previous node
                cout << endl << "Deleting: " << cnode << endl;
                pnode->next = cnode->next;
            }
        }
        else {
            // don't delete
            pnode = cnode;
        }
        cnode = cnode->next;
    }
}
Wolf
  • 9,679
  • 7
  • 62
  • 108
code_theworks
  • 9
  • 1
  • 1
  • 3
  • If `PrePosition` is *really* the node *before* the node to delete, then this looks more like a test for common sense. But this will probably remain the secret of the questioner for all time. – Wolf Jan 27 '21 at 07:59

4 Answers4

3

1: Take the pointer from the previous node and point it to the next one after the one you want to delete

2: Delete the pointer from the previous node to the current node

3: Delete the pointer from the next node to the current node (if it is a doubly-linked list)

2

Three cases of delete in a singly linked-list:

  1. delete the first node

    void delete_first()
    {
        node *temp=new node;
        temp=head;
        head=head->next;
        delete temp;
    }
    
  2. delete the last node

    void delete_last()
    {
        node *current = new node;
        node *previous = new node;
        current=head;
        while(current->next != NULL)
        {
          previous = current;
          current = current->next;  
        }
        tail = previous; // if you have a Node* tail member in your LinkedList
        previous->next = NULL;
        delete current;
    }
    
  3. delete at a particular position (your case)

    void LinkedList::delete_position(int pos)
    {
        node *current=new node;
        node *previous=new node;
        current=head;
        for(int i=1; i < pos; i++) //or i = 0; i < pos-1
        {
           previous=current;
           current=current->next;
        }
        previous->next=current->next;
        delete current;
    }
    

    ^^ from codementor ^^

However if your function signature intends delete_node(Node* nodeToDelete) [PrePosition is not a good name in this case] and you want delete the node passed to the function without knowing its position in the list we can modify delete_position() like so:

void LinkedList::delete_node(Node* nodeToDelete)
{
    node *current= head;
    node *previous= nullptr;

    if (head == nodeToDelete){
        head = nodeToDelete->next;
        delete nodeToDelete;
        return
    }//else
    while(current != nodeToDelete)
    {
        previous = current;
        current = current->next
    }
    previous->next = current->next;
    delete nodeToDelete;
}

Also in your original code, if it's skipping the line you mentioned, pnode is always null when cnode has a non-null value in it.

Elfen Dew
  • 114
  • 1
  • 9
  • If you already have a pointer to the previous position, why iterate the list to find it? – user4581301 Nov 26 '17 at 00:41
  • @user4581301 Because its a singly-linked list so you can't just delete the pointer. You have to iterate to find the previous node. I think though that I assumed incorrectly in OPs case, the pointer was more than likely what its name suggested: a pointer to the node prior to the one to delete. – Elfen Dew Oct 13 '18 at 23:27
  • 1
    There's a really neat trick with a pointer to a pointer you can use to get around that.Instead of passing in a pointer to the node you want deleted, pass in a pointer to the next pointer you want updated. Function looks like `void LinkedList::delete_node(Node** nodeToDelete) { if (*nodeToDelete) { node * temp = *nodeToDelete; *nodeToDelete = (*nodeToDelete)->next; delete temp; } }` [Variant of the alternative provided here.](https://stackoverflow.com/questions/22121257/how-do-i-properly-delete-nodes-of-linked-list-in-c)(https://stackoverflow.com/a/22122095/4581301) – user4581301 Oct 14 '18 at 03:39
  • That codementor code has some bugs. In the first snippet, `temp` is never used. In the others, it's unclear who is responsible for freeing `previous`. They shouldn't have to make any heap allocations at all. – ZachB May 04 '23 at 21:57
0

Here are the full code

    class SportShoe  {
    private:
        struct nodeSport {
            int ShoeID;
            char BrandShoe[SIZE]; 
            char TypeShoe[SIZE];
            char ColourShoe[SIZE];
            int SizeShoe;
            float PriceShoe; 
            nodeSport *last;
            };
            nodeSport *first = NULL; 

    public:
        int MenuSportShoe();
        void AddSportShoe();
        void DisplaySportShoe();
        void DeleteSportShoe();
        static void ExitSportShoe();
    };

   int SportShoe::MenuSportShoe() {
     int OptionSportShoe = 0;

    cout << endl;
    cout << "Please select from the menu:" << endl;
    cout << ":: 1 :: Add item to shoe list" << endl;
    cout << ":: 2 :: Display shoes list" << endl;
    cout << ":: 3 :: Delete item from the list" << endl;
    cout << ":: 4 :: Back" << endl;
    cout << "=>> ";
    cin >> OptionSportShoe;

    while (OptionSportShoe == 1){
        AddSportShoe();
    }

    while (OptionSportShoe == 2){
        DisplaySportShoe();
      }

    while (OptionSportShoe == 3){
        DeleteSportShoe();
    }

    while (OptionSportShoe == 4){
        ExitSportShoe();
    }

    return 0;
  }

void SportShoe::AddSportShoe() {    
    nodeSport *tempShoe1, *tempShoe2; 

    tempShoe1 = new nodeSport;
    cout << "Please enter the Shoe ID : (eg. 43210) " << endl;
    cout << "=>> ";
    cin >> tempShoe1->ShoeID;

    cout << "Please enter the Shoe Brand: (eg. Adidas) " << endl;
    cout << "=>> ";
    cin.sync();
    cin.getline(tempShoe1->BrandShoe,SIZE);

    cout << "Please enter the Shoe Type : (eg. Running) " << endl;
    cout << "=>> ";
    cin.sync();
    cin.getline(tempShoe1->TypeShoe,SIZE);

    cout << "What is the Shoe Colour : (eg. Grey) " << endl;
    cout << "=>> ";
    cin.sync();
    cin.getline(tempShoe1->ColourShoe,SIZE);

    cout << "Please enter Shoe Size : (eg. 9) " << endl;
    cout << "=>> ";
    cin >> tempShoe1->SizeShoe; 

    cout << "Please enter the price of the Shoe : (eg. RM123.45) " << endl;
    cout << "=>> RM ";
    cin >> tempShoe1->PriceShoe;


    tempShoe1->last = NULL;  


    if (first == NULL)  
        first = tempShoe1;
    else  
    {
        tempShoe2 = first; 
        while (tempShoe2->last != NULL) 
            tempShoe2 = tempShoe2->last;

        tempShoe2->last = tempShoe1;
    }

    system("PAUSE");
    MenuSportShoe();
  }

void SportShoe::DisplaySportShoe() {
    nodeSport *tempShoe1;
    tempShoe1 = first;

    while(tempShoe1){
        cout << "ID : " << tempShoe1->ShoeID << endl;
        cout << "Brand : " << tempShoe1->BrandShoe << endl;
        cout << "Type : " << tempShoe1->TypeShoe << endl;
        cout << "Colour : " << tempShoe1->ColourShoe << endl;
        cout << "Size : " << tempShoe1->SizeShoe << endl;
        cout << "Price : " << tempShoe1->PriceShoe << endl;
        cout << endl;
        tempShoe1 = tempShoe1->last;
    }

    system("PAUSE");
    MenuSportShoe();
  }

 void SportShoe::DeleteSportShoe(){
    nodeSport *tempShoe1, *tempShoe2; 
    int DataShoe;
    tempShoe2 = tempShoe1 = first;

    if(tempShoe1 == NULL)
    {
        cout << "\nList is empty!" << endl;
        system("PAUSE");
        MenuSportShoe();
    }

    while(tempShoe1 != NULL)
    {
        cout << "\nEnter the Shoes ID to be deleted: (eg. 123) ";
        cin >> DataShoe;

        tempShoe2 = tempShoe1;
        tempShoe1 = tempShoe1->last;

        if(DataShoe == tempShoe1-> ShoeID){ 
            if(tempShoe1 == first)  {
                first = first->last;
                cout << "\nData deleted ";
            }

            else{
                tempShoe2->last = tempShoe1->last;
                if(tempShoe1->last == NULL){
                    tempShoe2 = tempShoe2;
                }
                cout << "\nData deleted ";
            }

            delete(tempShoe1);

            system("PAUSE");
            MenuSportShoe();
        }

        else{
            cout << "\nRecord not Found!!!" << endl;
            system("PAUSE");
            MenuSportShoe();
        }
    }
  }

  void SportShoe::ExitSportShoe(){
    int sepatu;

    cout << endl;
    cout << "Please choose the option below."<<endl;
    cout << ":: 1 :: Sport Shoe." << endl;
    cout << ":: 2 :: Ladies High Heel." << endl;
    cout << ":: 3 :: Exit" << endl;
    cout << "=>> ";
    cin >> sepatu;

    while(sepatu == 1){
        SportShoe listShoe;
        listShoe.MenuSportShoe();
    }

    while(sepatu == 2){
        HighHeel listShoe;
        listShoe.MenuHighHeel();
    }

    while(sepatu == 3){
        cout << "Thank you. Till we meet again."<< endl;
        exit(1);
    }

  }

  main() {

    cout << "Hello! Welcome to MySepatu Online Shop administrator."<< endl;
    cout << endl;

    SportShoe::ExitSportShoe();
    HighHeel::ExitHighHeel();

    return 0;
  }
0
public class linkedList {
    int count = 0;
    
    class Node {
        int element;
        Node next;
        Node(int element) {
            this.element = element;
        }
    } 
    
    Node head = null;
    Node tail = null;
    
    public void addNode(int Object) {
        Node newNode = new Node(Object);
        if (head == null) {
            head = tail = newNode;
        } else {
            tail.next = newNode;
            tail = newNode;
        }
    }
    
    public void Display() {
        Node current = head;
        while (current!=null) {
            System.out.println(current.element);
            count ++;
            current = current.next;
        }
    }
    
    public void Length() {
        System.out.println(count);   
    }
    
    public void Remove(int node) {
        Node curr = head;
        while (curr!=null) { // looping the nodes
            if (curr.element == node ) {
                curr.element = curr.next.element;
                curr = curr.next;
                // To fix the Duplicates
                while (curr!= tail) {
                    curr.element = curr.next.element;
                    curr = curr.next;
                }
                RemoveEnd();
                break;
            }
            curr = curr.next;
        }
    }
    
    public void RemoveEnd() {
        Node current3 = head;
        while (current3.next != tail) {
            current3 = current3.next;
        }
        tail = current3;
        tail.next = null;
    }
}
Wolf
  • 9,679
  • 7
  • 62
  • 108
Douf Jawad
  • 11
  • 1
  • 1
    Hi Douf. Welcome to stack overflow. As you might have seen, it is about solving coding problems. A question (and its answers) are usually flagged when they are more about doing "someone's homework" (it already happened to me). I'm actually puzzled that this question was not flagged, but I guess there's some value on it. – JACH Jan 26 '21 at 23:49
  • Please read the "most voted" answer, because your answer to the specific question (removing a node) is not correct, or at least not efficient, since you're in fact moving all the data in the list when you only needed to remove one item from it. Please fix, or your answer will be downvoted. – JACH Jan 26 '21 at 23:54
  • 1
    Even if the idea behind your answer *may* be applicable, it's in Java while OP is asking for a solution in C++. – Wolf Jan 27 '21 at 07:37