Abstract Data Type ExampleOnemoreexampleofADT:integerlinkedlist usingclassAclass with dynamicobjects:Copy constructorDestructor
Abstract Data Type Example One more example of ADT: integer linked list using class A class with dynamic objects: Copy constructor Destructor
#include <iostream>using namespace std;// a node on the liststruct Node(public://a data item on the listint data;Node *nexti// pointer to next nodetypedef Node* Nodeptr; // pointer to node
#include <iostream> 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
struct Node(public:int data;Node *next;typedef Node* Nodeptriclasslistclasspublic:listclass();// constructorlistclass(const listclass& listl);//copy constructor~listclass();//destructor// boolean functionbool empty()const;// access functionsint headElement()const;//add to the headvoid addHead(int newdata);// delete the headvoid delHead();int length() const;utilityfunction//void print() constioutputprivate:Nodeptr Head;
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; };
How to use itvoid main ()listclass L;// constructor called automatically here for LL.print();(]L.addHead(30)(30)L.print();L.addHead (13)(1330)L.print();L.addHead (40);(401330)L.printO;L.addHead(50)(50401330)L.printO;listclassN(L);(30134050)N.print();listclass R;R.print();湾if(R.empty())cout<<"ListRempty"<<endl;L.delHead ();(401330)L.print);L.delHead ();(1330)L.printO;if(L.empty())cout <<"List Lempty"<<endl;elselcout<<"List Lcontains "<<L.length()<<"nodes"<<endl;cout << "Head element of list L is:" << L.headElement() << endl;// destructor called automatically here for L
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
ImplementationSomesimplememberfunctions:listclass::listclass()(Head=NULL;bool listclass::empty() const(if(Head=-NULL)return true;elsereturn falseiintlistclass::headElement()constif(Head!= NULL)return Head->data;else(cout << "error: trying to find head of empty list" << endl;return-;
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 -; } } Some simple member functions: Implementation