2 dimensional linked list

Discussion in 'C' started by s11049151, Oct 19, 2009.

  1. s11049151

    s11049151 New Member

    Joined:
    Oct 19, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hi Friends,
    Can you please help me write 3 functions in C++.
    It's based on a 2 dimensional linked-list

    The 3 functions that i have left to write are:

    1.get_data: If data retrieval is successful return true otherwise return false.

    2. update_data: This function updates the data located in the specified row and column of the linked list and above, returns true if data update is successful and returns false otherwise.

    3. remove_data: This function removes a node located in the specified row and column from the linked list. If the node does not exist then return false other wise
    remove the node and return true.

    Attached is the project that i have managed to complete with the help of my tutor.

    Can any one help me please, this will give me a bonus 5% in my course work.

    Thank you.
     

    Attached Files:

  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Why should you get the bonus if we write the code for you?

    I'm not opening an attachment - it might contain anything. What exactly is the problem you're struggling with at the moment?
     
  3. s11049151

    s11049151 New Member

    Joined:
    Oct 19, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    finally......i got the programme to work.......but not sure if it is correct......can i get some help now?
    please


    the required functions is in driver.cpp
     

    Attached Files:

  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I'm still not going to open an attachment. Post code using code tags (described in the posting guidelines).
    If the program works why aren't you sure if it's correct?
    What's wrong with it, and if there's something wrong, why do you think that means it works?
     
  5. s11049151

    s11049151 New Member

    Joined:
    Oct 19, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    I'm not sure if it is working...though it compiles and runs.

    I just want to know if what I have done is correct.

    Here is the code that I've written:


    Code:
    template <class T>
    bool linkedlist_2d<T>::get_data(int row, int col, T & data){
        bool bsuccess;
    
        NODE < NODE<T>* > * cur_row = NULL;
        NODE<int> *cell = NULL;            //node pointer variables
        
        cur_row = base_head; //cur_row points to base_head 
        
        int x = 0;
        while(x<row) //loop until row size
        {
            if(cur_row->pNext==NULL)
            {
                 return false; //return false if next node is NULL
                 }
                 
            else cur_row = cur_row->pNext; //else point to next node
            
            x++;
        }
        cell = cur_row->nData; //make cell points first column node 
        int y = 0;
        while(y<col)//loop until col size 
        {
            if(cell->pNext==NULL)//return false if if next node is NULL
            {
                 return false;
                 }
                 
            else cell = cell->pNext; //cell points to next node
            
            y++;
        }
        
         data=cell->nData;   //data is data of node
         bsuccess = true;   
                                                
    
        return bsuccess;    //return true    
    }
    
    template <class T>
    bool linkedlist_2d<T>::update_data(int row, int col, const T data){
        bool bsuccess;
    
        NODE < NODE<T>* > * cur_row = NULL;
        NODE<int> *cell = NULL;            //node pointer variables
        
        cur_row = base_head; //cur_row points to base_head 
        
        int x = 0;
        while(x<row) //loop until row size
        {
            if(cur_row->pNext==NULL)
            {
                 return false; //return false if next node is NULL
                 }
                 
            else cur_row = cur_row->pNext; //else point to next node
            
            x++;
        }
        cell = cur_row->nData; //make cell points first column node 
        int y = 0;
        while(y<col)//loop until col size 
        {
            if(cell->pNext==NULL)//return false if if next node is NULL
            {
                 return false;
                 }
                 
            else cell = cell->pNext; //cell points to next node
            
            y++;
        }
        
         cell->nData = data;   //update cell data with data
         bsuccess = true;   
                                                
    
        return bsuccess;    //return true    
    
    
    }
    
    template <class T>
    bool linkedlist_2d<T>::remove_data(int row, int col){
        bool bsuccess;
    
        NODE < NODE<T>* > * cur_row = NULL;
        NODE<int> *cell = NULL;
        NODE<int> *tail = NULL;
        NODE<T> * cur_row_tail;
        NODE<T> * cur_row_head;   //declare variables
        
        cur_row = base_head;
        
        int x = 0;
        while(x<row)
        {
            if(cur_row->pNext==NULL)
            {
                 return false; //return false if next node is NULL
                 }
                 
            else cur_row = cur_row->pNext; //point to next node
            
            x++;
        }
        
        cell = cur_row->nData;
        cur_row_head = cell; //assign tail head after row loop
        int y = 0;
        while(y<col)
        {
            if(cell->pNext==NULL)
            {
                 return false;
                 }
                 
            else cell = cell->pNext;
            
            y++;
        }
        tail = cell;
        
        if(tail->pNext==NULL)
        {
             cur_row_tail = tail; //if next node is NULL assign it as tail
         }                     
         else{
              while(tail->pNext!=NULL){
                    tail = tail->pNext;    //else loop until next node is not NULL               
                    }
              }           
              cur_row_tail = tail;//assign as tail
        
         RemoveNode(cell,&cur_row_head,&cur_row_tail); //call function to delete node  
         bsuccess = true;
    
        return bsuccess; //return true
            
    }
    
    I might probably be wrong...
     
    Last edited by a moderator: Nov 4, 2009
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Believe it or not, usually the way to determine if code is correct is to run it in a suitable testbed. It's way too easy to miss something just by reading the code.

    So I suggest you create a testbed to exercise this code. So create a new 2d list, add some items, display the list in such a way as you can check that something is wrong, for example if you enter an item at (5,10) and it displays at (10,5) then you've got an x-y transposition bug somewhere.

    Cos that's what I would have to do to verify your code, and it doesn't really help much for me to do that; you may as well do it. If you've written the above code then you should have the skills to create a suitable testbed for it.
     
  7. fulla

    fulla New Member

    Joined:
    Mar 6, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    hi ..
    Interesting code, goode luke
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice