July_One_Week—linked list
#include <stdio.h>
#include <stdlib.h> typedef struct linklist
{
unsigned int count;
struct linklist *next;
}linklist; linklist* CreatLinklist(int len)
{
linklist *head = NULL, *pre = NULL;
head = (linklist*)malloc(sizeof(linklist));//头指针。
pre = (linklist*)malloc(sizeof(linklist)); //pre-previous, 前一个;相对于head来说是后一个。
printf("Please input the data:\n");
scanf("%u", &pre->count);
head->next = pre; //head的后一个是pre。
pre->next = NULL; //pre的next是NULL。 for (int i = ; i < len; i++)
{
linklist* tmp = (linklist*)malloc(sizeof(linklist));
scanf("%u", &tmp->count);
pre->next = tmp; //新加的节点放在pre后。
tmp->next = NULL; //新加节点的next是NULL。
pre = tmp; //新加的节点变成pre,等待下一个tmp。
}
return head;
} int Outputlinklist(linklist *head)
{
printf("#### Output linklist.\n");
if (head != NULL)
{
while (head->next != NULL)
{
printf("%u\n", head->next->count);
head = head->next;
}
}
} int Lenlinklist(linklist *head)
{
int len = ;
if (NULL == head->next)
return len;
len++;
linklist *p = NULL;
p = head->next;
while(p->next != NULL)
{
len++;
p = p->next;
}
return len;
} int InsertLinklist(linklist *head, int index)
{
int len = ;
len = Lenlinklist(head);
printf("the length of linklist:%d, the insert index:%d\n", len, index);
if (index < || index > len)
return -;
int cur_index = ;
linklist *p = NULL;
p = head->next;
while(cur_index < index)
{
p = p->next;
cur_index++;
}
linklist *node = (linklist*)malloc(sizeof(linklist));
printf("Please input the new node data:\n");
scanf("%u", &node->count);
node->next = p->next;
p->next = node;
return ;
} int DeleteNode(linklist *head, int index)
{
int len = ;
len = Lenlinklist(head); if((index < ) || (index > len))
return -; int cur_index = ;
linklist *p = NULL;
linklist *q = NULL;
p = head->next;
while(cur_index < index)
{
p = p->next;
cur_index++;
}
q = p->next;
p->next = q->next;
free(q);
q = NULL;
} linklist *Reverselinklist(linklist *head)
{
linklist *p=NULL, *pre=NULL, *pnext=NULL; p = head->next;
while(p!=NULL)
{
pnext = p->next;
if(pnext == NULL)
head->next = p;
p->next = pre;
pre = p;
p = pnext;
}
return head;
} int main(void)
{
linklist *mylist = NULL; printf("#### Creat linklist.\n");
mylist = CreatLinklist();
Outputlinklist(mylist); printf("#### Insert new node.\n");
InsertLinklist(mylist, );
Outputlinklist(mylist); printf("#### Delete exist node.\n");
DeleteNode(mylist, );
Outputlinklist(mylist); printf("#### Reverse Linklist.\n");
Reverselinklist(mylist);
Outputlinklist(mylist); printf("Handle complete!");
}
1.线性表的链式存储和顺序存储的比较。
常见的数组就是线性表的顺序存储,各个结点之间被分配连续的物理内存,因此在查找上直接可以使用下标,速度很快,但是对于插入和删除操作需要在操作点之后执行移动操作,较为麻烦,并且数组在之前需要分配内存的大小,也就是需要我们知道结点的数量,这样就有一定的限制。
链表就是线性表的链式存储,各个结点之间在物理内存中是随机分配的,在查找时需要逐个遍历,但是在插入和删除操作时仅仅是局部性的指针切换,并且链表只有创建结点和删除结点时才会对内存进行操作,事先不需要知道结点数量。
2.单向链表属于线性表链式存储中最简单的一类,常见的操作包括:创建、删除、查找、插入、反序等。
July_One_Week—linked list的更多相关文章
- [LeetCode] Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [LeetCode] Plus One Linked List 链表加一运算
Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...
- [LeetCode] Odd Even Linked List 奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- [LeetCode] Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- 百万并发中间件系统的内核设计看Java并发性能优化
“ 这篇文章,给大家聊聊一个百万级并发的中间件系统的内核代码里的锁性能优化. 很多同学都对Java并发编程很感兴趣,学习了很多相关的技术和知识.比如volatile.Atomic.synchroniz ...
- vue中eslintrc.js配置最详细介绍
本文是对vue项目中自带文件eslintrc.js的内容解析, 介绍了各个eslint配置项的作用,以及为什么这样设置. 比较详细,看完能对eslint有较为全面的了解,基本解除对该文件的疑惑. /* ...
- Windows上安装运行hadoop
0.自己编译安装步骤在这里,有英文版本连接:<英文传送门>. 自己编译尝试后不成功,换为下面使用别人编译好的版本的方法.参考博客:<初学hadoop,windows下安装> 1 ...
- 实例对比 hibernate, spring data jpa, mybatis 选型参考
原文: 最近重构以前写的服务,最大的一个变动是将mybatis切换为spring data jpa,切换的原因很简单,有两点:第一.它是spring的子项目能够和spring boot很好的融合,没有 ...
- FireFox(火狐)浏览器的相关问题
如何加快FireFox(火狐)浏览器浏览网页速度 大部分网页加载缓慢的原因:1.宽带连接.网速不稳定2.浏览器本身问题,如果多开窗口浏览会占大量内存,而且磁盘空间没有做过优化,就这样电脑资源不够用,也 ...
- 记一次排查局网内的ARP包 “不存在的” MAC 地址及 “不存在的”IP 所发的ARP包
xu言: 最近生了一场病,虽然不是给自己找理由不写.不过果不其然还是没有坚持每天发一篇啊.不过,有时间我还是会把一些有意思的事情记录下来.以作备忘吧.这人老了记性就不好了.哈哈哈,当然,也侧面说明了. ...
- 基于BindingSource的WinForm开发
BindingSource控件介绍 BindingSource控件介绍 BindingSource控件是.NET Framework 2.0提供的新控件之一.BindingSource控件与数据源建立 ...
- ubuntu+anaconda
1.下载anaconda 查看ubuntu是32位还是64位 命令: uname -m 如果显示i686,你安装了32位操作系统 如果显示 x86_64,你安装了64位操作系统 uname -a 查看 ...
- Numpy常用API
目录 一.输入和输出 1.1 NumPy二进制文件(NPY,NPZ) 1.2 文本文件 1.3 正则表达式解析 1.4 原始二进制文件 1.5 内存映射文件 1.6 Base-n相关 1.7 数据源 ...
- SWUST OJ(952)
单链表的插入操作实现 #include <stdio.h> #include <stdlib.h> typedef struct LinkList { int data; st ...