TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
cll.cpp
1
/*
2
A simple class for Cicular Linear Linked List
3
*/
4
#include "cll.h"
5
using namespace
std;
6
7
/* Constructor */
8
cll::cll() {
9
head = NULL;
10
total = 0;
11
}
12
13
cll::~cll() {
/* Desstructure, no need to fill */
14
}
15
16
/* Display a list. and total element */
17
void
cll::display() {
18
if
(head == NULL)
19
cout <<
"List is empty !"
<<
endl
;
20
else
{
21
cout <<
"CLL list: "
;
22
node
*current = head;
23
for
(
int
i = 0; i < total; i++) {
24
cout << current->data <<
" -> "
;
25
current = current->next;
26
}
27
cout << head->data <<
endl
;
28
cout <<
"Total element: "
<< total <<
endl
;
29
}
30
}
31
32
/* List insert a new value at head in list */
33
void
cll::insert_front(
int
new_data) {
34
node
*newNode;
35
newNode =
new
node
;
36
newNode->data = new_data;
37
newNode->next = NULL;
38
if
(head == NULL) {
39
head = newNode;
40
head->next = head;
41
}
else
{
42
node
*current = head;
43
while
(current->next != head) {
44
current = current->next;
45
}
46
newNode->next = head;
47
current->next = newNode;
48
head = newNode;
49
}
50
total++;
51
}
52
53
/* List insert a new value at head in list */
54
void
cll::insert_tail(
int
new_data) {
55
node
*newNode;
56
newNode =
new
node
;
57
newNode->data = new_data;
58
newNode->next = NULL;
59
if
(head == NULL) {
60
head = newNode;
61
head->next = head;
62
}
else
{
63
node
*current = head;
64
while
(current->next != head) {
65
current = current->next;
66
}
67
current->next = newNode;
68
newNode->next = head;
69
}
70
total++;
71
}
72
73
/* Get total element in list */
74
int
cll::get_size() {
return
total; }
75
76
/* Return true if the requested item (sent in as an argument)
77
is in the list, otherwise return false */
78
bool
cll::find_item(
int
item_to_find) {
79
if
(head == NULL) {
80
cout <<
"List is empty !"
<<
endl
;
81
return
false
;
82
}
else
{
83
node
*current = head;
84
while
(current->next != head) {
85
if
(current->data == item_to_find)
86
return
true
;
87
current = current->next;
88
}
89
return
false
;
90
}
91
}
92
93
/* Overloading method*/
94
int
cll::operator*() {
return
head->data; }
95
96
/* Overload the pre-increment operator.
97
The iterator is advanced to the next node. */
98
void
cll::operator++() {
99
if
(head == NULL) {
100
cout <<
"List is empty !"
<<
endl
;
101
}
else
{
102
node
*current = head;
103
while
(current->next != head) {
104
current = current->next;
105
}
106
current->next = head->next;
107
head = head->next;
108
}
109
total--;
110
}
node
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition
avltree.cpp:13
endl
#define endl
Definition
matrix_exponentiation.cpp:36
data_structures
cll
cll.cpp
Generated by
1.18.0