Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Data
- {
- private:
- int m_items;
- struct Node
- {
- int data;
- Node *next;
- Node *prev;
- };
- Node *head;
- Node *tail;
- Node *curr;
- public:
- Data();
- ~Data();
- bool isFull();
- bool isEmpty();
- void put_tail(int item);
- void put_head(int item);
- void stack();
- void queue();
- int count();
- void report();
- };
- Data::Data()
- {
- tail = NULL;
- head = NULL;
- m_items = 0;
- }
- Data::~Data()
- {
- Node *tmp;
- while (head!=NULL)
- {
- tmp=head;
- head=head->next;
- delete tmp;
- }
- tail = NULL;
- }
- void Data::report()
- {
- cout << endl;
- if (m_items==0)
- {
- cout << "--structure is empty--" << endl;
- }
- else
- {
- cout << "items in structure are: " << endl;
- cout << "--------head--------" << endl;
- int i=0;
- for(Node *p=head;(i<m_items);p=p->next,i++)
- {
- cout << i+1 << ": " << p->data << endl;
- }
- cout << "--------tail-------" << endl;
- cout << endl;
- }
- }
- bool Data::isEmpty()
- {
- return (m_items == 0);
- }
- int Data::count()
- {
- return m_items;
- }
- void Data::put_tail(int item)
- {
- cout << "putting item to the tail: " << item << endl;
- Node *add=new Node;
- add->data=item;
- add->next=NULL;
- add->prev=NULL;
- m_items++;
- if (head == NULL)
- {
- head = add;
- tail=add;
- }
- else
- {
- curr=tail;
- add->prev=curr;
- tail->next=add;
- }
- tail= add;
- curr=add;
- }
- void Data::put_head(int item)
- {
- cout << "putting item to the head: " << item << endl;
- Node *add=new Node;
- add->data=item;
- add->next=NULL;
- add->prev=NULL;
- m_items++;
- if (head == NULL)
- {
- head=add;
- tail=add;
- }
- else
- {
- curr=head;
- add->next=curr;
- head->prev=add;
- }
- head=add;
- curr=add;
- }
- void Data::queue()
- {
- if (isEmpty())
- {
- cout << "structure is empty"<<endl;
- }
- else
- {
- Node *tmp;
- tmp=head;
- m_items--;
- head->prev=NULL;
- head->next->prev=NULL;
- head->next=NULL;
- head=tmp->next;
- cout << "removing item from head: ";
- cout << tmp->data<<endl;
- delete tmp;
- if (m_items==0)
- {
- head=NULL;
- tail=NULL;
- }
- }
- }
- void Data::stack()
- {
- if (isEmpty())
- {
- cout << "structure is empty" <<endl;
- }
- else
- {
- Node *tmp;
- tmp=tail;
- tail->next=NULL;
- tail->prev->next=NULL;
- tail=tmp->prev;
- cout << "removing item from tail: ";
- cout <<tmp->data<< endl;
- m_items--;
- delete tmp;
- if (m_items==0)
- {
- head=NULL;
- tail=NULL;
- }
- }
- }
- int main()
- {
- Data data;
- data.put_tail(1);
- data.put_tail(2);
- data.put_tail(3);
- data.put_tail(4);
- data.put_tail(5);
- data.report();
- cout << endl;
- data.queue();
- data.report();
- data.queue();
- data.report();
- data.queue();
- data.report();
- data.queue();
- data.report();
- data.queue();
- data.report();
- cout << endl;
- /*
- data.put_head(1);
- data.report();
- data.put_head(2);
- data.report();
- data.put_head(3);
- data.report();
- data.put_head(4);
- data.report();
- data.put_head(5);
- data.report();
- cout << endl;
- data.stack();
- data.report();
- data.stack();
- data.report();
- data.stack();
- data.report();
- data.stack();
- data.report();
- data.stack();
- data.report();
- cout<< endl;
- data.put_head(1);
- data.report();
- data.put_head(2);
- data.report();
- data.put_tail(3);
- data.report();
- data.stack();
- data.report();
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment