Computer notes - List

636 views

Published on

We have designed the interface for the List; we now must consider how to implement that interface. Implementing Lists using an array: for example, the list of integers

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
636
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
34
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Computer notes - List

  1. 1. Class No.02 Data Structures http://ecomputernotes.com
  2. 2. Implementing Lists <ul><li>We have designed the interface for the List; we now must consider how to implement that interface. </li></ul>http://ecomputernotes.com
  3. 3. Implementing Lists <ul><li>We have designed the interface for the List; we now must consider how to implement that interface. </li></ul><ul><li>Implementing Lists using an array: for example, the list of integers (2, 6, 8, 7, 1) could be represented as: </li></ul>http://ecomputernotes.com A 6 8 7 1 1 2 3 4 5 2 current 3 size 5
  4. 4. List Implementation <ul><li>add(9); current position is 3. The new list would thus be: (2, 6, 8, 9, 7, 1) </li></ul><ul><li>We will need to shift everything to the right of 8 one place to the right to make place for the new element ‘9’. </li></ul>http://ecomputernotes.com current 3 size 5 step 1: A 6 8 7 1 1 2 3 4 5 2 6 current 4 size 6 step 2: A 6 8 7 1 1 2 3 4 5 2 6 9 notice: current points to new element
  5. 5. Implementing Lists <ul><li>next(): </li></ul>http://ecomputernotes.com current 4 size 6 A 6 8 7 1 1 2 3 4 5 2 6 9 5
  6. 6. Implementing Lists <ul><li>There are special cases for positioning the current pointer: </li></ul><ul><ul><ul><li>past the last array cell </li></ul></ul></ul><ul><ul><ul><li>before the first cell </li></ul></ul></ul>http://ecomputernotes.com
  7. 7. Implementing Lists <ul><li>There are special cases for positioning the current pointer: </li></ul><ul><ul><ul><li>past the last array cell </li></ul></ul></ul><ul><ul><ul><li>before the first cell </li></ul></ul></ul><ul><li>We will have to worry about these when we write the actual code. </li></ul>http://ecomputernotes.com
  8. 8. Implementing Lists <ul><li>remove(): removes the element at the current index </li></ul>http://ecomputernotes.com current 5 size 6 A 6 8 1 1 2 3 4 5 2 6 9 5 Step 1: current 5 size 5 A 6 8 1 1 2 3 4 5 2 9 Step 2:
  9. 9. Implementing Lists <ul><li>remove(): removes the element at the current index </li></ul><ul><li>We fill the blank spot left by the removal of 7 by shifting the values to the right of position 5 over to the left one space. </li></ul>current 5 A 6 8 1 1 2 3 4 5 2 9 Step 2: size 5 current 5 size 6 A 6 8 1 1 2 3 4 5 2 6 9 5 Step 1:
  10. 10. Implementing Lists <ul><li>find(X): traverse the array until X is located. int find(int X) { int j; for(j=1; j < size+1; j++ ) if( A[j] == X ) break; if( j < size+1 ) { // found X current = j; // current points to where X found </li></ul><ul><li> return 1; // 1 for true } return 0; // 0 (false) indicates not found </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
  11. 11. Implementing Lists <ul><li>Other operations: get()  return A[current]; update(X)  A[current] = X; length()  return size; back()  current--; start()  current = 1; end()  current = size; </li></ul>http://ecomputernotes.com
  12. 12. Analysis of Array Lists <ul><li>add </li></ul><ul><ul><li>we have to move every element to the right of current to make space for the new element. </li></ul></ul><ul><ul><li>Worst-case is when we insert at the beginning; we have to move every element right one place. </li></ul></ul><ul><ul><li>Average-case: on average we may have to move half of the elements </li></ul></ul>http://ecomputernotes.com
  13. 13. Analysis of Array Lists <ul><li>remove </li></ul><ul><ul><li>Worst-case: remove at the beginning, must shift all remaining elements to the left. </li></ul></ul><ul><ul><li>Average-case: expect to move half of the elements. </li></ul></ul><ul><li>find </li></ul><ul><ul><li>Worst-case: may have to search the entire array </li></ul></ul><ul><ul><li>Average-case: search at most half the array. </li></ul></ul><ul><li>Other operations are one-step. </li></ul>http://ecomputernotes.com
  14. 14. List Using Linked Memory <ul><li>Various cells of memory are not allocated consecutively in memory. </li></ul>http://ecomputernotes.com
  15. 15. List Using Linked Memory <ul><li>Various cells of memory are not allocated consecutively in memory. </li></ul><ul><li>Not enough to store the elements of the list. </li></ul>http://ecomputernotes.com
  16. 16. List Using Linked Memory <ul><li>Various cells of memory are not allocated consecutively in memory. </li></ul><ul><li>Not enough to store the elements of the list. </li></ul><ul><li>With arrays, the second element was right next to the first element. </li></ul>http://ecomputernotes.com
  17. 17. List Using Linked Memory <ul><li>Various cells of memory are not allocated consecutively in memory. </li></ul><ul><li>Not enough to store the elements of the list. </li></ul><ul><li>With arrays, the second element was right next to the first element. </li></ul><ul><li>Now the first element must explicitly tell us where to look for the second element. </li></ul>http://ecomputernotes.com
  18. 18. List Using Linked Memory <ul><li>Various cells of memory are not allocated consecutively in memory. </li></ul><ul><li>Not enough to store the elements of the list. </li></ul><ul><li>With arrays, the second element was right next to the first element. </li></ul><ul><li>Now the first element must explicitly tell us where to look for the second element. </li></ul><ul><li>Do this by holding the memory address of the second element </li></ul>http://ecomputernotes.com
  19. 19. Linked List <ul><li>Create a structure called a Node . </li></ul><ul><li>The object field will hold the actual list element. </li></ul><ul><li>The next field in the structure will hold the starting location of the next node. </li></ul><ul><li>Chain the nodes together to form a linked list. </li></ul>http://ecomputernotes.com object next
  20. 20. Linked List <ul><li>Picture of our list (2, 6, 7, 8, 1) stored as a linked list: </li></ul>http://ecomputernotes.com 2 6 8 7 1 head current size=5
  21. 21. Linked List <ul><li>Note some features of the list: </li></ul><ul><li>Need a head to point to the first node of the list. Otherwise we won’t know where the start of the list is. </li></ul>http://ecomputernotes.com
  22. 22. Linked List <ul><li>Note some features of the list: </li></ul><ul><li>Need a head to point to the first node of the list. Otherwise we won’t know where the start of the list is. </li></ul><ul><li>The current here is a pointer, not an index. </li></ul>http://ecomputernotes.com
  23. 23. Linked List <ul><li>Note some features of the list: </li></ul><ul><li>Need a head to point to the first node of the list. Otherwise we won’t know where the start of the list is. </li></ul><ul><li>The current here is a pointer, not an index. </li></ul><ul><li>The next field in the last node points to nothing . We will place the memory address NULL which is guaranteed to be inaccessible. </li></ul>http://ecomputernotes.com
  24. 24. Linked List <ul><li>Actual picture in memory: </li></ul>1051 1052 1055 1059 1060 1061 1062 1063 1064 1056 1057 1058 1053 1054 2 6 8 7 1 1051 1063 1057 1060 0 head 1054 1063 current 6 8 7 1 head current 1065 http://ecomputernotes.com 2

×