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 ...
随机推荐
- 学习笔记58—3D杯子设计
软件下载:http://www.i3done.com/ 界面如下: 3D杯子设计步骤(参考:http://www.i3done.com/news/video/402.html): 生成杯体 1.点击基 ...
- 用友软件系统管理员账号admin密码忘记了,如何解决?
1.打开数据库. 2.点开 数据库-UFSystem. 3.找到dbo.UA_user表,鼠标右键,点打开表. 4.打开后,找到admin,cPassword列即可找到系统管理员密码.
- Go使用protobuf
WIN7 + Go1.9.2+protobuf3.5.1 1.首先定义一个用于测试的proto文件test.proto,内容如下: syntax = "proto3"; packa ...
- php oracle数据库NCOLB字段ORA-01704
php oracle数据库NCOLB字段ORA-01704 对clob更新 ORA-01704: 字符串文字太长 解决办法:把字符赋值给一个变量,然后赋值update语句 declarev_clob ...
- SVN图标各种标注
黄色感叹号(有冲突):--这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别人 ...
- eclipse---->error and exception 和常用配置
1.eclipse打开后报错:An internal error occurred during: "Initializing Java Tooling". java.lang.N ...
- 中标麒麟Linux7 如何关闭广播消息
chmod 600 /usr/libexec/sysnotify pkill -9 sysnotify
- ssh登陆报错:packet_write_wait: Connection to x.x.x.x port 22: Broken pipe
ssh登陆报错:packet_write_wait: Connection to x.x.x.x port 22: Broken pipe 参考文章: https://patrickmn.com/as ...
- 『TensorFlow』SSD源码学习_其六:标签整理
Fork版本项目地址:SSD 一.输入标签生成 在数据预处理之后,图片.类别.真实框格式较为原始,不能够直接作为损失函数的输入标签(ssd向前网络只需要图像就行,这里的处理主要需要满足loss的计算) ...
- linux的越墙方法
.首先要安装openSSH, Ubuntu缺省没有安装SSH Server,使用以下命令安装: sudo apt-get install openssh-server 但是系统有时候会出现E类错误,无 ...