forked from casafurix/c-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListThroughArray
More file actions
210 lines (204 loc) · 4.65 KB
/
LinkedListThroughArray
File metadata and controls
210 lines (204 loc) · 4.65 KB
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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
struct
{
int list[MAX];
int element; //element to be inserted
int pos; //position of the element to be inserted/deleted
int length; //total elements
}l;
enum boolean
{
true, false
};
typedef enum boolean boolean;
int menu(void);
void create(void);
void insert(int, int);
void delet(int);
void find(int);
void display(void);
boolean islistfull(void);
boolean islistempty(void);
void main()
{
int ch;
int element;
int pos;
l.length = 0;
while(1)
{
ch = menu();
switch (ch)
{
case 1: l.length = 0;
create();
break;
case 2:
if (islistfull() != true)
{
printf("Enter New element: ");
scanf("%d", &element);
printf("Enter the Position : ");
scanf("%d", &pos);
insert(element, pos);
}
else
{
printf("List is Full. Cannot insert the element");
printf("\n Press any key to continue...");
getch();
}
break;
case 3:
if (islistempty() != true)
{
printf("Enter the position of element to be deleted : ");
scanf("%d", &pos);
delet(pos);
}
else
{
printf("List is Empty.");
printf("\n Press any key to continue...");
getch();
}
break;
case 4:
printf("No of elements in the list is %d", l.length);
printf("\n Press any key to continue...");
getch();
break;
case 5:
printf("Enter the element to be searched : ");
scanf("%d", &element);
find(element);
break;
case 6:
display();
break;
case 7:
printf("Exit");
exit(0);
break;
default: printf("Invalid Choice");
printf("\n Press any key to continue...");
getch();
}
}
} //function to display the list of elements
int menu()
{
int ch;
//clrscr();
printf("1. Create\n2. Insert\n3. Delete\n4. Count\n5. Find\n6. Display\n7.Exit\n\n Enter your choice : ");
scanf("%d", &ch);
printf("\n\n");
return ch;
}
void create(void)
{
int element;
int flag=1;
while(flag==1)
{
printf("Enter element : ");
scanf("%d", &element);
l.list[l.length] = element;
l.length++;
printf("To insert another element press '1' : ");
scanf("%d", &flag);
}
}
void display(void)
{
int i;
for (i=0; i<l.length; i++)
printf("Element %d : %d \n", i+1, l.list[i]);
printf("Press any key to continue...");
getch();
}
void insert(int element, int pos)
{
int i;
if (pos == 0)
{
printf("\nCannot insert an element at 0th position")
getch();
return;
}
if (pos-1 > l.length)
{
printf("\nOnly %d elements exit. Cannot insert at %d position", l.length, pos);
printf("\n Press any key to continue...");
getch();
}
else
{
for (i=l.length; i>=pos-1; i--)
{
l.list[i+1] = l.list[i];
}
l.list[pos-1] = element;
l.length++;
}
}
void delet(int pos)
{
int i;
if(pos == 0)
{
printf("\nCannot delete at an element 0th position");
getch();
return;
}
if (pos > l.length)
{
printf("\n\n Only %d elements exit. Cannot delete", l.length, pos);
printf("\n Press any key to continue...");
getch();
return;
}
for (i=pos-1; i<l.length; i++)
{
l.list[i] = l.list[i+1];
}
l.length--;
}
void find(int element)
{
int i;
int flag = 1;
for (i=0; i<l.length; i++)
{
if(l.list[i] == element)
{
printf ("%d exists at %d position",element, i+1);
flag = 0;
printf("\n Press any key to continue...");
getch();
break;
}
}
if(flag == 1)
{
printf("Element not found.\n Press any key to continue...");
getch();
}
}
boolean islistfull(void)
{
if (l.length == MAX)
return true;
else
return false;
}
boolean islistempty(void)
{
if (l.length == 0)
return true;
else
return false;
}