- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个双向链表并打印它。
我有两个函数,一个在列表的最前面添加,一个在列表的后面添加。但它打印不正确..我觉得我搞砸了一些东西,我知道我声明下一个和上一个指针的方式有问题,但我不太明白它是什么..任何反馈都值得赞赏。谢谢。
这是我的代码
#include <stdio.h>
#include <stdlib.h>
struct node
{
int val;
struct node *next;
struct node *prev;
};
struct list
{
struct node *head;
struct node *tail;
};
void initialize(struct list *l)
{
l->head = NULL;
l->tail = NULL;
}
void push_front(struct list *l, int value)
{
if(l->head == NULL)
{
l->head = (struct node *)malloc(sizeof(struct node));
l->head->prev = NULL;
l->head->val = value;
if(l->tail == NULL)
l->head->next = l->tail;
} else {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
l->head->prev = temp;
temp->next = l->head;
temp->val = value;
temp->prev = NULL;
l->head = temp;
}
}
void push_back(struct list *l, int value)
{
if(l->tail == NULL)
{
l->tail = (struct node *)malloc(sizeof(struct node));
l->tail->next = NULL;
l->tail->val = value;
if(l->head == NULL)
l->tail->prev = l->head;
} else {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
l->tail->next = temp;
temp->prev = l->tail;
temp->val = value;
temp->next = NULL;
l->tail = temp;
}
}
void print(struct list *l)
{
if(l->head == NULL)
{
printf("%s", "List is empty..");
} else {
struct node *current;
current = l->head;
while(current != NULL)
{
printf("%d", current->val);
current = current->next;
}
}
}
int main()
{
struct list l;
initialize(&l);
push_front(&l, 6);
push_front(&l, 7);
push_back(&l, 10);
push_front(&l, 8);
push_front(&l, 9);
push_back(&l, 11);
print(&l);
return 0;
}
最佳答案
除其他问题外,您的 push_front()
代码无法确保设置 l->tail
。它是:
void push_front(struct list *l, int value)
{
if(l->head == NULL)
{
l->head = (struct node *)malloc(sizeof(struct node));
l->head->prev = NULL;
l->head->val = value;
if(l->tail == NULL)
l->head->next = l->tail;
} else {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
l->head->prev = temp;
temp->next = l->head;
temp->val = value;
temp->prev = NULL;
l->head = temp;
}
}
它应该检查内存分配是否成功。它应该确保当 l->head
为 null 时设置 l->tail
。等等
void push_front(struct list *l, int value)
{
struct node *node = (struct node *)malloc(sizeof(*node));
if (node == 0)
err_error("Failed to allocate memory for a node\n"); // Does not return
node->val = value;
node->prev = NULL;
if (l->head == NULL)
{
assert(l->tail == NULL);
l->head = node;
node->next = NULL;
l->tail = node;
}
else
{
l->head->prev = node;
node->next = l->head;
l->head = node;
}
}
在 push_back()
中也需要进行类似的更改:
void push_back(struct list *l, int value)
{
struct node *node = (struct node *)malloc(sizeof(*node));
if (node == NULL)
err_error("Failed to allocate memory for a node\n"); // Does not return
node->val = value;
node->next = NULL;
if (l->tail == NULL)
{
assert(l->head == NULL);
l->head = node;
l->tail = node;
node->prev = NULL;
}
else
{
assert(l->tail->next == NULL);
l->tail->next = node;
node->prev = l->tail;
l->tail = node;
}
}
最好有一个 print_backwards()
和一个 print_forwards()
函数。您可以在每次操作后使用它们,以确保列表有意义。您可以添加更多断言以确保事情有意义。
修改print()
和main()
:
void print(struct list *l)
{
if (l->head == NULL)
printf("%s\n", "List is empty..");
else
{
struct node *current;
current = l->head;
while (current != NULL)
{
printf(" %2d", current->val);
current = current->next;
}
putchar('\n');
}
}
int main(void)
{
struct list l;
initialize(&l);
printf("Push front 6: ");
push_front(&l, 6);
print(&l);
printf("Push front 7: ");
push_front(&l, 7);
print(&l);
printf("Push back 10: ");
push_back(&l, 10);
print(&l);
printf("Push front 8: ");
push_front(&l, 8);
print(&l);
printf("Push front 9: ");
push_front(&l, 9);
print(&l);
printf("Push back 11: ");
push_back(&l, 11);
print(&l);
return 0;
}
示例输出:
Push front 6: 6
Push front 7: 7 6
Push back 10: 7 6 10
Push front 8: 8 7 6 10
Push front 9: 9 8 7 6 10
Push back 11: 9 8 7 6 10 11
这看起来很合理。
关于c - C 中的双向链表 - 链已损坏,不确定在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37649436/
循环不起作用,我想就是这样,也许 list[i].removeChild(list[i]);我想删除 lt 列表类。 function removeDone () { var lista = do
我正在制作一个java程序,它能够使用他们的api将(主要是mp4)文件上传到主机openload.co,你可以找到here . 我的程序可以上传 mp4 视频,但无法播放。当我下载以前上传的视频并通
我在服务器端使用 jquery-file-upload 和 Python-Flask。每当我上传 100mb 以上的大文件时,上传的版本比原始版本稍大并且无法打开(已损坏)。我对 10mb block
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我有一个按钮,它是我的 Magento 主题中提供的代码的一部分,根据日期/时间戳,我没有无意中编辑它。我确信它在某些时候是有效的,但回顾一下上周我的源代码控制,我似乎无法找出哪里出了问题。 这是按钮
我有一个将一些数据写入新文件的简单函数。它可以工作,并且文件已写入,但在 MSVS Express 2013 中调试时出现上述错误。 void writeSpecToFile(const char *
我试图包含我的应用程序的国际化,并且仅出于测试目的,我在文件 Localized.string 中添加了一行简单的代码。 这是我的整个文件: "Test locale" = "Test locale"
这是代码: console.log(234, document.links); console.log(235, document.links.length); 输出显示数组中有值,但返回 0。 这是
我在尝试使用 ASP.NET MVC3 的 Ajax.BeginForm 方法时遇到了一个奇怪的问题。表单通过正确的操作属性在页面上正确呈现。但是,当提交表单时,“OnFailure”事件返回“Not
我是 10 年级的学生,我在 APCS 的最终项目是制作一个游戏/应用程序并提交代码、运行视频和书面回复。我的问题是整个类(class)都是使用 Code.org 的 AppLab 进行教学的,并且
我正在使用带有 Microsoft auth 的 azure webapp。几个月来一直工作良好。 然而今天它坏了。login.microsoftonline.com 页面因 css 和 js 加载错
我遇到了一个奇怪的问题。直到几分钟前才出现问题。在不对 less/css 文件进行任何更改的情况下(从不),引导按钮和导航栏标题的外观突然发生了变化。在第一张图片上,您可以看到按钮应该是什么样子,在第
我正在开发一个用于核心转储处理的工具。使用 sysctl 我将其设置为获取工具输入的核心转储。一切都运转良好。但今天我面临着(我不知道为什么)coredump 损坏的情况。当我将常规文件设置为核心转储
我对一个我继承但并非最初创建的网站感到非常头疼。几乎所有的样式都是通过一个样式表完成的。问题是自动调整大小不再有效,所有页面现在都以指定的最小宽度显示。随着时间的推移,各种不同的人添加了它,CSS 变
我正在尝试使用 AngularJS 甚至纯 JavaScript 将 HTML 表格转换为 XLSX。 当我使用下面的方法转换为 XLS 时,它的下载正常并且 XLS 文件正在打开。 var blob
需要根帮助导致 vtable 损坏问题(不确定这是否正在发生)。这是代码的非常简化的版本。 class CBase { public: CBase() virtual ~CBase()
一段时间未在 Play 商店中更新我的应用程序后,我尝试使用我的 keystore 对我的一个 APK 进行签名,结果发现 keystore 和别名密码不再起作用。我可以使用此要点重置的 keysto
已安装 XCode 6在 Mavericks .它不运行。将操作系统更新为 Yosemite 10.10 :现在 Xcode.app(通过 *.dmg 安装)启动失败并显示错误消息 “Xcode” i
我有一个在我的应用程序中使用的 plist 文件,我可以对其进行本地化,因此我在我的项目中得到两个条目,一个用于英语,一个用于西类牙语,当我编译运行该应用程序时,它可以工作,但当然在这个阶段内容是相同
我相信这是来自 androidsnippets.org 的 fragment - 为什么它在 Android 2.3 上不起作用?如何解决? 错误 03-05 23:19:17.479: WARN/S
我是一名优秀的程序员,十分优秀!