-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularlinked.c
More file actions
80 lines (79 loc) · 1.95 KB
/
circularlinked.c
File metadata and controls
80 lines (79 loc) · 1.95 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
struct node {
int data ;
struct node * next;
};
void traversal(struct node *head){
struct node *ptr = head;
do{
printf("Element %d \n", ptr-> data);
ptr = ptr ->next;
}while(ptr !=head);
}
struct node*deletefirstelement(struct node*head){
struct node*q= head;
struct node *p = head->next;
while (p->next != head){
p= p->next;
}
p->next = q->next;
head = q->next;
free(q);
return head;
}
struct node* insertfirst(struct node* head, int data){
struct node*ptr = (struct node*) malloc(sizeof(struct node));
struct node *p = head ->next;
ptr ->data = data;
while (p->next != head){
p=p->next;
}
p->next = ptr;
ptr -> next = head;
head = ptr;
return head;
}
struct node* insertindex(struct node*head , int data , int postion){
struct node *ptr = (struct node* )malloc(sizeof(struct node));
struct node *p = head;
ptr -> data = data;
int i =0;
while (i != postion-1){
p=p->next;
i++;
}
ptr -> next = p->next;
p-> next = ptr;
return head;
}
int main (){
struct node *head ;
struct node *second;
struct node * third;
struct node * fourth ;
head = (struct node *)malloc (sizeof(struct node));
second = (struct node*)malloc(sizeof(struct node));
third = (struct node *)malloc(sizeof(struct node));
fourth = (struct node*)malloc (sizeof(struct node));
head -> data = 32;
head -> next = second;
second -> data = 72;
second -> next = third;
third -> data = 992;
third -> next = fourth;
fourth -> data = 320;
fourth -> next = head;
traversal(head);
printf(" \n");
head = insertfirst(head, 54);
head = insertfirst(head, 77);
traversal(head);
printf(" \n");
insertindex(head , 45 , 4);
traversal (head);
printf(" \n");
head = deletefirstelement(head);
traversal(head);
return 0;
}