Created
July 7, 2018 16:29
-
-
Save miladabc/b25dd1f018f638525df8351d36f8265b to your computer and use it in GitHub Desktop.
Circular Singly Linked List
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
//Milad Abbasi | |
//06-07-2018 | |
//Circular Singly Linked List | |
#include <iostream> | |
using namespace std; | |
struct Node | |
{ | |
int data; | |
Node *next; | |
}; | |
class CSList | |
{ | |
Node *last; | |
Node *tmp; | |
void createNode(int value) | |
{ | |
tmp = new Node; | |
tmp->data = value; | |
last = tmp; | |
tmp->next = last; | |
} | |
public: | |
CSList() | |
{ | |
last = NULL; | |
} | |
void insertStart(int value) | |
{ | |
if(isEmpty()) | |
createNode(value); | |
else | |
{ | |
tmp = new Node; | |
tmp->data = value; | |
tmp->next = last->next; | |
last->next = tmp; | |
} | |
} | |
void insertEnd(int value) | |
{ | |
if(isEmpty()) | |
createNode(value); | |
else | |
{ | |
tmp = new Node; | |
tmp->data = value; | |
tmp->next = last->next; | |
last->next = tmp; | |
last = tmp; | |
} | |
} | |
void insertAfter(int pos, int value) | |
{ | |
if(isEmpty()) | |
createNode(value); | |
else | |
{ | |
tmp = new Node; | |
tmp->data = value; | |
Node *current = new Node; | |
current = last->next; | |
for(int i = 1; i < pos; i++) | |
current = current->next; | |
tmp->next = current->next; | |
current->next = tmp; | |
} | |
} | |
void deleteStart() | |
{ | |
if(isEmpty()) | |
{ | |
cout<<"List is empty"; | |
return; | |
} | |
tmp = new Node; | |
tmp = last->next; | |
last->next = tmp->next; | |
delete tmp; | |
} | |
void deleteEnd() | |
{ | |
if(isEmpty()) | |
{ | |
cout<<"List is empty"; | |
return; | |
} | |
Node *current = new Node; | |
current = last->next; | |
tmp = last; | |
while(current->next != last) | |
current = current->next; | |
current->next = last->next; | |
last = current; | |
delete tmp; | |
} | |
void deletePosition(int pos) | |
{ | |
if(isEmpty()) | |
{ | |
cout<<"List is empty"; | |
return; | |
} | |
tmp = last; | |
Node *current, *previous; | |
current = last->next; | |
for(int i = 1; i < pos; i++) | |
{ | |
previous = current; | |
current = current->next; | |
} | |
previous->next = current->next; | |
} | |
bool isEmpty() | |
{ | |
return last == NULL; | |
} | |
void print() | |
{ | |
tmp = new Node; | |
tmp = last->next; | |
while(tmp != last) | |
{ | |
cout<<tmp->data<<"\t"; | |
tmp = tmp->next; | |
} | |
cout<<tmp->data<<endl; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment