Thread: deleting a node by index

  1. #1
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    Unhappy 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...

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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
    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
    signature under construction

  4. #4
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49
    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...

  5. #5
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM