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 | #include<iostream.h> #include<conio.h> int count=0; int list[10]; int size,i; //veribles int op,ch,dele; int flag=1; class lists { public: void input() { cout<<"ENTER ARRAYS SIZE\n"; //reading array size from user cin>>size; } void insert() { clrscr(); if(count<size) //element can inserted only if count is { less than array size. count++; //increment count by 1 when next to create cout<<"ENTER ELEMENT\n"; memory for next element. cin>>list[count]; //reading array element from user. } else //if count become equal to the array size { which means List is full. cout<<"LIST IS FULL\n"; } } void del() //Function to delete. { if(count<=0) { //List is empty if count is less than cout<<"THE LIST IS EMPTY\n"; or equal to 0. } else //choice to delete element. { cout<<"1:TO DELETE FROM TOP\n"; cout<<"2:TO DELETE FROM BOTTUM\n"; cout<<"3:TO DELETE USING ADDRESS\n"; cin>>ch; switch(ch) { case 1 : //if choice is selected 1 its easly delete by count--; decrement count by 1 to delete element from top. break; case 2 : for(i=1;i<=count;i++) //if choice is 2 selected to delete element from bottom { list[i]=list[i+1]; //update current element of aarray to next element and } count--; //decrement count by 1. break; case 3 : //if choice selected 3 to delete by entering address of for(i=1;i<=count;i++) the element. { cout<<"\n"<<i<<" "<<list[i]; //programm to display address and its elements. } cout<<"\nENTER ADDRESS TO DELETE\n"; //recieve address from user to delete. cin>>dele; list[dele]=NULL; //make that addressed memory location to NULL. for(i=dele;i<=count;i++) //update current values of array to next value of array { from specified address "dele" list[i]=list[i+1]; } count--; //decrement count by 1. break; } } } void display() //function to display elements of list. { if(count==0) //List is empty if count is equal to 0. { cout<<"THE LIST IS EMPTY\n"; } else //else display element. { cout<<"ELEMENTS IN THE LIST\n"; for(i=1;i<=count;i++) { cout<<list[i]<<","; } } } }; void main() { lists s; if(flag==1) { clrscr(); s.input(); flag=0; } cout<<"\n1:TO INSERT ITEM\n"; cout<<"2:TO DELETE ITEM\n"; cout<<"3:TO DISPLAY ITEM\n"; cin>>op; switch(op) { case 1 : s.insert(); main(); break; case 2 : s.del(); main(); break; case 3 : s.display(); main(); break; } } |
Search This Blog
Saturday, 28 December 2013
IMPLEMENT LIST USING ARRAY.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment