Results 1 to 8 of 8

Thread: Linked list

  1. #1

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362

    Linked list

    I want to delete a value that is in my linked list.

    But I need something that can contain an adress. C++ have a variable that can hold an adress of a pointer without pointing on it?

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Youve lost me =):
    Code:
    struct n
    {
      n* next;
      n* prev;
      int dat; // yeah, no templates here
    };
    
    remove_item(int data_to_remove,struct n*& head)
    {
        struct n* temp = head;
        if(head->dat == data_to_remove)
        {
             head = head->next;
             delete temp;
             return 0;
        }
        while(temp->next)
        {
             if(temp->next->dat == data_to_remove)
             {
                 struct n* del = temp->next;
                 temp->next = temp->next->next;
                 delete del;
                 return 0;
              }
          }
          return 1;
    }
    Should do what you want.

    Z.

  3. #3

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Originally posted by Zaei
    Youve lost me =):
    Code:
    struct n
    {
      n* next;
      n* prev;
      int dat; // yeah, no templates here
    };
    
    remove_item(int data_to_remove,struct n*& head)//I think you dont need to put struct ?
    {
        struct n* temp = head;
        if(head->dat == data_to_remove) //Check if is the last n to remove, if yes then delete all
        {
             head = head->next;
             delete temp;
             return 0;
        }
        while(temp->next)//if it's not the last we remove only the one we wan to delete.
        {
             if(temp->next->dat == data_to_remove)
             {
                 struct n* del = temp->next;
                 temp->next = temp->next->next;
                 delete del;
                 return 0;
              }
          }
          return 1;
    }
    Should do what you want.

    Z.
    I added comment on your code, tell me if I am wrong or not?

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Its C, you do need the struct (if I am remembering right =).

    The second checks to see if the FIRST item is the one we are looking for, if so, simply chop it off.

    Z.

  5. #5

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    ok thx

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    C++ have a variable that can hold an adress of a pointer without pointing on it?

    Huh?

    Why not have a pointer, it's guaranteed to be the right size, and you don't HAVE to dereference it...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    CB is right. Plus, what he is saying:
    Code:
    char *ptr;     when you: ptr++ is increments by one
    long *ptr1;   when you: ptr1++ it increments by 4 (length of long)
    the compiler handles this automatically.

  8. #8
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Yes, and u can do something along the lines of:

    PHP Code:
    // singly linked list
    class Node {
    public:
      
    Node (int data 0) : Data(data) {} 
      ~
    Node () { if (pNextdelete pNextpNext 0Data=0; }

      
    Node pNext;
      
    int Data;
    }

    class List {
      
    Node m_pHead;
      
    int m_count;
    public:
      List () : 
    m_pHead(0) {}
      ~List () { if (
    m_pHeaddelete m_pHeadm_pHead=0;m_count=0; }

      
    bool Add(int dat) {
        
    Node pNew = new Node(dat);
        if (
    pNew) {
          
    m_pHead->pNext pNew;
          
    m_count++;
          return 
    true;
        }
        return 
    false;
      }

      
    bool Remove (int dat) {
        
    Node pTemp m_pHead;
        
    Node pDelete 0;
        
    // find it first
        
    if (pTemp->Data == dat) {
           
    pDelete pTemp;
           
    // disconect, delete, and reconnect
           
    m_pHead m_pHead->pNext;
           
    pDelete->pNext 0;
           
    delete pDelete;
           
    pDelete 0;
           
    m_count--;
           return 
    true;
        }
        while (
    pTemp->pNext) {
          if (
    pTemp->pNext->pData == dat) {
            
    m_count--;
            
    pDelete pTemp->pNext;

            
    // disconnect and delete
            
    pTemp->pNext pDelete->pNext;
            
    delete pDelete;
            
    pDelete 0;
            return 
    true;
          }

          return 
    false;
        }

        
    Node First () {
           return 
    m_pHead;
        }

        
    // Dont use this (BAD)
        // cuz the list gets added to at the top, bottom, left, right,
        // and everywhere!! so just use the first and iterate thru it
        
    Node Item (int index) {
          
    int i = -1;
          
    Node pFound m_pHead;
          while (
    pFound) {
            if (++
    == index) {
              return 
    pFound;
            }
          }
          return 
    0;
        }

        
    int Count () const {
          return 
    m_count;
        }
    }; 
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width