-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSLLP.C
124 lines (119 loc) · 2.04 KB
/
SLLP.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
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
117
118
119
120
121
122
123
124
#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
char ch;
struct node * next;
}NODE;
NODE *newnode,*start,*tptr,*prev;
void addNode(int input)
{
// int * ptr=(int*)malloc(noe*sizeof(int));
newnode=(NODE*)malloc(1*sizeof(NODE));
newnode -> data = input;
newnode->next = NULL;
if(start==NULL)
start=newnode;
else
{
for(prev=NULL,tptr = start;tptr!=NULL && tptr->data < newnode->data;
prev=tptr, tptr=tptr->next);
if(tptr==start) //begin
{
newnode->next =tptr;
start= newnode;
}
else//mid or end
{
newnode->next =tptr;
prev->next = newnode;
}
}
SLLdisplay();
}
SLLdisplay()
{
for(printf("\n\t"),tptr = start;tptr!=NULL; tptr=tptr->next)
printf("%d ",tptr->data);
// getch();
}
void SLLreverse()
{
NODE * curr,*left , *safe;
curr=start;
left=NULL;
while(curr)
{
safe = curr->next;
curr->next = left;
left = curr;
curr=safe;
}
start=left;
}
int loop()
{
NODE * slow,*fast,*first;
fast = slow= start;
do
{
fast =fast->next;
if(fast)
fast=fast->next;
if(fast==NULL)
break;
slow= slow->next;
}while(slow!=fast);
if(slow == fast)
{
first =start;
while(slow!=first)
{
prev =slow;
slow=slow->next;
first= first->next;
}
prev->next=NULL;
printf("\n\tLoop found and removed");
}
else
printf("\n\tNot Found");
}
int findMP()
{
NODE * slow,*fast;
fast = slow= start;
while(1)
{
fast =fast->next;
if(fast)
fast=fast->next;
if(fast==NULL)
break;
slow= slow->next;
}
return slow->data;
}
int main()
{
clrscr();
addNode(80);
addNode(30);
addNode(20);
addNode(50);
addNode(10);
addNode(90);
addNode(70);
addNode(60);
addNode(100);
// SLLreverse();
// SLLdisplay();
// printf("\n\t Mid Value : %d",findMP());
for(tptr=start; tptr -> next !=NULL; tptr= tptr->next);
tptr->next = start->next->next->next->next;
loop();
SLLdisplay();
getch();
return 0 ;
}