Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   2 dimensional linked list (http://www.go4expert.com/forums/2-dimensional-linked-list-t19819/)

s11049151 20Oct2009 02:45

2 dimensional linked list
 
1 Attachment(s)
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.

xpi0t0s 20Oct2009 12:25

Re: 2 dimensional linked list
 
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?

s11049151 4Nov2009 14:38

Re: 2 dimensional linked list
 
2 Attachment(s)
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

xpi0t0s 4Nov2009 17:24

Re: 2 dimensional linked list
 
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?

s11049151 4Nov2009 17:48

Re: 2 dimensional linked list
 
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...

xpi0t0s 4Nov2009 20:08

Re: 2 dimensional linked list
 
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.

fulla 7Mar2011 19:45

Re: 2 dimensional linked list
 
hi ..
Interesting code, goode luke


All times are GMT +5.5. The time now is 03:00.