Skip to content

Commit 9c663b4

Browse files
authored
Merge pull request #109 from Assassin-compiler/main
Create CircularLinkedlist.cpp
2 parents ffb996d + 878130b commit 9c663b4

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

CircularLinkedlist.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
/* structure for a node */
5+
class Node
6+
{
7+
public:
8+
int data;
9+
Node *next;
10+
};
11+
12+
/* Function to insert a node at the beginning
13+
of a Circular linked list */
14+
void push(Node **head_ref, int data)
15+
{
16+
Node *ptr1 = new Node();
17+
Node *temp = *head_ref;
18+
ptr1->data = data;
19+
ptr1->next = *head_ref;
20+
21+
/* If linked list is not NULL then
22+
set the next of last node */
23+
if (*head_ref != NULL)
24+
{
25+
while (temp->next != *head_ref)
26+
temp = temp->next;
27+
temp->next = ptr1;
28+
}
29+
else
30+
ptr1->next = ptr1; /*For the first node */
31+
32+
*head_ref = ptr1;
33+
}
34+
35+
/* Function to print nodes in
36+
a given Circular linked list */
37+
void printList(Node *head)
38+
{
39+
Node *temp = head;
40+
if (head != NULL)
41+
{
42+
do
43+
{
44+
cout << temp->data << " ";
45+
temp = temp->next;
46+
}
47+
while (temp != head);
48+
}
49+
}
50+
51+
/* Driver program to test above functions */
52+
int main()
53+
{
54+
/* Initialize lists as empty */
55+
Node *head = NULL;
56+
57+
/* Created linked list will be 11->2->56->12 */
58+
push(&head, 12);
59+
push(&head, 56);
60+
push(&head, 2);
61+
push(&head, 11);
62+
63+
cout << "Contents of Circular Linked List\n ";
64+
printList(head);
65+
66+
return 0;
67+
}
68+

0 commit comments

Comments
 (0)