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

Download Report

Transcript 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(); //

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;
int headElement() const;
// boolean function
// access functions
void addHead(int newdata);
void delHead();
// add to the head
// delete the head
int length() const;
void print() const;
private:
Nodeptr head;
};
// utility function
// output
How to use it
void main(){
list L;
L.print();
L.addHead(30);
L.print();
L.addHead(13);
L.print();
L.addHead(40);
L.print();
L.addHead(50);
L.print();
list N(L);
N.print();
list R;
R.print();
if(R.empty())
cout <<
L.delHead();
L.print();
L.delHead();
L.print();
if(L.empty())
cout <<
else{
cout <<
cout <<
}
}
// constructor called automatically here for L
{ }
{ 30 }
{ 13 30 }
{ 40 13 30 }
{ 50 40 13 30 }
{ 50 40 13 30 }
{ }
"List R empty" << endl;
{ 40 13 30 }
{ 13 30 }
"List L empty" << endl;
"List L contains " << L.length() << " nodes" << endl;
"Head element of list L is: " << L.headElement() << endl;
// destructor called automatically here for L
int i(0);
Int j(10);
Int k(j);
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

linked-list (dynamic size)
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;
int headElement() const;
// boolean function
// access functions
void addHead(int newdata);
void delHead();
// add to the head
// delete the head
int length() const;
void print() const;
private:
int head[10000];
int size;
// utility function
// output
Or int head[DIM]; int size; cont int DIM=10000;
Implementation
Some simple member functions:
list::list(){
head = NULL;
}

size = 0;
bool list::empty() const{
if(head==NULL)
If (size==0) return true; else return false;
return true;
else
return false;
}
int list::headElement() const {
if(head != NULL)
return head->data;
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) {
head = NULL;
Nodeptr cur = list1.head;
while(cur != NULL) {
// addEnd(cur->data);
addHead(cur->data); // inverse list order
cur = cur->next;
}
}
If (list1.size!=0)
for (int i=0; i<list1.size; i++) head[i]=list1.head[i];
Destructor: deallocation function
list::~list(){
Nodeptr cur;
while(head!=NULL){
cur = head;
head = head->next;
delete cur;
}
}
 Nothing here as it’s static.
Adding an element to the head:
void list::addHead(int newdata){
Nodeptr newPtr = new Node;
newPtr->data = newdata;
newPtr->next = head;
head = newPtr;
}
If (size<10000) {
for (int i=size; i>=;i--) head[i+1]=head[i];
head[0]=newdata;
size++;
}
Deleting the head:
void list::delHead(){
if(head != NULL){
Nodeptr cur = head;
head = head->next;
delete cur;
}
}
for (int i=1; i<size;i++) head[i-1]=head[i];
size--;
Print the list:
void list::print() const{
cout << "{";
Nodeptr cur = head;
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;
Nodeptr cur = head;
while(cur != NULL){
n++;
cur = cur->next;
}
return n;
}
 return size;
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;
int headElement() const;
// boolean function
// access functions
void addHead(int newdata);
void delHead();
// add to the head
// delete the head
int length() const;
void print() const;
private:
int* head;
int size;
// utility function
// output
Implementation
Some simple member functions:
list::list(){
…
}
bool list::empty() const{
}
int list::headElement() const {
}
(explicitly defined) copy constructor:
list::list(const list& list1) {
}
Other functions …
list::~list(){
delete[] head;
}
List as a template
template<typename T>
class list {
public:
list();
// constructor
list(const list& list1); // copy constructor
~list();
// destructor
bool empty() const;
int headElement() const;
void addHead(T newdata);
void delHead();
int length() const;
void print() const;
private:
T* head;
int size;
};
// boolean function
// access functions
// add to the head
// delete the head
// utility function
// output
Implementation
Some simple member functions:
template<typename T>
list::list(){
head=NULL; size=0;
}
template<typename T>
bool list::empty() const{
}
template<typename T>
T list::headElement() const {
}
Other functions …
template<typename T>
list::~list(){
delete T[] head;
}