Thread: help with nodeType function?

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    Code:
     void IPlist::insertInOrder(string address)
                   // precondition : list is ordered and address is not in list
                   // postcondition : list is ordered and contains address
                   {
                                    
                      if(list==NULL)
                      {
                                  list=new node;
                                      list->IPaddress=address;
                                       cout<<"list null"<<endl;
                                      }
                      while(list!=NULL)
                      {
                                       cout<<"list!null"<<endl;
                        cout<<list->IPaddress<<endl;
                      
                        if (list->IPaddress < address )
                        {
                            node* current=new node;
                            current->IPaddress =address;
                            current->link =list->link;
                            list->link = current;
                            list=current->link;                        
                        }
                        else
                        {
                            list=list->link;
                        }
                      }
                   }
    i cant seem to get this to stop looping, could someone help?

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > how to insert data with the insertinorder if the list is empty?
    Well if the list is empty, then the list is just the node you create.

    > void IPlist::insertInOrder(string address)
    There are two serious problems with this.
    1. you create a new node each time around the while loop.
    2. you keep searching, even after you've found the place to insert.

    At the start of main, have something like this
    Code:
    ip.display();  // should be empty!
    ip.insertInOrder("b"); // initial
    ip.display(); // 1 element
    ip.insertInOrder("a"); // at start
    ip.display();
    ip.insertInOrder("d"); // at end
    ip.display();
    ip.insertInOrder("a"); // already present, increment count
    ip.display();
    Then single step through the code with the debugger, making sure everything points to where it should. Say for example you insert "b" OK, but when you come to print it, it's gone, then you've found a bug. Backtrack through the code to find out where you "lost" it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    if the list is empty i just add it to list

    1.i create a new node so as to insert the new data into the list?
    2.how to make it stop looping after its inserted? im getin confused

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The while loop just finds the place to insert into the list.

    I usually use two pointers, next and prev
    Code:
    prev = NULL
    next = head of list
    while ( search is false ) {
      prev = next;
      next = next->next;
    }
    // insert
    There are several cases to worry about
    next and prev both null - the list is empty
    prev is null - insert at the head of the list
    next is null - insert at the tail of the list
    otherwise, insert between prev and next
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    i think thats a doubly linked list, im just using a normal list
    list->info(IPaddress in this case) and list->link

    could you show me how to fix the loop?

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    With less than 3 minutes effort on your part - nope - try harder.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #22
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    let me show you what i have altered already
    Code:
        void IPlist::insertInOrder(string address)
                   // precondition : list is ordered and address is not in list
                   // postcondition : list is ordered and contains address
        {
                   
               if(list!=NULL)
               {
                        cout<<"list!null"<<endl;
                        cout<<list->IPaddress<<endl;
                      
                        if (list->IPaddress > address )
                        {//if the data is bigger then the current node then insert it 
                            node* current=new node;
                            current->IPaddress =address;
                            current->link =list->link;
                            list->link = current;
                            list=current->link;                        
                        }
                        else
                        {//list is not empty and the data not bigger then current
                            node* current=new node;
                            current->IPaddress =address;
                            current->link =list->link;
                            list->link = current;
                            list=current->link; 
                        }
               }    
                if(list==NULL)
               {
                      list=new node;
                      list->IPaddress=address;
                      cout<<"list null"<<endl;
               }     
              
               
        }
    
    and the main function thats calling that:
    
    int main()
    {
        IPlist ip;
    
        string data[]={"a","b","c","a","b","c","a","d"};
        // some strings to insert into IPlist for testing
        // they don't really need to be IP addresses    
        for (int i = 0; i < 8; i++)
        {
           if (!ip.isPresent(data[i]))
              ip.insertInOrder(data[i]);
           else
              ip.updateCount(data[i]);
        }
        ip.display();
    }
    when i execute it it just couts the 1st NULL list then runtime error
    am i still having the same problems as before?

  8. #23
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    im sure the main is correct(or is it?) but whats wrong with the insertinorder function?

    the 2nd if function(the if(list==NULL) is correct)

    that leaves the 1st if function incorrect, am i correct in all these assumptions?

  9. #24
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    i also drew diagrams for the 1st function in the insertinorder function to follow it line to line, i really dont see where the error is am i just stupid?

  10. #25
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    *shrug*

    All the //!! bits are things I've changed
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    struct node
    {
       string IPaddress;
       int count;
       node *link;
    };
    
    class IPlist
    {
       // contains an ordered list of IP addresses and a count of each
       // occurrence
       public:
    
        IPlist();  // default constructor
                   // creates a new empty list
    
        ~IPlist(); // destructor - reallocates all dynamic memory of list
    
        bool empty() const; 
                   // precondition : none
                   // postcondition : returns true if list is empty 
                   //                 and otherwise false
    
        void insertInOrder(string address);
                   // precondition : list is ordered and address is not in list
                   // postcondition : list is ordered and contains address
    
        void display() const; 
                   // precondition : none
                   // postcondition : the list has been displayed 
                   // one record per line, tab-separated
                   // with heading IPaddress Count
        private:
        
           node *list;
    };
    
        IPlist::IPlist()
        // default constructor
        // creates a new empty list
    {
        list=NULL;
    }
    
        IPlist::~IPlist() 
        // destructor - reallocates all dynamic memory of list
    {
        //!! delete [] list;
        //!! Walk the list and delete each node
        node *head = list;
        while ( head ) {
            node *temp = head->link;
            delete head;
            head = temp;
        }
    }
    
        bool IPlist::empty() const 
        // precondition : none
        // postcondition : returns true if list is empty 
        //                 and otherwise false
    {
             return list==NULL;
    }
                   
    
        void IPlist::insertInOrder(string address)
    // precondition : list is ordered and address is not in list
    // postcondition : list is ordered and contains address
    {
        node *next = list, *prev = NULL;
        // find the right place
        while ( next != NULL && next->IPaddress < address ) {
            prev = next;
            next = next->link;
        }
    
        // create a new node (make it a class with a constructor)
        node *newnode = new node;
        newnode->IPaddress = address;
        newnode->count = 1;
        newnode->link = NULL;
    
        // check cases
        if ( next == NULL && prev == NULL ) {
            // empty list
            list = newnode;
        } else
        if ( prev == NULL ) {
            // new head of list, next being the old head
            newnode->link = next;
            list = newnode;
        } else
        if ( next == NULL ) {
            // new tail of list, prev being the last of the current list
            prev->link = newnode;
        } else {
            // in the middle
            newnode->link = prev->link;
            prev->link = newnode;
        }
    }
    
        void IPlist::display() const
                   // precondition : none
                   // postcondition : the list has been displayed 
                   // one record per line, tab-separated
                   // with heading IPaddress Count
    {
       node *current=list;
       cout<<"IPaddress   Count "<<endl; //!!
       while(current!=NULL)
       {
           cout<<current->IPaddress<<"   "<<current->count<<endl; //!!
           current=current->link;
       }
       cout << endl; //!!
    }               
    
    int main()
    {
        IPlist ip;
        if (ip.empty())
        {
          cout << "empty works" << endl;
        }
    
        ip.display();  // should be empty!
        ip.insertInOrder("b"); // initial
        ip.display(); // 1 element
        ip.insertInOrder("a"); // at start
        ip.display();   
        ip.insertInOrder("d"); // at end
        ip.display();
    
        return EXIT_SUCCESS;
    }
    My results
    Code:
    $ ./a.exe
    empty works
    IPaddress   Count
    
    IPaddress   Count
    b   1
    
    IPaddress   Count
    a   1
    b   1
    
    IPaddress   Count
    a   1
    b   1
    d   1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #26
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    you did all that in 3mins? i thought there was just like 1-2 lines wrong with mine....-_-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM