-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularLinkedList.cpp
67 lines (57 loc) · 1.33 KB
/
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
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void LinkedListTraversal(struct Node * head)
{
struct Node *p = head;
do
{
cout << "Element : " << p->data << endl;
p = p->next;
}while (p != head);
}
// Circular Link List Insertion
struct Node * insertAtFirst(struct Node * head, int data)
{
struct Node * ptr = new struct Node[1];
struct Node *p = head->next;
ptr -> data = data;
while (p->next != head)
{
p = p->next;
}
// At this point p points to the last node of this circular linked list
p->next = ptr;
ptr->next = head;
head = ptr;
return head;
};
int main()
{
struct Node *head;
struct Node *second;
struct Node *third;
struct Node *fourth;
head = new Node[1];
second = new Node[1];
third = new Node[1];
fourth = new Node[1];
head->data = 7;
head->next = second;
second->data = 37;
second->next = third;
third->data = 38;
third->next = fourth;
fourth->data = 70;
fourth->next = head;
// cout << "Linked list Before" << endl;
// LinkedListTraversal(head);
cout << "Linked list After" << endl;
head = insertAtFirst(head,89);
LinkedListTraversal(head);
return 0;
}