Merge pull request #701 from danghai/master

Add simple circular linkedlist
This commit is contained in:
Hai Hoang Dang
2020-01-07 16:24:16 -08:00
committed by GitHub
4 changed files with 227 additions and 0 deletions

127
data_structure/cll/cll.cpp Normal file
View File

@@ -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 <<endl;
}
}
/* List insert a new value at head in list */
void cll::insert_front(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;
}
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--;
}

45
data_structure/cll/cll.h Normal file
View File

@@ -0,0 +1,45 @@
/*
* Simple data structure CLL (Cicular Linear Linked List)
* */
#include <cstring>
#include <cctype>
#include <iostream>
#include <cstdlib>
#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

View File

@@ -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: "<<endl;
list1.display();
cout << "After insert 10 3 7 at front: "<<endl;
list1.insert_front(10);
list1.insert_front(3);
list1.insert_front(7);
list1.display();
cout << "----------- Test insert tail -----------" << endl;
cout << "After insert 18 19 20 at tail: "<<endl;
list1.insert_tail(18);
list1.insert_tail(19);
list1.insert_tail(20);
list1.display();
cout << "----------- Test find item -----------" << endl;
if (list1.find_item(10))
cout << "PASS" << endl;
else
cout << "FAIL" << endl;
if (!list1.find_item(30))
cout << "PASS" << endl;
else
cout << "FAIL" << endl;
cout << "----------- Test * operator -----------" << endl;
int value = *list1;
cout << "Value at *list1: " << value <<endl;
cout << "----------- Test ++ operator -----------" << endl;
list1.display();
++list1;
cout << "After ++list1: " <<endl;
list1.display();
return 0;
}

View File

@@ -0,0 +1,11 @@
CC= g++
CFLAGS = -c -Wall
all: cll
cll.o: cll.cpp main_cll.cpp
$(CC) $(CFLAGS) cll.cpp main_cll.cpp
cll: cll.o
$(CC) main_cll.o cll.o -o cll
clean:
rm *o cll