Page cover

Singly Linked List

SinglyLinkedList.h
#include <iostream>
#pragma once

using namespace std;

class Node {
public:
	int data;
	Node* next;
	explicit Node(const int d) : data(d), next(nullptr) {}
};

class SinglyLL {

public:
	Node* head ;
	int elements;
	SinglyLL(): elements(0) {
		head = nullptr;
	}	

	SinglyLL(const SinglyLL& list);
	
	SinglyLL& operator=(const SinglyLL& rhs);
	
	void insert(const int val);
	
	void insertAtHead(const int val);
	
	bool search(const int val) const;
	
	bool remove(const int val);
	
	bool removeAtHead(int& headData);

	void reverse();
	
	void sort();

	friend ostream& operator<< (ostream& os, const SinglyLL& list);

	~SinglyLL();
};
// Copy Constructor
SinglyLL :: SinglyLL(const SinglyLL& list) {
	Node* curHead = list.head;

	Node* cur = new Node(curHead  -> data);
	curHead = curHead -> next;
	head = cur;

	while (curHead != nullptr) {
		Node* temp = new Node(curHead -> data);
		cur -> next = temp;
		cur = temp; 
		curHead = curHead -> next;
	}
	elements = list.elements;
}

// Assignment Operator
SinglyLL& SinglyLL :: operator= (const SinglyLL& rhs) {
	if(this == &rhs) return *this;

	while (head != nullptr) {
		Node* temp = head;
		head = head->next;
		delete temp;
	}
	
	elements = rhs.elements;

	Node* curHead = rhs.head;
	Node* cur = new Node(curHead -> data);
	curHead = curHead -> next;
	head = cur;

	while (curHead != nullptr) {
		Node* temp = new Node(curHead->data);
		cur -> next = temp; 
		cur = temp;
		curHead = curHead -> next;
	}
	return *this;
}

// destructor
SinglyLL :: ~SinglyLL() {
	while (head != nullptr) {
		Node* temp = head;
		head = head->next;
		delete temp;
	}
}

// Insert At Any Position
void SinglyLL :: insert(const int val) {
	if(elements == 0) {
		Node* newNode = new Node(val);
		head = newNode;
		elements++;
		return;
	}
	
	Node* newNode = new Node(val);
	Node* cur = head;
	
	while (cur -> next != nullptr) {
		cur = cur -> next;
	}

	cur -> next = newNode;
	elements++;
}

// Insert At Head
void SinglyLL :: insertAtHead(const int val) {
	if(elements == 0) {
		Node* newNode = new Node(val);
		head = newNode;
		elements++;
		return;
	}
	
	Node* newNode = new Node(val);
	newNode -> next = head;
	head = newNode;
	elements++;
}

// Search Element
bool SinglyLL :: search(const int val) const {
	if(elements == 0) return false;
	
	Node* temp = head;
	while (temp != nullptr) {
		if(temp -> data == val) return true;
		temp = temp -> next;
	}
	return false;
}
// Remove At Any Position
bool SinglyLL :: remove(const int val) {
	if(elements == 0) return false;

	// if only 1 element present
	if(elements == 1) {
		delete head;
		head = nullptr;
		elements--;
		return true;
	}
	
	if(head -> data == val) {
		Node* cur = head;
		head = head->next;
		delete cur;
		elements--;
		return true;
	}

	Node* cur = head;
	Node* prev = nullptr;

	while (cur != nullptr) {
		if(cur -> data == val) {
			prev -> next = cur -> next;
			delete cur;
			elements--;
			return true;
		}
		prev = cur;
		cur = cur -> next;
	}
	return false;
}

// Remove Head
bool SinglyLL :: removeAtHead(int& headData) {
	if(elements == 0) return false;

	if(elements == 1) {
		headData = head -> data;
		delete head;
		elements--;
		return true;
	}

	Node* cur = head;
	headData = cur -> data;
	head = head -> next;
	delete cur;
	elements--;
	return true;
}

// Print data
ostream& operator<< (ostream& os, const SinglyLL& list) {
	Node* cur = list.head;

	while (cur != nullptr) {
		os << cur -> data <<" ";
		cur = cur -> next;
	} os << endl;
	os << "Head: " << list.head -> data << endl;
	os << "ELements: " << list.elements<< endl;
	return os;
}

// Reverse List
void SinglyLL :: reverse() {
	Node* prev = nullptr;
	Node* cur = head;
	Node* nx = head -> next;

	while (cur != nullptr) {
		cur -> next = prev;
		prev = cur;
		cur = nx;

		if(cur != nullptr) nx = nx -> next;
	}
	head = prev;
}

// Sort List
void SinglyLL :: sort() {
	if(elements == 0 || elements == 1) return;

	for (int i = 1; i < elements; i++) {
		
		Node* prev = nullptr;
		Node* cur = head;
		Node* nxt = head -> next;
		bool isSorted = true;

		for (int j = 0; j < elements - i; j++) {
		
			if(cur -> data > nxt -> data) {
				cur -> next = nxt -> next;
				nxt -> next = cur;
				
				isSorted = false;

				if(prev != nullptr){
					prev -> next = nxt;
				}
				else
					head = nxt;
				
				cur = nxt;
				nxt = nxt -> next;
			}
			prev = cur; 
			cur = cur -> next;
			nxt = nxt -> next;
		}
		if(isSorted) return;
	}
}

Last updated

Was this helpful?