Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class ArrayEx{
- private:
- string msg;
- public:
- ArrayEx(string msg){
- this->msg = msg ;
- }
- string getMsg()const{
- return msg ;
- }
- };
- class Array {
- private:
- int *ar ;
- int count ;
- int countIndex ;
- public:
- Array(int size){
- ar = new int[size];
- count = size ;
- countIndex = -1 ;
- }
- void push_back(int val){
- ar[++countIndex] = val;
- if(countIndex + 1 > count ){
- throw ArrayEx("Array is Full");
- }
- }
- // 0 1 2 3 4
- void insert(int val , int index){
- countIndex++;
- for (int i = countIndex ; i>=index-1 ; --i)
- {
- ar[i+1] = ar[i];
- }
- ar[index-1] = val;
- if(countIndex +1 > count ){
- throw ArrayEx("Array is Full");
- }
- }
- void remove(int index){
- for (int i = index-1 ; i <= countIndex; ++i)
- {
- ar[i] = ar[i+1];
- }
- countIndex--;
- if(countIndex < -1){
- throw ArrayEx("Array is Empty");
- }
- }
- int search(int val){
- for (int i = 0; i < countIndex ; ++i)
- {
- if(ar[i]==val){
- return i+1 ;
- }
- }
- return -1; //throw ArrayEx("Item Not Found");
- }
- bool empty(){
- return countIndex == -1 ;
- }
- void display(){
- for (int i = 0; i <= countIndex ; ++i)
- {
- cout << ar[i] << endl;
- }
- }
- };
- int main(){
- Array a(6);
- a.push_back(1);
- a.push_back(2);
- a.push_back(3);
- a.push_back(4);
- a.remove(1);
- a.display();
- if(a.empty()){
- cout << "YES" << endl ;
- }else{
- cout << "NO" << endl ;
- }
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement