-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path75_LinkedList-CodeChallengeDeletionOfNode.c
104 lines (92 loc) · 2.26 KB
/
75_LinkedList-CodeChallengeDeletionOfNode.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct clist
{
int data;
struct clist *link;
};
typedef struct clist node;
node * create(node *start)
{
node *temp,*ptr;
char ch;
int num;
do
{
printf("\n\t Enter the value of number : ");
scanf("%d",&num);
temp = (node*) malloc(sizeof(node));
temp->data = num ;
if(start==NULL)
{
start = temp;
ptr = start;
}
else
{
ptr->link = temp;
ptr = ptr->link; // ptr=temp
}
temp->link = start;
printf("\n\t Do you want to add more nodes (y/n) : ");
fflush(stdin);
scanf("%c",&ch);
} while(ch=='y'||ch=='Y');
return(start);
}
void display(node *start)
{
node *temp;
printf("\n\n Base address Number Link");
printf("\n ===============================");
printf("\n%10u %10d %10u",start,start->data,start->link);
for(temp=start->link;temp != start;temp=temp->link)
printf("\n%10u %10d %10u",temp,temp->data,temp->link);
getch() ;
return;
}
node* del_if_zero(node *start)
{
node *temp,*ptr,*end,*loc,*i;
char ch;
int num;
loc = start;
// following post-tested loop get holdof the address of the last node
do
{
end = loc;
loc = loc->link;
}while(loc!=start);
// temp = start; // preseving address of first node into temp
if( start->link==start && start->data==0) // only one node in list and contains 0
{
free(start) ; // garbage collection.... freeing up space
start = NULL; // circular linked list is empty
}
else if(start->data==0) // first node but not only single node
{
ptr = start;
start = start->link;
end->link = start; // assigning address of new first node into link of last node
free(ptr); // deallocation ..... freeing up space
}
return(start); // retunung the updated value of start
}
// following is the definitionof main()
int main()
{
node *start;
int choice,num,data;
char ch;
start = NULL;
system("cls");
printf("\n\t\t **** CREATE BLOCK ****\n");
start = create(start);
printf("\n Nodes before deletion");
display(start);
start = del_if_zero(start);
printf("\n Nodes after deletion, if took place");
display(start);
}
/******************** End of the Program ****************************/