1 / 18

List Data Structure

List Data Structure. What is List?. A list is a sequential data structure. It differs from the stack and queue data, structures in that additions and removals can be made at any position in the list. List operations. Add : adds a new node Set : update the contents of a node

lula
Download Presentation

List Data Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. List Data Structure

  2. What is List? • A list is a sequential data structure. • It differs from the stack and queue data, structures in that additions and removals can be made at any position in the list.

  3. List operations • Add : adds a new node • Set : update the contents of a node • Remove : removes a node • IsEmpty : reports whether the list is empty • IsFull : reports whether the list is full • Initialize : creates/initializes the list • Destroy : deletes the contents of the list (may be implemented by re-initializing the list)

  4. Illustration/example • Initialize(L) Create a new empty List named L • Add(1,X,L) adds the value X to list L at position 1 (the start of the list is position 0), shifting subsequent elements up • Set(2,Z,L) updates the values at position 2 to be Z • Remove(Z,L) removes the node with value Z • Get(2,L) returns the value of the third node, i.e. C • IndexOf(X,L) returns the index of the node with value X, i.e. 1

  5. Illustration/example List’s contents Return value Operation 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. Exercise: List Operation What would the contents of a list be after the following operations? Initialise(L) Add(0,A,L) Add(0,F,L) Add(1,X,L) Add(1,G,L) Add(3,P,L) Add(2,V,L) Set(3,K,L) Set(0,H,L) Remove(V,L) Remove(P,L) • H G K A What values would be returned by the following operations? IndexOf(A,L) IndexOf(H,L) Get(3,L)

  7. Storing a list in a static data structure (Array List) • This implementation stores the list in an array. • The position of each element is given by an index from 0 to n-1, where n is the number of elements. • 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. • 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. • 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.

  8. Storing a list in a dynamic data structure (Linked List) • The Link List is stored as a sequence of linked nodes. • 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. • The list can grow and shrink as needed • The position of each element is given by an index from 0 to n-1, where n is the number of elements. • 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. • 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. • The first node is accessed using the name LinkedList.Head • Its data is accessed using LinkedList.Head.DataItem • The second node is accessed using LinkedList.Head.NextNode

  9. Adding a node The new node is to be added at a specified index in the list A special case is that the list is empty. In this case, the first node is set to be the new node. Another special case is that the specified index is 0 (the node is to be added at the front of the list).

  10. Adding a node (2) In the general case, an object reference Probe moves through the list until the node before the required index is reached. Here, the node is to be added at index 2. 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. Adding a node (3)A possible algorithm (pseudocode) for the Add operation Add(index, Data, LinkedList) Declare NewNode and initialise NewNode.DataItem with data If(List.isEmpty) Set NewNode.NextNode to NULL Copy NewNode to LinkedList.Head Else If (index is 0) Copy LinkedList.Head to NewNode.NextNode Copy NewNode to LinkedList.Head Else Copy LinkedList.Head to Probe i = 0 While i < index-1 And end of list not reached Copy Probe.NextNode to Probe Increment i End While Copy Probe.NextNode to NewNode.NextNode Copy NewNode to Probe.NextNode End If End If Set NewNode to NULL Set Probe to NULL 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. Removing a node A TargetNode object is created. 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. 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. Removing a node (2)

  14. Accessing nodes (Get, Set, IndexOf, View) 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). IndexOf works in a similar way to Remove – Probe moves through the list until the target node is found, and returns the index. View – Probe moves through the list from beginning to end and the DataItem is returned at each node.

  15. Variations on Linked Lists Circularly linked lists The tail of the list always points to the head of the list 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. List Implementation

  17. List Implementation in Java • The Java Collections Framework in the most recent version of Java now includes list classes. • As you did for the stack & queue, you will create your own List class in order to learn how a list is implemented. • Your class will again be a bit simpler than the Collections Framework one but it will do essentially the same job • 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. • 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.

  18. List Implementation in Java Notice that the LinkedList class makes use of a Node class which is exactly the same as the one used for the dynamic queue.

More Related