You are on page 1of 15

Class Declaration :

class MyCharListPtr { If you notice, this


 public:
   MyCharListPtr(); class interface is
   ~MyCharListPtr(); EXACTLY the
   MyCharListPtr(const MyCharListPtr& list); SAME with
   MyCharListPtr& operator= (const MyCharListPtr& list);
   void push_front(char ch); the earlier array-
   void pop_front(); based linked list
   void push_back(char ch); class.
   void pop_back();
   char& at(int index); This is a GOOD
   char& operator[](int index);
   char& front();
example of data
   char& back(); abstraction, the
   int size() const; interface of a ADT is
   bool empty() const;
   void clear();
independent of its
   void display(); implementation !!
   void erase(int index);
   void insert(int index, char ch);
 private: . . . .
};
Private data and member functions :

s MyCharListPtr {
lic:
. . 
vate:
ruct ListNode

char item;

ListNode* next;
;

istNode* getPointerToNode(int nodeNo);


nt Size;

istNode *head, *tail, *cur, *prev; 


Default Constructor / Destructor :

MyCharListPtr::MyCharListPtr(): Size(0)
{

 head = tail = cur = prev = NULL;


}

MyCharListPtr::~MyCharListPtr()
{
while ( !empty() )
pop_front();
} Click to add text
class MyCharListPtr {
public:
. . .
private:
struct ListNode
{
char item;

ListNode* next;
};

Click to add
ListNode* getPointerToNode(int text
nodeNo);
int Size;

ListNode *head, *tail, *cur, *prev;


};
these pointers
are now REAL
C++ pointer of
Click tom, add text type LIstNode*.
Assignment operator :
MyCharListPtr& MyCharListPtr::operator= (const MyCharListPtr& list)
{
if (this==&list) return *this;
Size = list.Size;

head = tail = NULL;


cur = list.head;
while (cur != NULL) {
push_back( cur->item );
cur = cur->next;
}
return *this;
}
Copy Constructor :
MyCharListPtr& MyCharListPtr::operator= (const MyCharListPtr& list)
{
 if (this==&list) return *this;
 Size = list.Size;

head = tail = NULL;


cur = list.head;
while (cur != NULL) {
push_back( cur->item );
cur = cur->next;
}
 return *this;   
}     
capacity / size functions :

int MyCharListPtr::size() const {


 return Size;
}
bool MyCharListPtr::empty() const {
 if (head == NULL) return true;
}
void MyCharListPtr::clear()
{
  while ( !empty() )
     pop_front();
}
Push data to front of the list :
void MyCharListPtr::push_front(char ch) {
 if ( empty() ) {
head = new ListNode;
head->item = ch;
head->next = NULL;
tail = head;
  } 
  else {
ListNode* slot = new ListNode;
slot->item = ch;
slot->next = head;
head = slot;
    }
 Size++;    
}
Push data to back of the list :
void MyCharListPtr::push_back(char ch) {
 if ( empty() ) {
  head = new ListNode;
  head->item = ch;
  head->next = NULL;
  tail = head;
  } 
  else {
  ListNode* slot = new ListNode;
  slot->item = ch;
  slot->next = NULL;
  tail->next = slot;
  tail = slot;
    }
 Size++;    
}
Displaying the content of the list :

void MyCharListPtr::display()
{
 cout << "[";
 cur = head;
while (cur != NULL)
 {
 cout << cur->item;
 cur = cur->next;
 }
 cout << "]" << endl; 
}
Index operation :
 ListNode* MyCharListPtr::getPointerToNode(int nodeNo) {
 if ( empty() ) { cout << "Error : empty list" << endl;  exit(1);  } 
 if (nodeNo >= Size) { cout << "Error : past the end" << endl; exit(1); }     
 cur = head;  int i = 0;
 while (i < nodeNo) {
   cur = cur->next;
    i++; } 
 return cur;   
}
char& MyCharListPtr::at(int nodeNo) {
 ListNode* p = getPointerToNode(nodeNo);
 return p->item;
}
char& MyCharListPtr::operator[](int nodeNo) {
 ListNode* p = getPointerToNode(nodeNo);
 return p->item;
}
Erasing a node :
void MyCharListPtr::erase(int nodeNo) {
if ( empty() ) {
cout << "Error : empty list" << endl;
exit(1);
}
if (nodeNo==0)
{
ListNode* oldhead = head;
head = head->next;
delete oldhead;
} else {
prev = getPointerToNode(nodeNo-1);
cur = prev->next;
prev->next = cur->next;
delete cur;
}
Size--;
}
inserting a node :
void MyCharListPtr::insert(int nodeNo, char ch) {
  ListNode* slot = new ListNode;
  slot->item = ch;
 if (nodeNo==0) {
    slot->next = head;
     head = slot;
    } else {
        prev = getPointerToNode(nodeNo-1);
        cur = prev->next;
        prev->next = slot;
        slot->next = cur;
        if (cur == NULL)
       tail = slot;
     } 
 Size++;
}
Other functions :
void MyCharListPtr::pop_front() {
 if ( empty() ) {
    cout << "Error : pop empty list" << endl; exit(1); } 
ListNode* oldhead = head;
head = head->next;
delete oldhead; 
Size--;
}
void MyCharListPtr::pop_back() {
 if ( empty() ) {
    cout << "Error : pop empty list" << endl; exit(1); } 
 if ( Size == 1 ) pop_front();
 prev = getPointerToNode(Size-2);
 cur = prev->next;
 delete cur;
 prev->next = NULL;
 tail = prev;  Size--; 
}
char& MyCharListPtr::front() {
 if ( empty() ) {
    cout << "Error : empty list in front()" << endl;
    exit(1);
   }
 return head->item;
}

char& MyCharListPtr::back() {
 if ( empty() ) {
    cout << "Error : empty list in back()" << endl;
    exit(1);
   } 
 return tail->item;
}

You might also like