2 dimensional linked list

s11049151's Avatar
Light Poster
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
File Type: zip student_program.zip (2.5 KB, 17 views)
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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?
0
s11049151's Avatar
Light Poster
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
File Type: pdf assignment2.pdf (50.4 KB, 24 views)
File Type: rar student_program(working).rar (3.0 KB, 15 views)
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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?
0
s11049151's Avatar
Light Poster
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 shabbir; 4Nov2009 at 19:28.. Reason: Code blocks
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
fulla's Avatar, Join Date: Mar 2011
Light Poster
hi ..
Interesting code, goode luke