-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c742660
commit 71b5963
Showing
5 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
#include <iostream> | ||
|
||
#pragma once | ||
|
||
//namespace data_structures | ||
//{ | ||
|
||
//namespace linked_list | ||
//{ | ||
|
||
// Class Node | ||
class Node | ||
{ | ||
public: | ||
int32_t val; | ||
Node *next; | ||
}; | ||
|
||
// Class list | ||
class list | ||
{ | ||
private: | ||
Node *head; | ||
|
||
public: | ||
list() | ||
{ | ||
head = nullptr; | ||
} | ||
|
||
bool isEmpty(); // returns true if the list is empty | ||
void insert(int32_t new_elem); // inserts a new element | ||
void reverseList(); // reverses the linked list | ||
void display(); // displays the linked list | ||
int32_t top(); // returns the top element | ||
int32_t last(); // returns the last element | ||
int32_t traverse(int32_t index); | ||
}; | ||
|
||
|
||
|
||
bool list::isEmpty() { return head == nullptr; } | ||
|
||
/** | ||
* Inserts a new element into the linked list. | ||
* | ||
* @param n the value of the new element to be inserted | ||
* | ||
* @return void | ||
*/ | ||
void list::insert(int32_t n) | ||
{ | ||
// Create a new node | ||
Node *new_node = new Node(); | ||
|
||
// Set the value of the new node | ||
new_node->val = n; | ||
|
||
// Initialize the next pointer of the new node to null | ||
new_node->next = nullptr; | ||
|
||
// Check if the list is empty | ||
if (isEmpty()) | ||
{ | ||
// If the list is empty, set the new node as the head | ||
head = new_node; | ||
} | ||
else | ||
{ | ||
// Find the last node in the list | ||
Node *temp = head; | ||
while (temp->next != nullptr) | ||
{ | ||
temp = temp->next; | ||
} | ||
|
||
// Set the next pointer of the last node to the new node | ||
temp->next = new_node; | ||
} | ||
} | ||
|
||
/** | ||
* Reverses the linked list. | ||
* | ||
* This function iterates through the list, reversing the direction of the pointers. | ||
* It updates the head of the list to point to the new first node. | ||
* | ||
* @return void | ||
*/ | ||
void list::reverseList() | ||
{ | ||
Node *current = head; | ||
Node *prev=nullptr, *next = nullptr; | ||
while (current != nullptr) | ||
{ | ||
next = current->next; | ||
current->next = prev; | ||
prev = current; | ||
current = next; | ||
} | ||
head = prev; | ||
} | ||
|
||
/** | ||
* Display the linked list. | ||
* | ||
* @return void | ||
*/ | ||
void list::display() | ||
{ | ||
Node *temp = head; | ||
std::cout <<"Display linkedlist: "; | ||
while (temp != nullptr) | ||
{ | ||
std::cout << temp->val << " "; | ||
temp = temp->next; | ||
} | ||
std::cout << std::endl; | ||
} | ||
|
||
/** | ||
* Returns the top element in the linked list. | ||
* | ||
* @return The value of the top element in the list, or 0 if the list is empty. | ||
*/ | ||
int32_t list::top() | ||
{ | ||
if (head == nullptr) { | ||
return 0; | ||
} | ||
|
||
return head->val; | ||
} | ||
|
||
/** | ||
* Returns the last element in the linked list. | ||
* | ||
* @return The value of the last element in the list. | ||
*/ | ||
int32_t list::last() | ||
{ | ||
Node *temp = head; | ||
while (temp->next != nullptr) | ||
{ | ||
temp = temp->next; | ||
} | ||
return temp->val; | ||
} | ||
|
||
/** | ||
* Traverses the linked list to the specified index and returns the value at that index. | ||
* | ||
* @param index the index of the value to be retrieved | ||
* | ||
* @return the value at the specified index | ||
* | ||
*/ | ||
int32_t list::traverse(int32_t index) | ||
{ | ||
Node *temp = head; | ||
for (int i = 0; i < index; i++) | ||
{ | ||
temp = temp->next; | ||
} | ||
return temp->val; | ||
} | ||
//} // namespace linked_list | ||
//} // namespace data_structures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include <iostream> | ||
|
||
#include "../inc/LinkedList.hpp" | ||
|
||
using namespace std; | ||
|
||
//using namespace data_structures; | ||
//using namespace linked_list; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class Tiger | ||
{ | ||
public: | ||
int data1; | ||
int data2; | ||
|
||
|
||
public: | ||
void setdata() | ||
{ | ||
cout<<"enter the Input: "<<endl; | ||
cin>>data1>>data2; | ||
} | ||
|
||
Tiger(int x, int y) | ||
{ | ||
data1 = x; | ||
data2 = y; | ||
} | ||
|
||
void getdata() | ||
{ | ||
cout<<"Output: "<<data1<<" "<<data2<<endl; | ||
} | ||
}; | ||
|
||
|
||
|
||
int class_demo() | ||
{ | ||
Tiger t1(10,20); | ||
//t1.setdata(); | ||
t1.getdata(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
#include "../inc/LinkedList.hpp" | ||
|
||
int linkedlisttest() | ||
{ | ||
//data_structures::linked_list::list l; | ||
list l; | ||
l.insert(11); | ||
l.insert(12); | ||
l.insert(13); | ||
l.display(); | ||
std::cout << "top: " << l.top() << std::endl; | ||
std::cout << "last: " << l.last() << std::endl; | ||
std::cout << "traverse at index 1: " << l.traverse(1) << std::endl; | ||
l.reverseList(); | ||
l.display(); | ||
std::cout << "top: " << l.top() << std::endl; | ||
std::cout << "last: " << l.last() << std::endl; | ||
std::cout << "traverse at index 1: " << l.traverse(1) << std::endl; | ||
return 0; | ||
} |