Upcoming SlideShare
×

# List Data Structure

32,730 views

Published on

Penjelasan mengenai list

Published in: Education, Technology, Sports
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• great

Are you sure you want to  Yes  No
• nice presentations

Are you sure you want to  Yes  No
• great material....

Are you sure you want to  Yes  No
Views
Total views
32,730
On SlideShare
0
From Embeds
0
Number of Embeds
860
Actions
Shares
0
840
3
Likes
2
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>