-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_linked.c
More file actions
49 lines (47 loc) · 1.22 KB
/
circular_linked.c
File metadata and controls
49 lines (47 loc) · 1.22 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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
void circular_linked_list_traversal(struct node *ptr){
struct node *p;
p=ptr;
do{
printf("The element is %d\n",p->data);
p=p->next;
}while(p!=ptr);
}
struct node *insertatFirst(struct node *ptr,int val){
struct node *a = (struct node *)malloc(sizeof(struct node *));
a->data=val;
struct node *p = (struct node *)malloc(sizeof(struct node));
p= ptr->next;
while(p->next!=ptr){
p = p->next;
}
p->next = a;
a->next = ptr;
ptr=a;
return ptr;
}
int main(){
struct node *head = (struct node *)malloc(sizeof(struct node));
struct node *first = (struct node *)malloc(sizeof(struct node));
struct node *second = (struct node *)malloc(sizeof(struct node));
head->data = 8;
head->next= first;
first->data = 45;
first->next= second;
second->data = 83;
second->next= head;
printf("Circular Linked list before insertion\n");
circular_linked_list_traversal(head);
head = insertatFirst(head,46);
head = insertatFirst(head,67);
head = insertatFirst(head,36);
head = insertatFirst(head,666);
printf("Circular Linked list after insertion\n");
circular_linked_list_traversal(head);
return 0;
}