how to remove even elements from list

This is a discussion on how to remove even elements from list within the C Programming forums, part of the General Programming Boards category; How to remove even elements from list without using a counter?...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    20

    how to remove even elements from list

    How to remove even elements from list without using a counter?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    As was mentioned in a similar thread recently, a boolean value is one way. Just a flip-flop switch.

    Code:
    pseudo-code:
    i = even = 0
    while (array[i] has another element)  {
       if (even = 0)  {
            zero out that element of the array
            even = 1
       else
            even = 0
       } 
       increment i   
    }
    If you're asking how to do this without an i variable to scan through the subscript values, then I have no idea. You could use memory addresses (pointers), but that's just another form of a counter.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    20
    I am talking about pointer lists

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    In a pedantic sense you could just start at one end of the list and free every other element. I assume odd items are first, but for example...
    Code:
    node *walk;
    node *del;
    
    while( walk != NULL && walk->next != NULL && walk->next->next != NULL ) {
       del = walk->next->next;
       walk = del->next;
       delete( del );
    }
    You might need to edit that if you need to look at node data to figure out what's even or odd.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. urgent help please...
    By peter_hii in forum C++ Programming
    Replies: 11
    Last Post: 10-30-2006, 05:37 AM
  2. Doubly-Linked List
    By jgs in forum C Programming
    Replies: 7
    Last Post: 04-18-2005, 01:39 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 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