-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circular_Linked_list.h
88 lines (83 loc) · 2.5 KB
/
Circular_Linked_list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
using namespace std;
// This is the node structure used in the Circular Linked List
struct node{
char data;
struct node* next;
node(char c){
data = c;
next = NULL;
}
};
// This class is for creation of a circular linked list
// For the use of deleting the letters at flame number
class circularLinkedList{
private:
node *head;
int size;
public:
circularLinkedList(){
head = NULL;
size = 0;
}
// Insert the data but as in this project the data is already defined as:
// F->L->A->M->E->S->F and so on
void insert(char c){
if(head == NULL){
node* n = new node(c);
head = n;
head->next = head;
size++;
}else{
node* n = new node(c);
node* temp = head;
while(temp->next!=head){
temp = temp->next;
}
temp->next = n;
n->next = head; // Making the list circular
size++;
}
}
int getSize(){
return size;
}
// When only one letter is left it will be surely at head
// So it has to be return for checking with the predefined
// Switch cases
char returnLast(){
return head->data;
}
// The Brain of the class
// Does the deleting of letter at flame number
char flames(int pos){
while(getSize()!=1){
// Since flame number being to equal to head letter number
// It becomes a special as head as to be updated
if(pos==1){
node* temp = head;
while(temp->next!=head){
temp=temp->next;
}
node* todelete = temp->next;
temp->next=temp->next->next;
head = temp->next; // Update head
delete todelete;
size--;
}else{
int count=1;
node* temp = head;
while(count!=pos-1){
temp=temp->next;
count++;
}
node* todelete = temp->next;
temp->next = temp->next->next;
delete todelete; // Deleting of the letter at flame number
size--;
head = temp->next;
}
}
return head->data; // Return the only letter left
}
};