List_insert
List_insert
/* 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(1)
{
if( current->next == NULL || current->next->key > k )
{
new_node->next = current->next;
current->next = new_node;
break;
}
else
current = current->next;
}
}
} 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()
{
Insert(15);
Insert(12);
Insert(5); Print(); return 0;
}
Tips:
malloc()和free()的基本概念以及基本用法:
函数原型及说明:
void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针。如果分配失败,则返回一个空指针(NULL)。
关于分配失败的原因,应该有多种,比如说空间不足就是一种。
void free(void *FirstByte): 该函数是将之前用malloc分配的空间还给程序或者是操作系统,也就是释放了这块内存,让它重新得到自由。
List_insert的更多相关文章
- 调试多线程 & 查死锁的bug & gcore命令 & gdb对多线程的调试 & gcore & pstack & 调试常用命令
gdb thread apply all bt 如果你发现有那么几个栈停在 pthread_wait 或者类似调用上,大致就可以得出结论:就是它们几个儿女情长,耽误了整个进程. 注意gdb的版本要高于 ...
- 快速学习C语言四: 造轮子,ArrayList
高级语言里的列表是最常用的数据结构,在C里造个轮子玩玩,C没有泛型,先用int练习. Collection的ADT一般有hasnext,next,add, remove操作,List一般还加了remo ...
- Linux网络编程5——使用UDP协议实现群聊
引言 本文实现的功能类似于我之前所写的一篇博文(Linux之select系统调用_2),区别在于进程之间的通信方式有所不同.之前的文章中,我所使用的是管道,而本文我将会使用socket接口. 需求 客 ...
- GDB基本调试
调试时gcc -g -Wall -o Hello Hello.c gdb Hello -tui -g: 生成调试信息 -Wall: 编译器警告信息 -W: 警告信息 在调用GDB时,命令行指定-tui ...
- linux编程之线性表
#include"stdio.h" #define MAX 100 typedef struct List{ int length; int num[MAX]; }List_seq ...
- Pintos-斯坦福大学操作系统Project详解-Project1
转载请注明出处. 前言: 本实验来自斯坦福大学cs140课程,只限于教学用途,以下是他们对于Pintos系统的介绍: Pintos is a simple operating system fra ...
- 数据结构算法集---C++语言实现
//数据结构算法集---C++语言实现 //各种类都使用模版设计,可以对各种数据类型操作(整形,字符,浮点) /////////////////////////// // // // 堆栈数据结构 s ...
- gdb调试线程
gdb thread apply all bt 如果你发现有那么几个栈停在 pthread_wait 或者类似调用上,大致就可以得出结论:就是它们几个儿女情长,耽误了整个进程. 注意gdb的版本要高于 ...
- sort-桶排序
void list_insert(list<int> &t,int num) { auto iter=t.begin(); for(;iter!=t.end();++iter) { ...
随机推荐
- linux平台总线驱动设备模型之点亮LED
这一节里,我们来使用平台驱动设备这一套架构来实现我们之前使用简单的字符设备驱动点亮LED,这里并无实际意义,只是告诉大家如果编写平台总线驱动设备. 问:如何编写平台总线驱动设备这一套架构的设备驱动? ...
- HTML篇
要内容 web标准 浏览器介绍 开发工具介绍 HTML介绍 HTML颜色介绍 HTML规范 HTML结构详解 一.web标准 web准备介绍: w3c:万维网联盟组织,用来制定web标准的机构(组织) ...
- C++深度解析教程学习笔记(4)C++中的新成员
1. 动态内存分配 (1)C++通过 new 关键字进行动态内存申请,是以类型为单位来申请空间大小的 (2)delete 关键字用于内存释放 ▲注意释放数组时要加[],否则只释放这个数组中的第 1 个 ...
- Libevent使用例子,从简单到复杂
转载请注明出处:http://blog.csdn.net/luotuo44/article/details/39670221 本文从简单到复杂,展示如何使用libevent.网上的许多例子都是只有服务 ...
- Python编程总结之常用三方模块
1.excel读写 利用python进行excel读写是经常遇到的事情,最常用的excel读写模块必属xlrd和xlwt,前者负责读,后者负责写,配合起来可实现读写. 举例1):使用xlrd读取exc ...
- vbs获取html内容
Dim content,name,password,arr,pos msg1="请输入ip和端口号地址"&chr(13)&chr(10)&"如ht ...
- 读书笔记 Week4 2018-3-29
读书笔记 Week 4 <我是一只IT小小鸟> 首先不得不说,这周的个人编程任务占据了我绝大多数的精力.,虽然在接触到题目的第一时间就有了大致的思路,但当我真正上手开始编程的时候,却几乎每 ...
- 62-U型数字
https://nanti.jisuanke.com/t/20683 #include <iostream> using namespace std; int main(){ int ct ...
- Hyperledger Fabric Chaincode解析
首先看下Blockchain结构,除了header指向下一个block的hash value外,block是由一组transaction构成, Transactions --> Blocks - ...
- Luogu 2403 [SDOI2010]所驼门王的宝藏
BZOJ 1924 内存要算准,我MLE了两次. 建立$n + r + c$个点,对于一个点$i$的坐标为$(x, y)$,连边$(n + x, i)$和$(n + r + y, i)$,代表这一列和 ...