struct Nodelpublic:int dataiNode* next;class Listpublic:List);constructorList(const List& listl)/copy constructor~List();destructor//boolean functionboolempty()const;// access functionsint headElement() const;//add to the headvoid addHead(int newdata);// delete the headvoid delHead();int length()const;//utilityfunction//outputvoid print()const;private:int head[looo0l;Or int head[DIM];int size;cont int DIM=10000;intsize;
struct Node{ public: int data; Node* next; }; class List { public: List(); // constructor List(const List& list1); // copy constructor ~List(); // 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: int head[10000]; int size; }; Or int head[DIM]; int size; cont int DIM=10000;
ImplementationSomesimplememberfunctions:List::ListOsize = O;hedd- NULL,bool List::empty()const(司if(head--NULL)If(size==o)returntrue;else returnfalse;return true;elsereturn false;intList::headElementOconst(if(head!=NULL)return head->data;If (size!=0) return head[o]; else ...;elsefcout<<"error:trying to find head of empty list"<< endl;exit(l);
List::List(){ head = NULL; → size = 0; } bool List::empty() const{ if(head==NULL) return true; else return false; } int List::headElement() const { if(head != NULL) return head->data; else{ cout << "error: trying to find head of empty list" << endl; exit(1); } } Some simple member functions: Implementation If (size==0) return true; else return false; If (size!=0) return head[0]; else .;
(explicitly defined) copy constructor:List::List(const list& listl)(head =NULL;Node* cur = listl.head;while(cur!=NULL)//addEnd(cur->data);addHead(cur->data); //inverse list ordercur = cur->next;If (list1.size!=0)for(inti=0;i<list1.size;i++)head[i]=list1.head[i];
List::List(const list& list1) { head = NULL; Node* cur = list1.head; while(cur != NULL) { // addEnd(cur->data); addHead(cur->data); // inverse list order cur = cur->next; } } (explicitly defined) copy constructor: If (list1.size!=0) for (int i=0; i<list1.size; i++) head[i]=list1.head[i];