/
deleteNode.c
94 lines (78 loc) · 1.6 KB
/
deleteNode.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
# include <stdio.h>
# include <stdlib.h>
struct node{
int value;
struct node* next;
}*ptr;
int delete1(int value);
int insertIntoLL(struct node *temp);
void printLL();
int calculateLength();
int calculateLength(){
struct node *runner = ptr;
//printf(" %d --> ", runner->value);
int count=0;
while(runner!=NULL){
count++;
runner=runner->next;
}
return count;
}
int delete1(int value){
struct node* runner = ptr;
int count = 0;
while(runner!=NULL){
count+=1;
if(runner->value == value){
break;
}
runner=runner->next;
}
runner=ptr;
// printf("This is the position of the element : %d\n", count);
for(int i=1; i<(count-1); i++){
runner=runner->next;
}
runner->next = runner->next->next;
}
int insertIntoLL(struct node *temp){
if(ptr==NULL){
ptr=temp;
return 1;
}
struct node *runner = ptr;
while(runner->next!=NULL)
runner=runner->next;
runner->next = temp;
temp->next=NULL;
return 1;
}
void printLL(){
struct node *runner = ptr;
//printf(" %d --> ", runner->value);
while(runner!=NULL){
printf(" %d --> ", runner->value);
runner=runner->next;
}
}
int main(){
// struct node *ptr1;
ptr = NULL;
for(int i=1; i<=10; i++){
struct node *temp = (struct node*)malloc(sizeof(struct node*));
temp -> value=i;
temp -> next=NULL;
insertIntoLL(temp);
}
struct node *temp = (struct node*)malloc(sizeof(struct node*));
temp -> value=4;
temp -> next=NULL;
insertIntoLL(temp);
struct node *temp1 = (struct node*)malloc(sizeof(struct node*));
temp1 -> value=11;
temp1 -> next=NULL;
insertIntoLL(temp1);
struct node* runner = ptr;
int a[20];
int length = 0, j;
}