Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
data_structures::linked_list::list Class Reference
Collaboration diagram for data_structures::linked_list::list:
[legend]

Public Member Functions

 list ()
 
bool isEmpty ()
 Utility function that checks if the list is empty. More...
 
void push_back (int new_elem)
 
void push_front (int new_elem)
 
void erase (int old_elem)
 
void display ()
 
std::shared_ptr< linksearch (int find_elem)
 
void reverse ()
 
 list ()
 
bool isEmpty ()
 
void insert (int32_t new_elem)
 Utility function that adds a new element at the end of the list. More...
 
void reverseList ()
 Utility function for reversing a list. More...
 
void display ()
 
int32_t top ()
 Utility function to find the top element of the list. More...
 
int32_t last ()
 
int32_t traverse (int32_t index)
 Utility function to find the i th element of the list. More...
 

Private Attributes

std::shared_ptr< linkfirst
 link before the actual first element
 
std::shared_ptr< linklast
 last link on the list More...
 
Nodehead
 

Detailed Description

A list class containing a sequence of links

Constructor & Destructor Documentation

◆ list() [1/2]

data_structures::linked_list::list::list ( )
inline

List constructor. Initializes the first and last link.

89 {
90 // Initialize the first link
91 first = std::make_shared<link>();
92 // Initialize the last link with the first link
93 last = nullptr;
94 }
std::shared_ptr< link > first
link before the actual first element
Definition: linked_list.cpp:83
std::shared_ptr< link > last
last link on the list
Definition: linked_list.cpp:84

◆ list() [2/2]

data_structures::linked_list::list::list ( )
inline

List constructor. Initializes the first link.

59 {
60 head = nullptr; // Initialize the first link
61 }

Member Function Documentation

◆ display()

void list::display ( )

function displays all the elements in the list

Returns
'void'
181 {
182 if (isEmpty()) {
183 std::cout << "List is Empty!";
184 return;
185 }
187 while (t->succ() != nullptr) {
188 std::cout << t->succ()->val() << "\t";
189 t = t->succ();
190 }
191}
bool isEmpty()
Utility function that checks if the list is empty.
Definition: linked_list.cpp:111
Here is the call graph for this function:

◆ erase()

void list::erase ( int  old_elem)

function erases old element from the list

Parameters
old_elemto be erased from the list
152 {
153 if (isEmpty()) {
154 std::cout << "List is Empty!";
155 return;
156 }
158 std::shared_ptr<link> to_be_removed = nullptr;
159 while (t != last && t->succ()->val() != old_elem) {
160 t = t->succ();
161 }
162 if (t == last) {
163 std::cout << "Element not found\n";
164 return;
165 }
166 to_be_removed = t->succ();
167 t->succ() = t->succ()->succ();
168 to_be_removed.reset();
169 if (t->succ() == nullptr) {
170 last = t;
171 }
172 if (first == last){
173 last = nullptr;
174 }
175}
T reset(T... args)
Here is the call graph for this function:

◆ insert()

void list::insert ( int32_t  n)

Utility function that adds a new element at the end of the list.

Parameters
new_elemelement be added at the end of the list
82 {
83 try {
84 Node *new_node = new Node();
85 Node *temp = nullptr;
86 new_node->val = n;
87 new_node->next = nullptr;
88 if (isEmpty()) {
89 head = new_node;
90 } else {
91 temp = head;
92 while (temp->next != nullptr) {
93 temp = temp->next;
94 }
95 temp->next = new_node;
96 }
97 } catch (std::bad_alloc &exception) {
98 std::cerr << "bad_alloc detected: " << exception.what() << "\n";
99 }
100}
Definition: linkedlist_implentation_usingarray.cpp:14
T what(T... args)
Here is the call graph for this function:

◆ isEmpty()

bool list::isEmpty ( )

Utility function that checks if the list is empty.

function checks if list is empty

Returns
true if list is empty
false if list is not empty
true if the list is empty
false if the list is not empty
111 {
112 if (last == nullptr) {
113 return true;
114 } else {
115 return false;
116 }
117}

◆ push_back()

void list::push_back ( int  new_elem)

function adds new element to the end of the list

Parameters
new_elemto be added to the end of the list
123 {
124 if (isEmpty()) {
125 first->succ() = std::make_shared<link>(new_elem);
126 last = first->succ();
127 } else {
128 last->succ() = std::make_shared<link>(new_elem);
129 last = last->succ();
130 }
131}
Here is the call graph for this function:

◆ push_front()

void list::push_front ( int  new_elem)

function adds new element to the beginning of the list

Parameters
new_elemto be added to front of the list
137 {
138 if (isEmpty()) {
139 first->succ() = std::make_shared<link>(new_elem);
140 last = first->succ();
141 } else {
142 std::shared_ptr<link> t = std::make_shared<link>(new_elem);
143 t->succ() = first->succ();
144 first->succ() = t;
145 }
146}
Here is the call graph for this function:

◆ reverseList()

void list::reverseList ( )

Utility function for reversing a list.

Using the current, previous, and next pointer.

Returns
void
107 {
108 Node *curr = head;
109 Node *prev = nullptr, *next_node = nullptr;
110 while (curr != nullptr) {
111 next_node = curr->next;
112 curr->next = prev;
113 prev = curr;
114 curr = next_node;
115 }
116 head = prev;
117}
T prev(T... args)

◆ search()

std::shared_ptr< link > list::search ( int  find_elem)

function searchs for

Parameters
find_elemin the list
find_elemto be searched for in the list
197 {
198 if (isEmpty()) {
199 std::cout << "List is Empty!";
200 return nullptr;
201 }
203 while (t != last && t->succ()->val() != find_elem) {
204 t = t->succ();
205 }
206 if (t == last) {
207 std::cout << "Element not found\n";
208 return nullptr;
209 }
210 std::cout << "Element was found\n";
211 return t->succ();
212}
Here is the call graph for this function:

◆ top()

int32_t list::top ( )

Utility function to find the top element of the list.

Returns
the top element of the list
123 {
124 if (!isEmpty()) {
125 return head->val;
126 } else {
127 throw std::logic_error("List is empty");
128 }
129}
Here is the call graph for this function:

◆ traverse()

int32_t list::traverse ( int32_t  index)

Utility function to find the i th element of the list.

Returns
the i th element of the list
149 {
150 Node *current = head;
151
152 int count = 0;
153 while (current != nullptr) {
154 if (count == index) {
155 return (current->val);
156 }
157 count++;
158 current = current->next;
159 }
160
161 /* if we get to this line,the caller was asking for a non-existent element
162 so we assert fail */
163 exit(1);
164}
T exit(T... args)

Member Data Documentation

◆ last

int32_t list::last
private

last link on the list

Utility function to find the last element of the list.

Returns
the last element of the list

The documentation for this class was generated from the following files: