2019-08-21 10:10:08 +08:00
|
|
|
#include <iostream>
|
2017-12-24 01:30:49 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
struct list
|
|
|
|
{
|
|
|
|
int data[50];
|
2019-08-21 10:10:08 +08:00
|
|
|
int top = 0;
|
|
|
|
bool isSorted = false;
|
|
|
|
|
2017-12-24 01:30:49 +08:00
|
|
|
int BinarySearch(int *array, int first, int last, int x)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
if (last < first)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
int mid = (first + last) / 2;
|
|
|
|
if (array[mid] == x)
|
2017-12-24 01:30:49 +08:00
|
|
|
return mid;
|
2019-08-21 10:10:08 +08:00
|
|
|
else if (x < array[mid])
|
|
|
|
return (BinarySearch(array, first, mid - 1, x));
|
|
|
|
else if (x > array[mid])
|
|
|
|
return (BinarySearch(array, mid + 1, last, x));
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int LinarSearch(int *array, int x)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < top; i++)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
if (array[i] == x)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Search(int x)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
int pos = -1;
|
2017-12-24 01:30:49 +08:00
|
|
|
|
|
|
|
if (isSorted)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
pos = BinarySearch(data, 0, top - 1, x);
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
pos = LinarSearch(data, x);
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
if (pos != -1)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << "\nElement found at position : " << pos;
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << "\nElement not found";
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2017-12-24 01:30:49 +08:00
|
|
|
void Sort()
|
|
|
|
{
|
|
|
|
int i, j, pos;
|
2019-08-21 10:10:08 +08:00
|
|
|
for (i = 0; i < top; i++)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
int min = data[i];
|
|
|
|
for (j = i + 1; j < top; j++)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
if (data[j] < min)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
pos = j;
|
|
|
|
min = data[pos];
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
|
|
|
|
int temp = data[i];
|
|
|
|
data[i] = data[pos];
|
|
|
|
data[pos] = temp;
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
isSorted = true;
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2017-12-24 01:30:49 +08:00
|
|
|
void insert(int x)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
if (!isSorted)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
if (top == 49)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << "\nOverflow";
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
data[top] = x;
|
2017-12-24 01:30:49 +08:00
|
|
|
top++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < top - 1; i++)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
if (data[i] <= x && x <= data[i + 1])
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
pos = i + 1;
|
2017-12-24 01:30:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
if (pos == 0)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
pos = top - 1;
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2017-12-24 01:30:49 +08:00
|
|
|
for (int i = top; i > pos; i--)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
data[i] = data[i - 1];
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
top++;
|
2019-08-21 10:10:08 +08:00
|
|
|
data[pos] = x;
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Remove(int x)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
int pos = Search(x);
|
|
|
|
cout << "\n"
|
|
|
|
<< data[pos] << " deleted";
|
2017-12-24 01:30:49 +08:00
|
|
|
for (int i = pos; i < top; i++)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
data[i] = data[i + 1];
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
top--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Show()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < top; i++)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << data[i] << "\t";
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
list L;
|
|
|
|
int choice;
|
|
|
|
int x;
|
|
|
|
do
|
2019-08-21 10:10:08 +08:00
|
|
|
{
|
|
|
|
cout << "\n1.Insert";
|
|
|
|
cout << "\n2.Delete";
|
|
|
|
cout << "\n3.Search";
|
|
|
|
cout << "\n4.Sort";
|
|
|
|
cout << "\n5.Print";
|
|
|
|
cout << "\n\nEnter Your Choice : ";
|
|
|
|
cin >> choice;
|
2017-12-24 01:30:49 +08:00
|
|
|
switch (choice)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
case 1:
|
|
|
|
cout << "\nEnter the element to be inserted : ";
|
|
|
|
cin >> x;
|
|
|
|
L.insert(x);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
cout << "\nEnter the element to be removed : ";
|
|
|
|
cin >> x;
|
|
|
|
L.Remove(x);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
cout << "\nEnter the element to be searched : ";
|
|
|
|
cin >> x;
|
|
|
|
L.Search(x);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
L.Sort();
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
L.Show();
|
|
|
|
break;
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
} while (choice != 0);
|
2017-12-24 01:30:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|