SlideShare a Scribd company logo
List ADT

April 25, 2012
Definition
A collection of contiguous elements or
 items
  a1, a2, a3, …, an
  n is the size
  a2 precedes a3
  a3 succeeds a2
Definition
Operations
  insert
  deleteItem/erase
  access
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++;
         }
}
Implementation
Array
Dynamic Array
Linked-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];
}
Advantages
Running time
  Access
Downside
Static storage
Solution
  Dynamic Array
Dynamic Array Implementation
class 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;
}
Expand
void 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


 49      9      17
Linked List Implementation


49     -2     9      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 Implementation
class 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


49   -2        9   17



          23
Append


49   -2        9   17



          23
Append


49   -2        9   17




          23
Append
void List::append(int x);
     node * n = new node(x);
     tail->next = n;
     tail = n;
     size++;
}
Append
void List::append(int x);
      node * n = new node(x);
      if(size==0)
             head = tail = n;
      else{
             tail->next = n;
             tail = n;
      }
      size++;
}
insertFront

      49   -2        9   17




                23

103
insertFront

      49   -2        9   17




                23

103
insertFront

      49   -2        9   17




                23

103
insertFront
void 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
      49       -2        9        17




                    23

103
                             67
Insert at Position 6
      49       -2        9        17




                    23

103
                             67
Insert at Position 6
      49       -2        9        17




                    23

103
                             67
Insert at Position 6
      49       -2        9        17




                    23

103
                             67
Insert at Position 6
      49       -2        9        17




                    23

103
                             67
Insert at Position 6
      49       -2        9        17




                    23

103
                             67
Insert at Position 6
      49       -2        9        17




                    23

103
                             67
Insert at Position 6
      49       -2        9        17




                    23

103
                             67
Insert at Position 6
      49       -2        9        17




                    23

103
                             67
Insert at Position 6
      49       -2        9        17




                    23

103
                             67
General Insert
bool 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


49         -2     9       17
Delete Item at Position 3


49         -2     9       17
Delete Item at Position 3


49         -2     9       17
Delete Item at Position 3


49         -2     9       17
Delete Item at Position 3


49         -2     9       17
Delete Item at Position 3


49         -2     9       17
Delete Item at Position 3


49         -2     9       17
Delete Item at Position 3


49         -2             17
deleteItem/erase
bool 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;
              }
}
itemAt
int 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.”
       }
}

More Related Content

What's hot (20)

PDF
662305 10
Nitigan Nakjuatong
 
PPT
Lecture 12: Classes and Files
Dr. Md. Shohel Sayeed
 
PDF
The Ring programming language version 1.9 book - Part 94 of 210
Mahmoud Samir Fayed
 
PDF
oop presentation note
Atit Patumvan
 
PPTX
Why learn new programming languages
Jonas Follesø
 
PPT
Data structures cs301 power point slides lecture 03
Nasir Mehmood
 
PDF
Rデバッグあれこれ
Takeshi Arabiki
 
PDF
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
Subhajit Sahu
 
PDF
Polymorphism
mohamed sikander
 
DOC
Awt
Swarup Saha
 
PPT
Computer Programming- Lecture 9
Dr. Md. Shohel Sayeed
 
PDF
Stl algorithm-Basic types
mohamed sikander
 
DOCX
C++ file
simarsimmygrewal
 
PDF
The Ring programming language version 1.10 book - Part 101 of 212
Mahmoud Samir Fayed
 
PPTX
Chapter 7 functions (c)
hhliu
 
PDF
Rのスコープとフレームと環境と
Takeshi Arabiki
 
KEY
Haskellで学ぶ関数型言語
ikdysfm
 
PDF
Diving into byte code optimization in python
Chetan Giridhar
 
PPTX
Lecture1 classes1
Noor Faezah Mohd Yatim
 
PDF
C programs
Koshy Geoji
 
Lecture 12: Classes and Files
Dr. Md. Shohel Sayeed
 
The Ring programming language version 1.9 book - Part 94 of 210
Mahmoud Samir Fayed
 
oop presentation note
Atit Patumvan
 
Why learn new programming languages
Jonas Follesø
 
Data structures cs301 power point slides lecture 03
Nasir Mehmood
 
Rデバッグあれこれ
Takeshi Arabiki
 
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
Subhajit Sahu
 
