Upcoming SlideShare
×

Class list data structure

378 views

Published on

Basic info about how to create class of list.

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
378
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
7
0
Likes
0
Embeds 0
No embeds

No notes for slide

Class list data structure

1. 1. List ADTApril 25, 2012
2. 2. DefinitionA collection of contiguous elements or items a1, a2, a3, …, an n is the size a2 precedes a3 a3 succeeds a2
3. 3. DefinitionOperations insert deleteItem/erase access
4. 4. void main(){ Humble Numbers List<int> l; srand((unsigned int)time(NULL)); for(int i=0; i<10; i++) l.insert(rand()%50,rand()%10+1); int count=0; for(int i=1; i<=l.getSize(); i++){ int item = l.itemAt(i); bool flag = true; for(int cd=2; cd*cd<=item; cd++){ if(item%cd==0 && prime(cd) && cd>7){ flag = false; break; } } if(flag) count++; }}
6. 6. Array Implementation Array for storage Sizeclass List{private: int items[MAX]; int size;public: List(); bool append(int); bool insertFront(int); bool insert(int,int); bool erase(int); int itemAt(int);};
7. 7. Array Implementation
8. 8. Append10
9. 9. Append10 8
10. 10. Append10 8 -7
11. 11. Appendbool List::append(int x){ if(size==MAX) return false; items[size++] = x; return true;}
12. 12. Insert At Position 210 8 -7
13. 13. Insert At Position 210 8 -7 -7
14. 14. Insert At Position 210 8 8 -7
15. 15. Insert At Position 210 23 8 -7
16. 16. General Insertbool List::insert(int x, int pos){ if(pos<1 || pos > size+1 || size==MAX) return false; for(int i=size; i>pos-1; i--) items[i] = items[i-1]; items[pos-1] = x; size++; return true;}
17. 17. Insert at Front10 23 8 -7
18. 18. Insert at Front10 23 8 -7 -7
19. 19. Insert at Front10 23 8 8 -7
20. 20. Insert at Front10 23 23 8 -7
21. 21. Insert at Front10 10 23 8 -7
22. 22. Insert at Front49 10 23 8 -7
23. 23. Insert Frontbool List::insertFront(T x){ if(size==MAX) return false; for(int i=size; i>0; i--) items[i] = items[i-1]; items[0] = x; size++; return true;}
24. 24. Delete Item at Position 249 10 23 8 -7
25. 25. Delete Item at Position 249 23 23 8 -7
26. 26. Delete Item at Position 249 23 8 8 -7
27. 27. Delete Item at Position 249 23 8 -7 -7
28. 28. DeleteItem/Erasebool List::erase(int pos){ if(pos < 1 || pos > size) return false; for(int i=pos-1; i<size-1; i++) items[i] = items[i+1]; size--; return true;}
29. 29. Appending 1349 23 8 -7 -7
30. 30. Appending 1349 23 8 -7 13
31. 31. Accessing an Item49 23 8 -7 13
32. 32. itemAtint List::itemAt(int pos){ return items[pos-1];}int List::itemAt(int pos){ if(pos<1 || pos>size) throw “Invalid Position.”; return items[pos-1];}
34. 34. DownsideStatic storageSolution Dynamic Array
35. 35. Dynamic Array Implementationclass template<T>;class List{private: int *items; int size; void expand();public: List(); bool append(int); bool insertFront(int); bool insert(int,int); bool erase(int); int itemAt(int);};
36. 36. ConstructorList::List(){ items = new int[10]; size = 0;}List::~List(){ delete items;}
37. 37. Expandvoid List::expand(){ int *copy = new int[size]; for(int i=0; i<size; i++) copy[i] = items[i]; delete items; items = new int[size+10]; for(int i=0; i<size; i++) items[i] = copy[i]; delete copy;}
38. 38. Linked List Implementation 9
39. 39. Linked List Implementation 9 17
40. 40. Linked List Implementation 49 9 17
41. 41. Linked List Implementation49 -2 9 17
42. 42. Memory Allocation (Heap) 0 32 17 NULL 64 9 32 96 128 49 182 150 182 -2 64
43. 43. Linked List Implementationclass node{public: int item; node *next; node(int x){ item = x; next = NULL; }};
44. 44. Linked List Implementationclass List{private: node *head, *tail; int size;public: List(); ~List(); void append(int); void insertFront(int); bool insert(int,int); int itemAt(int); bool erase(int);};
45. 45. ConstructorList::List(){ head = tail = NULL; size = 0;}
46. 46. Append49 -2 9 17 23
47. 47. Append49 -2 9 17 23
48. 48. Append49 -2 9 17 23
49. 49. Appendvoid List::append(int x); node * n = new node(x); tail->next = n; tail = n; size++;}
50. 50. Appendvoid List::append(int x); node * n = new node(x); if(size==0) head = tail = n; else{ tail->next = n; tail = n; } size++;}
51. 51. insertFront 49 -2 9 17 23103
52. 52. insertFront 49 -2 9 17 23103
53. 53. insertFront 49 -2 9 17 23103
54. 54. insertFrontvoid List::insertFront(int x){ node *n = new node(x); if(size==0) head = tail = n; else{ n->next = head; head = n; } size++;}
55. 55. Insert at Position 6 49 -2 9 17 23103 67
56. 56. Insert at Position 6 49 -2 9 17 23103 67
57. 57. Insert at Position 6 49 -2 9 17 23103 67
58. 58. Insert at Position 6 49 -2 9 17 23103 67
59. 59. Insert at Position 6 49 -2 9 17 23103 67
60. 60. Insert at Position 6 49 -2 9 17 23103 67
61. 61. Insert at Position 6 49 -2 9 17 23103 67
62. 62. Insert at Position 6 49 -2 9 17 23103 67
63. 63. Insert at Position 6 49 -2 9 17 23103 67
64. 64. Insert at Position 6 49 -2 9 17 23103 67
65. 65. General Insertbool List::insert(int x, int pos){ if(pos < 1 || pos > size+1) return false; if(pos==1) insertFront(x); else if(pos==size+1) append(x); else{ node * tmp = head; for(int i=1; i<pos-1;i++) tmp = tmp->next; n->next = tmp->next; tmp->next = n; size++; } return true;}
66. 66. Delete Item at Position 349 -2 9 17
67. 67. Delete Item at Position 349 -2 9 17
68. 68. Delete Item at Position 349 -2 9 17
69. 69. Delete Item at Position 349 -2 9 17
70. 70. Delete Item at Position 349 -2 9 17
71. 71. Delete Item at Position 349 -2 9 17
72. 72. Delete Item at Position 349 -2 9 17
73. 73. Delete Item at Position 349 -2 17
74. 74. deleteItem/erasebool List::erase(int p){ if(p < 1 || p > size) return false; else{ node *del; if(pos==1){ del = head; head = head->next; if(head==NULL) tail = NULL; } else{ node * tmp = head; for(int i=1; i<pos-1;i++) tmp =tmp->next; del = tmp->next; tmp->next = del->next; if(del==tail) tail = tmp; } delete del; size--; return true; }}
75. 75. itemAtint List::itemAt(int pos){ try{ node *tmp=head; for(int i=1; i<pos; i++) tmp = tmp->next; return tmp->item; } catch(…){ throw “Invalid Position.” }}