Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Need help with Linked Lists!!

Hybrid HunterHybrid Hunter Posts: 3Member
I have to write a program in C which reads a file through arguments which is a maze consisting of '#' as walls and a 's' and 'f' as start and finish, and spaces to denote a path.

The program has to store the maze into a linked list and then proceed to check that the maze does have a start and finish and that it is inside the maze and not outside. It also has to check how many deadends and branches the maze has and display this to the user.
A dead end being that you reach a '#' and you can't go anywhere except back and a branch is reaching a point where you can go multiple ways.
So in the following maze
### ###
#d# #s# d is a deadend.
### ### # and the 1 is how many branches there are.
#f 1 # you can go straight or take the 1 branch to the side.
#########
So this maze has 1 dead end and 1 branch.

I'm having a lot of trouble getting this program done.
I've posted the code below, as you can see my programming knowledge isn't that good yet.
I want to be able to read the maze and store it in a linked list properly so i can check through it for dead ends and branches. Can anyone show me code for how this is done?
Thanks

------------
#include
#include

struct maze
{
struct maze *next;
char node;
};
typedef struct maze maze;

FILE* openFile(char *filename, char *mode);
void readFile(char *filefrom);

int main(int argc, char *argv[])
{
/* Check that only one argument is passed */

if (argc != 2)
{
printf("Invalid number of file arguments,");
printf("please enter one file only
");
exit(0);
}

readFile(argv[1]);
/* printMaze(*node);*/

return 0;
}

void readFile(char *filefrom)
{
maze *temp;

FILE *fin = openFile(filefrom, "r");
char character;
int Scount = 0, Fcount=0;

/* build new maze */
temp = (maze *) malloc(sizeof(maze));
temp->next = NULL;

while (!feof(fin))
{
fscanf(fin, "%c", &character);
temp->node = character;

/* check that there is one start and finish */
if(character == 's')
{
Scount++;
}
else if(character == 'f')
{
Fcount++;
}

if(Scount >1)
{
printf("Error - multiple start found in maze
");
exit(0);
}
if(Fcount>1)
{
printf("Error - multiple finish found in maze
");
exit(0);
}

temp->node = character;
printf("%c", temp->node);
}
fclose(fin);
}

FILE* openFile(char *filename, char *mode)
{
/* open a FILE stream to filename with given mode */

FILE *file = fopen(filename, mode);

if(file == NULL)
{
printf("ERROR - Unable to open file");
exit(0);
}

return file;
}

----------------

Comments

  • IDKIDK Posts: 1,784Member
    : I have to write a program in C which reads a file through arguments which is a maze consisting of '#' as walls and a 's' and 'f' as start and finish, and spaces to denote a path.
    :
    : The program has to store the maze into a linked list and then proceed to check that the maze does have a start and finish and that it is inside the maze and not outside. It also has to check how many deadends and branches the maze has and display this to the user.
    : A dead end being that you reach a '#' and you can't go anywhere except back and a branch is reaching a point where you can go multiple ways.
    : So in the following maze
    [code]
    : ### ###
    : #d# #s# d is a deadend.
    : ### ### # and the 1 is how many branches there are.
    : #f 1 # you can go straight or take the 1 branch to the side.
    : #########
    [/code]
    : So this maze has 1 dead end and 1 branch.
    :
    : I'm having a lot of trouble getting this program done.
    : I've posted the code below, as you can see my programming knowledge isn't that good yet.
    : I want to be able to read the maze and store it in a linked list properly so i can check through it for dead ends and branches. Can anyone show me code for how this is done?
    : Thanks
    :
    : ------------
    [code]
    : #include
    : #include
    :
    : struct maze
    : {
    : struct maze *next;
    : char node;
    : };
    : typedef struct maze maze;
    :
    : FILE* openFile(char *filename, char *mode);
    : void readFile(char *filefrom);
    :
    : int main(int argc, char *argv[])
    : {
    : /* Check that only one argument is passed */
    :
    : if (argc != 2)
    : {
    : printf("Invalid number of file arguments,");
    : printf("please enter one file only
    ");
    : exit(0);
    : }
    :
    : readFile(argv[1]);
    : /* printMaze(*node);*/
    :
    : return 0;
    : }
    :
    : void readFile(char *filefrom)
    : {
    : maze *temp;
    :
    : FILE *fin = openFile(filefrom, "r");
    : char character;
    : int Scount = 0, Fcount=0;
    :
    : /* build new maze */
    : temp = (maze *) malloc(sizeof(maze));
    : temp->next = NULL;
    :
    : while (!feof(fin))
    : {
    : fscanf(fin, "%c", &character);
    : temp->node = character;
    :
    : /* check that there is one start and finish */
    : if(character == 's')
    : {
    : Scount++;
    : }
    : else if(character == 'f')
    : {
    : Fcount++;
    : }
    :
    : if(Scount >1)
    : {
    : printf("Error - multiple start found in maze
    ");
    : exit(0);
    : }
    : if(Fcount>1)
    : {
    : printf("Error - multiple finish found in maze
    ");
    : exit(0);
    : }
    :
    : temp->node = character;
    : printf("%c", temp->node);
    : }
    : fclose(fin);
    : }
    :
    : FILE* openFile(char *filename, char *mode)
    : {
    : /* open a FILE stream to filename with given mode */
    :
    : FILE *file = fopen(filename, mode);
    :
    : if(file == NULL)
    : {
    : printf("ERROR - Unable to open file");
    : exit(0);
    : }
    :
    : return file;
    : }
    [/code]

    Code tags makes everyone happier.
Sign In or Register to comment.