Skip to content

Commit

Permalink
[5] LinkedList (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sureshvazrala authored Aug 10, 2024
1 parent c742660 commit 71b5963
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 0 deletions.
168 changes: 168 additions & 0 deletions inc/LinkedList.hpp
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
5 changes: 5 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
#include "inc/NumberManipulation.hpp"
#include "src/Numbermanipulation.cpp"
#include "test/Numbermanipulationtest.cpp"
#include "test/LinkedListtest.cpp"

#include "src/OOPS/class.cpp"

using namespace std;
int main()
{
cout<<"Welcome to main function "<<endl;
source();
test();
class_demo();
linkedlisttest();
return 0;
}
11 changes: 11 additions & 0 deletions src/DATA_STRUCTURES/LinkedList.cpp
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;



39 changes: 39 additions & 0 deletions src/OOPS/class.cpp
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;
}
21 changes: 21 additions & 0 deletions test/LinkedListtest.cpp
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;
}

0 comments on commit 71b5963

Please sign in to comment.