Unsorted List Class - Delete Question

This is a discussion on Unsorted List Class - Delete Question within the C++ Programming forums, part of the General Programming Boards category; I am designing a Unsorted List Class, right now its static (no pointers) - later it will be converted into ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    32

    Unsorted List Class - Delete Question

    I am designing a Unsorted List Class, right now its static (no pointers) - later it will be converted into dynamic.

    One of the member functions is: DeleteItem and it gets passed a location. This function deletes an item in the array, and then has to move all the existing elements down in the array.

    I was just wondering if my code below makes scense, any input to improve would be greatly appreciated.

    Also 'end' keeps track where the current end of array is.

    Code:
    void GList::DeleteItem(int loc)
    {
    	if(loc - 1 > end)
    		return;
    
    	if(loc - 1 == end)
    	{
    		end--;
    		list[loc - 1] = -1
    	}
    
    	if(loc - 1 < end)
    	{
    		int temp;
    		temp = end - (loc - 1);
    
    			for(int i = 0; i < temp; i++)
    			{
    				list[loc - 1] = list[loc];
    				loc--;
    			}
    		
    		end--;
    	}
    }
    Thanks!

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Take another look at loc-- in the for loop.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    What would be a better way to run through replacing one with the other?

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I think you're going in the wrong direction. You're starting at the item to be deleted, & then moving towards the head of the list instead of moving towards the end.

    i.e. if you say DeleteItem(4) when there are 8 items in the array, you will copy array[4] into array[3], array[3] into array[2], array[2] into array[1], and array[1] into array[0], so you end up with the item that was in array[4] copied into the first 4 elements.
    Last edited by R.Stiltskin; 10-26-2005 at 10:57 PM.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    Ohhh, so loc++, that does make scense. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete an element from a linked list
    By hinman in forum C Programming
    Replies: 6
    Last Post: 10-17-2007, 08:30 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21