Top Banner
List ADT April 25, 2012
75

Class list data structure

Jun 23, 2015

Download

Education

Katang Isip

Basic info about how to create class of list.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Class list data structure

List ADT

April 25, 2012

Page 2: Class list data structure

DefinitionA collection of contiguous elements or

itemsa1, a2, a3, …, an

n is the sizea2 precedes a3

a3 succeeds a2

Page 3: Class list data structure

DefinitionOperations

insertdeleteItem/eraseaccess

Page 4: Class list data structure

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++;

}

}

Page 5: Class list data structure

ImplementationArrayDynamic ArrayLinked-list

Page 6: Class list data structure

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);

};

Page 7: Class list data structure

Array Implementation

Page 8: Class list data structure

Append

10

Page 9: Class list data structure

Append

10 8

Page 10: Class list data structure

Append

10 8 -7

Page 11: Class list data structure

Append

bool List::append(int x){

if(size==MAX)

return false;

items[size++] = x;

return true;

}

Page 12: Class list data structure

Insert At Position 2

10 8 -7

Page 13: Class list data structure

Insert At Position 2

10 8 -7 -7

Page 14: Class list data structure

Insert At Position 2

10 8 8 -7

Page 15: Class list data structure

Insert At Position 2

10 23 8 -7

Page 16: Class list data structure

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;

}

Page 17: Class list data structure

Insert at Front

10 23 8 -7

Page 18: Class list data structure

Insert at Front

10 23 8 -7 -7

Page 19: Class list data structure

Insert at Front

10 23 8 8 -7

Page 20: Class list data structure

Insert at Front

10 23 23 8 -7

Page 21: Class list data structure

Insert at Front

10 10 23 8 -7

Page 22: Class list data structure

Insert at Front

49 10 23 8 -7

Page 23: Class list data structure

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;

}

Page 24: Class list data structure

Delete Item at Position 2

49 10 23 8 -7

Page 25: Class list data structure

Delete Item at Position 2

49 23 23 8 -7

Page 26: Class list data structure

Delete Item at Position 2

49 23 8 8 -7

Page 27: Class list data structure

Delete Item at Position 2

49 23 8 -7 -7

Page 28: Class list data structure

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;

}

Page 29: Class list data structure

Appending 13

49 23 8 -7 -7

Page 30: Class list data structure

Appending 13

49 23 8 -7 13

Page 31: Class list data structure

Accessing an Item

49 23 8 -7 13

Page 32: Class list data structure

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];

}

Page 33: Class list data structure

AdvantagesRunning time

Access

Page 34: Class list data structure

DownsideStatic storageSolution

Dynamic Array

Page 35: Class list data structure

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);

};

Page 36: Class list data structure

Constructor

List::List(){

items = new int[10];

size = 0;

}

List::~List(){

delete items;

}

Page 37: Class list data structure

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;

}

Page 38: Class list data structure

Linked List Implementation

9

Page 39: Class list data structure

Linked List Implementation

9 17

Page 40: Class list data structure

Linked List Implementation

9 1749

Page 41: Class list data structure

Linked List Implementation

-2 949 17

Page 42: Class list data structure

Memory Allocation (Heap)

0

32 17 NULL

64 9 32

96

128 49 182

150

182 -2 64

Page 43: Class list data structure

Linked List Implementation

class node{

public:

int item;

node *next;

node(int x){

item = x;

next = NULL;

}

};

Page 44: Class list data structure

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);

};

Page 45: Class list data structure

Constructor

List::List(){

head = tail = NULL;

size = 0;

}

Page 46: Class list data structure

Append

-2 949 17

23

Page 47: Class list data structure

Append

-2 949 17

23

Page 48: Class list data structure

Append

-2 949 17

23

Page 49: Class list data structure

Append

void List::append(int x);

node * n = new node(x);

tail->next = n;

tail = n;

size++;

}

Page 50: Class list data structure

Appendvoid List::append(int x);

node * n = new node(x);

if(size==0)

head = tail = n;

else{

tail->next = n;

tail = n;

}

size++;

}

Page 51: Class list data structure

insertFront

-2 949 17

23

103

Page 52: Class list data structure

insertFront

-2 949 17

23

103

Page 53: Class list data structure

insertFront

-2 949 17

23

103

Page 54: Class list data structure

insertFrontvoid List::insertFront(int x){

node *n = new node(x);

if(size==0)

head = tail = n;

else{

n->next = head;

head = n;

}

size++;

}

Page 55: Class list data structure

Insert at Position 6

-2 949 17

23

10367

Page 56: Class list data structure

Insert at Position 6

-2 949 17

23

10367

Page 57: Class list data structure

Insert at Position 6

-2 949 17

23

10367

Page 58: Class list data structure

Insert at Position 6

-2 949 17

23

10367

Page 59: Class list data structure

Insert at Position 6

-2 949 17

23

10367

Page 60: Class list data structure

Insert at Position 6

-2 949

23

17

10367

Page 61: Class list data structure

Insert at Position 6

-2 949

23

17

10367

Page 62: Class list data structure

Insert at Position 6

-2 949

23

17

10367

Page 63: Class list data structure

Insert at Position 6

-2 949

23

17

10367

Page 64: Class list data structure

Insert at Position 6

-2 949

23

17

10367

Page 65: Class list data structure

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;

}

Page 66: Class list data structure

Delete Item at Position 3

-2 949 17

Page 67: Class list data structure

Delete Item at Position 3

-2 949 17

Page 68: Class list data structure

Delete Item at Position 3

-2 949 17

Page 69: Class list data structure

Delete Item at Position 3

-2 949 17

Page 70: Class list data structure

Delete Item at Position 3

-2 949 17

Page 71: Class list data structure

Delete Item at Position 3

-2 949 17

Page 72: Class list data structure

Delete Item at Position 3

-2 949 17

Page 73: Class list data structure

Delete Item at Position 3

-249 17

Page 74: Class list data structure

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;

}

}

Page 75: Class list data structure

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.”

}

}