- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我设法想出了一种仅适用于整数的语法。我如何编写所有数据类型的语法,最好使用 typedef
和void*
现在也想知道如何使用 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/
一个雇主有几个雇员,因此雇员与他们的雇主有(多对一)关系。假设我们忽略了从雇主到雇员的反向关系(一对多),那么从雇员的角度来看,这可以表示为一对一的关系!? 最佳答案 乍一看,它可能看起来如此 - 使
因此,我研究了neo4j,由于它的数据模型可能非常适合我的项目,因此我可能会在下一个项目中使用它。我浏览了文档,但仍然需要这个问题的答案: 我可以将关系设置为单向吗? 看来neo4j人们喜欢看电影,所
我在用: Spring Boot Spring Data JPA Spring Data Rest hibernate 嵌入式H2数据库 我正在尝试定义 2 个类,First和 Second ,这样就
给定以下代码 public class Course { @Id @GeneratedValue private Long id; private String nam
我的帐户和组之间存在多对多关系。组表的数据已预先填充,无需添加其他组。 使用 Hibernate,我的愿望是: 将帐户添加到帐户表, 向 account_group 表添加一条记录,并且 不向组表中添
我有一个 SKSpriteNode 作为球,它的所有 SKPhysicsBody 属性都可以向各个方向移动。我现在想要的是使其单向(仅朝它之前未移动到的方向移动,并且不返回到它已移动的路径)。目前我对
我已经看到多个关于此功能可用于蓝牙 4.0/LE 的引用资料,但我不确定从哪里开始或如何实现它。 明确地说,我指的是真正的无连接,只有一台设备上有一个接收器,另一台设备上有一个发射器。 (或者,更具体
给定以下代码 public class Course { @Id @GeneratedValue private Long id; private String nam
我在删除单向 OneToMany 关系中的所有者对象时遇到问题。 相关的部分是我有三个对象:Acronym、Expansion 和 Tag。 Acronym 与 Expansion 具有单向 OneT
我已经基于 this 在 JavaScript 中实现了一个 ElGamal 方案(代码很糟糕,只是想快速测试一下)解释。 var forge = require('node-forge'); var
我在我的模型中应用领域驱动设计聚合模式。我遵循常见的 DDD 聚合模式示例:Order 是管理一个或多个 LineItem 的根聚合。在我在网络上找到的许多聚合模式示例中,LineItem 应该不知道
这是我的场景, 我有 SubForum 类,它包含一组主持人,该集的每个实体 int 都是 User 类。 这是子论坛的映射
我想保存/保留一个实体(父实体),该实体(父实体)具有仅由父对象包含的唯一子对象。一切正常,直到出现重复的 child ,在这里我得到以下异常: Caused by: com.mysql.jdbc.e
这是我为汉诺塔问题编写的 Python 代码,其中塔必须从左桩转移到中间桩,使用右桩作为备用: def hanoi(n, origin = "1", destination = "2", spare
我有几个单向 JPA2 @OnetoMany 关系的失败案例下面是代码片段 @Entity @Table(name="CUSTOMER") @Access(AccessType.FIELD) publ
我想构建 Hibernate OneToMany 关系,其中父级有一个复合主键,子级有一个主键(hibernate 自动生成)。以下是我的工作示例代码: class Parent{ @Embe
我的项目中有以下实体: AccountGroup AccountItem AccountSegment 具有以下关系: AccountGroup有List AccountItem 有 List 一切正
是否可以使用自动布局和界面生成器进行单向尺寸匹配? 例如,我可能有两个标签。我不希望标签 A 大于标签 B,我希望 B 具有其固有大小。但使用“匹配高度/宽度”可能会导致大量文本增加 A 的大小,从而
尝试使用 Weblogic 11g 设置单向 SSL 并遇到问题。我有一个自定义身份(我自己的证书),但我只想执行单向 SSL,那么为什么我需要指定信任 keystore ? (我没有使用信任,因为我
注意:这不是 this 的副本问题,因为我了解您何时会使用单向 channel 。我一直这样做。我的问题是为什么这个程序是有效的: func main() { ch := make(chan<
我是一名优秀的程序员,十分优秀!