2013-12-16 58 views
1

我想將一個字符串傳入函數,並返回鏈表,每個節點包含字符串。 因此,在主要功能,你可以看到如何將字符串列表存儲到節點中?

list = newTB("hello\ngood\nworld"); 

然後,newTB應該返回列表,這就好比..

[hello]-> [good]-> [world]->null 

必須有我的newTB功能錯誤,因爲賽格故障不斷出來..

請人幫助我..

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#include "textbuffer.h" 

#define MAX_TEXT 256 

typedef struct textbuffer *TB; 

struct textbuffer { 
    char *texts; 
    int count;  // counts how many nodes in the list 
    TB next; 
}; 

void printBuffer(TB tb){ 
    TB curr = tb; 
    int i=0; 
    while(curr != NULL){ 
     printf("POS %d : %s\n", i++, curr->texts); 
     curr = curr->next; 
    } 
} 

int linesTB(TB tb) { 
    return (tb->count); 
} 

TB newTB (char text[]){ 
    TB newText = malloc(sizeof(struct textbuffer)); 
    char *cpy = (char *)malloc(MAX_TEXT * sizeof(char)); 
    strcpy(cpy,text); 
    newText->count = 0; 
    newText->next = NULL; 
    int i = 0; 
    int j = 0; 

    while(cpy[i] != '\0') { 
     if(j == 0) { 
      newText->texts = (char *)malloc(MAX_TEXT * sizeof(char)); 
     } 
     if(cpy[i] == '\n') { 
      newText->count++; 
      newText->next = malloc(sizeof(struct textbuffer)); 
      newText = newText->next; 
      j = 0; 
     } else { 
      newText->texts[j++] = cpy[i++]; 
     } 
    } 
    return newText; 
} 

void releaseTB (TB tb) { 
    TB head = tb; 
    TB tmp; 

    while(head != NULL) { 
     tmp = head; 
     head = head->next; 
     free(tmp->texts); 
     free(tmp); 

    } 
} 

int main(int argc, char * argv[]) { 
    TB list = NULL; 
    list = newTB("hello\ngood bye\nworld\n"); 

    printf("**THERE ARE %d LINES IN TEXTBUFFER**\n", linesTB(list)); 
    printBuffer(list); 
    printf("%s\n",dumpTB(list)); 
    releaseTB(list); 
    return 0; 
} 
+0

它在哪裏段錯? – woolstar

+0

我想,你在這裏想念你的頭:'newText = newText-> next;'不是嗎? –

+0

並且您只爲頭節點初始化newText-> count。 –

回答

0

您在這裏沒有遞增i。所以它只在那裏循環。

if(cpy[i] == '\n') 
{ 
    newText->count++; 
    newText->next = malloc(sizeof(struct textbuffer)); 
    newText = newText->next; 
    j = 0; 
} 

我修改了代碼,這是我所做的。

TB newTB (char text[]){ 
    TB newText = (TB)malloc(sizeof(struct textbuffer)); 
    char *cpy = (char *)malloc(MAX_TEXT * sizeof(char)); 
    TB root = newText; // Store the first node to return 
    TB prv = newText; // not required. Added for testing by me 
    int cnt = 0; 
    strcpy(cpy,text); 
    newText->count = 0; 
    newText->next = NULL; 
    int i = 0; 
    int j = 0; 


    while(cpy[i] != '\0') { 
     if(j == 0) { 
      prv->texts = (char *)malloc(MAX_TEXT * sizeof(char)); 
     } 
     if(cpy[i] == '\n') { 
      prv->texts[j++] = '\0'; // As Rohan suggested. 
      //newText->count++; 
      prv->next = (TB)malloc(sizeof(struct textbuffer)); 
      prv = prv->next; // not required. Added for testing by me 
      //newText = newText->next; 
      j = 0; 
      i++; // Increment i here 
     } else { 
      prv->texts[j++] = cpy[i++]; 
     } 
    } 
    return root; // Return the root node. 
} 

編輯

//Printing the node 
void printBuffer(TB tb){ 
    TB curr = tb; 
    int i=0; 
    while(curr->next != NULL){ // changed from curr!=NULL to curr->next!=NULL 
     printf("POS %d : %s\n", i++, curr->texts); 
     curr = curr->next; 
    } 
} 

還增加

prv->next = NULL; // Added this line in newTB function 
return root; 
+0

此代碼似乎工作..但在列表的末尾,它也打印出'null'以及.. –

+0

它正確鏈接..但是當我打印出測試..POS 0:你好 POS 1:再見吧 POS 2:什麼 POS 3:(null) ///它應該是像.. POS 0:你好POS 2:再見POS 3:什麼 –

+0

我已經更新了代碼...添加prv-> next = NULL;在返回根節點之前在newTB函數中。 – 2013-12-16 06:42:24

1

存在L小問題OTS,但這裏的大單:

if(cpy[i] == '\n') { 
     newText->count++; 
     newText->next = malloc(sizeof(struct textbuffer)); 
     newText = newText->next; 
     j = 0; 
    } 

如果cpy[i]是換行,你犯了一個newText。猜猜你沒做什麼:提前i。所以你一直重複運行這段代碼,直到內存不足。

如果我要重寫這個,我不會逐字掃描代碼字符,而是使用strchr()找到換行符。此外,您已經複製了一次字符串(使用strdup會更有效率和更容易),請使用該複製的字符串並將其切分並將它的部分分配到textblock,以便每個textblock不需要自己的字符串。

0

一旦你通過字符newText->texts複製字符與'\0',使之成爲適當空終止字符串,終止它。

 if(cpy[i] == '\n') { 
      nextText->texts[j] = '\0'; //set null terminated string. 
      newText->count++; 
      newText->next = malloc(sizeof(struct textbuffer)); 
      newText = newText->next; 
      j = 0; 
     } else { 
      newText->texts[j++] = cpy[i++]; 
     } 

否則,在打印字符串中newText->texts不會找到終止字符,並保持印花和訪問無效的內存,導致段錯誤。

相關問題