2012-10-14 49 views
0

我正在编写解决迷宫的程序。它从开始广场“o”开始,以“*”结束。 “#”是墙壁和“。”是可以探索的正方形。该算法是在程序开始时将开始方块添加到空队列中。之后的每一个后续步骤都是将队列从队列中取出,并检查它是否是已记录的方块,是完成方块还是可检测。如果可以探索,则将相邻的非墙壁方块添加到队列中。我的问题是,当我将起始方块添加到队列中时,出现了分段错误,我不知道为什么。下面是相关代码:将结构插入队列时出现分段错误

mazeQ.c:

#include <stdio.h> 
#include <stdlib.h> 
#include "QueueInterface.h" 

int main(void) 
{ 
    Queue Q; 
    InitializeQueue(&Q); 

    int row; 
    int column; 

    int x; 
    int y; 

    scanf("%d %d", &column, &row); //scan dimensions of maze 

    char input[row][column]; 
    ItemType maze[row][column]; 
    for(x = 0; x <= row; x++) { 
      fgets(input[x],column+2,stdin); 
    } 
    row++; 

    for (x = 1; x < row; x++){ 
      for (y = 0; y < column; y++) { 
        maze[x][y].squareType = input[x][y]; 
        maze[x][y].xCoordinate = x-1; 
        maze[x][y].yCoordinate = y; 
        maze[x][y].recordedSquare = 0; 
      } 
    } 

    for(x = 1; x < row; x++){ 
      for(y = 0; y < column; y++) { 
        if (maze[x][y].squareType == 'o') 
         Insert(maze[x][y],&Q); //INSERTED HERE 
      } 
    } 
    for (x = 1; x < row; x++) { 
      for (y = 0; y < column; y++) { 
        printf("%c", maze[x][y].squareType); 
      } 
    printf("\n"); 
    } 

} 

UserTypes.h:

#include <stdio.h> 
#include <stdlib.h> 
typedef struct { 
    char squareType; 
    int xCoordinate; 
    int yCoordinate; 
    int recordedSquare; 
} ItemType; 

下面的两个文件必须保持他们的方式。 QueueImplementation.c:

#include <stdio.h>     
#include <stdlib.h>     
#include "QueueInterface.h"   


void SystemError(char *errorMsg) {fprintf(stderr,errorMsg);} 

void InitializeQueue(Queue *Q) 
{ 
    Q->Front = NULL; 
    Q->Rear = NULL; 
} 

int Empty(Queue *Q) 
{ 
    return (Q->Front == NULL); 
} 

int Insert(ItemType R, Queue *Q) 
{ 
    QueueNode *Temp; 
               /* attempt to allocate */ 
    Temp = (QueueNode *) malloc(sizeof(QueueNode));  /* a new node */ 

    if (Temp == NULL) {    /* Temp = NULL signals allocation */ 
    SystemError("system storage is exhausted");   /* failure */ 
    return 0; 
    } else { 
    Temp->Item = R; 
    Temp->Link = NULL; 
    if (Q->Rear == NULL) { 
     Q->Front = Temp; 
     Q->Rear = Temp; 
    } else { 
     Q->Rear->Link = Temp; 
     Q->Rear = Temp; 
    } 
    } 
    return 1; 
} 

QueueInterface.h:

#include "UserTypes.h"       /* get ItemType definition */ 

typedef struct QueueNodeTag { 
     ItemType    Item; 
     struct QueueNodeTag *Link; 
    }QueueNode; 

typedef struct {         /* a queue is empty iff */ 
     QueueNode *Front;      /* its Front == NULL */ 
     QueueNode *Rear; 
    }Queue; 


/* defined operations */ 

extern void InitializeQueue(Queue *Q); 
    /* Initialize the queue Q to be the empty queue */ 

extern int Empty(Queue *Q); 
    /* Returns TRUE == 1 if and only if the queue Q is empty */ 

extern int Full(Queue *Q); 
    /* Returns TRUE == 1 if and only if the queue Q is full */ 

extern int Insert(ItemType R, Queue *Q); 
    /* If Q is not full, insert a new item R onto the rear of Q */ 

extern int Remove(Queue *Q, ItemType *F); 
    /* If Q is non-empty, remove the frontmost item of Q and put it in F */ 

该计划是由输入mazeQ < sampleMaze

运行

sampleMaze

12 10 
############ 
#..........# 
#.#.######.# 
#.#....#...# 
#.###.*#.#.# 
#...####.#.# 
#.#.#..#.#.# 
#.#.#.##.#.# 
#o#......#.# 
############ 
+0

你有没有考虑过使用调试器? –

+1

请注意<= here:'for(x = 0; x <= row; x ++)' –

+0

@EdHeal我已经按照gdb调试分段错误的指导,但是我没有找到问题。 – user1684402

回答

1

我不知道为什么你会得到seg-fault吗?我通过运行它得到这个输出:

$ gcc *.c -o maze 
$ ./maze <sample 
ļ## 
ļ#### 
ļ###### 
ļ######## 
########## 
############ 
###########. 
#########... 
#######..... 
#####......# 

好吧,一些清理工作后,我得到这个代码:

#include <stdio.h> 
#include <stdlib.h> 
#include "QueueInterface.h" 

int main(void) 
{ 
    Queue Q; 
    InitializeQueue(&Q); 

    int row; 
    int column; 

    int x; 
    int y; 

    scanf("%d %d\n", &column, &row); //scan dimensions of maze 


    char input[row][column+2]; 
    ItemType maze[row][column]; 
    for(x = 0; x < row; x++) { 
      fgets(input[x],column+2,stdin); 
    } 

    for (x = 0; x < row; x++){ 
      for (y = 0; y < column; y++) { 
        maze[x][y].squareType = input[x][y]; 
        maze[x][y].xCoordinate = x; 
        maze[x][y].yCoordinate = y; 
        maze[x][y].recordedSquare = 0; 
      } 
    } 

    for(x = 0; x < row; x++){ 
      for(y = 0; y < column; y++) { 
        if (maze[x][y].squareType == 'o') 
         Insert(maze[x][y],&Q); //INSERTED HERE 
      } 
    } 
    for (x = 0; x < row; x++) { 
      for (y = 0; y < column; y++) { 
        printf("%c", maze[x][y].squareType); 
      } 
    printf("\n"); 
    } 

} 

而且它的输出:

############ 
#..........# 
#.#.######.# 
#.#....#...# 
#.###.*#.#.# 
#...####.#.# 
#.#.#..#.#.# 
#.#.#.##.#.# 
#o#......#.# 
############ 

试试吧。

+0

这仍然给我一个段错误。 – user1684402

+0

@ user1684402我不知道那么 - 我编译它并运行。实际上,你对迷宫样本的输入方式并不是最好的,也不是防错的。 – Serge

+0

@Serge只是为了确认您的代码正在工作。 – SparKot

0

显然问题是,我把我的程序文件的目录有太多其他垃圾文件占用内存。我所要做的只是将我的程序文件移动到另一个目录中。我想我学到了我的经验,总是为新程序创建一个新目录。感谢大家的帮助。