Linked Lists - Deleting Nodes - Beginner

So I've been searching forums, but im still very new to the language and linked lists so I can barely decipher the results.

basically I made a delete function for my linked list.
I can currently Create a list, traverse the list, sort the list, search the list, and insert before any node in the linked list. I recycled some code from the insert to locate the point in the list where I could delete. My main point of confusion is how to link the previous points to the node that is after the one I am deleting.

my code looks like this
I really am looking to learn but hours of drawing this out is not really helping me..

This is the structure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<stdlib.h>
using namespace std;

struct nodetype         // defines user data type
{                       // which is the node for the linked list
    int adata;
    nodetype *ptr;  
};
void acreate(nodetype *&start); // passing reference parameter (*& for reference parameter)
void atraverse(nodetype *start); // passing a value parameter
void asort(nodetype *start);
bool asearch(nodetype *start);
void ainsert(nodetype *start);
void adelete(nodetype *start);
void amenu();


this is the code in question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 nodetype *entry, *last, *next;
    int akey;
    entry=start->ptr;
    last=start;
    next=last->ptr;
    cout<<"Input Data You Would Like To Delete"<<endl;
    cin>>akey;

    while((akey!=entry->adata)&&(entry->ptr !=NULL))
    {

    if(entry->adata==akey)
    {
    entry=last->ptr;
    last->ptr=next->ptr;
    delete entry;
 
    }
    }
    }


I would appreciate any help I can get! Thank You!
Last edited on
1
2
3
4
5
6
void delete_node(nodetype* target)
{
    target -> next -> prev = target -> prev;
    target -> prev -> next = target -> next;
    delete target;
}

Find node you want to delete and use this function.

Wait. You doesn't have a double linked list, right? One moment, I'll fix it.


EDIT:
1
2
3
4
5
6
void delete_after(nodetype* target)
{
    to_delete = target -> ptr;
    target -> ptr = target -> ptr -> ptr ;
    delete to_delete;
}

Use it as a private service function inside of proper public method which will search for needed nodes, make sure that all pointers points where they should etc.
Last edited on
hmm does that really work it looks so simple? let me try it out.
not working for me :(
what the problem is?
Topic archived. No new replies allowed.