List_Delete
/*Sorting from little to large use List*/ #include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free */ struct node
{
int key;
struct node *next;
}; typedef struct node Node; Node *Head = NULL;
Node *current; void Insert(int k)
{
Node *new_node; new_node = (Node *)malloc(sizeof(Node));//It is important but i can't understand now new_node->key = k; /* new node is inserted at the begining of the list*/ if ( Head == NULL || Head->key > k )
{
new_node->next = Head;
Head = new_node;
} /* new node is inserted somewhere inside the list */
else
{
current = Head; /* Check what is the value in the next node , after the current node */
/* if it is larger, or if the next node not exist */
/* then we shuold insert the node to next current */
/* else, update current to point to next node */ while()
{
if( current->next == NULL || current->next->key > k )
{
new_node->next = current->next;
current->next = new_node;
break;
}
else
current = current->next;
}
}
} Node *Delete(int k)
{
Node *answer;
Node *current = Head; /* Handle the special case that the first node is to be deleted */
if ( Head != NULL && Head->key == k )
{
Head = Head->next;
return current;
} else if ( Head != NULL && Head->key > k )
{
return NULL;
} while( current != NULL )
{
/* check the next node is to be deleted */
if ( current->next != NULL && current->next->key == k )
{
answer = current->next;
current->next = current->next->next;
return answer;
} else if ( current->next != NULL && current->next->key > k )
return NULL; /* else, updated current */
current = current->next;
}
return NULL; /* if current is NULL, then k is not in the list, to return NULL */ } void Print()
{
if( Head == NULL )
printf("The list is empty!\n");
else
{
current = Head; while( current != NULL )
{
printf("%d ", current->key);
current = current->next;
} printf("\n");
}
} int main()
{
Node *x; Insert();
Insert();
Insert(); printf("15, 12, 5 are inserted"); Print(); x = Delete();
if (x != NULL )
{
printf("5 is deleted: ");
free(x);
Print();
} x = Delete();
if ( x != NULL )
{
printf("12 is deleted: ");
free(x);
Print();
} Insert();
Insert();
Insert(); printf("6, 8, 7 is inserted: ");
Print(); x = Delete();
if ( x != NULL )
{
printf("8 is deleted: ");
free(x);
Print();
} return ;
}
List_Delete的更多相关文章
- Linux网络编程5——使用UDP协议实现群聊
引言 本文实现的功能类似于我之前所写的一篇博文(Linux之select系统调用_2),区别在于进程之间的通信方式有所不同.之前的文章中,我所使用的是管道,而本文我将会使用socket接口. 需求 客 ...
- linux编程之线性表
#include"stdio.h" #define MAX 100 typedef struct List{ int length; int num[MAX]; }List_seq ...
- portal开发"下拉框"“日期框”查询要怎么配置
下面的这些是我今天的成果! 总的来说是一步一步摸索出来的!还是等感谢超哥的耐心指导,犯了一些错误! 1.比如在wd配置文件中中写id=“check_it_two”,在java中写成 checki_it ...
- 侵入式单链表的简单实现(cont)
前一节介绍的侵入式链表实现在封装性方面做得不好,因为会让消费者foo.c直接使用宏container_of().这一节对list的定义做了一点改进,如下所示: typedef struct list_ ...
- C语言顺序表的实现
今天本来想写段代码练练手,想法挺好结果,栽了个大跟头,在这个错误上徘徊了4个小时才解决,现在分享出来,给大家提个醒,先贴上代码: /********************************** ...
- C基础 之 list 库奥义
前言 - 关于 list 思考 list 是最基础的数据结构也是数据结构的基础. 高级 C 代码纽带也是 list. 扯一点, 当你走进了 C 的殿堂, 那么你和 list 增删改查那就是一辈子丫 ~ ...
- 线性表的链式存储——C语言实现
SeqList.h #ifndef _WBM_LIST_H_ #define _WBM_LIST_H_ typedef void List; typedef void ListNode; //创建并且 ...
- 链表的艺术——Linux内核链表分析
引言: 链表是数据结构中的重要成员之中的一个.因为其结构简单且动态插入.删除节点用时少的长处,链表在开发中的应用场景许多.仅次于数组(越简单应用越广). 可是.正如其长处一样,链表的缺点也是显而易见的 ...
- 数据结构——算法之(027)( 在O(1)时间内删除链表结点)
[申明:本文仅限于自我归纳总结和相互交流,有纰漏还望各位指出. 联系邮箱:Mr_chenping@163.com] 题目:在O(1)时间内删除链表结点.且不知道链表头 题目分析: 1.把要删除节点的下 ...
随机推荐
- (转) 读懂IL
引言 转自园子里的一片关于IL的好文,分享的同时,方便自己今后查阅. 原文链接:http://www.cnblogs.com/brookshi/p/5225801.html ------ 略过作者调侃 ...
- python学习(十五) 屏幕抓取
15.1 屏幕抓取 15.1.1 Tidy和XHTML解析 Tidy:用来修复不规范且随意的HTML文档的工具. 为什么用XHTML: 和旧版本的HTML之间最主要的区别:HTML可能只用一个开始标签 ...
- Informatica PowerCenter下载地址
https://edelivery.oracle.com/EPD/Download/get_form?egroup_aru_number=12854075
- pandas分组和聚合
Pandas分组与聚合 分组 (groupby) 对数据集进行分组,然后对每组进行统计分析 SQL能够对数据进行过滤,分组聚合 pandas能利用groupby进行更加复杂的分组运算 分组运算过程:s ...
- Tuple、list的区别以及dict和set
元组(Tuple): 定义方法:使用小括号() 使用方法: count:可以统计某个元组段在整个元组中出现的次数 index:可以查询某个元组段在整个元组中的元组号 name_tuple = ('xi ...
- mysql\redis局域网链接
mysql: GRANT SELECT,DELETE,UPDATE,INSERT ON int_worm.* TO 'root'@'%' IDENTIFIED BY 'root'; redis: co ...
- 【原创】11. MYSQL++ 之 Quoting 与 Escaping
1. 综述 其实一看到这两个单词的时候我有点莫名其妙,可能英语没有学好,我的理解就是quoting是“引用”的意思,而Escaping是“逃脱”的意思.后来在看到了作者的TUTORIAL之后才大致明白 ...
- Linux Resin4.0 安装配置
Resin,是一个非常流行的application server,对servlet和JSP提供了良好的支持,性能优良,resin自身采用Java语言开发.Resin Pro版本支持缓存和负载均衡,收费 ...
- ArcGIS JS API实现的距离测量与面积量算
转自https://www.cnblogs.com/deliciousExtra/p/5490937.html
- MAT(Memory Analyzer tool)使用
当线上环境出现OOM/内存泄漏了,怎么办? 让虚拟机在发生内存溢出时 Dump 出当前的内存堆转储快照,配置-XX:+HeapDumpOnOutOfMemoryError, 当出现OOM时,分析dum ...