我在寫一個使用DFS算法和堆棧解決迷宮的程序。我正在考慮將用於結束的路徑座標存儲到包含整數x
,y
座標的結構中,然後將該結構推送到堆棧上以執行其他指令(打印,彈出等)。是否可以將結構存儲到鏈接列表中?
我已經搜遍遍了,還沒有找到任何有用的東西。所以我繼續設置它,但我得到一個關於類型兼容性的錯誤,因爲我有我的節點數據爲一個int,但我試圖把一個結構。作爲鏈接列表的新手我只看到數據爲int或char。最後,甚至有可能做我想做的事情?如果不是,你能建議一種將x
,y
座標傳遞到堆棧的方法嗎?先謝謝你。
下面是我的代碼示例,在哪裏可以節省空間a1
是COORD
的實例,列表已初始化,以及迷宮等。
typedef struct node {
int data; /* Value or data stored in node*/
struct node *pNext; /* Reference to the next node address */
} NODE;
/*Structure declares pointers for front and back of the list*/
typedef struct LIST {
NODE *front;
NODE *back;
} LIST;
/* Structure to pass multiple values onto stack */
typedef struct COORD{
int x;
int y;
}COORD;
/*Example of one of the functions */
void lst_push_front(LIST *l, COORD *a1) {
NODE *p = malloc(sizeof(NODE));
p->data = a1;
p->pNext = l->front;
l->front = p;
if(l->back == NULL) // was empty, now one elem
l->back = p;
}
變化'成員INT data'到'COORD data'以正確的方式,和'對 - >數據= a1'到'對 - >數據= * a1' 。 – 2015-02-12 07:49:27
...或者將int data作爲一個void * data代替一個通用的方法...在你的'pull'函數中,你明確地必須強制轉換'(COORD *)(p-> data)'然後儘管回到正確的指針類型... – 2015-02-12 08:25:13