Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1.  
  2. struct element
  3.  
  4. {
  5. int data;
  6. element* next;
  7. };
  8.  
  9. class LinkedList
  10. {
  11. private:
  12. element* first;
  13. public:
  14. LinkedList()
  15. {
  16. first = NULL;
  17. }
  18. void additem(int d);
  19. void showitem();
  20. void deleteitem();
  21. void sum();
  22. };
  23.  
  24. void LinkedList::additem(int d)
  25. {
  26. element* newfirst = new element;
  27. newfirst->data = d;
  28. newfirst->next = first;
  29. first = newfirst;
  30. }
  31.  
  32. void LinkedList::showitem()
  33. {
  34. element* current = first;
  35. while (current)
  36. {
  37. cout « current->data « endl;
  38. current = current->next;
  39. }
  40.  
  41. }
  42.  
  43.  
  44.  
  45. void LinkedList::sum()
  46. {
  47. element *current = first;
  48. int sum = 0;
  49. while(current)
  50. {
  51. sum += current->data;
  52. current = current->next;
  53. }
  54. cout « "sum " « sum;
  55. }
  56.  
  57. int main()
  58. {
  59. LinkedList li;
  60. li.additem(3);
  61. li.additem(5);
  62. li.additem(17);
  63. li.showitem();
  64. li.sum();
  65. _getch();
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement