/*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. JavaScript笔记——正则表达式

    正则表达式(regular expression)是一个描述字符模式的对象.JavaScript的 RegExp 类 表示正则表达式,而 String 和 RegExp 都定义了使用正则表达式进行强大 ...

  2. java成神之——线程操作

    线程 Future CountDownLatch Multithreading synchronized Thread Producer-Consumer 获取线程状态 线程池 ThreadLocal ...

  3. 真是服了:.EndEdit(); 如果没哟这个一句(c#更新ACCESS,datagridview无法更新第一行)

    //保存 this.jbbBindingSource3.EndEdit(); this.jbbTableAdapter3.Update(this.databaseDataSet3.jbb);

  4. 【292】Python 关于中文字符串的操作

    参考:什么是字符编码? 参考:Unicode 和 UTF-8 有何区别? 参考:python中文decode和encode转码 一.相关说明 Python 中关于字符串的操作只限于英文字母,当进行中文 ...

  5. java之多态性

    多态性(Polymorphism):一个东西,在不同的情况下,呈现出不同的行为两类:静态多态性:函数重载void add(int a,int b){}void add(int a,int b,int ...

  6. Python嵌套、递归、高阶函数

    一.嵌套函数 1.嵌套函数简单的理解可以看作是在函数的内部再定义函数,实现函数的“私有”. 2.特点: <1> 函数内部可以再次定义函数. <2> 只有被调用时才会执行(外部函 ...

  7. SpringMVC——映射请求参数

    Spring MVC 通过分析处理方法的签名,将 HTTP 请求信息绑定到处理方法的相应人参中. @PathVariable @RequestParam @RequestHeader 等) Sprin ...

  8. C语言多线程

    http://www.cnblogs.com/lixiaohui-ambition/archive/2012/07/26/2610336.html

  9. 现代C++学习笔记之二入门篇1

    现代 C++ 强调: 基于堆栈的范围,而非堆或静态全局范围. 自动类型推理,而非显式类型名称. 智能指针而不是原始指针. std::string 和 std::wstring 类型(请参见 <s ...

  10. UIView的alpha、hidden和opaque属性之间的关系和区别[转]

    UIView的alpha.hidden和opaque属性之间的关系和区别 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/ ...