I have implemented Single Linked List. Need Suggestions for improving it ? Also i need to know how i can pass a list object to a function? Thanks in advance.
Node.hpp
#ifndef NODE_H
#define NODE_H
class Node
{
public:
int data;
Node *next;
};
#endif
LinkedList.hpp
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.hpp"
class LinkedList {
private:
Node *head;
public:
LinkedList();
~LinkedList();
/*Insert Before the Value Method : Insert a Value before the passed value.*/
void insert_bfv(int, int);
/*Insert Nth Method : Insert a value at nth Location in the list if exist.*/
void insert_nth(int, int);
/*Move To Nth Method : Move a Node to the Nth Location.*/
void moveto_nth(int, int);
/*Insert First Method : Insert a Value in the start of list.*/
void insert_first(int);
/*Insert Last Method : Insert a Value at the end of list.*/
void insert_last(int);
/*Delete Nth Method : Delete nth value from the list.*/
void delete_nth(int);
/*Delete Value Method : Search and Delete a passed Value from list.*/
void delete_val(int);
/*Search value Method : Check if a passed value is in the list.*/
void search_val(int);
/*Delete First Method : Delete the first value in the list.*/
void delete_first();
/*Delete Last Method : Delete the last value in the list.*/
void delete_last();
/*Is Empty Method : To check if list is Empty.*/
bool is_empty();
/*Print List Method : Print all values in the list.*/
void print_l();
/*Length Method : Return the number of nodes in the list.*/
int length();
/*First Method : Return the first value of the list else (-1).*/
int first();
/*Sum List Method : Return the Sum of data Values of the list.*/
int sum_l();
};
#endif
LinkedList.cpp
#include "LinkedList.hpp"
#include "Node.hpp"
#include <iostream>
using namespace std;
/*Constructor*/
LinkedList::LinkedList() {
head = NULL;
}
/*Destructor*/
LinkedList::~LinkedList() {
if (!is_empty()) {
Node *temp = head, *prev = NULL;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
delete prev;
}
delete temp;
}
}
/*Is Empty Method : To check if list is Empty.*/
bool LinkedList::is_empty() {
return head == NULL;
}
/*First Method : Return the first value of the list else (-1).*/
int LinkedList::first() {
if (!is_empty())
return head->data;
else
return -1;
}
/*Insert First Method : Insert a Value in the start of list.*/
void LinkedList::insert_first(int x) {
Node *newer = new Node;
newer->data = x;
newer->next = head;
head = newer;
}
/*Insert Last Method : Insert a Value at the end of list.*/
void LinkedList::insert_last(int x) {
Node *newer = new Node;
newer->data = x;
if (is_empty()) {
newer->next = head;
head = newer;
}
else {
Node *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
newer->next = temp->next;
temp->next = newer;
}
}
/*Print List Method : Print all values in the list.*/
void LinkedList::print_l() {
if (is_empty()) {
cout << "List is Empty." << endl;
}
else {
Node *temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
}
/*Search value Method : Check if a passed value is in the list.*/
void LinkedList::search_val(int x) {
if (is_empty())
cout << "List is Empty." << endl;
else {
bool check = false;
Node *temp = head;
while (temp != NULL) {
if (temp->data == x) {
cout << "Value found." << endl;
check = true;
break;
}
else
temp = temp->next;
}
if (check == false)
cout << "Value not found." << endl;
}
}
/*Delete First Method : Delete the first value in the list.*/
void LinkedList::delete_first() {
if (is_empty())
cout << "List is already Empty." << endl;
else {
Node *temp = head;
head = temp->next;
delete temp;
}
}
/*Delete Last Method : Delete the last value in the list.*/
void LinkedList::delete_last() {
if (is_empty())
cout << "List is already Empty." << endl;
else if (head->next == NULL) {
Node *temp = head;
head = NULL;
delete temp;
}
else {
Node *temp = head, *prev = NULL;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
prev->next = NULL;
delete temp;
}
}
/*Insert Before the Value Method : Insert a Value before the passed value.*/
void LinkedList::insert_bfv(int x, int k) {
if (is_empty())
cout << "List is Empty." << endl;
else {
Node *newer = new Node;
newer->data = x;
Node *temp = head, *prev = NULL;
while (temp->next != NULL && temp->data != k) {
prev = temp;
temp = temp->next;
}
if (temp->next == NULL && temp->data != k) {
cout << "Passed value is not in the list." << endl;
}
else if (temp == head) {
newer->next = temp;
head = newer;
}
else {
newer->next = temp;
prev->next = newer;
}
}
}
/*Insert Nth Method : Insert a value at nth Location in the list if exist.*/
void LinkedList::insert_nth(int x, int n) {
if (!(n <= 0 && n>length())) {
Node *newer = new Node;
newer->data = x;
Node *temp = head;
int count = 1;
while (temp != NULL && count != n) {
count++;
temp = temp->next;
}
newer->next = temp->next;
temp->next = newer;
}
else {
cout << "Location range is incorect." << endl;
}
}
/*Delete Value Method : Search and Delete a passed Value from list.*/
void LinkedList::delete_val(int k) {
Node *temp = head, *prev = NULL;
if (is_empty())
cout << "List is Empty." << endl;
else if (temp->data == k) {
head = temp->next;
delete temp;
}
else {
while (temp->data != k && temp->next != NULL) {
prev = temp;
temp = temp->next;
}
if (temp->next == NULL && temp->data != k)
cout << "Value Not Found in the list." << endl;
else {
prev->next = temp->next;
delete temp;
}
}
}
/*Delete Nth Method : Delete nth value from the list.*/
void LinkedList::delete_nth(int k) {
Node *temp = head, *prev = NULL;
if (is_empty())
cout << "List is Empty." << endl;
else {
if (k > length() && k <= 0)
cout << "Node does not exist." << endl;
else {
int count = 1;
while (temp != NULL && count != k) {
prev = temp;
temp = temp->next;
count++;
}
prev->next = temp->next;
delete temp;
}
}
}
/*Length Method : Return the number of nodes in the list.*/
int LinkedList::length() {
if (is_empty())
return 0;
else {
int count = 0;
Node *temp = head;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
}
/*SUM LIST Method : Return the Sum of data Values of the list.*/
int LinkedList::sum_l() {
Node *temp = head;
int sum = 0;
while (temp != NULL) {
sum = sum + temp->data;
temp = temp->next;
}
return sum;
}
/*Move To Nth Method : Move a Node to the Nth Location.*/
void LinkedList::moveto_nth(int n1, int n2) {
if(n1 < n2 && n1 < length() && n2 <= length() && n1 > 0 && n2 > 1 && n1 != n2) {
Node *temp1 = head, *temp2 = head, *prev = NULL;
int count1 = 1, count2 = 1;
while (temp1 != NULL && count1 != n1) {
prev = temp1;
temp1 = temp1->next;
count1++;
}
while (temp2 != NULL && count2 != n2) {
temp2 = temp2->next;
count2++;
}
if(prev == NULL)
head = head->next;
else
prev->next = temp1->next;
temp1->next = temp2->next;
temp2->next = temp1;
}
else {
cout << "Range is incorect." << endl;
}
}
main.cpp
#include "LinkedList.hpp"
int main() {
LinkedList list;
list.insert_last(12);
list.insert_last(13);
list.print_l();
list.delete_first();
list.print_l();
}
main()with 3-4 operations will be ok. \$\endgroup\$