I am creating a program to manipulate structures that are similar to linked lists. The problem i am having is in a text file that i am reading in there are delete lines like this.
d 1 2
d 2 3
d 3 4
d 2 0

with the first number as the row and the second as the column of the array. I have read in the variables correctly, but i am not quite sure how i would go about deleting the object at the specified position of the array. So i am suppose to read in the file and when it reaches the delete lines it deletes the row 1 column 2 object and then sets the top,bottom,left, and right objects again so the array stays linked after the specified object is deleted,and then sets currentObject equal to the first position in the linked list which is row 0 column 0, and then it repeats this step for the other specified row and column objects.

Any help would be greatly appreciated.

Recommended Answers

All 4 Replies

Could you expand on the problem statement a bit more? What do you mean by "structures similar to linked list" and the fact you store them in a multidimensional array? How many dimensions?

If we're considering a 2D array, what does it mean to delete an element? I would think you either somehow mark the element as unused/available, or you move the elements below it up, or you move the elements to its right to the left. These last two methods are the only way to really "delete" something from an array. They will, however, leave the right edge or bottom edge uneven, if the data was initially evenly distributed in the rows/columns.

I am creating a program to manipulate structures that are similar to linked lists. The problem i am having is in a text file that i am reading in there are delete lines like this.
d 1 2
d 2 3
d 3 4
d 2 0

with the first number as the row and the second as the column of the array. I have read in the variables correctly, but i am not quite sure how i would go about deleting the object at the specified position of the array. So i am suppose to read in the file and when it reaches the delete lines it deletes the row 1 column 2 object and then sets the top,bottom,left, and right objects again so the array stays linked after the specified object is deleted,and then sets currentObject equal to the first position in the linked list which is row 0 column 0, and then it repeats this step for the other specified row and column objects.

Any help would be greatly appreciated.

You say the structure is "similar" to a linked list and it is a two dimensional array. I don't know what you want your results to be after you have "deleted" an array element. Does the array get smaller? Do all the array elements "move up" into the empty position? Does the array stay the same, but the data at the element that is "deleted" is flagged with some flag saying it's been "deleted"? I'm trying to picture the structure that is similar to a linked list but that is in the form of a two dimensional array, but it's hard since they don't seem very similar to me. You should probably post some code since you are referring to a variable in your code (currentObject).

Sorry for not describing it better haha. Here is the full text file i am suppose to read in, with comments on what each line does.

4 5 // dimensiones of the grid. 4 rows, 5 columns
0 0 0 // the top leftmost node has value of “0”
0 1 0.1 // the node at row 0, column 1 has value of “0.1”
0 2 0.2
0 3 0.3
0 4 0.4
1 0 1 // the node at row 1, column 2 has value of “1”
1 1 1.1
1 2 1.2
1 3 1.3
1 4 1.4
2 0 2
2 1 2.1
2 2 2.2
2 3 2.3
2 4 2.4
3 0 3
3 1 3.1
3 2 3.2
3 3 3.3
3 4 3.4

// Note: The numbers in first column are integers, the numbers in the other columns are floats.
// You need implement templated class for this

t r 4 d 3 // reach the bottom rightmost node “3.4”
t l 4 u 3 // back to the top leftmost node “0”
t r 2 d 2 // reach the node “2.2”
d 1 2 // remove node “1.2”, back to the top leftmost node
d 2 3 // remove node “2.3”, back to the top left most node
d 3 4 // remove node “3.4”, back to the top left most node
d 2 0 // remove node “2”, back to the top left most node
t r 2 d 2 // reach the node “3.2”
t r 1 u 2 // reach the node “0.3”
t r 1 d 3 // try to reach removed node “3.4”, stop at “2.4”, throw an exception info
t l 3 // try to reach removed node “2”, stop at “2.1”, throw an exception info
t d 1 l 1 // reach node “3”
t u 2 // back to the top leftmost node “0”

