Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Github: https://github.com/nezvers
- Youtube: https://www.youtube.com/channel/UCb4-Y0E6mmwjtawcitIAzKQ
- Twitter: https://twitter.com/NeZversStudio
- */
- #include <iostream>
- using namespace std;
- typedef struct SLNode
- {
- int data;
- SLNode *next = nullptr;
- SLNode(int _data, SLNode *_next)
- {
- data = _data;
- next = _next;
- }
- }SLNode;
- class SingleLinkList
- {
- public:
- SingleLinkList(): head(nullptr){cout << "List initialized" << endl;}
- void push(int _data)
- {
- SLNode *node = new SLNode(_data, head);
- head = node;
- cout << "Push data: " << node->data << endl;
- }
- void pop()
- {
- SLNode *temp = head;
- head = head->next;
- cout << "Pop: " << temp->data << endl;
- delete temp;
- }
- void print_data()
- {
- SLNode *temp = head;
- while (temp != NULL)
- {
- cout << "Print: " << temp->data << endl;
- temp = temp->next;
- }
- }
- void reverse_data()
- {
- cout << "Reversing data " << endl;
- SLNode *prev = nullptr;
- SLNode *current = head;
- SLNode *next = nullptr;
- while (current != NULL)
- {
- next = current->next;
- current->next = prev;
- prev = current;
- current = next;
- }
- head = prev;
- }
- ~SingleLinkList()
- {
- cout << "Deleting data pointers" << endl;
- if (head != NULL)
- {
- while (head != NULL)
- {
- pop();
- }
- }
- }
- private:
- SLNode *head;
- };
- int main()
- {
- SingleLinkList MyList = SingleLinkList();
- MyList.push(9);
- MyList.push(4);
- MyList.push(7);
- MyList.push(6);
- MyList.push(1);
- MyList.push(3);
- MyList.push(8);
- MyList.push(2);
- MyList.push(5);
- MyList.print_data();
- MyList.reverse_data();
- MyList.print_data();
- MyList.pop();
- MyList.pop();
- MyList.pop();
- MyList.pop();
- MyList.print_data();
- cout << "Hello world!" << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment