-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharray.c
More file actions
186 lines (175 loc) · 2.79 KB
/
array.c
File metadata and controls
186 lines (175 loc) · 2.79 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
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define N 5
int a[5],i=-1;
void insertbeg();
void insertmid();
void insertlast();
void deletebeg();
void deletemid();
void deletelast();
void displaybeg();
void displaylast();
int main()
{
int c,ch;
while(1){
printf("\nEnter Your Choice\n1 for Insertion\n2 for Deletion\n3 for Display\n4 for Exit\n");scanf("%d",&c);
switch(c)
{
case 1:
printf("\nEnter a Choice\n1 to Insert at beginning \n2 to Insert in Middle\n3 to Insert at End\n");scanf("%d",&ch);
switch(ch){
case 1:
insertbeg();
break;
case 2:
insertmid();
break;
case 3:
insertlast();
break;
}
break;
case 2:
printf("Enter a Choice\n1 to Delete at Beginning \n2 to Delete an Element at Middle\n3 to Delete at End\n");scanf("%d",&ch);
switch(ch){
case 1:
deletebeg();
break;
case 2:
deletemid();
break;
case 3:
deletelast();
break;
}
break;
case 3:
printf("\nEnter a Choice\n1 to Display from Beginning\n2 to Display from Last\n");scanf("%d",&ch);
switch(ch){
case 1:
displaybeg();
break;
case 2:
displaylast();
break;
}
break;
case 4:
exit(0);
default :
printf("\n Enter A Valid Choice:\n");
}
}
}
void insertbeg(){int n,j;
if(i==N){
printf("Overflow");
}
else{
printf("\nEnter a number to be Inserted : ");
scanf("%d",&n);
for(j = i;j>=0;j--){
a[j+1]=a[j];
}
a[0]=n;
i++;
}
}
void insertmid(){int n,p;
if(i==N){
printf("\nOverflow\n");
}
else{int j;
printf("\nEnter a number to be Inserted : ");
scanf("%d",&n);
printf("\nEnter Position at which You want to Insert : ");
scanf("%d",&p);
p=p-1;
for(j=p;j>=0;j--){
a[j+1]=a[j];
}
a[p]=n;
i++;
}
}
void insertlast(){int n;
printf("\nEnter element to be Inseted : ");
scanf("%d",&n);
i++;
a[i]=n;
}
void deletebeg(){
if(i==-1){
printf("\nUnderflow\n");
}
else
{
int j;
printf("\nDeleted %d\n",a[0]);
for(j=0;j<=i;j++){
a[j]=a[j+1];
}
i--;
}
}
void deletemid(){
if(i==-1){
printf("\nUnderflow\n");
}
else
{
int e,j,p;
printf("\nEnter Element to be Deleted\n");
scanf("%d",&e);
for(j=0;j<=i;j++){
if(a[j]==e)
{
p=j;
break;
}
}
printf("\nDeleted %d\n",a[p]);
for(j=p;j<=i;j++)
{
a[j]=a[j+1];
}
i--;
}
}
void deletelast(){
if(i==-1){
printf("\nUnderflow\n");
}
else
{
printf("\nDeleted Element %d",a[i]);
i--;
}
}
void displaybeg(){int j;
if(i==-1){
printf("\nUnderflow\n");
}
else
{
for(j=0;j<=i;j++)
{
printf("\nElement at position %d : %d",j+1,a[j]);
}
}
}
void displaylast(){int j;
if(i==-1){
printf("\nUnderflow\n");
}
else
{
for(j=i;j>=0;j--)
{
printf("\nElement at position %d : %d",j+1,a[j]);
}
}
}