So when it says delete 1 2 i am suppose to delete the node from row 1 column 2 with value 1.2. So when i delete this node then the node to the left of node 1.2 will be linked to the node to the right of 1.2 and the node to the top will be linked to the node to the bottom, so the matrix will still be linked. All the other nodes will still be in the same positions, but the specified nodes from the delete lines will be removed from the linked list. I was trying earlier to set the node to null somehow, but that didnt work, so i am not quite sure how to remove the specified node from the linked list, so if it is being traversed to, the program will through an exception, because a node doesnt exist at that position.

here is some code to. First the row and column are read in. Then the values are set to the nodes in the specified positions. After this the right,left,top, and bottom objects are set to link the matrix. After this the traverse statements are created for the various directions and inside these statements are exceptions if the right,left,top, or bottom object is null and is trying to be reached then through an exception and stop at the object before it. Then i created the delete if statement which i have commented out now. If i do a[dir][step].value=NULL then it sets the value equal to 0 of the specified object, but if i do &a[dir][step]==NULL, it does nothing. I cant think of any other way of removing the object from the matrix Thanks for the help.

if (!cin.eof())
{
    //row variable is read in
    cin>>row; 
    //column variable is read in
    cin>>column;


    //for loop created to set values matrix
    for(int i=0;i<row;i++)
    {
        //for loop created to set values of matrix
        for ( int j = 0; j < column; j++ )
        {
            //currentObject is set to matrix position row 0 column 0
            currentObject=&a[0][0];
            //rowpos is read in
            cin>>rowpos;
            //colpos is read in
            cin>>colpos;
            //value2 is read in
            cin>>value2;

            //matrix values are set
            a[ rowpos ][ colpos ].value =value2  ;
            //if rowpos-1 is not equal to 0 then the top object is set 
            if(rowpos-1>=0)
            {
                a[rowpos][colpos].top=&a[rowpos-1][colpos];
            }

            //if rowpos+1 is less or equal to row then the bottom object is set
            if(rowpos+1<=row)
            {
                a[rowpos][colpos].bottom=&a[rowpos+1][colpos];
            }
            //if colpos-1 is greater than or equal to 0 then the left object is set
            if(colpos-1>=0)
            {
                a[rowpos][colpos].left=&a[rowpos][colpos-1];
            }
            //if colpos+1 is less than or equal to column then the right object is set
            if(colpos+1<=column)
            {
                a[rowpos][colpos].right=&a[rowpos][colpos+1];
            }



        }

    }

}
//currentObject value is displayed
cout<<currentObject->value;
cout<<endl;
//while end of file is not reached then loop is entered
while(!cin.eof())
{



    //if end of file is not reached in if statement is entered
    if (!cin.eof())
    {
        //traverse and delete lines are read in line by line
        cin.getline(action,10,'\n');
        //action1 is set to action[0]
        action1=action[0];
        //dir is set to action[2]
        dir = action[2];
        //step is set to action[4]-'0'
        step= action[4]-'0';
        //dir2 is set to action[6]
        char dir2=action[6];
        //step2 is set to action[8]
        int step2=action[8]-'0';

        //if action1 is equal to t then if statement is entered
        if(action1=='t')
        {
            dir=action[2];
            //if dir is equal to r then if statement is entered
            if(dir=='r')
            {
                //for loop created to perform right traverse
                for(int i=0;i<step;i++)
                {
                    if(currentObject==NULL)
                    {
                        try
                        {
                            throw Exception();
                        }
                        catch(Exception ex)
                        {
                            currentObject=currentObject->left;
                        }
                    }
                    else
                    {
                        //currentObject is set to currentObject's right object
                        currentObject=currentObject->right;
                        //currentObject's value is displayed
                        cout<<currentObject->value;
                        cout<<endl;
                    }
                }

            }
            //if dir2 is equal to r then if statement is entered
            if(dir2=='r')
            {
                //for loop created to do second right traverse if there is one
                for(int i =0;i<step2;i++)
                {
                    if(currentObject==NULL)
                    {
                        try
                        {
                            throw Exception();
                        }
                        catch(Exception ex)
                        {
                            currentObject=currentObject->left;

                        }
                    }

                    //currentObject is set to currentObject's right object
                    else
                    {
                        currentObject=currentObject->right;
                        //currentObject's value is displayed
                        cout<<currentObject->value;
                        cout<<endl;
                    }
                }
            }
            //if dir is equal to l then if statement is entered
            if(dir=='l')
            {
                //for loop created to do left traverse
                for(int i =0;i<step;i++)
                {
                    if(currentObject==NULL)
                    {
                        try
                        {
                            throw Exception();
                        }
                        catch(Exception ex)
                        {
                            currentObject=currentObject->right;

                        }
                    }
                    //currentObject is set to currentObject's left object
                    else
                    {
                        currentObject=currentObject->left;
                        //currentObject's value is displayed
                        cout<<currentObject->value;
                        cout<<endl;
                    }
                }
            }
            //if dir2 is equal to l then if statement is entered
            if(dir2=='l')
            {
                //for loop created to do second left traverse if there is one
                for(int i =0;i<step2;i++)
                {
                    if(currentObject==NULL)
                    {
                        try
                        {
                            throw Exception();
                        }
                        catch(Exception ex)
                        {

                            currentObject=currentObject->right;
                        }
                    }
                    //currentObject is set to currentObject's left object
                    else
                    {
                        currentObject=currentObject->left;
                        //currentObject's value is displayed
                        cout<<currentObject->value;
                        cout<<endl;
                    }
                }
            }
            //if dir is equal to u then if statement is entered
            if(dir=='u')
            {
                //for loop created to do top traverse
                for(int i =0;i<step;i++)
                {
                    if(currentObject==NULL)
                    {
                        try
                        {
                            throw Exception();
                        }
                        catch(Exception ex)
                        {
                            currentObject=currentObject->bottom;

                        }
                    }

                    //currentObject is set to currentObject's top object
                    else{
                        currentObject=currentObject->top;
                        //currentObject's value is displayed
                        cout<<currentObject->value;
                        cout<<endl;
                    }
                }
            }
            //if dir2 is equal to u then if statement is entered
            if(dir2=='u')
            {
                //for loop created to do second top traverse if there is one
                for(int i =0;i<step2;i++)
                {
                    if(currentObject==NULL)
                    {
                        try
                        {
                            throw Exception();
                        }
                        catch(Exception ex)
                        {
                            currentObject=currentObject->bottom;
                        }
                    }
                    //currentObject is set to currentObject's top object
                    else{
                        currentObject=currentObject->top;
                        //currentObject's value is displayed
                        cout<<currentObject->value;
                        cout<<endl;
                    }
                }
            }
            //if dir is equal to d then if statement is entered
            if(dir=='d')
            {
                //for loop created to do bottom traverse
                for(int i =0;i<step;i++)
                {
                    if(currentObject==NULL)
                    {
                        try
                        {
                            throw Exception();
                        }
                        catch(Exception ex)
                        {
                            currentObject=currentObject->top;
                        }
                    }
                    //currentObject is set to currentObject's bottom object
                    else{
                        currentObject=currentObject->bottom;
                        //currentObject's value is displayed
                        cout<<currentObject->value;
                        cout<<endl;
                    }
                }
            }
            //if dir2 is equal to d then if statement is entered
            if(dir2=='d')
            {
                //for loop created to do second bottom traverse if there is one
                for(int i =0;i<step2;i++)
                {
                    if(currentObject==NULL)
                    {
                        try
                        {
                            throw Exception();
                        }
                        catch(Exception ex)
                        {
                            currentObject=currentObject->top;


                        }
                    }
                    //currentObject is set to currentObject's bottom object
                    else{
                        currentObject=currentObject->bottom;
                        //currentObject's value is displayed
                        cout<<currentObject->value;
                        cout<<endl;
                    }
                }
            }
        }
        /*		if(action1=='d')
        {

        dir=action[2]-'0';
        a[dir][step].value=NULL;
        &a[dir][step]==NULL; 

        currentObject=&a[0][0];	
        }*/

You didn't post the struct itself. I am guessing that it's something like this:

struct foo
{
     float value;
     foo* right;
     foo* left;
     foo* top;
     foo* bottom;
};

I'm assuming that a is a 2-D array of type foo (or whatever you called it) and that currentObject is of type foo also. Yes?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.