-
deleting a node by index
hi,
i am working(still)..on a linked list where nodes are assigned
and given an index number.
I am assigning the indexes incrementally according to the size
of the linked list.
Upon deletion (node chosen by index), I delete the node
and decrement the size, but when i return to print the list
I am getting junk when I encounter the spot where the
deleted node index was to be listed (and I crash).
Am I
a) assigning the index number incorrectly?
or
b) not re-assigning all indexes once a node is deleted.
-for example if I delete node 2 out of 3, should i re-assign
node 3 to be node 2?
here is where they are assigned
Code:
void ListofTeles::setdata()
{
char Name [MAX];
char Number[MAX];
long index = this->size+1;
TeleEntry *Tele;
cout << "Enter Name (Last,First) :"<< endl;
cin >> Name;
cout << "Enter Number :"<< endl;
cin >> Number;
Tele = new TeleEntry;
strcpy (Tele->EntryNumber, Number);
strcpy (Tele->EntryName, Name);
Tele->index = index;
this->Add(Tele);
}
here is my delete function
Code:
void ListofTeles::Delete(int position)
{
TeleEntry *current, *dead=new TeleEntry;
if (Head==NULL){cout << "List is Empty - Invalid Choice"<<endl;
return;}
else{
current = Head;
while(current!=NULL){
cout<< "current index is " << current->index<< endl;
if (current->index == position){
cout << current->EntryName<< " will be deleted"<< endl;
if(current->index!=1){
current->Next=current->Next->Next;
dead=current;
delete dead; cout << "delete dead" << endl;
dead=NULL;
size--; cout << "size now" << size<<endl;
return;}
else
{delete current; cout << "delete current is HEAD" << endl;
size--; cout << "size now" << size<<endl;
return;}
}
else
current = current->Next;
}
}
}
thanks to everyone who helps here...
-
How does that even compile? You have two else in the if-statement.
I don't see you updating the pointers in the list when deleting. They still point at the memory location you deallocated.
-
sry, i am in a hurry
but here some tips:
.) whenever a node is appended at the END of the linked list, its index is size; (not size + 1) (when you start counting by 0) (when you insert an element into a list with no (zero) items, the first element you insert has index 0.
.) whever you insert an element at some index i, the inserted elements index is (of course) i. all indices of elements past i must be increased by one.
thus:
.) whenever you delete an element at index i all indices of elements past i must be decreased by one
.) in case you delete the head, you must decrease the index of all elements and make head.next the new head.
.) make sure you dont use the next pointer of deleted objects
(thus first unchain, then delete)
.) also make sure you dont forget updating the pointers when deleting something
i just wonder why do anyone would do his own linked list implementation (unless this is an assignment)?
i wasted so much time on creating my own basic data structures - and eventually the stl was always faster (amazingly malloc is very slow compared to the stl allocator)
i suggest using stl (standard template library) to anyone :D
and dont be scared of iterators they are basically just wrapper classes of pointers - that means you use them the way youd use a pointer
-
the 2nd else belongs to the first if statement, it actually does complile-
but yeah, there are problems...
i will look at updating the pointers after i delete.
no one ever said it was going to be easy, I know...
-
yes, this is an assignment.
I'm an 'older' night student who (obviously)
is new to C++. and myr esources are very limited.
I very much appreciate your tips.
thanks
M