-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path3_DS_SLL.C
179 lines (171 loc) · 4.35 KB
/
3_DS_SLL.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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
struct node *nxt;
} * head, *newnode, *prenode, *ptr;
void createll()
{
int i = 1;
char j;
head = (struct node *)malloc(sizeof(struct node));
printf("Input data for node %d : ", i);
scanf("%d", &head->data);
head->nxt = NULL;
prenode = head;
printf("\tWant to enter more ? (y/n) : ");
scanf("%s", &j);
while (j == 'y')
{
i++;
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter data for node %d : ", i);
scanf("%d", &newnode->data);
newnode->nxt = NULL;
prenode->nxt = newnode;
prenode = newnode;
printf("\tWant to enter more ? (y/n) : ");
scanf("%s", &j);
}
}
{
printf("Node - %d | Data - %d | Address - %d | Next - %d \n",
i, ptr->data, ptr, ptr->nxt);
printf("----------------------------------------------------\n");
ptr = ptr->nxt;
i++;
} while (ptr != NULL);
}
void addnode()
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter Data of new node : ");
scanf("%d", &newnode->data);
newnode->nxt = NULL;
prenode = head;
do
{
if ((newnode->data) < (prenode->data))
{
newnode->nxt = head;
head = newnode;
break;
}
else if (newnode->data >= prenode->data && newnode->data <= (prenode->nxt)->data)
{
newnode->nxt = prenode->nxt;
prenode->nxt = newnode;
break;
}
else if (prenode->nxt == NULL)
{
prenode->nxt = newnode;
break;
}
prenode = prenode->nxt;
} while (prenode != NULL);
printf("\nNode added successfully.\n\n");
}
int delnode()
{
int del;
char c;
do
{
printf("\n\nEnter data to delete : ");
scanf("%d", &del);
prenode = head;
do
{
if (head == NULL)
{
printf("\nList is empty !!!\n");
return 0;
}
else if (prenode->data == del)
{
head = prenode->nxt;
printf("Node with matching data deleted successfully.\n");
if (head == NULL)
{
printf("List is now empty !\n");
return 0;
}
break;
}
else if ((prenode->nxt)->data == del)
{
prenode->nxt = (prenode->nxt)->nxt;
printf("Node with matching data deleted successfully.\n");
if (head == NULL)
{
printf("\nList is now empty !\n");
return 0;
}
break;
}
else
{
prenode = prenode->nxt;
if (prenode == NULL)
{
printf("\nElement not found !!!\n");
}
}
} while (prenode != NULL);
displayll();
printf("Want to delete more ? (y/n) : ");
scanf("%s", &c);
} while (c == 'y');
return 0;
}
void numberofnodes()
{
int i = 1;
ptr = head;
do
{
ptr = ptr->nxt;
i++;
} while (ptr != NULL);
printf("\n\nNumber of nodes : %d\n", i);
}
void main()
{
int c = 0;
clrscr();
while (c != 5)
{
printf("\n\n\t-: Sorted Linked List :-\n\n");
printf("1. Create New List\n");
printf("2. Display List\n");
printf("3. Add Node\n");
printf("4. Delete node\n");
printf("5. Exit\n\n");
printf("Input your choice : ");
scanf("%d", &c);
switch (c)
{
case 1:
createll();
break;
case 2:
displayll();
break;
case 3:
addnode();
break;
case 4:
delnode();
break;
case 5:
break;
default:
printf("\nPlease enter valid choice number !!!\n");
}
}
printf("Press any key to EXIT ...");
getch();
}