Skip to content

Instantly share code, notes, and snippets.

@mirsahib
Last active October 14, 2017 14:13
Show Gist options
  • Save mirsahib/e1c0600c953531ad5eeaa3144b44bfd3 to your computer and use it in GitHub Desktop.
Save mirsahib/e1c0600c953531ad5eeaa3144b44bfd3 to your computer and use it in GitHub Desktop.
draft of singly linked list
//============================================================================
// Name : LinkedList.cpp
// Author : Mir Sahib
// Version : 0.1(beta)
// Copyright : MIT License
// Description : Singly Linked List implementation
//============================================================================
#include <iostream>
using namespace std;
struct node{
int data;
node* next;
};
class List{
private:
node* head;
node* curr;
node* temp;
node* newNode;
public:
//constructor
List(){
head = NULL;
curr = NULL;
temp = NULL;
newNode = NULL;
}
//getter function
node* getFirst(){
return head;
}
//insert function
void addFirst(int val){// add new node at the head
temp = new node;
temp->data = val;
temp->next = head;
head = temp;
}
void addLast(int val){//add new node at the tail
temp = new node;
temp->data = val;
temp->next = NULL;
if(head == NULL){
head = temp;
}else{
curr = head;
while(curr->next!=NULL){
curr = curr->next;
}
curr->next = temp;
}
}
void addAt(int index,int val){ // add node at the specific index
if(isEmpty()|| index==0){
addFirst(val);
}else if(sizeOflist()<index){
//cout<<"\nIndex is out of bound node will be added at the last node"<<endl;
addLast(val);
}else{
temp = new node;
temp->data = val;
temp->next = NULL;
curr = head;
for(int i=0;i<index-1;i++){
curr=curr->next;
}
temp->next = curr->next;
curr->next = temp;
}
}
// optional insert function
void addAfterLastOccurrence(int searchVal,int val){//add node after getting the last search value
curr=head;
int counter=0;
if(isEmpty()){
cout<<"List is empty"<<endl;
}else{
temp = new node;
while(curr!=NULL){//traversing through the list
//int x = curr->data;
if(curr->data==searchVal){//if search value is found increment the counter and store the current node
temp = curr;
counter++;
}
curr=curr->next;
}
if(counter!=0){//if counter is not zero than declare a new node
newNode = new node;
newNode->data = val;
newNode->next = temp->next;//linking new node with the node which current node is pointing
temp->next = newNode;//current node is pointing to the new node(updating current node pointer)
}else{
cout<<"\nSearch node not found";
}
}
}
void addAfterFirstOccurrence(int searchVal,int val){//add node after getting the first search value
if(isEmpty()){
cout<<"\nList is Empty"<<endl;
}else{
curr = head;
int counter = 0;
while(curr!=NULL){
if(curr->data==searchVal){
counter++;
break;
}
curr=curr->next;
}
if(counter!=0){
newNode = new node;
newNode->data = val;
newNode->next = curr->next;
curr->next = newNode;
}else{
cout<<"\nSearch node not found";
}
}
}
void addAfterNthOccurrence(int searchVal,int nthOccurence,int val){ //add node after getting nth search value
if(isEmpty()){
cout<<"\nList is Empty"<<endl;
}else{
curr=head;
int counter=0;
while(curr!=NULL){
if(curr->data==searchVal){
counter++;
}
if(counter==nthOccurence){
temp = new node;
temp->data = val;
temp->next = curr->next;
curr->next = temp;
break;
}
curr=curr->next;
}
if(counter!=nthOccurence){
cout<<"\n"<<nthOccurence<<" occurrence of "<<searchVal<<" not found";
}
}
}
//delete function
void removeFirst(){// remove first node
if(isEmpty()){
cout<<endl<<"List is Empty"<<endl;
}else{
curr = head;
head = curr->next;
delete curr;
}
}
void removeLast(){//remove last node
if(isEmpty()){
cout<<endl<<"List is Empty"<<endl;
}else{
curr=head;
while(curr->next!=NULL){
temp = curr;
curr=curr->next;
}
temp->next=NULL;
delete curr;
}
}
void removeAt(int index){
if(isEmpty()){
cout<<"\nList is empty"<<endl;
}else if(sizeOflist()<index){
cout<<"\nIndex doesn't exist"<<endl;
}else if(index==0){
removeFirst();
}else if(index==sizeOflist()){
removeLast();
}else{
curr=head;
for(int i=0;i<index-1;i++){
curr=curr->next;
}
temp = curr->next;
curr->next = temp->next;
delete temp;
}
}
//optional delete function
void removeSearchNode(int searchVal){
if(isEmpty()){
cout<<"\nList is empty"<<endl;
}else if(head->data==searchVal){
removeFirst();
}else{
int counter=0;
curr = head;
while(curr!=NULL){
if(curr->next->data==searchVal){
counter++;
break;
}
curr=curr->next;
}
if(counter!=0){
temp= curr->next;
curr->next = temp->next;
delete temp;
}else{
cout<<"\nSearch node not found"<<endl;
}
}
}
void removeNthOccurrence(int searchVal,int nthOccurrence){
int index=1;
if(isEmpty()){
cout<<"\nList is empty"<<endl;
}else if(head->data==searchVal && nthOccurrence==1){
removeFirst();
}else{
int counter=0;
curr=head;
while(curr!=NULL){
if(curr->data==searchVal){
counter++;
}
if(counter==nthOccurrence){
removeAt(index);
break;
}
index++;
curr=curr->next;
}
if(counter!=nthOccurrence || nthOccurrence==0)cout<<"\n"<<nthOccurrence<<" occurrence of value "<<searchVal<<" not found";
}
}
void clearAll(){// remove all the node and set head pointer to null
temp = head;
while(temp->next!=NULL){
curr = temp;
temp=temp->next;
delete curr;
}
head=NULL;
}
//print function
void print(){
if(isEmpty()){
cout<<endl<<"List is empty"<<endl;
}else{
temp = head;
cout<<endl;
while(temp!=NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
}
}
void reversePrint(node* e){
if(e!=NULL){
reversePrint(e->next);
cout<<e->data<<" ";
}
}
//miscellaneous function
int sizeOflist(){
int counter = 1;
temp = head;
while(temp->next!=NULL){
counter++;
temp=temp->next;
}
return counter;
}
bool isEmpty(){
return head==NULL;
}
};
int main(){
int element;
List myList;
for(int i=0;i<5;i++){
cin>>element;
myList.addLast(element);
}
myList.print();
cout<<"\nReversePrint"<<endl;
myList.reversePrint(myList.getFirst());
myList.addAt(0,30);
myList.addAt(5,12);
myList.addAt(5,20);
cout<<"\nPrint";
myList.print();
myList.clearAll();// clear the list
//adding node at specific index
myList.addAt(0,30);
myList.addAt(3,12);
myList.addAt(2,45);
cout<<"\nPrint with new value";
myList.print();
cout<<"\nRemove First";
myList.removeFirst();
myList.print();
cout<<"\nRemove Last";
myList.removeLast();
myList.print();
//adding node at specific index
cout<<"\nPrint with new value";
myList.addFirst(67);
myList.addFirst(58);
myList.addFirst(90);
myList.addLast(67);
myList.print();
cout<<"\nPrint After Last Occurrence";
myList.addAfterLastOccurrence(67,91);
//myList.addAfterLastOccurence(1,91);
myList.print();
cout<<"\nPrint After First Occurrence";
myList.addAfterFirstOccurrence(67,21);
myList.print();
myList.addFirst(67);
cout<<"\nPrint After nth Occurrence";
myList.addAfterNthOccurrence(623,4,45);
myList.print();
cout<<"\nRemove at nth index";
myList.removeAt(2);
myList.print();
cout<<"\nRemove at search node";
myList.removeSearchNode(91);
myList.print();
cout<<"\nRemove nth occurrence";
myList.removeNthOccurrence(6,3);
myList.print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment