gpt4 book ai didi

c - 所有数据类型的单向链表,实现C语言的插入、删除、查找元素

转载 作者:行者123 更新时间:2023-11-30 20:32:48 25 4
gpt4 key购买 nike

所以我设法想出了一种仅适用于整数的语法。我如何编写所有数据类型的语法,最好使用 typedefvoid*现在也想知道如何使用 void push_back命令。?

它创建一个链接列表,添加节点,删除节点并查找列表中的任何节点。第一个节点始终可以通过全局“头”指针进行访问。当第一个节点被删除时,该指针会被调整。类似地,有一个“curr”指针包含列表中的最后一个节点。当删除最后一个节点时,也会对此进行调整。每当向链表添加节点时,总是检查链表是否为空,然后将其添加为第一个节点。

有没有其他方法可以在不使用 #include<stdbool.h> 的情况下添加、删除和查找链表中的节点?

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

struct test_struct
{
int val;
struct test_struct *next;
};

struct test_struct *head = NULL;
struct test_struct *curr = NULL;

struct test_struct* create_list(int val)
{
printf("\n creating list with headnode as [%d]\n",val);
struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
if(NULL == ptr)
{
printf("\n Node creation failed \n");
return NULL;
}
ptr->val = val;
ptr->next = NULL;

head = curr = ptr;
return ptr;
}

struct test_struct* add_to_list(int val, bool add_to_end)
{
if(NULL == head)
{
return (create_list(val));
}

if(add_to_end)
printf("\n Adding node to end of list with value [%d]\n",val);
else
printf("\n Adding node to beginning of list with value [%d]\n",val);

struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
if(NULL == ptr)
{
printf("\n Node creation failed \n");
return NULL;
}
ptr->val = val;
ptr->next = NULL;

if(add_to_end)
{
curr->next = ptr;
curr = ptr;
}
else
{
ptr->next = head;
head = ptr;
}
return ptr;
}

struct test_struct* search_in_list(int val, struct test_struct **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;

printf("\n Searching the list for value [%d] \n",val);

while(ptr != NULL)
{
if(ptr->val == val)
{
found = true;
break;
}
else
{
tmp = ptr;
ptr = ptr->next;
}
}

if(true == found)
{
if(prev)
*prev = tmp;
return ptr;
}
else
{
return NULL;
}
}

int delete_from_list(int val)
{
struct test_struct *prev = NULL;
struct test_struct *del = NULL;

printf("\n Deleting value [%d] from list\n",val);

del = search_in_list(val,&prev);
if(del == NULL)
{
return -1;
}
else
{
if(prev != NULL)
prev->next = del->next;

if(del == curr)
{
curr = prev;
}
else if(del == head)
{
head = del->next;
}
}

free(del);
del = NULL;

return 0;
}

void print_list(void)
{
struct test_struct *ptr = head;

printf("\n -------Printing list Start------- \n");
while(ptr != NULL)
{
printf("\n [%d] \n",ptr->val);
ptr = ptr->next;
}
printf("\n -------Printing list End------- \n");

return;
}

int main(void)
{
int i = 0, ret = 0;
struct test_struct *ptr = NULL;

print_list();

for(i = 5; i<10; i++)
add_to_list(i,true);

print_list();

for(i = 4; i>0; i--)
add_to_list(i,false);

print_list();

for(i = 1; i<10; i += 4)
{
ptr = search_in_list(i, NULL);
if(NULL == ptr)
{
printf("\n Search [val = %d] failed, no such element found\n",i);
}
else
{
printf("\n Search passed [val = %d]\n",ptr->val);
}

print_list();

ret = delete_from_list(i);
if(ret != 0)
{
printf("\n delete [val = %d] failed, no such element found\n",i);
}
else
{
printf("\n delete [val = %d] passed \n",i);
}

print_list();
}

return 0;
}

最佳答案

Is there anyother way to add,delete and find nodes in a linked list without using #include<stdbool.h> ?

是的,不要在代码中使用任何 bool 类型。

关于c - 所有数据类型的单向链表,实现C语言的插入、删除、查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46920735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com