-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSLL
More file actions
116 lines (115 loc) · 1.92 KB
/
SLL
File metadata and controls
116 lines (115 loc) · 1.92 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
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
struct head
{
int count;
struct node *link;
};
void insert(struct head *heads)
{
struct node *current=(struct node*)malloc(sizeof(struct node));
if(heads->count==0)
{
printf("Enter the data");
scanf("%d",¤t->data);
current->link=NULL;
heads->link=current;
heads->count++;
}
else
{int pos,c=1;
printf("Enter the position :");
scanf("%d",&pos);
if(pos==1)
{
printf("Enter the data");
scanf("%d",¤t->data);
current->link=heads->link;
heads->link=current;
heads->count=heads->count+1;
}
else
{struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=heads->link;
while(c<pos-1)
{
temp=temp->link;
c++;
}
if(pos>heads->count+1)
printf("Invalid Position");
else
{
printf("Enter the data");
scanf("%d",¤t->data);
current->link=temp->link;
temp->link=current;
heads->count=heads->count+1;
}
}
}
}
void delet(struct head *heads)
{ int pos,c=1,val;
struct node *current=(struct node*)malloc(sizeof(struct node));
struct node *temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the position to delete");
scanf("%d",&pos);
if(pos==1)
{current=heads->link;
printf("\n Deleted: %d",current->data);
heads->link=current->link;
heads->count=heads->count-1;
}
else
{
temp=heads->link;
while(c<pos-1)
{
temp=temp->link;
c++;
}
if(pos>heads->count+1)
printf("Invalid Position");
else
{current=temp->link;
val=current->data;
printf("\n Deleted:",val);
temp->link=current->link;
heads->count=heads->count-1;
}
}
}
void display(struct head *heads)
{
struct node *current=(struct node*)malloc(sizeof(struct node));
current=heads->link;
while(current!=NULL)
{
printf("%d\t",current->data);
current=current->link;
}
}
void main()
{ int i;
struct head *heads=(struct head*)malloc(sizeof(struct head));
clrscr();
heads->count=0;
heads->link=NULL;
for(i=0;i<5;i++)
{
insert(heads);
display(heads);
}
for(i=0;i<5;i++)
{
delet(heads);
display(heads);
}
getch();
}