Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Linked lists and 2D arrays in C?

If I had board with 5 rows and 5 columns, how would I use linked lists to check each row for it's current value. I want to create a board which looks like

. . O . .

. . O . .

OOOOO

. . O . .

. . O . .

The dots represent empty cells and the O's represent little checkers pieces. The pieces can only move if the jump a neighboring piece and land on an empty cell (as in checkers). I know how to print a board but I need to write something that would show all the possible moves the board would make. I don't want any user input (other than maybe start game) . Any hints on how I would do this?

ps: Only one piece can move per round and obviously the table will change with every round, until there are no/or one peg left.

1 Answer

Relevance
  • ?
    Lv 7
    8 years ago
    Favorite Answer

    In a (singly) linked list, you have essentially two pieces of data: The location of the next position, and the contents of the position. You have (abstract) knowledge that the board is 5 x 5 - you don't have that information encoded anywhere. (You could also use a string as a one-dimensional array here, as the board is always 25 characters long no matter how it is filled.)

    So, to print the board, you'd do something like (I haven't checked whether I've done anything stupid with the iterator, but the general idea is this):

    - counter = 0

    - for each element in the linked list:

    -- print element

    -- increment counter

    -- if counter divides neatly into 5, print a newline character (if (counter % 5) == 0)

Still have questions? Get your answers by asking now.