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 ...
随机推荐
- 力扣(LeetCode)1016. 子串能表示从 1 到 N 数字的二进制串
给定一个二进制字符串 S(一个仅由若干 '0' 和 '1' 构成的字符串)和一个正整数 N,如果对于从 1 到 N 的每个整数 X,其二进制表示都是 S 的子串,就返回 true,否则返回 false ...
- 《剑指offer》第六十一题(扑克牌的顺子)
// 面试题61:扑克牌的顺子 // 题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的. // 2-10为数字本身,A为1,J为11,Q为12,K为13,而大.小王可以看成任意 ...
- 《剑指offer》第四十四题(数字序列中某一位的数字)
// 面试题44:数字序列中某一位的数字 // 题目:数字以0123456789101112131415…的格式序列化到一个字符序列中.在这 // 个序列中,第5位(从0开始计数)是5,第13位是1, ...
- python实战小程序之购物车
# Author:南邮吴亦凡 # 商品列表 product_list = [ ('Iphone',5800), # 逗号一定不可以省略! ('Mac',4800), ('smartphone',400 ...
- boke练习: springboot整合springSecurity出现的问题,传递csrf
boke练习: springboot整合springSecurity出现的问题,传递csrf freemarker模板 在html页面中加入: <input name="_csrf&q ...
- tornado web
tornado web frame: 非阻塞服务器,速度快,运用epoll 模板语言+render(),实现根据用户输入,自动渲染页面的动态效果. 在使用模板前需要在setting中设置模板路径: s ...
- Winform开发框架之框架演化
Winform开发框架方面的文章我介绍很多了,有宏观介绍,也有部分技术细节的交流,每次我希望能从不同角度,不同方面来介绍我的WInform开发框架,这些其实都是来源于客户的需求,真实的项目场景.本文主 ...
- C#使用 System.Net.Mail发送邮件功能
.NET 里包含了很多很丰富的邮件发送与接受的API在 System.Net.Mail命名空间里,使得我们开发发送和接受邮件相关功能变得简单,下面是一个简单发送邮件的功能: private void ...
- hdu6331 Walking Plan
题意: sol: 考虑floyed 直接暴力做的话复杂度是kn^3会炸. 考虑一个比较神仙的分块做法. 注意到我们是可以直接求单独某个k的矩阵,使用矩阵快速幂即可(取min的矩阵乘法). 单独求一次的 ...
- MySQL表类型和存储引擎
一.基本介绍 从事务安全性的角度,可以把存储引擎分为两大类: 事务安全: BDB和innodb; 事务非安全性: myisam 和 memory 二.存储引擎的比较图 看你的mysql当前默认的存储引 ...