8-LinkedList

LinkedList和array的优缺点

Linked List Array
优点

1,they can adapt their ordering by changing the items they point to – no memory is wasted

3,extra links can easily be added

1,New elements can be stored anywhere and a reference is created for the new element using pointers.

2,Random accessing is possible.

3,Memory is allocated during the run-time (Dynamic memory allocation).

4,Array elements can be accessed randomly using the array index.

5,Insertion and Deletion operations are fast and easy.

缺点

1,With a linked list, if we need to find the special link, we have to start at the beginning of the chain, and work the way along in order to find an item

2,Binary search not possible – big disadvantage of linked lists

1. waste space

2. not easy to extend an array.

Size of the array must be specified at the time of array declaration/initialization.

3. to insert a new element is not easy

Linked List 头节点=空数据+第一个link的reference

![image1](../../assets/568e762370654efa9de462c60e2c7d61.png)

Double-Ended Linked Lists 头节点=first reference+last reference

![image2](../../assets/5c69c6a6cdf84ac4aa8d001b5bb5bc08.png)

Singly Linked List 无上述的头节点,直接第一个开始【data+reference】

![image3](../../assets/975f8c15382a4f7cbbaffa98a5185eb2.png)

Sorted Lists
Doubly Linked Lists 头节点=first reference+last reference

![image4](../../assets/0fb7f416c6cf43b39de1099d7894e302.png)

一、Linked List 1, A linked list is an abstract data structure consisting of a sequence of links image5

• Link objects are created for each link in the list • All contain references to the next link in the list

2,结构 image6 这里的first相当于head,头结点,不存储数据,只是一个指向头 this is only a reference to the next Link,does not represent the actual object itself

• Linked list only contains a reference to the first link • This is originally set to null image7

2,操作 • The head is the first link • The tail is the last link

Inserting at the Head

• 1. Create a new link

• 2. Have new link point to old first link

• 3. Update Linked List to point to new link

![image8](../../assets/39e46019d9384693a955d5d9fce12b9b.png)

![image9](../../assets/726b2a29273944e9ab4518fcfc76130f.png)

Deleting at the Head

• Update head to point to next node in the list

• Garbage collector will now reclaim the former first node

![image10](../../assets/cc2532f738994c2cb45a5ccbf2c3ffca.png)

![image11](../../assets/e3129a43976a4925b130167d506b3a76.png)

Traversing a linked list

必须从头开始遍历,

而且二分法不能用【大大滴缺点】

![image12](../../assets/d572582212d74a7aae80c5af7167c015.png)

find or delete one particular node

• Take in the value to be found or deleted

• Start at the beginning of the list

• Keep moving down the links until we find the correct

one

• All the while keep tracking the current link and the

previous link (so we can join them up when required)

• Now update the references to bypass the link to be

deleted

![image13](../../assets/a48c5b00afe94fa8b0a12e173ceebd8e.png)

![image14](../../assets/05eabdbb0b174309bb36cc889ca6ec0c.png)

二、Double-Ended Linked Lists 双头链表 1,• It as a reference to the last link as well as the first image15

2,操作 image16

Inserting at the Tail

• Create a new link

• Have new link point to null

• Have old last link point to new node

• Update tail of Linked List object to point to new node

![image17](../../assets/2bb440769d5b4e91ac1a88c86682361d.png)

Removing at the Tail

• Removing at the tail of a singly-linked double ended list

is not efficient!

• There is no constant time way to update the tail to point

to the previous node

• We need to use

double links (that point to both next and previous link)

![image18](../../assets/915939fcf0df438ea1ddf7cb24cf9bf8.png)

三、Linked-List Efficiency image19 用堆栈来实现均可 image20

四、Singly Linked List 1,Stack with a Singly Linked List 此时无头结点 • The top element is stored at the first node of the list • The space used is 𝑶(𝒏) and each operation of the Stack ADT takes 𝑶(𝟏) time image21

2,Queue with a Singly Linked List • The front element is stored at the first node • The rear element is stored at the last node image22

五、Sorted Lists priority queue – this required the items to be sorted 1,Only way to find the correct location is to search through the list – can’t use binary search • When you find the correct location (i.e. when you find a value bigger than the one you want to insert) update the pointers of the relevant links

2,操作

Insertion

The new element must be set to point to the next

element in the linked list

The previous element must be updated to point to

the new element

![image23](../../assets/b3646dda3e5647b4a789206fa9d939fd.png)

![image24](../../assets/bf1f18126d5146b594975b710edc88f4.png)

3、Efficiency of Sorted Linked Lists image25

4,Using linked lists for array sorting image26

六、Doubly Linked Lists 双链表 单链表不能backwards 1,A linked list where you can traverse it forwards or backwards is a doubly linked list (references going both ways)

现在是data+(next reference和prev reference)

操作的时候记得更新两个索引

list中,

first头结点的next是第一个link

第一个link的pre是空,

![image4](../../assets/0fb7f416c6cf43b39de1099d7894e302.png)

2,操作

Insertion at begining

– Change prev of first link

– Change next of new link

– Change first of Linked List

![image27](../../assets/66477d536ab1467b91254b9c02b79101.png)

![image28](../../assets/358332c23a274bdc894fc7e9df09d2b7.png)

Insertion in order

![image29](../../assets/4c1e11fdad5448b5a27b4cef3026aae9.png)

![image30](../../assets/19a72083e4264582a82f99e2f87c5af5.png)

![image31](../../assets/a27cd897b01944be9aa419b41ebfa9d9.png)
Deletion

– Change next of left link

– Change prev of right link

![image32](../../assets/197baa662b1a46ea9a750920aa5dd552.png)

![image33](../../assets/53066ab39b2c420580c1d0e61ca5f3f4.png)

七、Iterators[!!重点,看不懂]

1,• Iterators always point to some link in the list • They are associated with the list but are not part of it image34

2,Objects containing references to items in data structures, used to traverse these structures, are commonly called iterators image35

• If our linked list isn’t doubly-linked then the iterator should store both current and previous so that it can delete links • It is also handy for the iterator to store a reference to the linked list class so it can access the first element of the list image36

3,操作 image37

Removing a Link using the Iterator【注意】

![image38](../../assets/63b3fbbd13ba4b0fa665c6d9b209f837.png)

Adding a Link using the Iterator【注意】
image39
The atEnd() Method

![image40](../../assets/dd7db82b13d74eb6b5d4d6227cd150ea.png)

Stacks using Linked Lists

![image41](../../assets/5031cceb13a24429b17ea1b865ff0b7d.png)

Linked List代码

![image42](../../assets/7488efb5b5ec432581e29b7210a89354.png)

![image43](../../assets/b6ea8266c42a4bb188ac1a4c98bb8f19.png)

![image44](../../assets/ab19ac0b711344c6ace94c66676fd45e.png)

image45