- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
void del_duplicated(struct node *first) {
struct node *current = listfirst, *prev = listfirst, *tmp;
if (current == NULL)
printf("Nothing to delete !\n");
while (current != NULL) {
/* Keeping traverse the list to find the duplicated elements. */
prev = current;
current = current->next;
/* for the first and second duplicated node such as 1 1 2 3 5 */
if (prev->data == listfirst->data && current->data == listfirst->data) {
listfirst = current->next;
printf("LIST FIRST NOW AT %d", listfirst->data);
}
/* The rest requirement such as 1 2 4 4 5 convert to 1 2 5 */
else if ((prev->next)->data == (current->next)->data) {
(prev->next) = (current->next)->next; /*this is point 2 to 5*/
tmp = current; /*delete the node*/
free(tmp);
tmp = current->next;
free(tmp);
}
}
}
我有一个链表问题,需要我从列表中删除“2 个重复节点”。就像 1 1 2 3 5
转换为 2 3 5
一样。并且 1 2 4 4 5
将转换为 1 2 5
。
但是我的程序崩溃了,因为指针指向一个奇怪的地方,我不知道为什么。 (.exe 已停止工作)我认为我的移动指针逻辑是好的,但是...... error like this我的逻辑附在我的源代码的注释中。
我是一名刚在台湾学习编程和计算机科学的学生。请原谅我糟糕的英语。
1小时后编辑。
我在两个判断语句中添加了 BREAK,效果很好。但我不知道为什么。
void del_duplicated(struct node *first) {
struct node *current = listfirst, *prev = listfirst, *tmp, *tmp2;
if (current == NULL)
printf("Nothing to delete !\n");
while (current != NULL) {
/* Keeping traverse the list to find the duplicated elements. */
prev = current;
current = current->next;
//printf("prev %d,current %d\n", prev->data, current->data);
//system("pause");
/* for the first and second duplicated node such as 1 1 2 3 5 */
if (prev->data == listfirst->data && current->data == listfirst->data) {
listfirst = current->next;
system("pause");
break;
}
/* The rest requirement such as 1 2 4 4 5 convert to 1 2 5 */
else if (current->data == (current->next)->data) {
(prev->next) = (current->next)->next; /* this is point 2 to 5 */
tmp = current->next;
tmp2 = current; /* delete the node */
current = (current->next)->next;
free(tmp);
free(tmp2);
break;
}
}
}
最佳答案
#include <stdio.h>
#include <stdlib.h>
typedef struct node NODE;
struct node{
int data;
NODE *next;
NODE *prev;
};
NODE * find_node( NODE *first, int value){
while( first ){
if( first->data == value )
return first;
first = first->next;
}
return NULL;
}
NODE* delete_node( NODE **pnd ){
NODE *tmp, *nd;
if(!pnd || !*pnd)
return NULL;
nd = *pnd;
tmp = nd->next;
if(nd->next)
nd->next->prev = nd->prev;
if(nd->prev)
nd->prev->next = nd->next;
free( nd );
*pnd=NULL;
return tmp;
}
void del_duplicated( NODE **first ) {
NODE *tmp,
*dup,
*current=*first;
int val;
while( current ){
dup = find_node(tmp,current->data); // find in next nodes
tmp = current->next;
val= current->data;
while(1){
dup=find_node(tmp,val);
if(!dup) break;
while(dup){ // delete all occurences
if(dup == tmp)
tmp = dup->next;
delete_node(&dup);
dup=find_node(tmp,val);
}
delete_node(¤t); // delete current one aswell
}
current=tmp;
}
}
NODE *new_node(NODE *parent,int value){
NODE *nd = malloc( sizeof( NODE ) );
nd->prev = parent;
if( parent ){
nd->next = parent->next;
if(parent->next)
parent->next->prev = nd;
parent->next = nd;
}else
nd->next = NULL;
nd->data = value;
return nd;
}
void free_nodes(NODE *root){
NODE *tmp;
if(root && root->prev){
root->prev->next = NULL;
}
while(root){
tmp = root;
root = root->next;
free( tmp );
}
}
void print_nodes( NODE *root, char *text){
if(text)
printf("%s:\n", text);
while( root ){
printf("%d ", root->data );
root = root->next;
}
printf("\n");
}
int main( void ){
NODE *root;
root = new_node(NULL,10);
new_node( root, 25);
new_node( root, 25);
new_node( root, 25);
new_node( root, 20);
new_node( root, 15);
new_node( root, 16);
new_node( root, 16);
new_node( root, 5);
print_nodes( root ,"\nWith duplicated items");
del_duplicated(&root);
print_nodes( root ,"\nDuplicated items removed");
free_nodes(root);
return 0;
}
关于c - 从排序链表中一次性删除 "2 duplicated"个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34691083/
我无法理解 XmlWriter 在 C# 中的工作方式。假设以下代码在某处使用过。 StringBuilder builder = new StringBuilder(); XmlWriterSett
两者的主要区别是什么 一次性 一次性观察者 复合 Material 一次性 何时使用它们中的每一个 最佳答案 Disposable 是可以取消的作业。CompositeDisposable 是一个父作
我使用 QML 列布局,但我只想禁用(或减少)两个元素之间的间距。其余之间的间距应单独保留。 这可能吗? 谢谢。 最佳答案 减少是困难的,但增加两个相邻项之间的间距很容易:只需在它们之间插入一个空的
我有一个“设置” View 和关联的 Controller ,在从源代码管理克隆站点后,我立即使用它们来配置 MVC 站点的数据库和 web.config。 运行一次后,我想防止“设置” Contro
我正在尝试从 Peter Seibel 的《Practical Common Lisp》一书中学习 Lisp。在 chapter 8 : "Macros: Defining your own" ,我遇
我正在实现一个从 Resource 发出行的可观察对象. 问题是这个资源真的不喜欢从创建它的不同线程关闭(它会杀死一只小狗并在发生这种情况时抛出异常)。 当我处理订阅时,资源 Cancellable/
假设我使用以下命令启动了一次性流程:heroku run:detached "node do-some-stuff.js" --app my-app命令的输出是这样的: /usr/local/hero
是否可以识别正在运行一次性 dyno(即 heroku run rails console)的用户(可能通过 Heroku 电子邮件)?用例自动将更改归因于该用户。 最佳答案 我认为这是不可能的,因为
前一段时间我使用 setup_environ() 编写了一个从命令行运行的一次性 python 脚本,它不太适合作为自定义 manage.py 命令(我的首选)。它很好地设置了一切。我假设我们弃用了这
当我使用 RXJava 1 时,我总是跟踪我的订阅以在 onDestroy Activity 中执行取消订阅。示例:https://medium.com/@scanarch/how-to-leak-m
在较旧的 MVC HTML 帮助程序中,可以使用 IDisposable 来包装内容 - 例如 BeginForm 帮助程序会自动包装 *stuff*带有结束 form 标记 *stuff*
我想使用 System.Threading.Timer 执行一次。该计时器应该在不再需要时(即回调触发时)通过调用 Dispose 来确定性地清理。 问题在于回调无法可靠地获取对 Timer 的引用!
我是 Angular 1.5 的新手,正在学习单向数据流的最佳实践。我要离开这个 jsfiddle,我真的很困惑特别是一种行为。 我理解数据从父级向下流向子级并且是单向绑定(bind)的,即子级的变化
我正在尝试抓取一个使用大量 ajax 效果在表格中显示数据的网站。 当您与网站交互时,会通过 JSON 返回一些数据。 我知道 URL 以及如何构造它,但如果我尝试重新请求此 JSON,服务器会返回
是否可以在一次性级触发模式下使用epoll? 当我搜索时,我找不到任何关于它的信息;好像大家都用边沿触发的方式。 最佳答案 When the EPOLLONESHOT flag is selected
在阅读了我能找到的关于延续的几乎所有内容后,我仍然无法理解它们。也许是因为所有的解释都与 lambda 演算密切相关,我很难理解。 通常,在您完成当前的事情(即计算的其余部分)之后,continuat
我有一个 block 的自定义实现,它的工作方式很像 Html.BeginForm() .实现基本如下: public class MyBlock : IDisposable { privat
我们有一个广泛的经典 ASP 站点,我们正在寻求升级到 ASP .Net(很可能是最新版本)。显然,一次升级所有页面将是一项艰巨的任务,因此我们一开始只希望在 ASP .Net 中编写新页面(和页面重
我有一个 Django 和 django 休息框架项目,我希望移动设备能够请求 token ,然后在断开连接之前使用该 token x 分钟。我不想为每个移动设备创建一个用户,我只想要一个一次性密码。
我正在通过 Forte.js 集成 ACH eCheck 付款。 文档说第一步是获取一次性安全 token ,我就是这样做的,使用 forte.js . 那么我应该将此 token 用于 REST A
我是一名优秀的程序员,十分优秀!