Thread: Linkedlist deletion

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    4

    Question Linkedlist deletion

    hey everyone so i was given a task with linked list to delete a specific number in the DOUBLY LINKED LIST but my code is giving an access violation error even after multiple dry runs i cant figure out what is wrong the task basically was to create a search function which finds a specific number in linked list and a deletion function which deletes that specific link

    insert
    Code:
        node* search(int val){
            node* cur=head;
            while(cur!=NULL){
                if(cur->data==val){
                    cout<<"value found "<<val<<endl;
                    return cur;
                }
                cur=cur->next;
            }
            cout<<"value not exist"<<endl;
                        return NULL;
        }
        bool delspval(int val){
            node*temp=0;
            if(search(val)==NULL){
                return 0;
            }
            else{
                temp=search(val);
                temp->next->prev=temp->next;
                delete temp;
                temp=0;
                cout<<"specific value "<<val<<" deleted"<<endl;
                return 1;
                    }
                }
    in the above given code the line "temp->next->prev=temp->next;" is giving the error im pretty much a beginner at linked lists so any help would be appreciated thanks
    Last edited by Hadi Durrani; 09-16-2022 at 12:07 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First, learn how to indent code.
    Your indentation is all over the place, making your code impossible to follow.
    Code:
    node *search(int val)
    {
      node *cur = head;
      while (cur != NULL) {
        if (cur->data == val) {
          cout << "value found " << val << endl;
          return cur;
        }
        cur = cur->next;
      }
      cout << "value not exist" << endl;
      return NULL;
    }
    
    bool delspval(int val)
    {
      node *temp = 0;
      if (search(val) == NULL) {
        return 0;
      } else {
        temp = search(val);
        temp->next->prev = temp->next;
        delete temp;
        temp = 0;
        cout << "specific value " << val << " deleted" << endl;
        return 1;
      }
    }
    Second, Doubly linked list - Wikipedia
    The first and last nodes of a double linked list are special, you need to take this into account.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Nitpick

    Code:
    bool delspval(int val)
    {
      node *temp = 0;
      if (search(val) == NULL) {
        return 0;
      } else {
        temp = search(val);
        temp->next->prev = temp->next;
        delete temp;
        temp = 0;
        cout << "specific value " << val << " deleted" << endl;
        return 1;
      }
    }

    You want to search through the list only once....normally.
    Last edited by ghoul; 09-18-2022 at 04:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LinkedList
    By DenDen Bata in forum C Programming
    Replies: 4
    Last Post: 05-03-2012, 01:30 PM
  2. LinkedList implementation
    By rire1979 in forum C Programming
    Replies: 1
    Last Post: 12-19-2009, 02:39 PM
  3. Help LinkedList!!
    By dagbai in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 12:56 PM
  4. LinkedList
    By bejiz in forum C++ Programming
    Replies: 1
    Last Post: 06-12-2006, 04:26 AM
  5. A linkedlist that has no end
    By Jslam9 in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2002, 05:43 PM

Tags for this Thread