-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortedList.cpp
More file actions
165 lines (136 loc) · 2.52 KB
/
Copy pathsortedList.cpp
File metadata and controls
165 lines (136 loc) · 2.52 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
#include <stdio.h>
#include <stdlib.h>
#define size 20
void display(int a[],int *place)
{
if((*place)==0)
printf("List is empty.\n");
int i;
for(i=0;i<(*place);i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
void insert(int a[],int *place)
{
int x;
if((*place)==size)
{
printf("List is full.\n");
return;
}
else
{
printf("Enter the value to be inserted : ");
scanf("%d",&x);
int j,k;
for( j=0;j<=*place;j++)
{
if(a[j]>x)
{
break;
}
}
for( k=*place;k>=j;k--)
{
a[k+1]=a[k];
}
a[j]=x;
(*place)++;
}
}
void delet(int a[],int *place)
{
if((*place)==0)
{
printf("List is empty\n\n");
return;
}
else
{
int pos;
printf("Enter the position(1 to %d) of the element to be deleted : ",(*place));
scanf("%d",&pos);
if(pos>(*place) || pos<1)
{
printf("Enter a valid position!\n");
return;
}
int i;
for(i=0;i<(*place);i++)
{
a[pos-1]=a[pos];
}
(*place)--;
}
}
void search(int a[], int *place)
{
int k;
printf("Enter the element to be searched : ");
scanf("%d",&k);
int aa=0,h=*place,mid=0;
while(aa<h)
{
mid=(aa+h)/2;
if(a[mid]>k)
{
h=mid-1;
}
else if(a[mid]<k)
{
aa=mid+1;
}
else if(a[mid]==k)
{
printf("Element found at index %d\n",mid+1);
return;
}
}
}
int findMax(int a[], int *place){
int n = *place;
}
int main()
{
int ar[size];
int place = 0;
while(1)
{
int c;
printf("Enter your choice :\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Search\n");
printf("0. Quit\n");
printf("Enter choice : ");
scanf("%d", &c);
printf("\n");
switch(c)
{
case 0:
return 0;
break;
case 1:
insert(ar, &place);
display(ar,&place);
break;
case 2:
delet(ar, &place);
display(ar,&place);
break;
case 3:
display(ar, &place);
break;
case 4:
search(ar, &place);
break;
default:
printf("Invalid input !\n\n");
break;
}
}
return 0;
}