Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct List
- {
- int data;
- List* next;
- List* previous;
- };
- class Node
- {
- private:
- List* first;
- List* last;
- public:
- Node()
- {
- first = NULL;
- last = first;
- }
- ~Node();
- void push(int n);
- void root();
- void shift();
- void unshift(int n);
- void pop();
- };
- void Node::push(int n)
- {
- List *temp = new List;
- temp->next = NULL;
- temp->data = n;
- if (first != NULL)
- {
- temp->previous = last;
- last->next = temp;
- last = temp;
- }
- else
- {
- temp->previous = NULL;
- first = last = temp;
- }
- }
- void Node::root()
- {
- List* current = first;
- while (current)
- {
- cout << current->data << "->";
- current = current->next;
- }
- cout << "NULL" << endl << endl;
- }
- void Node::unshift(int n)
- {
- List* temp = new List;
- temp->data = n;
- temp->next = first;
- temp->previous = NULL;
- first->previous = temp;
- first = temp;
- }
- void Node::shift()
- {
- List* temp = first;
- first = first->next;
- delete[] temp;
- }
- void Node::pop()
- {
- List* temp = last;
- last = last->previous;
- last->next = NULL;
- delete[] temp;
- }
- Node::~Node()
- {
- while (first)
- {
- last = first->next;
- delete[] first;
- first = last;
- }
- }
- int main()
- {
- Node list;
- int n;
- cout << "-----Push-----" << endl;
- for (int i=1;i<6;i++)
- {
- list.push(i);
- list.root();
- }
- cout << "-----Shift-----" << endl;
- for (int i=1;i<5;i++)
- {
- list.shift();
- list.root();
- }
- cout << "-----Unshift-----" << endl;
- for (int i=1;i<6;i++)
- {
- list.unshift(i);
- list.root();
- }
- cout << "-----Pop-----" << endl;
- for (int i=1;i<6;i++)
- {
- list.pop();
- list.root();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement