2013-02-22 7 views
-1

У меня есть 3 файла - String (для получения символов и сборки их в строку (как указатель, но не массив)), файл LinkedList и основной (тестовый файл). Строковая часть работает нормально, она протестирована. Но я застрял в LinkedList.C - связанный список

----> Я знаю, что проблема заключается в методе addString(), и это проблема в логике, потому что я поставил проверку печати в конце ее, и я никогда туда не попаду. Но я, кажется, не найти логическую проблему ... Вот код для LinkedList:

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

struct node 
{ 
    struct node *next; 
    struct node *previous; 
    struct string *str; 
}; 

static struct node *head; 
static struct node *tail; 

int count = 0; 

void initList() 
{ 
    head = NULL; 
    tail = NULL; 
} 

void addString(struct string *str_) 
{ 
    struct node *current = malloc(sizeof(struct node)); 
    if (head = NULL) 
    { 
     head = current; 
     tail = current; 
     current->next = tail; 
     current->previous = head; 
     current->str = str_; 
    } 
    else 
    { 
     current->previous = tail; 
     tail->next = current; 
     tail = current; 
     current->str = str_; 
    } 

    puts("\nA string has been added!"); 

} 

void deleteString(int index) 
{ 
    struct node *currentNode; 
    currentNode = head; 
    int i = 0; 

    if(index == 0) 
    { 
     head->str = NULL; 
     head->next = head; 
     // delete first node and relocate "head" to next node 
    } 
    while(currentNode != NULL) 
    { 
     if(i == index) 
     { 
      currentNode->str = NULL; 
      currentNode->previous->next = currentNode->next; 
      currentNode->next->previous = currentNode->previous; 
     } 
     else 
     { 
      currentNode = currentNode->next; 
      i++; 
     } 
     // 1.loop through and starting from 0 as first (head) element 
     // 2.when index is reached - delete it and replace the connections 
    } 
} 

void printAll() 
{ 
    struct node *currentNode; 
    currentNode = head; 

    while(currentNode !=NULL) 
    { 
     printf("%s", currentNode->str); 
     currentNode = currentNode->next; 
    }// need to iterate through list 
} 

и вот тестовый файл:

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

#include "String.h" 
#include "LinkedList.h" 

int main(int argc, char** argv) { 

    initList(); 

    char* c; 
    c = getChars(); 

    struct string *strp1; 
    strp1 = malloc(sizeof(struct string)); 
    strp1 = make_string(c); 
    addString(strp1); 
    printAll(); 

    printf("%s", *strp1); 
    puts("\nsome text"); 
    return (EXIT_SUCCESS); 
} 
+0

Пожалуйста, запишите свое название, описывая проблему. –

ответ

1

Как эдуфа, упомянутый в вашей функции addString, вы должны выполнить сравнение вместо назначения. Другой проблемой является установка currentNode->next и currentNode->previous. В функции printAll() вы повторяете до currentNode == NULL, что при условии, что currentNode->next = current node у вас будет бесконечный цикл. Оставьте currentNode->next/previous как NULL, пока у вас не будет более 1 элемента.

1

(head = NULL) является оператор присваивания, а не сравнение. Измените его на (head == NULL).

BTW, так как похоже, что вы только начинаете с C, включите предупреждение в своих флагах компилятора. Не запускайте свой код, пока не устраните все предупреждения.

+0

И спросите, как включить предупреждения с вашим компилятором, если вы не можете узнать, как из руководства/с помощью google. Для gcc используйте переключатели: -Wall -Wextra – hyde

Смежные вопросы