Upload
katang-isip
View
178
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Basic info about how to create class of list.
Citation preview
List ADT
April 25, 2012
DefinitionA collection of contiguous elements or
itemsa1, a2, a3, …, an
n is the sizea2 precedes a3
a3 succeeds a2
DefinitionOperations
insertdeleteItem/eraseaccess
Humble Numbersvoid main(){
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++;
}
}
ImplementationArrayDynamic ArrayLinked-list
Array Implementation Array for storage Size
class 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);
};
Array Implementation
Append
10
Append
10 8
Append
10 8 -7
Append
bool List::append(int x){
if(size==MAX)
return false;
items[size++] = x;
return true;
}
Insert At Position 2
10 8 -7
Insert At Position 2
10 8 -7 -7
Insert At Position 2
10 8 8 -7
Insert At Position 2
10 23 8 -7
General Insert
bool 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;
}
Insert at Front
10 23 8 -7
Insert at Front
10 23 8 -7 -7
Insert at Front
10 23 8 8 -7
Insert at Front
10 23 23 8 -7
Insert at Front
10 10 23 8 -7
Insert at Front
49 10 23 8 -7
Insert Front
bool 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;
}
Delete Item at Position 2
49 10 23 8 -7
Delete Item at Position 2
49 23 23 8 -7
Delete Item at Position 2
49 23 8 8 -7
Delete Item at Position 2
49 23 8 -7 -7
DeleteItem/Erase
bool 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;
}
Appending 13
49 23 8 -7 -7
Appending 13
49 23 8 -7 13
Accessing an Item
49 23 8 -7 13
itemAt
int 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];
}
AdvantagesRunning time
Access
DownsideStatic storageSolution
Dynamic Array
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);
};
Constructor
List::List(){
items = new int[10];
size = 0;
}
List::~List(){
delete items;
}
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;
}
Linked List Implementation
9
Linked List Implementation
9 17
Linked List Implementation
9 1749
Linked List Implementation
-2 949 17
Memory Allocation (Heap)
0
32 17 NULL
64 9 32
96
128 49 182
150
182 -2 64
Linked List Implementation
class node{
public:
int item;
node *next;
node(int x){
item = x;
next = NULL;
}
};
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);
};
Constructor
List::List(){
head = tail = NULL;
size = 0;
}
Append
-2 949 17
23
Append
-2 949 17
23
Append
-2 949 17
23
Append
void List::append(int x);
node * n = new node(x);
tail->next = n;
tail = n;
size++;
}
Appendvoid List::append(int x);
node * n = new node(x);
if(size==0)
head = tail = n;
else{
tail->next = n;
tail = n;
}
size++;
}
insertFront
-2 949 17
23
103
insertFront
-2 949 17
23
103
insertFront
-2 949 17
23
103
insertFrontvoid List::insertFront(int x){
node *n = new node(x);
if(size==0)
head = tail = n;
else{
n->next = head;
head = n;
}
size++;
}
Insert at Position 6
-2 949 17
23
10367
Insert at Position 6
-2 949 17
23
10367
Insert at Position 6
-2 949 17
23
10367
Insert at Position 6
-2 949 17
23
10367
Insert at Position 6
-2 949 17
23
10367
Insert at Position 6
-2 949
23
17
10367
Insert at Position 6
-2 949
23
17
10367
Insert at Position 6
-2 949
23
17
10367
Insert at Position 6
-2 949
23
17
10367
Insert at Position 6
-2 949
23
17
10367
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;
}
Delete Item at Position 3
-2 949 17
Delete Item at Position 3
-2 949 17
Delete Item at Position 3
-2 949 17
Delete Item at Position 3
-2 949 17
Delete Item at Position 3
-2 949 17
Delete Item at Position 3
-2 949 17
Delete Item at Position 3
-2 949 17
Delete Item at Position 3
-249 17
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;
}
}
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.”
}
}