main.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Project: Singly_Linked_List.cbp
  2. // File : main.cpp
  3. #include <iostream>
  4. #include "Node.h"
  5. #include "LinkedList.h"
  6. using namespace std;
  7. int main()
  8. {
  9. // NULL
  10. LinkedList<int> linkedList = LinkedList<int>();
  11. // 43->NULL
  12. linkedList.InsertHead(43);
  13. // 76->43->NULL
  14. linkedList.InsertHead(76);
  15. // 76->43->15->NULL
  16. linkedList.InsertTail(15);
  17. // 76->43->15->44->NULL
  18. linkedList.InsertTail(44);
  19. // Print the list element
  20. cout << "First Printed:" << endl;
  21. linkedList.PrintList();
  22. cout << endl;
  23. // 76->43->15->44->100->NULL
  24. linkedList.Insert(4, 100);
  25. // 76->43->15->48->44->100->NULL
  26. linkedList.Insert(3, 48);
  27. // 22->76->43->15->48->44->100->NULL
  28. linkedList.Insert(0, 22);
  29. // Print the list element
  30. cout << "Second Printed:" << endl;
  31. linkedList.PrintList();
  32. cout << endl;
  33. // Get value of the second index
  34. // It should be 43
  35. cout << "Get value of the second index:" << endl;
  36. Node<int> * get = linkedList.Get(2);
  37. if(get != NULL)
  38. cout << get->Value;
  39. else
  40. cout << "not found";
  41. cout << endl << endl;
  42. // Find the position of value 15
  43. // It must be 3
  44. cout << "The position of value 15:" << endl;
  45. int srch = linkedList.Search(15);
  46. cout << srch << endl << endl;
  47. // Remove first element
  48. cout << "Remove the first element:" << endl;
  49. linkedList.Remove(0);
  50. // 76->43->15->48->44->100->NULL
  51. linkedList.PrintList();
  52. cout << endl;
  53. // Remove fifth element
  54. cout << "Remove the fifth element:" << endl;
  55. linkedList.Remove(4);
  56. // 76->43->15->48->100->NULL
  57. linkedList.PrintList();
  58. cout << endl;
  59. // Remove tenth element
  60. cout << "Remove the tenth element:" << endl;
  61. linkedList.Remove(9);
  62. // Nothing happen
  63. // 76->43->15->48->100->NULL
  64. linkedList.PrintList();
  65. cout << endl;
  66. return 0;
  67. }