Skip to content

Instantly share code, notes, and snippets.

@ldclakmal
Created January 24, 2015 16:01
Show Gist options
  • Save ldclakmal/ca38a199c2e3ac425772 to your computer and use it in GitHub Desktop.
Save ldclakmal/ca38a199c2e3ac425772 to your computer and use it in GitHub Desktop.
Linked List (OOP)
// LinkedList_OOP.cpp : Defines the entry point for the console application.
// L.D.C.Lakmal
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef int E;
class Node {
private:
E value;
Node* next;
public:
Node(){};
E Node::getValue();
void Node::setValue(E val);
Node* Node::getNext();
void Node::setNext(Node* newNode);
};
E Node::getValue(){
return this->value;
}
void Node::setValue(E val){
this->value = val;
}
Node* Node::getNext(){
return this->next;
}
void Node::setNext(Node* newNode){
this->next = newNode;
}
class LinkedList {
private:
Node* head;
int length;
public:
void LinkedList::initList();
bool LinkedList::isEmpty();
Node* LinkedList::search(E value);
E LinkedList::deleteNode(E value);
E LinkedList::deleteAt(int i);
bool LinkedList::insert(E value);
bool LinkedList::insertAt(E value, int i);
int LinkedList::listLength();
void LinkedList::displayList();
};
void LinkedList::initList(){
head = NULL;
length = 0;
}
bool LinkedList::isEmpty(){
if(listLength() == 0){
return true;
}
return false;
}
Node* LinkedList::search(E value){
Node* temp = head;
while(temp != NULL){
if(temp->getValue() == value){
cout << "Node found !" << endl;
return temp;
}
temp = temp->getNext();
}
cout << "Node not found !" << endl;
return NULL;
}
E LinkedList::deleteNode(E value){
Node* temp = head;
if(head->getValue() == value){
head = head->getNext();
length--;
return value;
}
while(temp->getNext() != NULL){
if(temp->getNext()->getValue() == value){
temp->setNext(temp->getNext()->getNext());
length--;
return value;
}
temp = temp->getNext();
}
return NULL;
}
E LinkedList::deleteAt(int i){
Node* temp = head;
if(i == 1){
int tmpVal = head->getValue();
head = head->getNext();
length--;
return tmpVal;
}else{
int count = 1;
while(temp->getNext() != NULL){
if(count == i-1){
int tmpVal = temp->getNext()->getValue();
temp->setNext(temp->getNext()->getNext());
length--;
return tmpVal;
}
count++;
temp = temp->getNext();
}
}
return NULL;
}
bool LinkedList::insert(E value){
Node* newNode = new Node();
newNode->setValue(value);
newNode->setNext(NULL);
if(listLength() != 0){
newNode->setNext(head);
}
head = newNode;
length++;
return true;
}
bool LinkedList::insertAt(E value, int i){
Node* temp = head;
Node* newNode = new Node();
newNode->setValue(value);
if(i == 1){
newNode->setNext(head);
head = newNode;
length++;
return true;
}else{
int count = 1;
while(temp != NULL){
if(count == i-1){
newNode->setNext(temp->getNext());
temp->setNext(newNode);
length++;
return true;
}
count++;
temp = temp->getNext();
}
}
return NULL;
}
int LinkedList::listLength(){
return this->length;
}
void LinkedList::displayList(){
int i = 0;
Node* temp = head;
while(i != listLength()){
cout << temp->getValue() << " ";
temp = temp->getNext();
i++;
}
cout << endl;
}
void main(){
LinkedList list;
list.initList();
list.isEmpty();
list.insert(10);
list.insert(20);
list.insert(30);
list.insert(40);
list.insert(50);
list.displayList();
list.insertAt(100, 4);
list.displayList();
cout << "isEmpty : " << list.isEmpty() << endl;
list.search(30);
list.deleteNode(30);
list.displayList();
list.deleteAt(5);
list.displayList();
cout << "getLength : " << list.listLength() << endl;
system("PAUSE");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment