| #include<iostream.h> #include<conio.h> const int SIZE =10 ; class arrayList{ private: int list[SIZE]; int count ; public: arrayList(){ count=0;} int isFull() { return (count==SIZE);} int isEmpty() { return (count==0);} //============================================================ void insertPos ( int newdata , int pos ) { for (int last = count ; last >pos ; last --) list[last] = list[last-1]; list[pos] = newdata; count++; } //============================================================= int insertBack ( int newdata) { if(isFull()) return(0); else { list[count]=newdata; count++; return(1); } } //============================================================= int insertFront ( int newdata) { if(isFull()) return(0); else { insertPos(newdata,0); return(1); } } //============================================================= int insertConditional ( int newdata) { if(isFull()) return(0); else { int pos = count; // default at the end of the list for ( int i=0 ; i<count ; i++) if ( newdata < list[i]) { pos =i; // take the position of the number higher than newdata break; } insertPos(newdata,pos); return(1); } } //============================================================= // All the elements at the right of "pos" are shifted 1 location to the left void deletePos ( int pos) { count--; for ( int i = pos ; i<count ;i++) list[i] = list[i+1]; } //============================================================= int deleteFront ( ) { if ( isEmpty()) return 0; else { deletePos(0); return 1; } } //============================================================= int deleteBack ( ) { if ( isEmpty()) return 0; else { count--; return 1; } } //============================================================= int deleteSpecific ( int deldata ) { if ( isEmpty()) return 0; else { int pos = -1; // assume the key value not found for ( int i =0 ; i < count ; i++) if ( list[i] == deldata ) { pos = i; break; } if ( pos == -1) { cout << " Element not found "; return 0; } else { deletePos( pos ); return 1; }//end else } // end else } //============================================================= void Print() { for(int i=0 ; i<count; i++) cout <<"a["<<i<<"] : " << list[i]<<"\n" ; cout<<" Press any key to continue ..."; getch(); } }; // end of Class arrayList //============================================================ int main() { arrayList list; int choice,number,pos; while(1){ cout <<"please choose one of the following choices\n" <<" 1 -- Insert Front \n" <<" 2 -- Insert Back \n" <<" 3 -- Insert Conditional\n" <<" 4 -- Delete Front \n" <<" 5 -- Delete Back \n" <<" 6 -- Delete Conditional\n" <<" 7 -- Print the list\n" <<" 99 -- Exit\n"; cin >>choice; if (choice ==99) return 0; if ( choice == 1 || 2 || 3 || 4 ) { cout <<" Please enter your number"; cin >>number; } if (choice==1) list.insertFront(number); else if(choice==2) list.insertBack(number); else if (choice==3) list.insertConditional(number); else if(choice==4) list.deleteFront(); else if(choice==5) list.deleteBack(); else if(choice==6) list.deleteSpecific(number); else if(choice==7) list.Print(); else cout <<"Sorry, you have entered wrong number , please retry \n"; if(choice!=7) list.Print(); clrscr(); } return 0; } |