// 106677.cpp #include <stdio.h> class List { private: int* array; int size; int index; public: List(int s) { size = s; array = new int[size]; index = 0; } List() { size = 20; array = new int[20]; index = 0; } void Add(int n); void Delete(int n); void Sort(); int GetSize() { return index; } int BinarySearch(int n); void Print(); }; void List::Add(int n) { array[index] = n; index++; } void List::Delete(int n) { int i,k; for (i=0; i<index; i++) { if (array[i]==n) break; } if (i>=index) return; // n is not in the list for (k=i; k<index-1; k++) array[k] = array[k+1]; index--; } void List::Sort() // Bubble sort { int i,j; int temp; for (i=0; i<index; i++) { for (j=i+1; j<index; j++) { if (array[i]<array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } int List::BinarySearch(int n) { int left=0; int right=index-1; int middle; while(left<right) { middle = (left+right)/2; if(array[middle]==n) return middle; if(array[middle]>n) left=middle+1; if(array[middle]<n) right=middle; } return -1; } void List::Print() { int i; for (i=0; i<index; i++) { printf("%d ", array[i]); } printf("\n"); } void main() { int sample[5]={43,67,11,78,52}; List list(30); int flag; for (int i=0; i<5; i++) list.Add(sample[i]); printf("Printing 5 elements in the list...\n"); list.Print(); printf("The length of the list is %d\n", list.GetSize()); printf("Adding item 60...\n"); list.Add(60); list.Print(); printf("The length of the list is %d\n", list.GetSize()); printf("Sorting the list in descending order...\n"); list.Sort(); list.Print(); printf("Deleting item 52...\n"); list.Delete(52); list.Print(); printf("The length of the list is %d\n", list.GetSize()); flag = list.BinarySearch(70); if(flag<0) printf("Item 70 not found in the list\n"); else printf("Item 70 is at the %dth position of the list\n",flag); }