Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.

Similar presentations


Presentation on theme: "Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor."— Presentation transcript:

1 Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor

2 #include using namespace std; struct Node{ // a node on the list public: int data; // a data item on the list Node *next;// pointer to next node }; typedef Node* Nodeptr; // pointer to node

3 struct Node{ public: int data; Node *next; }; typedef Node* Nodeptr; class listClass { public: listClass(); // constructor listClass(const listClass& list1); // copy constructor ~listClass(); // destructor bool empty() const; // boolean function int headElement() const; // access functions void addHead(int newdata); // add to the head void delHead(); // delete the head int length() const; // utility function void print() const; // output private: Nodeptr Head; };

4 void main(){ listClass L; // constructor called automatically here for L L.print(); { } L.addHead(30); L.print(); { 30 } L.addHead(13); L.print(); { 13 30 } L.addHead(40); L.print(); { 40 13 30 } L.addHead(50); L.print(); { 50 40 13 30 } listClass N(L); N.print(); { 30 13 40 50 } listClass R; R.print(); { } if(R.empty()) cout << "List R empty" << endl; L.delHead(); L.print(); { 40 13 30 } L.delHead(); 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 How to use it

5 listClass::listClass(){ Head = NULL; } bool listClass::empty() const{ if(Head==NULL) return true; else return false; } int listClass::headElement() const { if(Head != NULL) return Head->data; else{ cout << "error: trying to find head of empty list" << endl; return -999999; } Some simple member functions: Implementation

6 Destructor: deallocation function listClass::~listClass(){ Nodeptr cur; while(Head!=NULL){ cur = Head; Head = Head->next; delete cur; }

7 void listClass::addHead(int newdata){ Nodeptr newPtr = new Node; newPtr->data = newdata; newPtr->next = Head; Head = newPtr; } Adding an element to the head:

8 void listClass::delHead(){ if(Head != NULL){ Nodeptr cur = Head; Head = Head->next; delete cur; } Deleting the head:

9 listClass::listClass(const listClass& list1) { Head = NULL; Nodeptr cur = list1.Head; while(cur != NULL) { // addEnd(cur->data); addHead(cur->data); // inverse list order cur = cur->next; } (explicitly defined) copy constructor:

10 void listClass::print() const{ cout << "{"; Nodeptr cur = Head; while(cur != NULL){ cout data << " "; cur = cur->next; } cout << "}" << endl; } Print the list:

11 int listClass::length() const{ int n=0; Nodeptr cur = Head; while(cur != NULL){ n++; cur = cur->next; } return n; } Computing the number of elements of a given list:


Download ppt "Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor."

Similar presentations


Ads by Google