数组和链表

The Linked List data structure

链表也是用来放数据的,是数组的进化版:
这里写图片描述

长这样的都是数据data:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

长这样的叫指针pointer:
这里写图片描述

数据和指针合在一起构成一个节点node:
这里写图片描述
特殊的有:头节点head,没有数据只有头指针
这里写图片描述
尾节点tail,有数据但尾指针置空
这里写图片描述

数组是连续存储全占用:
这里写图片描述

链表用了指针就可以跳了,跳着用,见缝插针比较节省。。。不是连续存储了:
这里写图片描述

Linked List:Inserting插入 a new node

int List::Insert ( const int x, const int i )

Insert a node with data equal to x after the i-1’th element. (i.e., when i = 1, insert the node as the first element; when index = 2, insert the node after the first element, and so on)
就是说插完了之后,x是不算空的头结点head的情况下,从第一个有数据和指针的节点往后查的第i个节点
If the insertion is successful, return 1.
Otherwise, return 0. (If index is < 1 or > length+1 of the list, the insertion will fail.)

Steps:
这里写图片描述
Locate定位 i-1’th element
Allocate分配 memory for the new node
Point the new node to its successor后继
Point the new node’s predecessor前身(就是i-1’th element)to the new node


Array versus对比 Linked Lists

Linked lists are more complex to code and management than arrays, but they have some distinct明显 advantages.
Dynamic动态: a linked list can easily grow and shrink收缩 in size.
-We don’t need to know how many nodes will be in the list. They are created in memory as needed.
-In contrast相比之下, the size of a C array is fixed固定的 at compilation time在编译时.
Easy and fast insertions插入 and deletions删除
-To insert or delete an element in an array, we need to copy to temporary临时 variables to make room for new elements or close the gap豁口 caused by deleted elements.
-With a linked list, no need to move other nodes. Only need to reset重置 some pointers.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值