forked from tanwanimohit/Competitive-Programming-Archives
-
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.
Merge pull request tanwanimohit#19 from Jacobite747/master
Add Simple Single Linked List Implementation (C++)
- Loading branch information
Showing
3 changed files
with
203 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,144 @@ | ||
#include <iostream> | ||
|
||
#include "linked_list.h" | ||
|
||
ListElement::ListElement() | ||
: data_(0), | ||
next_(nullptr) { | ||
} | ||
|
||
ListElement::ListElement(int data) | ||
: data_(data), | ||
next_(nullptr) { | ||
} | ||
|
||
List::List() | ||
: head_(nullptr), | ||
tail_(nullptr){ | ||
} | ||
|
||
void List::AddElementEnd(int data) { | ||
ListElement *elem = new ListElement; | ||
elem->setData(data); | ||
if (head_ == nullptr) { | ||
head_ = elem; | ||
tail_ = elem; | ||
} else { | ||
tail_->setNext(elem); | ||
tail_ = elem; | ||
} | ||
} | ||
|
||
void List::AddElementStart(int data) { | ||
ListElement *elem = new ListElement; | ||
elem->setData(data); | ||
if (head_ == nullptr) { | ||
head_ = elem; | ||
tail_ = elem; | ||
} else { | ||
elem->setNext(head_); | ||
head_ = elem; | ||
} | ||
} | ||
|
||
void List::AddElementAt(int data, int pos) { | ||
ListElement *elem = new ListElement; | ||
elem->setData(data); | ||
if (head_ == nullptr) { | ||
head_ = elem; | ||
tail_ = elem; | ||
} else { | ||
ListElement *current = new ListElement; | ||
ListElement *pre = new ListElement; | ||
elem->setData(data); | ||
current = head_; | ||
for (int i = 0; i < pos; i++) { | ||
pre=current; | ||
current = current->getNext(); | ||
} | ||
pre->setNext(elem); | ||
elem->setNext(current); | ||
} | ||
} | ||
|
||
void List::DeleteFirst() { | ||
if (head_ == nullptr) { | ||
return; | ||
} else if (head_->getNext() == nullptr) { | ||
head_ = nullptr; | ||
tail_ = nullptr; | ||
} else { | ||
ListElement *elem = new ListElement; | ||
elem = head_; | ||
head_ = head_->getNext(); | ||
delete elem; | ||
} | ||
} | ||
|
||
void List::DeleteLast() { | ||
if (head_ == nullptr) { | ||
return; | ||
} else if (head_->getNext() == nullptr) { | ||
head_ = nullptr; | ||
tail_ = nullptr; | ||
} else { | ||
ListElement *current = new ListElement; | ||
ListElement *pre = new ListElement; | ||
current = head_; | ||
while(current->getNext() != nullptr) { | ||
pre=current; | ||
current=current->getNext(); | ||
} | ||
tail_ = pre; | ||
pre->setNext(nullptr); | ||
delete current; | ||
} | ||
} | ||
|
||
void List::DeleteAt(int pos) { | ||
if (head_ == nullptr) { | ||
return; | ||
} else if (head_->getNext() == nullptr) { | ||
head_ = nullptr; | ||
tail_ = nullptr; | ||
} else { | ||
ListElement *current = new ListElement; | ||
ListElement *pre = new ListElement; | ||
current = head_; | ||
for(int i = 1; i < pos; i++) { | ||
pre = current; | ||
current = current->getNext(); | ||
} | ||
pre->setNext(current->getNext()); | ||
} | ||
} | ||
|
||
void List::DisplayList() { | ||
if (head_ == nullptr) { | ||
std::cout << "empty" << std::endl; | ||
} else { | ||
ListElement *temp; | ||
temp = head_; | ||
while(temp->getNext() != nullptr) { | ||
std::cout << "(" << temp->getData() << ")-->"; | ||
temp = temp->getNext(); | ||
} | ||
std::cout << "(" << temp->getData() << ")" << std::endl; | ||
} | ||
} | ||
|
||
int List::Length() { | ||
int count = 0; | ||
if (head_ == nullptr) { | ||
return 0; | ||
} else { | ||
ListElement *temp; | ||
temp = head_; | ||
while(temp->getNext() != nullptr) { | ||
count += 1; | ||
temp = temp->getNext(); | ||
} | ||
count += 1; | ||
} | ||
return count; | ||
} |
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,41 @@ | ||
#ifndef LINKEDLIST | ||
#define LINKEDLIST | ||
|
||
class ListElement { | ||
public: | ||
ListElement(); | ||
ListElement(int data); | ||
|
||
void setData(int data) { data_ = data; } | ||
int getData() { return data_; } | ||
|
||
void setNext(ListElement *next) { next_ = next; } | ||
ListElement* getNext() { return next_; } | ||
|
||
private: | ||
int data_; | ||
ListElement* next_; | ||
}; | ||
|
||
|
||
class List { | ||
public: | ||
List(); | ||
void AddElementEnd(int data); | ||
void AddElementStart(int data); | ||
void AddElementAt(int data, int pos); | ||
|
||
void DeleteLast(); | ||
void DeleteFirst(); | ||
void DeleteAt(int pos); | ||
|
||
void DisplayList(); | ||
bool isEmpty() { return (head_ == nullptr); } | ||
int Length(); | ||
|
||
private: | ||
ListElement* head_; | ||
ListElement* tail_; | ||
}; | ||
|
||
#endif |
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,18 @@ | ||
#include <iostream> | ||
#include "linked_list.h" | ||
|
||
int main() { | ||
|
||
List list; | ||
list.AddElementEnd(2); | ||
list.AddElementEnd(4); | ||
list.AddElementEnd(5); | ||
list.AddElementStart(1); | ||
list.AddElementAt(3, 2); | ||
list.DisplayList(); | ||
|
||
list.DeleteLast(); | ||
list.DeleteAt(2); | ||
list.DeleteFirst(); | ||
list.DisplayList(); | ||
} |