-
Notifications
You must be signed in to change notification settings - Fork 0
/
62.LinkedList_CircularLinkedList.cpp
69 lines (63 loc) · 1.23 KB
/
62.LinkedList_CircularLinkedList.cpp
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
#include<iostream>
using namespace std;
struct node{
int data;
node* next;
};
node* create(int arr[],int num){
node *head = nullptr;
node *temp, *last;
last = head;
int i=0;
while(i<num){
temp = new node;
temp->data = arr[i];
if(head == nullptr){
head = temp;
head->next = nullptr;
last = head;
} else{
last->next = temp;
last = temp;
last->next = nullptr;
}
i++;
}
return head;
}
node* create_circular(int arr[],int num){
node *head = nullptr;
node *last,*temp;
int i=0;
while(i<num){
temp = new node;
temp->data = arr[i];
if(head==nullptr){
head = temp;
head->next = nullptr;
last = head;
} else{
last->next = temp;
last = temp;
last->next = nullptr;
}
i++;
}
last->next = head;
return head;
}
void display(node *p){
while(p){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
int main(){
int a[] = {1,2,3,4,5};
node *l1,*l2;
l1 = create(a,5);
display(l1);
l2 = create_circular(a,5);
display(l2);
}