我在C中创建了一个具有以下功能的简单链接列表:Create(创建列表);Find(搜索列表中的一个元素);Insert(在列表开头插入一个值);Destroy(销毁整个列表)。它只使用整数(为了简单),我想知道可能出现的错误,以及如何对其进行总体改进。
节点结构:
typedef struct sllist
{
int val;
struct sllist* next;
}
sllnode;职能:
#include <stdio.h>
#include <stdlib.h>
#include "node.c"
/*
*Creates the linked list with a single value
*/
sllnode *create(int value)
{
sllnode *node;
node = (sllnode *) malloc(sizeof(sllnode));
if (node == NULL)
{
puts("Memory error");
return NULL;
}
node->val = value;
node->next = NULL;
return node;
}
/*
*Searches for an element in the list.
*Returns 1 if its found or 0 if not
*/
int find(sllnode *head, int value)
{
sllnode *trav = head;
do
{
if (trav->val == value)
{
return 1;
}
else
{
trav = trav->next;
}
} while (trav != NULL);
return 0;
}
/*
*Inserts a value at the beginning of the list
*/
sllnode *insert(sllnode *head, int value)
{
sllnode *new_node = (sllnode *) malloc(sizeof(sllnode));
if (new_node == NULL)
{
puts("Memory error");
return NULL;
}
new_node->val = value;
new_node->next = head;
head = new_node;
return head;
}
/*
*Destroys (i.e frees) the whole list
*/
void *destroy(sllnode *head)
{
sllnode *temp = head;
while (temp != NULL)
{
free(temp);
temp = temp->next;
}
free(head);
}
int main(void)
{
return 0;
} 发布于 2016-09-30 19:43:13
约定是在.h文件中声明所有共享的内容。
在destroy()中,您有返回类型void*。移除指针星就可以了。
sllnode *create(int value)
{
sllnode *node;
node = (sllnode *) malloc(sizeof(sllnode));
if (node == NULL)
{
puts("Memory error");
return NULL;
}
...滥用算法/数据结构中的标准输出是个坏主意。毕竟,所有可能出错的地方都是新节点没有足够的内存;返回NULL很可能说明了这种情况。
node = (sllnode *) malloc(sizeof(sllnode));更地道的C是
node = malloc(sizeof *node);将新节点“附加”到相反的方向。基本上,这是一个(链接)堆栈,而不是一个列表。
while (temp != NULL)
{
free(temp);
temp = temp->next;
}不要这样做。相反,要有这样的东西
while (temp)
{
next_node = temp->next;
free(temp);
temp = next_node;
}苏木兰(
)
首先,我想到了以下几点:
#include <stdio.h>
#include <stdlib.h>
typedef struct linked_list_node {
int value;
struct linked_list_node* next;
} linked_list_node;
typedef struct {
linked_list_node* head;
linked_list_node* tail;
} linked_list;
void linked_list_init(linked_list* list)
{
if (!list)
{
return;
}
list->head = NULL;
list->tail = NULL;
}
void linked_list_free(linked_list* list)
{
linked_list_node* current_node;
linked_list_node* next_node;
if (!list)
{
return;
}
current_node = list->head;
while (current_node)
{
next_node = current_node->next;
free(current_node);
current_node = next_node;
}
}
int linked_list_append(linked_list* list, int value)
{
linked_list_node* new_node;
if (!list)
{
return 1;
}
new_node = malloc(sizeof *new_node);
if (!new_node)
{
return 1;
}
new_node->value = value;
if (list->head)
{
list->tail->next = new_node;
}
else
{
list->head = new_node;
}
list->tail = new_node;
return 0;
}
int linked_list_index_of(linked_list* list, int value)
{
int index;
linked_list_node* node;
if (!list)
{
return -2;
}
for (node = list->head, index = 0; node; node = node->next, index++)
{
if (node->value == value)
{
return index;
}
}
return -1;
}
int main() {
int i;
linked_list my_list;
linked_list_init(&my_list);
for (i = 0; i < 10; ++i)
{
linked_list_append(&my_list, i);
}
printf("%d %d\n",
linked_list_index_of(&my_list, 4),
linked_list_index_of(&my_list, 100));
linked_list_free(&my_list);
return 0;
}希望这能帮上忙。
https://codereview.stackexchange.com/questions/142932
复制相似问题