0

hi every one i am having problem in accessing my 2d linked list..i am able to get a function in a 1d linked list but this is quite confusing.please if some one could give me a basic algorithm which i could work with i would appreciate it...thanks the function i need help with is get data().this function returns true if data retrival is succesful else it returns false.
here is my code for the linked list fucntion

#include "linkedlist_2d.h"
#include <iostream>

using namespace std;

template <class T> //default constructor
linkedlist_2d<T>::linkedlist_2d(){
    base_head = NULL;
    base_tail = NULL;
}
template <class T> //destructor
linkedlist_2d<T>::~linkedlist_2d(){
	NODE< NODE<T>* > * cur_row;
	NODE< T > * cell;

    NODE<T> * cur_row_head;
    NODE<T> * cur_row_tail;

	for (cur_row = base_head; cur_row != NULL; cur_row = cur_row->pNext){
		cur_row_head = cur_row->nData;
        for (cell = cur_row->nData; cell != NULL; cell = cell->pNext)
            cur_row_tail = cell;

		DeleteAllNodes(&cur_row_head, &cur_row_tail);
    }
		
	DeleteAllNodes(&base_head, &base_tail);
}

template <class T>
void linkedlist_2d<T>::append_row(){
    NODE< NODE<T>* > * pnode = new NODE< NODE<T>* >;
    pnode->nData = NULL;

    AppendNode(pnode, &base_head, &base_tail);
}

template <class T>
void linkedlist_2d<T>::append(int row, const T data){
    if (!append_col(row, data))
        cerr<<"Error appending data"<<endl;
}

template <class T>
bool linkedlist_2d<T>::append_col(int row, T data){  
    
    NODE < NODE<T>* > * cur_row = NULL;
    NODE <T> * last_col = NULL;
    NODE <T> * cell = new NODE<T>;

    if(row == 0 && base_head == NULL)
        append_row();     
    
    cur_row = base_head;

    if (cur_row == NULL)
        return false;

    //move to the specified row by checking existing rows
    int ilast_row;
    for(ilast_row = 1; ilast_row <= row; ilast_row++){
        cur_row = cur_row->pNext;
        if(cur_row == NULL){ 
            if (ilast_row == row){
                append_row(); 
                cur_row = base_tail;
            }    
            break;  
        }
    }

    if (cur_row == NULL)
        return false;
   
    //move to last column
    NODE<T> * cur_row_tail;
    NODE<T> * cur_row_head;

    cur_row_head = cur_row->nData;
    for (last_col = cur_row->nData; last_col != NULL; last_col = last_col->pNext){
        cur_row_tail = last_col;
    }
    cell->nData = data;            

    AppendNode(cell, &cur_row_head, &cur_row_tail);
	cur_row->nData = cur_row_head;

    return true;        
}


template <class T>
bool linkedlist_2d<T>::get_data(int row, int col, T & data){
	bool bsuccess;


// need help here


	return bsuccess;		
}





template <class T>
void linkedlist_2d<T>::print(){
    NODE < NODE<T>* > * cur_row = NULL;
    NODE<int> *cell = NULL;

    cout<<get_data(0, 0, data);
    for (cur_row = base_head; cur_row != NULL; cur_row = cur_row->pNext){
        for (cell = cur_row->nData; cell != NULL; cell = cell->pNext)
            cout << cell->nData<<" ";
        cout<<endl;
    }

}

template <class T>
void linkedlist_2d<T>::erase(){
	this->~linkedlist_2d();
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

The 2d linked lists I have worked with in the past is really nothing more than a linked list within a linked list. Something like this:

I have not done it using templates.

struct node
{
    struct node* next;
    void* data;
    struct node* head; // head of another linked list.
};

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.