Programming

Document Sample

```					List as an Abstract Data Type
struct Node{
public:
int data;
Node* next;
};
typedef Node* Nodeptr;

class list {
public:
list();                   // constructor
list(const list& list1); // copy constructor
~list();                  // destructor

bool empty() const;           // boolean function
int headElement() const;      // access functions

int length() const;          // utility function
void print() const;          // output

private:
};
How to use it
void main(){                                                                     int i(0);
list L;              // constructor called automatically here for L
L.print();                     { }                                           Int j(10);
L.print();                     { 30 }                                        Int k(j);
L.print();                     { 13 30 }
L.print();                     { 40 13 30 }
L.print();                     { 50 40 13 30 }
list N(L);
N.print();                     { 50 40 13 30 }

list R;
R.print();                     { }
if(R.empty())
cout <<    "List R empty" << endl;
L.print();                     { 40 13 30 }
L.print();                     { 13 30 }
if(L.empty())
cout <<    "List L empty" << endl;
else{
cout <<    "List L contains " << L.length() << " nodes" << endl;
cout <<    "Head element of list L is: " << L.headElement() << endl;
}
}                        // destructor called automatically here for L
Motivation
   list using static array
int myArray[1000];
int n;
We have to decide (to oversize) in advance the size of the array
(list)
   list using dynamic array
int* myArray;
int n;
cin >> n;
myArray = new int[n];

We allocate an array (list) of any specified size while the
program is running

size = ??
The list is dynamic. It can grow and shrink to any size.
Using a static array
struct Node{
public:
int data;
Node* next;
};
typedef Node* Nodeptr;

class list {
public:
list();                   // constructor
list(const list& list1); // copy constructor
~list();                  // destructor

bool empty() const;               // boolean function
int headElement() const;          // access functions

int length() const;              // utility function
void print() const;              // output

private:
Or int head[DIM]; int size; cont int DIM=10000;
int size;
Implementation

Some simple member functions:
list::list(){
head = NULL;        size = 0;
}

bool list::empty() const{
return true;      If (size==0) return true; else return false;
else
return false;
}

If (size!=0) return head[0]; else …;
else{
cout << "error: trying to find head of empty list" << endl;
exit(1);
}
}
(explicitly defined) copy constructor:

list::list(const list& list1) {
while(cur != NULL) {
cur = cur->next;
}

}        If (list1.size!=0)
Destructor: deallocation function

list::~list(){
Nodeptr cur;
cur = head;           Nothing here as it’s static.
delete cur;
}
}

Nodeptr newPtr = new Node;
newPtr->data = newdata;
}

If (size<10000) {
size++;
}

delete cur;
}
}
size--;
Print the list:

void list::print() const{
cout << "{";
while(cur != NULL){
cout << cur->data << " ";
cur = cur->next;
}
cout << "}" << endl;
}
for (int i=0; i<size;i++) cout << head[i];
Computing the number of elements of a given list:

int list::length() const{

int n=0;
while(cur != NULL){
n++;                    return size;
cur = cur->next;
}
return n;
}
Using a dynamic array
struct Node{
public:
int data;
Node* next;
};
typedef Node* Nodeptr;

class list {
public:
list();                   // constructor
list(const list& list1); // copy constructor
~list();                  // destructor

bool empty() const;            // boolean function
int headElement() const;       // access functions

int length() const;           // utility function
void print() const;           // output

private:
int size;
Implementation

Some simple member functions:

list::list(){
…
}

bool list::empty() const{

}

}
(explicitly defined) copy constructor:

list::list(const list& list1) {

}
Other functions …

list::~list(){
}
List as a template
template<typename T>
class list {
public:
list();                   // constructor
list(const list& list1); // copy constructor
~list();                  // destructor

bool empty() const;          // boolean function
int headElement() const;     // access functions

int length() const;         // utility function
void print() const;         // output

private:
int size;
};
Implementation
Some simple member functions:

template<typename T>
list::list(){
}

template<typename T>
bool list::empty() const{

}

template<typename T>
}
Other functions …

template<typename T>
list::~list(){