Anda di halaman 1dari 5

struct Date { unsigned short day; unsigned short month; unsigned short year; Date* next; }; class Singly_List

{ private: int size; Date* head; Date* tail; Date* current; Date* last_current; public: Singly_List(); void start(); to the very first element void end(); to the very last element void next(); ion forward one element void back(); ion backward one element void clear(); ll elements) void insert(Date* const); n pointed by current in the list void insert_at_start(Date* const); f the list void insert_at_end(Date* const); end of the list void remove(); pointed to by the current pointer Date get(); ed by current pointer void update(Date); s pointed by current pointer with Date values bool find(Date); e list else 0 int length(); nodes in the list void display(); }; //Constructor //Moves to

current

pointer pointer

//Moves to current

//Move the current posit //Move the current posit //Clear a list (remove a //Insert the node at the positio //Insert the node at the front o //Insert the node at the //Remove the node from the list //Returns the node point //Overrides the value of element //Returns 1 if the node is in th //Returns the number of

//////////////////////////////////////////////////////////////////////////////// /////////////////////////// #include <iostream> #include "My_List.h" using namespace std; Singly_List::Singly_List() { head = new Date(); //Constructor

head->day = 50; head->next = NULL; current = 0; last_current = 0; size = 0; tail = new Date(); tail->day = 51; tail->next = NULL; } void Singly_List::start() very first element { current = head->next; last_current = NULL; }

//Indicator for head // 0 means NULL // 0 means NULL //Indicator for tail

//Moves to

current

pointer to the

void Singly_List::end() //Moves to current t element { while(current->next->day != 51) { last_current = current; current = current->next; } }

pointer to the very las

void Singly_List::next() //Move the current position forw ard one element { if(current->next->day != 51) { last_current = current; current = current->next; } else cout<<"The current is pointing to the last node"<<endl<<endl; } void Singly_List::back() //Move the current position back ward one element { if(last_current->day != 50) { Date* temp = head; while(temp->next != last_current) temp = temp->next; current = last_current; last_current = temp; } else cout<<"The current is pointing to the first node"<<endl<<endl; } void Singly_List::clear() nts) { } //Clear a list (remove all eleme

void Singly_List::insert(Date* const node) n pointed by current in the list { if(current != NULL ) { node->next = current->next; current->next = node; last_current = current; current = node; } else { node->next = tail; head->next = node; last_current = head; current = node; } size++; }

//Insert the node at the positio

void Singly_List::insert_at_start(Date* const node) { current = head; node->next = head->next; head->next = node; last_current = head; current = node; size++; } void Singly_List::insert_at_end(Date* const node) { while(current->next != tail) { last_current = current; current = current->next; } node->next = current->next; current->next = node; last_current = current; current = node; size++; } void Singly_List::remove() //Remove the node pointed by the current pointer { if( current != NULL ) { last_current->next = current->next; delete current; current = last_current->next; size--; } } Date Singly_List::get() //Returns the node pointed by current po

inter { return *current; } void Singly_List::update(Date node) d by current pointer with Date values { current->day = node.day; current->month = node.month; current->year = node.year; } //Overrides the value of elements pointe

bool Singly_List::find(Date element) //Returns 1 if the node is in th e list else 0 { if(current != NULL) { Date* temp = head; while(temp->next != tail) { if(temp->next->day == element.day && temp->next->month = = element.month && temp->next->year == element.year) return true; else temp = temp->next; } } return false; } int Singly_List::length() { return size; } //Returns the size of the list

void Singly_List::display() { Date* temp = head; while(temp->next != tail) { cout<<"Node "<<temp->next->day<<"/"<<temp->next->month<<"/"<<te mp->next->year<<endl; temp = temp->next; } }

//////////////////////////////////////////////////////////////////////////////// ///////////// #include <iostream> #include "My_List.h" using namespace std; int main() {

Singly_List list1; Date* d1 = new Date; d1->day = 12; d1->month = 8; d1->year = 2012; list1.insert(d1); Date* d2 = new Date; d2->day = 21; d2->month = 7; d2->year = 1995; list1.insert(d2); Date* d3 = new Date; d3->day = 9; d3->month = 1; d3->year = 1987; list1.insert(d3); Date* d4 = new Date; d4->day = 2; d4->month = 11; d4->year = 1970; list1.insert_at_start(d4); list1.display(); cout<<endl<<endl; if(list1.find(*d1)) cout<<"Element found"<<endl; else cout<<"Element not found"<<endl; list1.remove(); list1.remove(); cout<<endl<<endl; if(list1.find(*d4)) cout<<"Element found"<<endl; else cout<<"Element not found"<<endl; list1.display();

return 0; }

Anda mungkin juga menyukai