Upcoming SlideShare
×

Like this presentation? Why not share!

# List Data Structure

## on Sep 30, 2008

• 28,853 views

Penjelasan mengenai list

Penjelasan mengenai list

### Views

Total Views
28,853
Views on SlideShare
28,459
Embed Views
394

Likes
1
578
3

### 9 Embeds394

 http://www.ustudy.in 328 http://www.slideshare.net 24 http://akuul.miniih.com 22 http://ustudy.in 6 http://kurdtech.blogspot.com 5 http://www.brijj.com 4 http://translate.googleusercontent.com 2 http://static.slidesharecdn.com 2 http://www.kurdtech.blogspot.com 1
More...

### Report content

• Full Name Comment goes here.
Are you sure you want to
• great
Are you sure you want to
• nice presentations
Are you sure you want to
• great material....
Are you sure you want to

## List Data StructurePresentation Transcript

• 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
• 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)
• Illustration/example
• Initialize(L)
• Create a new empty List named 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
• Illustration/example
• Operation
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
• Exercise: List Operation
• What would the contents of a list be after the following operations?
• Initialise(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)
• 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.
• 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.
• 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 ).
• 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.
• Adding a node (3)  A possible algorithm (pseudocode) for the Add operation
• Declare NewNode and initialise NewNode.DataItem with data
• If(List.isEmpty)
• Set NewNode.NextNode to NULL
• Else
• If (index is 0)
• Else
• 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
• 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.
• Removing a node (2)
• 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.