需要帮助:无法从双向链表中删除字符串:C
Assistance Needed: Unable to delete string from doubly linked list: C
我正在研究C中双链表的实现。我的列表按排序顺序添加。
我坚持删除这些值。下面的函数不会从双向链表中删除节点。
我认为我的逻辑是正确的,但我正在努力分配节点值。
a
表示添加,d
表示删除
struct Node
{
char name[42];
struct Node *next;
struct Node *prev;
};
struct Node* getNewNode(char *name)
{
struct Node *newNode = malloc(sizeof (struct Node));
strcpy(newNode->name, name);
newNode->prev = NULL;
newNode->next = NULL;
//printf("GetNewNode Name: %s", newNode->name);
return newNode;
}
void addSorted(struct Node **head, struct Node *newNode)
{
struct Node *current;
// Special case for the head end
if ((*head == NULL) || strcmp((*head)->name, newNode->name) >= 0)
{
newNode->next = *head;
*head = newNode;
}
else
{
// Locate the Node before the point of insertion
current = *head;
while (current->next != NULL &&
strcmp(current->next->name, newNode->name) <= 0)
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
void deleteValue(struct Node *head, char *name)
{
struct Node* current = head;
/* Find name */
while (current != NULL)
{
if (strcmp(current->name, name) == 0)
{
printf("Current At Break: %s \n", current->name);
break;
}
else if (strcmp(current->name, name) < 0)
{
printf("While Error: %s is not in the list\n", name);
return;
}
else
{
current = current->next;
printf("Current Is: %s \n", current->name);
}
}
if (current == NULL)
{
printf("If Error: %s is not in the list\n", name);
}
else
{ /* current != NULL */
if (current->prev == NULL && current->next == NULL)
{
/* Only node in head */
head = NULL;
printf("Deleting At 1st\n");
}
else if (current->prev == NULL)
{
/* First node in head */
head = current->next;
head->prev = NULL;
printf("Deleting At 2nd\n");
}
else if (current->next == NULL)
{
/* Last node in head */
head = current->prev;
head->next = NULL;
printf("Deleting At 3rd\n");
}
else
{
/* Node in middle of head */
current->prev->next = current->next;
current->next->prev = current->prev;
printf("Deleting At 4th\n");
}
}
free(current);
}
int main(void)
{
struct Node *head = NULL, *temp = NULL;
FILE * fp;
char * line = NULL, *name = NULL, *operator = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("data.txt", "r");
if (fp == NULL)
{
exit(EXIT_FAILURE);
}
while ((read = getline(&line, &len, fp)) != -1)
{
name = strtok(line, " ");
operator = strtok(NULL, "\n");
//printf("%s %s\n", name, operator);
if (strncmp(operator, "a", 1) == 0)
{
temp = getNewNode(name);
addSorted(&head, temp);
}
else if (strncmp(operator, "d", 1) == 0)
{
printf("****Try Deleting: %s\n", name);
deleteValue(&head, name);
}
}
fclose(fp);
if (line)
{
free(line);
}
}
文件
Beverly a
Kathy a
Radell a
Gary a
Chuck a
David a
kari a
Tom a
Tanya a
Scott a
Beverly d
Brenda a
Kathy a
Gary a
WenChen a
Chuck a
Mike a
Emanuel a
Linda a
Bernie a
Hassan a
Brian a
Gary d
Kathy d
Gary a
Eunjin a
Kathy a
Brenda a
Jun a
Peanut a
Travis a
You can use following modification:
/* Find name */
while (current != NULL)
{
if (strcmp(current->name, name) == 0)
{
printf("Current At Break: %s \n", current->name);
break;
}
current = current->next;
}
printf("Current Is: %s \n", current->name);
更改了一些功能。您可以将头部地址作为参数传递。希望这有帮助。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct Node
{
char name[42];
struct Node *next;
struct Node *prev;
};
struct Node* getNewNode(char *name)
{
struct Node *newNode = malloc(sizeof (struct Node));
strcpy(newNode->name, name);
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
char *trimwhitespace(char *str)
{
char *end;
// Trim leading space
while (isspace(*str)) str++;
if (*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && isspace(*end)) end--;
// Write new null terminator
*(end + 1) = 0;
return str;
}
void deleteValue(struct Node **head, char *name)
{
struct Node* current = *head;
/* Find name */
while (current != NULL)
{
if (strcmp(current->name, name) == 0)
{
printf("Current At Break: %s \n", current->name);
break;
}
else if (strcmp(current->name, name) > 0)
{
printf("While Error: %s is not in the list\n", name);
return;
}
else
{
current = current->next;
printf("Current Is: %s \n", current->name);
}
}
if (current == NULL)
{
printf("If Error: %s is not in the list\n", name);
}
else
{ /* current != NULL */
if (current->prev == NULL && current->next == NULL)
{
/* Only node in head */
*head = NULL;
printf("Deleted At 1st\n");
}
else if (current->prev == NULL)
{
/* First node in head */
*head = current->next;
(*head)->prev = NULL;
printf("Deleted At 2nd\n");
}
else if (current->next == NULL)
{
/* Last node in head */
current->prev->next = NULL;
printf("Deleted At 3rd\n");
}
else
{
/* Node in middle of head */
current->prev->next = current->next;
current->next->prev = current->prev;
printf("Deleted At 4th\n");
}
}
free(current);
current = NULL;
}
void addSorted(struct Node **head, struct Node *newNode)
{
struct Node *current;
// Special case for the head end
if ((*head == NULL) || strcmp((*head)->name, newNode->name) >= 0)
{
newNode->next = *head;
if (*head != NULL) (*head)->prev = newNode;
*head = newNode;
}
else
{
// Locate the Node before the point of insertion
current = *head;
while (current->next != NULL &&
strcmp(current->next->name, newNode->name) <= 0)
{
current = current->next;
}
newNode->next = current->next;
if (current->next != NULL)current->next->prev = newNode;
current->next = newNode;
newNode->prev = current;
}
}
int main(void)
{
struct Node *head = NULL, *temp = NULL;
FILE * fp;
char * line = NULL, *name = NULL, *operator = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("data.txt", "r");
if (fp == NULL)
{
exit(EXIT_FAILURE);
}
while ((read = getline(&line, &len, fp)) != -1)
{
name = strtok(line, " ");
operator = strtok(NULL, "\n");
name = trimwhitespace(name);
//printf("%s %s\n", name, operator);
if (strncmp(operator, "a", 1) == 0)
{
temp = getNewNode(name);
//printf("mainTemp Name: %s\n", temp->name);
addSorted(&head, temp);
}
else if (strncmp(operator, "d", 1) == 0)
{
printf("****Try Deleting: %s\n", name);
deleteValue(&head, name);
}
}
fclose(fp);
if (line)
{
free(line);
}
temp = head;
int i = 0;
printf("\n\n~~~~~~~~~\n");
while (temp != NULL)
{
printf("%s \n", temp->name);
temp = temp->next;
i++;
}
printf("%i\n", i);
}
我正在研究C中双链表的实现。我的列表按排序顺序添加。 我坚持删除这些值。下面的函数不会从双向链表中删除节点。 我认为我的逻辑是正确的,但我正在努力分配节点值。
a
表示添加,d
表示删除
struct Node
{
char name[42];
struct Node *next;
struct Node *prev;
};
struct Node* getNewNode(char *name)
{
struct Node *newNode = malloc(sizeof (struct Node));
strcpy(newNode->name, name);
newNode->prev = NULL;
newNode->next = NULL;
//printf("GetNewNode Name: %s", newNode->name);
return newNode;
}
void addSorted(struct Node **head, struct Node *newNode)
{
struct Node *current;
// Special case for the head end
if ((*head == NULL) || strcmp((*head)->name, newNode->name) >= 0)
{
newNode->next = *head;
*head = newNode;
}
else
{
// Locate the Node before the point of insertion
current = *head;
while (current->next != NULL &&
strcmp(current->next->name, newNode->name) <= 0)
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
void deleteValue(struct Node *head, char *name)
{
struct Node* current = head;
/* Find name */
while (current != NULL)
{
if (strcmp(current->name, name) == 0)
{
printf("Current At Break: %s \n", current->name);
break;
}
else if (strcmp(current->name, name) < 0)
{
printf("While Error: %s is not in the list\n", name);
return;
}
else
{
current = current->next;
printf("Current Is: %s \n", current->name);
}
}
if (current == NULL)
{
printf("If Error: %s is not in the list\n", name);
}
else
{ /* current != NULL */
if (current->prev == NULL && current->next == NULL)
{
/* Only node in head */
head = NULL;
printf("Deleting At 1st\n");
}
else if (current->prev == NULL)
{
/* First node in head */
head = current->next;
head->prev = NULL;
printf("Deleting At 2nd\n");
}
else if (current->next == NULL)
{
/* Last node in head */
head = current->prev;
head->next = NULL;
printf("Deleting At 3rd\n");
}
else
{
/* Node in middle of head */
current->prev->next = current->next;
current->next->prev = current->prev;
printf("Deleting At 4th\n");
}
}
free(current);
}
int main(void)
{
struct Node *head = NULL, *temp = NULL;
FILE * fp;
char * line = NULL, *name = NULL, *operator = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("data.txt", "r");
if (fp == NULL)
{
exit(EXIT_FAILURE);
}
while ((read = getline(&line, &len, fp)) != -1)
{
name = strtok(line, " ");
operator = strtok(NULL, "\n");
//printf("%s %s\n", name, operator);
if (strncmp(operator, "a", 1) == 0)
{
temp = getNewNode(name);
addSorted(&head, temp);
}
else if (strncmp(operator, "d", 1) == 0)
{
printf("****Try Deleting: %s\n", name);
deleteValue(&head, name);
}
}
fclose(fp);
if (line)
{
free(line);
}
}
文件
Beverly a
Kathy a
Radell a
Gary a
Chuck a
David a
kari a
Tom a
Tanya a
Scott a
Beverly d
Brenda a
Kathy a
Gary a
WenChen a
Chuck a
Mike a
Emanuel a
Linda a
Bernie a
Hassan a
Brian a
Gary d
Kathy d
Gary a
Eunjin a
Kathy a
Brenda a
Jun a
Peanut a
Travis a
You can use following modification:
/* Find name */
while (current != NULL)
{
if (strcmp(current->name, name) == 0)
{
printf("Current At Break: %s \n", current->name);
break;
}
current = current->next;
}
printf("Current Is: %s \n", current->name);
更改了一些功能。您可以将头部地址作为参数传递。希望这有帮助。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct Node
{
char name[42];
struct Node *next;
struct Node *prev;
};
struct Node* getNewNode(char *name)
{
struct Node *newNode = malloc(sizeof (struct Node));
strcpy(newNode->name, name);
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
char *trimwhitespace(char *str)
{
char *end;
// Trim leading space
while (isspace(*str)) str++;
if (*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && isspace(*end)) end--;
// Write new null terminator
*(end + 1) = 0;
return str;
}
void deleteValue(struct Node **head, char *name)
{
struct Node* current = *head;
/* Find name */
while (current != NULL)
{
if (strcmp(current->name, name) == 0)
{
printf("Current At Break: %s \n", current->name);
break;
}
else if (strcmp(current->name, name) > 0)
{
printf("While Error: %s is not in the list\n", name);
return;
}
else
{
current = current->next;
printf("Current Is: %s \n", current->name);
}
}
if (current == NULL)
{
printf("If Error: %s is not in the list\n", name);
}
else
{ /* current != NULL */
if (current->prev == NULL && current->next == NULL)
{
/* Only node in head */
*head = NULL;
printf("Deleted At 1st\n");
}
else if (current->prev == NULL)
{
/* First node in head */
*head = current->next;
(*head)->prev = NULL;
printf("Deleted At 2nd\n");
}
else if (current->next == NULL)
{
/* Last node in head */
current->prev->next = NULL;
printf("Deleted At 3rd\n");
}
else
{
/* Node in middle of head */
current->prev->next = current->next;
current->next->prev = current->prev;
printf("Deleted At 4th\n");
}
}
free(current);
current = NULL;
}
void addSorted(struct Node **head, struct Node *newNode)
{
struct Node *current;
// Special case for the head end
if ((*head == NULL) || strcmp((*head)->name, newNode->name) >= 0)
{
newNode->next = *head;
if (*head != NULL) (*head)->prev = newNode;
*head = newNode;
}
else
{
// Locate the Node before the point of insertion
current = *head;
while (current->next != NULL &&
strcmp(current->next->name, newNode->name) <= 0)
{
current = current->next;
}
newNode->next = current->next;
if (current->next != NULL)current->next->prev = newNode;
current->next = newNode;
newNode->prev = current;
}
}
int main(void)
{
struct Node *head = NULL, *temp = NULL;
FILE * fp;
char * line = NULL, *name = NULL, *operator = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("data.txt", "r");
if (fp == NULL)
{
exit(EXIT_FAILURE);
}
while ((read = getline(&line, &len, fp)) != -1)
{
name = strtok(line, " ");
operator = strtok(NULL, "\n");
name = trimwhitespace(name);
//printf("%s %s\n", name, operator);
if (strncmp(operator, "a", 1) == 0)
{
temp = getNewNode(name);
//printf("mainTemp Name: %s\n", temp->name);
addSorted(&head, temp);
}
else if (strncmp(operator, "d", 1) == 0)
{
printf("****Try Deleting: %s\n", name);
deleteValue(&head, name);
}
}
fclose(fp);
if (line)
{
free(line);
}
temp = head;
int i = 0;
printf("\n\n~~~~~~~~~\n");
while (temp != NULL)
{
printf("%s \n", temp->name);
temp = temp->next;
i++;
}
printf("%i\n", i);
}