Upcoming SlideShare
Loading in …5
×

# List data structure

1,841 views
1,755 views

Published on

1 Comment
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• v good slide tutorial

Are you sure you want to  Yes  No
Your message goes here
No Downloads
Views
Total views
1,841
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
57
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

### List data structure

1. 1. List Data Structure
2. 2. What is List? <ul><li>A list is a sequential data structure. </li></ul><ul><li>It differs from the stack and queue data, structures in that additions and removals can be made at any position in the list. </li></ul>
3. 3. List operations <ul><li>Add : adds a new node </li></ul><ul><li>Set : update the contents of a node </li></ul><ul><li>Remove : removes a node </li></ul><ul><li>IsEmpty : reports whether the list is empty </li></ul><ul><li>IsFull : reports whether the list is full </li></ul><ul><li>Initialize : creates/initializes the list </li></ul><ul><li>Destroy : deletes the contents of the list (may be implemented by re-initializing the list) </li></ul>
4. 4. Illustration/example <ul><li>Initialize(L) </li></ul><ul><li>Create a new empty List named L </li></ul><ul><li>Add(1,X,L) </li></ul><ul><li>adds the value X to list L at position 1 (the start of the list is position 0), shifting subsequent elements up </li></ul><ul><li>Set(2,Z,L) updates the values at position 2 to be Z </li></ul><ul><li>Remove(Z,L) removes the node with value Z </li></ul><ul><li>Get(2,L) returns the value of the third node, i.e. C </li></ul><ul><li>IndexOf(X,L) </li></ul><ul><li>returns the index of the node with value X, i.e. 1 </li></ul>
5. 5. Illustration/example <ul><li>Operation </li></ul>List’s contents Return value 1. Initialiaze(L) <empty> - 2. Add(0,A,L) A - 3. Add(0,B,L) B A - 4. Add(1,C,L) B C A - 5. Set(1,N,L) B N A - 6. Add(1,D,L) B D N A - 7. Remove(A,L) B D N A 8. Set(3,I,L) B D N I - 9. Remove(D,L) B N I D 10. Remove(N,L) B I N
6. 6. Exercise: List Operation <ul><li>What would the contents of a list be after the following operations? </li></ul><ul><li>Initialise(L) </li></ul><ul><li>Add(0,A,L) </li></ul><ul><li>Add(0,F,L) </li></ul><ul><li>Add(1,X,L) </li></ul><ul><li>Add(1,G,L) </li></ul><ul><li>Add(3,P,L) </li></ul><ul><li>Add(2,V,L) </li></ul><ul><li>Set(3,K,L) </li></ul><ul><li>Set(0,H,L) </li></ul><ul><li>Remove(V,L) </li></ul><ul><li>Remove(P,L) </li></ul><ul><li>H G K A </li></ul><ul><li>What values would be returned by the following operations? </li></ul><ul><li>IndexOf(A,L) </li></ul><ul><li>IndexOf(H,L) </li></ul><ul><li>Get(3,L) </li></ul>
7. 7. Storing a list in a static data structure (Array List) <ul><li>This implementation stores the list in an array. </li></ul><ul><li>The position of each element is given by an index from 0 to n-1, where n is the number of elements. </li></ul><ul><li>Given any index, the element with that index can be accessed in constant time – i.e. the time to access does not depend on the size of the list. </li></ul><ul><li>To add an element at the end of the list, the time taken does not depend on the size of the list. However, the time taken to add an element at any other point in the list does depend on the size of the list, as all subsequent elements must be shifted up. Additions near the start of the list take longer than additions near the middle or end. </li></ul><ul><li>When an element is removed, subsequent elements must be shifted down, so removals near the start of the list take longer than removals near the middle or end. </li></ul>
8. 8. Storing a list in a dynamic data structure (Linked List) <ul><li>The Link List is stored as a sequence of linked nodes. </li></ul><ul><li>As in the case of the stack and the queue, each node in a linked list contains data AND a reference to the next node. </li></ul><ul><li>The list can grow and shrink as needed </li></ul><ul><li>The position of each element is given by an index from 0 to n-1, where n is the number of elements. </li></ul><ul><li>Given any index, the time taken to access an element with that index depends on the index. This is because each element of the list must be traversed until the required index is found. </li></ul><ul><li>The time taken to add an element at any point in the list does not depend on the size of the list, as no shifts are required. It does, however, depend on the index. Additions near the end of the list take longer than additions near the middle or start. The same applies to the time taken to remove an element. </li></ul><ul><li>The first node is accessed using the name LinkedList.Head </li></ul><ul><li>Its data is accessed using LinkedList.Head.DataItem </li></ul><ul><li>The second node is accessed using LinkedList.Head.NextNode </li></ul>
9. 9. Adding a node  The new node is to be added at a specified index in the list <ul><li>A special case is that the list is empty . In this case, the first node is set to be the new node. </li></ul>Another special case is that the specified index is 0 (the node is to be added at the front of the list ).
10. 10. Adding a node (2) <ul><li>In the general case, an object reference Probe moves through the list until the node </li></ul><ul><li>before the required index is reached. Here, the node is to be added at index 2. </li></ul>If the new node is to be added at the end of the list, then the NextNode of the last element is set to refer to the new node.
11. 11. Adding a node (3)  A possible algorithm (pseudocode) for the Add operation <ul><li>Add (index, Data, LinkedList) </li></ul><ul><li>Declare NewNode and initialise NewNode.DataItem with data </li></ul><ul><li>If(List.isEmpty) </li></ul><ul><li>Set NewNode.NextNode to NULL </li></ul><ul><li>Copy NewNode to LinkedList.Head </li></ul><ul><li>Else </li></ul><ul><li>If (index is 0) </li></ul><ul><li>Copy LinkedList.Head to NewNode.NextNode </li></ul><ul><li>Copy NewNode to LinkedList.Head </li></ul><ul><li>Else </li></ul><ul><li>Copy LinkedList.Head to Probe </li></ul><ul><li>i = 0 </li></ul><ul><li>While i < index-1 And end of list not reached </li></ul><ul><li> Copy Probe.NextNode to Probe </li></ul><ul><li> Increment i </li></ul><ul><li>End While </li></ul><ul><li>Copy Probe.NextNode to NewNode.NextNode </li></ul><ul><li>Copy NewNode to Probe.NextNode </li></ul><ul><li>End If </li></ul><ul><li>End If </li></ul><ul><li>Set NewNode to NULL </li></ul><ul><li>Set Probe to NULL </li></ul>Additional operations can also be implemented to add to the beginning and end of the list. AddFirst (Data, LinkedList) calls Add(0, Data, LinkedList) AddLast (Data, LinkedList) calls Add(Size, LinkedList), where Size is itself a call to the Size operation of the list.
12. 12. Removing a node <ul><li>A TargetNode object is created. </li></ul><ul><li>There is a special case when TargetNode is equal to the first node. In this case, LinkedList is set to point to the second node. </li></ul>In the general case, an object reference Probe moves through the list until the required node is reached – at each node the current node (Probe) is compared with TargetNode. A Previous object reference is also required to keep track of the predecessor of the node to be removed. A special case is when both references point to the same node – this happens when the first node is to be deleted. To delete the current node we can set Previous.NextNode to be equal to Probe.NextNode.
13. 13. Removing a node (2)
14. 14. Accessing nodes (Get, Set, IndexOf, View) <ul><li>Get and Set work in a similar way to Add – Probe moves through the list until the required index is reached, and the DataItem is returned ( Get ) or updated ( Set ). </li></ul><ul><li>IndexOf works in a similar way to Remove – Probe moves through the list until the target node is found, and returns the index. </li></ul><ul><li>View – Probe moves through the list from beginning to end and the DataItem is returned at each node. </li></ul>
15. 15. Variations on Linked Lists <ul><li>Circularly linked lists </li></ul><ul><li>The tail of the list always points to the head of the list </li></ul>Doubly linked lists These permit scanning or searching of the list in both directions. (To go backwards in a simple list, it is necessary to go back to the start and scan forwards.) In this case, the node structure is altered to have two links: Sorted lists Lists can be designed to be maintained in a given order. In this case, the Add method will search for the correct place in the list to insert a new data item.
16. 16. List Implementation
17. 17. List Implementation in Java <ul><li>The Java Collections Framework in the most recent version of Java now includes list classes. </li></ul><ul><li>As you did for the stack & queue, you will create your own List class in order to learn how a list is implemented. </li></ul><ul><li>Your class will again be a bit simpler than the Collections Framework one but it will do essentially the same job </li></ul><ul><li>In this case you will look at Java implementations of an ArrayList and a LinkedList . Both lists have the same operations, so we can define a class List which includes minimal implementations of the operations. The ArrayList and LinkedList classes will be subclasses of List . </li></ul><ul><li>The List class will define the set of operations which a list class must have. This is sometimes known as the interface of a list. Both kinds of list will have the same interface. </li></ul>
18. 18. List Implementation in Java <ul><li>Notice that the LinkedList class makes use of a Node class which is exactly the same as the one used for the dynamic queue. </li></ul>