Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct element
- {
- int data;
- element* next;
- };
- class LinkedList
- {
- private:
- element* first;
- public:
- LinkedList()
- {
- first = NULL;
- }
- void additem(int d);
- void showitem();
- void deleteitem();
- void sum();
- };
- void LinkedList::additem(int d)
- {
- element* newfirst = new element;
- newfirst->data = d;
- newfirst->next = first;
- first = newfirst;
- }
- void LinkedList::showitem()
- {
- element* current = first;
- while (current)
- {
- cout « current->data « endl;
- current = current->next;
- }
- }
- void LinkedList::sum()
- {
- element *current = first;
- int sum = 0;
- while(current)
- {
- sum += current->data;
- current = current->next;
- }
- cout « "sum " « sum;
- }
- int main()
- {
- LinkedList li;
- li.additem(3);
- li.additem(5);
- li.additem(17);
- li.showitem();
- li.sum();
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement