-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_array_as_ADT.c
More file actions
131 lines (119 loc) · 3.06 KB
/
01_array_as_ADT.c
File metadata and controls
131 lines (119 loc) · 3.06 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
// Array as Abstract Data Type
#include<stdio.h>
#include<stdlib.h>
// creating an abstract array
struct Array
{
int total_size;
int used_size;
int *ptr;
};
// user-define data type
typedef enum
{
t, f
}boolean;
// Creates a Dynamic Array of tSize
void createArray(struct Array *a, int tSize, int uSize)
{
a->total_size = tSize;
a->used_size = uSize;
a->ptr = (int *)malloc(tSize*sizeof(int));
// Inserting elements
int index;
printf("Enter Values in Array\n");
for (index = 0; index < a->used_size; index++)
{
printf("Enter Element %d : ", index + 1);
scanf("%d", &(a->ptr)[index]);
}
}
// Inserting Element in Array
void insert(struct Array *a, int loc)
{
int index;
if (loc > a->total_size)
printf("Invalid Location\n");
else if (a->used_size == a->total_size)
printf("Array Overflow\n");
else
{
for(index = (a->used_size - 1) ; index >= loc - 1 ; index--)
(a->ptr)[index + 1] = (a->ptr)[index];
printf("Enter Element : ");
scanf("%d", &(a->ptr)[loc - 1]);
a->used_size++;
printf("Element Inserted Successfully\n");
}
}
// Deleting Element from Array
void delete(struct Array *a, int loc)
{
int index;
if (loc > a->total_size)
printf("Invalid Location\n");
else if (a->used_size == 0)
printf("Array Underflow\n");
else
{
for(index = loc - 1 ; index < a->used_size - 1 ; index++)
(a->ptr)[index] = (a->ptr)[index + 1];
a->used_size--;
printf("Element Deleted Successfully\n");
}
}
// Displaying Elements of Array
void displayArray(struct Array *a)
{
int index;
if(a->used_size == 0)
printf("Array Underflow");
else
{
printf("\nEntered Values are as follows\n");
for (index = 0; index < a->used_size; index++)
{
printf("Element %d : %d\n", index + 1, (a->ptr)[index]);
}
}
}
int main()
{
int n, choice, loc;
boolean exit_flag = f;
printf("Enter Size of Array : ");
scanf("%d", &n);
struct Array arr;
createArray(&arr, n * 2, n);
while(exit_flag != t)
{
printf("\nEnter\n");
printf("1. To Insert an Element\n");
printf("2. To Delete an Elemrnt\n");
printf("3. To Display Array\n");
printf("0. To Exit\n");
printf("Enter your Choice : ");
scanf("%d", &choice);
switch(choice)
{
case 0 : exit_flag = t; break;
case 1 :
{
printf("\nEnter Location where you want to Insert Element : ");
scanf("%d", &loc);
insert(&arr, loc);
break;
}
case 2 :
{
printf("\nEnter Location where you want to Delete Element from : ");
scanf("%d", &loc);
delete(&arr, loc);
break;
}
case 3 : displayArray(&arr); break;
default : printf("\nInvald Choice, Try Again\n");
}
}
return 0;
}