数组和链表

The Linked List data structure

链表也是用来放数据的,是数组的进化版:
数组和链表_第1张图片

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

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

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

数组是连续存储全占用:
数组和链表_第2张图片

链表用了指针就可以跳了,跳着用,见缝插针比较节省。。。不是连续存储了:
数组和链表_第3张图片

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:
数组和链表_第4张图片
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.

你可能感兴趣的:(数据结构,链表,指针)