diff --git a/data_structure/cll/cll.cpp b/data_structure/cll/cll.cpp new file mode 100644 index 000000000..efc068f3c --- /dev/null +++ b/data_structure/cll/cll.cpp @@ -0,0 +1,127 @@ +/* + A simple class for Cicular Linear Linked List +*/ +#include "cll.h" +using namespace std; + +/* Constructor */ +cll::cll() +{ + head = NULL; + total = 0; +} + +cll::~cll() +{ + /* Desstructure, no need to fill */ +} + +/* Display a list. and total element */ +void cll::display() +{ + if (head == NULL) + cout << "List is empty !" << endl; + else + { + cout << "CLL list: "; + node *current = head; + for (int i = 0; i < total; i++) + { + cout << current->data << " -> "; + current = current ->next; + } + cout << head->data << endl; + cout << "Total element: "<< total <data = new_data; + newNode->next = NULL; + if(head==NULL) { + head = newNode; + head -> next = head; + } else { + node *current = head; + while (current -> next != head) { + current = current->next; + } + newNode->next = head; + current->next = newNode; + head = newNode; + } + total++; +} + +/* List insert a new value at head in list */ +void cll::insert_tail(int new_data) +{ + node *newNode; + newNode = new node; + newNode->data = new_data; + newNode->next = NULL; + if(head==NULL) { + head = newNode; + head -> next = head; + } else { + node *current = head; + while (current -> next != head) { + current = current->next; + } + current->next = newNode; + newNode->next = head; + } + total++; +} + +/* Get total element in list */ +int cll::get_size() +{ + return total; +} + + +/* Return true if the requested item (sent in as an argument) +is in the list, otherwise return false */ +bool cll::find_item(int item_to_find) +{ + if (head == NULL) { + cout << "List is empty !" << endl; + return false; + } else { + node *current = head; + while (current -> next != head) { + if (current->data == item_to_find) + return true; + current = current->next; + } + return false; + } +} + +/* Overloading method*/ +int cll::operator*() +{ + return head->data; +} + +/* Overload the pre-increment operator. + The iterator is advanced to the next node. */ +void cll::operator++() +{ + if (head == NULL) { + cout << "List is empty !" << endl; + } else { + node *current = head; + while (current -> next != head) { + current = current -> next; + } + current->next = head -> next; + head = head -> next; + } + total--; +} diff --git a/data_structure/cll/cll.h b/data_structure/cll/cll.h new file mode 100644 index 000000000..ae71dcd01 --- /dev/null +++ b/data_structure/cll/cll.h @@ -0,0 +1,45 @@ +/* + * Simple data structure CLL (Cicular Linear Linked List) + * */ +#include +#include +#include +#include + +#ifndef CLL_H +#define CLL_H +/*The data structure is a linear linked list of integers */ +struct node +{ + int data; + node * next; +}; + +class cll +{ + public: + cll(); /* Construct without parameter */ + ~cll(); + void display(); /* Show the list */ + + /****************************************************** + * Useful method for list + *******************************************************/ + void insert_front(int new_data); /* Insert a new value at head */ + void insert_tail(int new_data); /* Insert a new value at tail */ + int get_size(); /* Get total element in list */ + bool find_item(int item_to_find); /* Find an item in list */ + + /****************************************************** + * Overloading method for list + *******************************************************/ + int operator*(); /* Returns the info contained in head */ + /* Overload the pre-increment operator. + The iterator is advanced to the next node. */ + void operator++(); + + protected: + node * head; + int total; /* Total element in a list */ +}; +#endif diff --git a/data_structure/cll/main_cll.cpp b/data_structure/cll/main_cll.cpp new file mode 100644 index 000000000..15388b822 --- /dev/null +++ b/data_structure/cll/main_cll.cpp @@ -0,0 +1,44 @@ +#include "cll.h" +using namespace std; + +int main() +{ + /* Test CLL */ + cout << "----------- Test construct -----------" << endl; + cll list1; + list1.display(); + cout << "----------- Test insert front -----------" << endl; + list1.insert_front(5); + cout << "After insert 5 at front: "<