У меня есть 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);
}
Пожалуйста, запишите свое название, описывая проблему. –