-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularList.cpp
More file actions
78 lines (69 loc) · 1.35 KB
/
circularList.cpp
File metadata and controls
78 lines (69 loc) · 1.35 KB
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
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node* prev;
Node(int val){
data = val;
next=prev=NULL;
}
};
class circularList{
Node* head;
Node* tail;
public:
circularList(){
head = tail = NULL;
}
//insert through Head
void insertAtHead(int val){
Node* newNode = new Node(val);
if(tail==NULL){
head=tail=newNode;
tail->next=head;
}
else{
newNode->next=head;
head=newNode;
tail->next=head;
}
}
//insert through Tail
void insertAtTail(int val){
Node* newNode = new Node(val);
if(tail==NULL){
head=tail=newNode;
tail->next=head;
}
else{
newNode->next=head;
tail->next=newNode;
tail=newNode;
}
}
void print(){
if(head==NULL)
return;
cout<<head->data<<" ->";
Node* temp = head->next;
while(temp !=head){
cout<<temp->data <<"->";
temp=temp->next;
}
cout<<temp->data<<endl;
}
};
int main(){
circularList cll;
cll. insertAtHead(1);
cll. insertAtHead(2);
cll. insertAtHead(3);
cll.print();
cll. insertAtTail(9);
cll. insertAtTail(8);
cll. insertAtTail(6);
cll.print();
return 0;
}