SlideShare une entreprise Scribd logo
1  sur  75
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.”
       }
}

Contenu connexe

Tendances

oop presentation note
oop presentation note oop presentation note
oop presentation note
Atit Patumvan
 
Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03
Nasir Mehmood
 

Tendances (20)

662305 10
662305 10662305 10
662305 10
 
Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
 
The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210
 
oop presentation note
oop presentation note oop presentation note
oop presentation note
 
Why learn new programming languages
Why learn new programming languagesWhy learn new programming languages
Why learn new programming languages
 
Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03
 
Rデバッグあれこれ
RデバッグあれこれRデバッグあれこれ
Rデバッグあれこれ
 
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : NotesCUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Awt
AwtAwt
Awt
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
C++ file
C++ fileC++ file
C++ file
 
The Ring programming language version 1.10 book - Part 101 of 212
The Ring programming language version 1.10 book - Part 101 of 212The Ring programming language version 1.10 book - Part 101 of 212
The Ring programming language version 1.10 book - Part 101 of 212
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
Rのスコープとフレームと環境と
Rのスコープとフレームと環境とRのスコープとフレームと環境と
Rのスコープとフレームと環境と
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Diving into byte code optimization in python
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python
 
Lecture1 classes1
Lecture1 classes1Lecture1 classes1
Lecture1 classes1
 
C programs
C programsC programs
C programs
 

Similaire à Class list data structure

Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
Mohd Arif
 
Implement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdfImplement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdf
feelingspaldi
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
FOREVERPRODUCTCHD
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
info30292
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
aathiauto
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
HIMANSUKUMAR12
 

Similaire à Class list data structure (20)

Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Notes
NotesNotes
Notes
 
Array notes
Array notesArray notes
Array notes
 
Data structures KTU chapter2.PPT
Data structures KTU chapter2.PPTData structures KTU chapter2.PPT
Data structures KTU chapter2.PPT
 
Chapter2
Chapter2Chapter2
Chapter2
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
Implement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdfImplement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdf
 
Chapter2
Chapter2Chapter2
Chapter2
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
Cleanup and new optimizations in WPython 1.1
Cleanup and new optimizations in WPython 1.1Cleanup and new optimizations in WPython 1.1
Cleanup and new optimizations in WPython 1.1
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
 
Array
ArrayArray
Array
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
 
Data structures
Data structures Data structures
Data structures
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
 
C++ practical
C++ practicalC++ practical
C++ practical
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
 

Plus de Katang Isip (6)

Reflection paper
Reflection paperReflection paper
Reflection paper
 
Punctuation tips
Punctuation tipsPunctuation tips
Punctuation tips
 
3 act story
3 act story3 act story
3 act story
 
Hash table and heaps
Hash table and heapsHash table and heaps
Hash table and heaps
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Time complexity
Time complexityTime complexity
Time complexity
 

Dernier

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Dernier (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 

Class list data structure