-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcirculare linked list deletion.c
More file actions
103 lines (99 loc) · 1.99 KB
/
Copy pathcirculare linked list deletion.c
File metadata and controls
103 lines (99 loc) · 1.99 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
void traverse(struct node *head)
{
struct node *temp=NULL;
temp=head->link;
if(head==NULL)
{
printf("link is empty\n");
}
do
{
printf("%d<=>",temp->data);
temp=temp->link;
}
while(temp!=head->link);
{
printf("%d",temp->data);
}
}
struct node* insert_begin(struct node *head,int data)
{
struct node *new=(struct node*)malloc(sizeof(struct node));
new->data=data;
new->link=NULL;
new->link=head->link;
head->link=new;
return head;
}
struct node* del_begin(struct node *head)
{
struct node *temp=head->link;
head->link=temp->link;
free(temp);
temp=NULL;
return head;
}
struct node* del_last(struct node *head)
{
struct node *temp=head->link;
while(temp->link!=head)
{
temp=temp->link;
}
struct node *new=head->link;
head->link=temp->link;
free(new);
new=NULL;
temp->link=head->link;
return head;
}
struct node* del_random(struct node *head,int pos)
{
struct node *temp=head->link;
struct node *new=head->link;
if(head==NULL)
{
printf("empty\n");
}
while(pos!=1)
{
new=head;
temp=temp->link;
pos--;
}
new->link=temp->link;
free(temp);
return head;
}
int main()
{
struct node *head=(struct node*)malloc(sizeof(struct node));
head->data=10;
head->link=head;
head=insert_begin(head,20);
traverse(head);
printf("\ndelet at begin\n");
head=del_begin(head);
traverse(head);
head=insert_begin(head,30);
traverse(head);
printf("\n delet at last\n");
head=del_last(head);
traverse(head);
printf("\nafter inserting\n");
head=insert_begin(head,40);
traverse(head);
printf("\nafter inserting\n");
head=insert_begin(head,50);
traverse(head);
printf("\ndelet at random\n");
head=del_random(head,2);
traverse(head);
}