-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11searchAll.c
executable file
·234 lines (234 loc) · 4.59 KB
/
11searchAll.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
int count(struct node **head);
int insertLast(struct node **head,int no);
int insertFirst(struct node **head,int no);
int deleteFirst(struct node **head);
int deleteLast(struct node **head);
int deleteAtPos(struct node **head,int pos);
int insertAtPos(struct node **head,int no,int pos);
int searchFirstOcc(struct node *head,int no);
int searchLastOcc(struct node *head,int no);
int searchSecLastOcc(struct node *head,int no);
int searchAll(struct node *head,int no);
void displayLL(struct node**);
int main(){
struct node *head=NULL;
int itr1,num,no,pos;
printf("Enter number of nodes to be created.\n");
scanf("%d",&num);
for(itr1=1;itr1<=num;itr1++){
printf("Enter data of node %d.\n",itr1);
scanf(" %d",&no);
insertLast(&head,no);
}
displayLL(&head);
printf("Enter element to be searched for all Occurences.\n");
scanf(" %d",&pos);
searchAll(head,pos);
displayLL(&head);
free(head);
return 0;
}
int searchAll(struct node *head,int no){
struct node *temp=head;
int cntOcc=0;
while(temp->next!=NULL){
if(temp->data==no){
cntOcc++;
}
temp=temp->next;
}
if(temp->data==no){
cntOcc++;
}
printf("No. of Occurences of %d is %d\n",no,cntOcc);
return 0;
}
int searchFirstOcc(struct node *head,int no){
struct node *temp=head;
int cnt=1;
while(temp->data!=no){
if(temp->next==NULL){
break;
}
cnt++;
temp=temp->next;
}
if(temp->data==no){
printf("First Occurence of %d is : %d\n",no,cnt);
return 0;
}else{
printf("Element not present in list.\n");
return 1;
}
}
int searchLastOcc(struct node *head,int no){
struct node *temp=head;
int cnt,cnt1;
cnt=count(&head);
int itr1=0,arr[cnt];
while(temp->next!=NULL){
arr[itr1]=temp->data;
itr1++;
temp=temp->next;
}
arr[itr1]=temp->data;
cnt1=cnt-1;
for(itr1=cnt1;itr1>=0;itr1--){
if(arr[itr1]==no){
printf("Last Occ of %d is %d\n",no,cnt);
break;
}
cnt--;
}
return 0;
}
int searchSecLastOcc(struct node *head,int no){
struct node *temp=head;
int secLast=0,cnt,cnt1;
cnt=count(&head);
int itr1=0,arr[cnt];
while(temp->next!=NULL){
arr[itr1]=temp->data;
itr1++;
temp=temp->next;
}
arr[itr1]=temp->data;
cnt1=cnt-1;
for(itr1=cnt1;itr1>=0;itr1--){
if(arr[itr1]==no){
secLast++;
}
if(secLast==2){
break;
}
cnt--;
}
printf("Second Last Occ of %d is %d\n",no,cnt);
return 0;
}
int deleteFirst(struct node **head){
struct node *temp=*head;
if(*head==NULL){
printf("nothing to delete.\n");
}else if((*head)->next==NULL){
free(temp);
*head=NULL;
}else{
*head=temp->next;
free(temp);
}
return 0;
}
int deleteLast(struct node **head){
struct node *temp=*head;
struct node *temp1=*head;
if(*head==NULL){
printf("Nothing to delete.\n");
}else if((*head)->next==NULL){
free(temp);
head=NULL;
}else{
while(temp->next!=NULL){
temp=temp->next;
}
while(temp1->next!=temp){
temp1=temp1->next;
}
temp1->next=NULL;
free(temp);
temp=NULL;
}
return 0;
}
int deleteAtPos(struct node **head,int pos){
int cnt;
struct node *temp=*head,*temp1;
cnt=count(head);
if(*head==NULL){
printf("Nothing to delete.\n");
}else if(pos==1){
deleteFirst(head);
}else if(cnt==pos){
deleteLast(head);
}else{
while(pos-1>1){
temp=temp->next;
pos--;
}
temp1=temp->next;
temp->next=temp1->next;
free(temp1);
}
return 0;
}
int count(struct node **head){
int cnt=1;
struct node *temp=*head;
while(temp->next!=NULL){
temp=temp->next;
cnt++;
}
return cnt;
}
int insertLast(struct node **head,int no){
struct node *newNode=(struct node*)malloc(sizeof(struct node));
newNode->data=no;
newNode->next=NULL;
if(*head==NULL){
*head=newNode;
newNode->next=NULL;
}else{
struct node *temp=*head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newNode;
}
return 0;
}
int insertFirst(struct node **head,int no){
struct node *newNode=(struct node*)malloc(sizeof(struct node));
newNode->data=no;
if(*head==NULL){
*head=newNode;
newNode->next=NULL;
}else{
newNode->next=*head;
*head=newNode;
}
return 0;
}
int insertAtPos(struct node **head,int no,int pos){
int cnt=0;
struct node *temp=*head;
cnt=count(head);
if(pos==1){
insertFirst(head,no);
}else if(cnt+1==pos){
insertLast(head,no);
}else{
struct node *newNode=(struct node*)malloc(sizeof(struct node));
while(pos-1>1){
temp=temp->next;
pos--;
}
newNode->data=no;
newNode->next=temp->next;
temp->next=newNode;
}
return 0;
}
void displayLL(struct node **head){
struct node *temp=*head;
while(temp->next!=NULL){
printf("|%d|->",temp->data);
temp=temp->next;
}
printf("|%d|\n",temp->data);
}