/*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的更多相关文章

  1. Linux网络编程5——使用UDP协议实现群聊

    引言 本文实现的功能类似于我之前所写的一篇博文(Linux之select系统调用_2),区别在于进程之间的通信方式有所不同.之前的文章中,我所使用的是管道,而本文我将会使用socket接口. 需求 客 ...

  2. linux编程之线性表

    #include"stdio.h" #define MAX 100 typedef struct List{ int length; int num[MAX]; }List_seq ...

  3. portal开发"下拉框"“日期框”查询要怎么配置

    下面的这些是我今天的成果! 总的来说是一步一步摸索出来的!还是等感谢超哥的耐心指导,犯了一些错误! 1.比如在wd配置文件中中写id=“check_it_two”,在java中写成 checki_it ...

  4. 侵入式单链表的简单实现(cont)

    前一节介绍的侵入式链表实现在封装性方面做得不好,因为会让消费者foo.c直接使用宏container_of().这一节对list的定义做了一点改进,如下所示: typedef struct list_ ...

  5. C语言顺序表的实现

    今天本来想写段代码练练手,想法挺好结果,栽了个大跟头,在这个错误上徘徊了4个小时才解决,现在分享出来,给大家提个醒,先贴上代码: /********************************** ...

  6. C基础 之 list 库奥义

    前言 - 关于 list 思考 list 是最基础的数据结构也是数据结构的基础. 高级 C 代码纽带也是 list. 扯一点, 当你走进了 C 的殿堂, 那么你和 list 增删改查那就是一辈子丫 ~ ...

  7. 线性表的链式存储——C语言实现

    SeqList.h #ifndef _WBM_LIST_H_ #define _WBM_LIST_H_ typedef void List; typedef void ListNode; //创建并且 ...

  8. 链表的艺术——Linux内核链表分析

    引言: 链表是数据结构中的重要成员之中的一个.因为其结构简单且动态插入.删除节点用时少的长处,链表在开发中的应用场景许多.仅次于数组(越简单应用越广). 可是.正如其长处一样,链表的缺点也是显而易见的 ...

  9. 数据结构——算法之(027)( 在O(1)时间内删除链表结点)

    [申明:本文仅限于自我归纳总结和相互交流,有纰漏还望各位指出. 联系邮箱:Mr_chenping@163.com] 题目:在O(1)时间内删除链表结点.且不知道链表头 题目分析: 1.把要删除节点的下 ...

随机推荐

  1. [Android]异步任务AsyncTask使用解析

    AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法.注意继承时需要设定三个泛型P ...

  2. 第十二章 MySQL触发器(待续)

    ······

  3. Pandas:表计算与数据分析

    目录 Pandas之Series Pandas之DataFrame 一.pandas简单介绍 1.pandas是一个强大的Python数据分析的工具包.2.pandas是基于NumPy构建的. 3.p ...

  4. zabbix监控MySQL部署实战

    1.部署zabbix监控. 1.1 建用户组和用户 groupadd zabbix useradd -d /home/zabbix -g zabbix -m zabbix passwd zabbix ...

  5. 22-从零玩转JavaWeb-代码块

    配套详解视频 局部代码块与初始化代码块 面向对象-静态代码块 代码块总结 组合关系与类的加载 静态代码块及字段初始化练习 一.什么是代码块 在类中或方法当中 使用{}括起来的一段代码 就称它是一个代码 ...

  6. Tornado之自定义session

      面向对象基础 面向对象中通过索引的方式访问对象,需要内部实现 __getitem__ .__delitem__.__setitem__方法 #!/usr/bin/env python # -*- ...

  7. UIWidget

    [UIWidget] UIWidget在NGUI中的层次如下. 根据上篇所述,UIRect实现实现了Anchor功能.而Widget提供的功能也很简单,如下: 可以看到,widget只提供四个属性,a ...

  8. 【bzoj3940】[Usaco2015 Feb]Censoring

    [题目描述] FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S.他有一个包含n个单词的列表,列表里的n个单词 记为t_1...t_N.他希望从S中删除这些单词.  FJ每次 ...

  9. 【bzoj2460】[BeiJing2011]元素

    2460: [BeiJing2011]元素 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 692  Solved: 372[Submit][Statu ...

  10. Docker学习笔记_安装和使用Redis

    一.准备 1.宿主机OS:Win10 64位 2.虚拟机OS:Ubuntu18.04 3.操作账号:Docker 二.安装过程 1.搜索Redis                         su ...