[問題] 雙向鏈結 以字串為內容 PRINTF問題
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
DEVC++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
資料結構的雙向鏈結串列
我的DATA記憶體儲存的是字串
但是編譯執行後就炸掉了==
餵入的資料(Input):
#include <stdio.h>
#include <stdlib.h>
#include "Ch4-5.h"
#include "createDList.c"
int main() {
char *data[6]={ "abc", "123", "def", "456", "ghi", "789" };
createDList(6, data);
printf("原來的串列: ");
printDList();
system("PAUSE");
return 0;
}
/*createDList.c檔*/
void createDList(int len, char *array) {
int i;
DList newnode, before;
first = (DList) malloc(sizeof(DNode));
first->data = array[0];
first->previous = NULL;
before = first;
now = first;
for ( i = 1; i < len; i++ ) {
/* 配置節點記憶體 */
newnode = (DList) malloc(sizeof(DNode));
newnode->data = array[i];
newnode->next = NULL;
newnode->previous=before;
before->next=newnode;
before = newnode;
}
}
/*顯示字串陣列 */
void printDList() {
DList current = first;
while ( current != NULL ) {
if ( current == now )
printf("#%s#", current->data);
else
printf("[%s]", current->data);
current = current->next;
}
printf("\n");
}
/*標頭檔*/
struct Node {
char *data;
struct Node *next;
struct Node *previous;
};
typedef struct Node DNode;
typedef DNode *DList;
DList first = NULL;
DList now = NULL;
extern void creatDList(int len, int *array);
extern void printDList();
預期的正確結果(Expected Output):
執行後會顯示出字串...
錯誤結果(Wrong Output):
執行後炸掉XD
程式碼(Code):(請善用置底文網頁, 記得排版)
補充說明(Supplement):
這是參考教科書上的程式碼修改
之前有做過類似的作業可以顯示(單向鏈結)
但這次就不行..
請版上前輩幫忙指教謝謝><
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.127.179.155
※ 編輯: ericievonne 來自: 140.127.179.155 (10/23 12:07)
→
10/23 12:13, , 1F
10/23 12:13, 1F
→
10/23 12:15, , 2F
10/23 12:15, 2F
→
10/23 12:15, , 3F
10/23 12:15, 3F
→
10/23 12:23, , 4F
10/23 12:23, 4F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章