Polymorphism
mohamed sikander
 
Computer Programming- Lecture 9
Dr. Md. Shohel Sayeed
 
Stl algorithm-Basic types
mohamed sikander
 
The Ring programming language version 1.10 book - Part 101 of 212
Mahmoud Samir Fayed
 
Chapter 7 functions (c)
hhliu
 
Rのスコープとフレームと環境と
Takeshi Arabiki
 
Haskellで学ぶ関数型言語
ikdysfm
 
Diving into byte code optimization in python
Chetan Giridhar
 
Lecture1 classes1
Noor Faezah Mohd Yatim
 
C programs
Koshy Geoji
 

Similar to Class list data structure (20)

PPTX
Data Structures and Agorithm: DS 02 Array List.pptx
RashidFaridChishti
 
PPT
Arrays and structures
Mohd Arif
 
PPT
Stack and queue
Katang Isip
 
PDF
Notes
Hitesh Wagle
 
PDF
Array notes
Hitesh Wagle
 
PPT
Data structures KTU chapter2.PPT
Albin562191
 
PPT
Chapter2
Nashra Akhter
 
PDF
C++ normal assignments by maharshi_jd.pdf
maharshi1731
 
DOC
Ds 2 cycle
Chaitanya Kn
 
PDF
Implement a function in c++ which takes in a vector of integers and .pdf
feelingspaldi
 
PPT
Chapter2
Krishna Kumar
 
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
DOCX
Computer Science Practical Science C++ with SQL commands
Vishvjeet Yadav
 
ZIP
Cleanup and new optimizations in WPython 1.1
PyCon Italia
 
PDF
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
FOREVERPRODUCTCHD
 
PPT
Array
Malainine Zaid
 
PDF
public class TrequeT extends AbstractListT { .pdf
info30292
 
PPTX
Data structures
Rokonuzzaman Rony
 
PPT
dynamicList.ppt
ssuser0be977
 
Data Structures and Agorithm: DS 02 Array List.pptx
RashidFaridChishti
 
Arrays and structures
Mohd Arif
 
Stack and queue
Katang Isip
 
Array notes
Hitesh Wagle
 
Data structures KTU chapter2.PPT
Albin562191
 
Chapter2
Nashra Akhter
 
C++ normal assignments by maharshi_jd.pdf
maharshi1731
 
Ds 2 cycle
Chaitanya Kn
 
Implement a function in c++ which takes in a vector of integers and .pdf
feelingspaldi
 
Chapter2
Krishna Kumar
 
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
Computer Science Practical Science C++ with SQL commands
Vishvjeet Yadav
 
Cleanup and new optimizations in WPython 1.1
PyCon Italia
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
FOREVERPRODUCTCHD
 
public class TrequeT extends AbstractListT { .pdf
info30292
 
Data structures
Rokonuzzaman Rony
 
dynamicList.ppt
ssuser0be977
 
Ad

More from Katang Isip (6)

PPSX
Reflection paper
Katang Isip
 
PPSX
Punctuation tips
Katang Isip
 
PPSX
3 act story
Katang Isip
 
PPTX
Hash table and heaps
Katang Isip
 
PPT
Binary Search Tree and AVL
Katang Isip
 
PPT
Time complexity
Katang Isip
 
Reflection paper
Katang Isip
 
Punctuation tips
Katang Isip
 
3 act story
Katang Isip
 
Hash table and heaps
Katang Isip
 
Binary Search Tree and AVL
Katang Isip
 
Time complexity
Katang Isip
 
Ad

Recently uploaded (20)

PDF
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
PPTX
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
PPTX
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
PPTX
Comparing Translational and Rotational Motion.pptx
AngeliqueTolentinoDe
 
PPT
21st Century Literature from the Philippines and the World QUARTER 1/ MODULE ...
isaacmendoza76
 
PDF
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
PPTX
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
PDF
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
PPTX
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
DOCX
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
PDF
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
PPTX
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
PPTX
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
PPTX
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
PDF
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
PDF
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
Comparing Translational and Rotational Motion.pptx
AngeliqueTolentinoDe
 
21st Century Literature from the Philippines and the World QUARTER 1/ MODULE ...
isaacmendoza76
 
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 

Class list data structure