Advertisement
Guest User

Untitled

a guest
Feb 26th, 2018
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4. int main(){
  5. List<int> x;
  6. // Insert
  7. x.InsertItem(4);
  8. x.InsertItem(3);
  9. x.InsertItem(7);
  10. x.InsertItem(9);
  11. x.InsertItem(13);
  12. x.InsertItem(2);
  13. x.InsertItem(6);
  14. // Delete
  15. x.DeleteItem(3);
  16. x.DeleteItem(9);
  17.  
  18. cout<<endl<<"After deletion process ..."<<endl
  19. <<"length is "<<x.Size()<<endl;
  20. //cout<<"the list contains : "<<endl;
  21. //x.ResetList();
  22. for(int i = 0;i<x.Size();i++){
  23. int temp ;
  24. x.GetNextItem(temp);
  25. cout<<temp<<" "<<endl;
  26. }
  27.  
  28. // RetrieveItem() function
  29. cout<<endl<<"searching for item 13 "<<endl;
  30. bool found;
  31. x.RetrieveItem(13,found);
  32. if(found)
  33. cout<<"found "<<endl;
  34. else
  35. cout<<"not found "<<endl;
  36. cout<<endl<<"searching for item 1 "<<endl;
  37. x.RetrieveItem(1,found);
  38. if(found)
  39. cout<<"found "<<endl;
  40. else
  41. cout<<"not found "<<endl;
  42.  
  43. return 0;
  44. }
  45. const int MAX_ITEMS = 30;
  46. template<class T>
  47. class List{
  48. private:
  49. int size;
  50. int currIndex;
  51. T data[MAX_ITEMS];
  52. public:
  53. List(){
  54. size = 0;
  55. currIndex = -1;
  56. }
  57. void InsertItem(T elem){
  58. if(size > 0){
  59. for(int i = 0;i<size;i++){
  60. if(min(data[i], elem) == elem){
  61. Shift(i);
  62. data[i] = elem;
  63. size++;
  64. break;
  65. }
  66. }
  67. } else {
  68. data[size] = elem;
  69. size++;
  70. }
  71. }
  72. void Shift(int index){
  73. for(int i=size-1; i>=index; --i)
  74. {
  75. data[i+1] = data[i];
  76. }
  77. }
  78. void RetrieveItem(T elem, bool& found){
  79. for(int i=0;i<size;i++){
  80. if(data[i] == elem){
  81. found = true;
  82. break;
  83. }
  84. }
  85. }
  86. void DeleteItem(T elem){
  87. for(int i=0;i<size;i++){
  88. if(elem == data[i]){
  89. for(int x = i; x < size; x++){
  90. data[x] = data[x+1];
  91. }
  92. size--;
  93. break;
  94. }
  95. }
  96. }
  97. void GetNextItem (T& elem){
  98. currIndex += 1;
  99. elem = data[currIndex];
  100. }
  101. int Size(){
  102. return size;
  103. }
  104.  
  105. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement