forked from msraghavganesh/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_singly_linked_list.c
57 lines (50 loc) · 1.06 KB
/
circular_singly_linked_list.c
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
#include<stdlib.h>
#include<stdio.h>
struct node{
int data;
struct node* link;
};
//adding element in empty list
struct node* addToEmpty(int data)
{
struct node* temp=malloc(sizeof(struct node));
temp->data=data;
temp->link=temp;
return temp;
}
//adding element at beginning
struct node* addToBeg(struct node*tail,int data)
{
struct node *newP=malloc(sizeof(struct node));
newP->data=data;
newP->link=tail->link;
tail->link=newP;
return tail;
}
//adding element at end
struct node* addAtEnd(struct node* tail,int data)
{
struct node* newP=malloc(sizeof(struct node));
newP->data=data;
newP->link=NULL;
newP->link=tail->link;
tail->link=newP;
tail=tail->link;
return tail;
}
void print(struct node* tail)
{
struct node* p=tail->link;
do{
printf("%d\n",p->data);
p=p->link;
}while(p!=tail->link);
}
int main()
{struct node*tail;
tail=addToEmpty(45);
tail=addToBeg(tail,34);
tail=addAtEnd(tail,50);
print(tail);
return 0;
}