Haval1

Quasetion two Data Structure

Jun 1st, 2019
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. // ConsoleApplication2.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include"string"
  6. #define size 5
  7. using namespace std;
  8. struct node
  9. {
  10.     int data;
  11.     node* next;
  12. };
  13. struct stack
  14. {
  15.     int data[size];
  16.     int top;
  17. };
  18. struct queue
  19. {
  20.     int data[size];
  21.     int front, rear;
  22. };
  23. void add(node*& head, node*& tail, int item)
  24. {
  25.     node* newNode = new node;
  26.     newNode->data = item;
  27.     newNode->next = NULL;
  28.     if (head == NULL)
  29.     {
  30.         head = tail = newNode;
  31.         return;
  32.     }
  33.     else
  34.     {
  35.         newNode->next = head;
  36.         head = newNode;
  37.     }
  38. }
  39.  
  40. void enqueue(queue* q, int item)
  41. {
  42.     if (q->rear > size - 1)
  43.     {
  44.         cout << "queue is full\n";
  45.         return;
  46.     }
  47.     else
  48.     {
  49.         q->rear++;
  50.         q->data[q->rear] = item;
  51.         if (q->front == -1)
  52.             q->front = 0;
  53.     }
  54. }
  55. int dqueue(queue* q)
  56. {
  57.     int item;
  58.     if (q->front == -1)
  59.     {
  60.         return -1;
  61.     }
  62.     else if(q->front<q->rear)
  63.     {
  64.         item = q->data[q->front];
  65.         q->front++;
  66.         return item;
  67.     }
  68.     else if (q->front == q->rear)
  69.     {
  70.         item = q->data[q->front];
  71.         q->front = q->rear = -1;
  72.         return item;
  73.     }
  74. }
  75. void push(stack *stk,int item)//push function for linked stack
  76. {
  77.    
  78.     if (stk->top > size-1)
  79.     {
  80.         cout << "stack is full";
  81.         return;
  82.     }
  83.     else
  84.     {
  85.         stk->data[stk->top++] = item;
  86.     }
  87. }
  88. int pop(stack* stk)
  89. {
  90.     int item;
  91.     if (stk->top == -1)
  92.         return -1;
  93.     else
  94.     {
  95.         item = stk->data[--stk->top];
  96.         return item;
  97.     }
  98. }
  99. void store(node*& head, stack* stk, queue* q)
  100. {
  101.     node* temp;
  102.     temp = head;
  103.     if (head == NULL)
  104.     {
  105.         cout << "list is empty\n";
  106.         return;
  107.     }
  108.     else
  109.     {
  110.         while (temp != NULL)
  111.         {
  112.             if (temp->data % 2 == 0)
  113.             {
  114.                 push(stk, temp->data);
  115.             }
  116.             else if (temp->data % 2 == 1)
  117.             {
  118.                 enqueue(q, temp->data);
  119.             }
  120.             temp = temp->next;
  121.         }
  122.     }
  123. }
  124. int main()
  125. {
  126.     node* head, * tail;
  127.     head = tail = NULL;
  128.     stack stk;
  129.     stk.top = -1;
  130.     queue q;
  131.     q.front = -1;
  132.     q.rear = -1;
  133.     int item, i;
  134.     for (i = 0; i < 10; i++)
  135.     {
  136.         cout << "Enter item "; cin >> item;
  137.         add(head, tail, item);
  138.     }
  139.     store(head, &stk, &q);
  140.     cout << "\n\nData of stack\n\n";
  141.     for (i = 0; i < size + 1; i++)
  142.     {
  143.         item = pop(&stk);
  144.         if (item == -1)
  145.             cout << "\nstack is empty";
  146.         else
  147.             cout << "\ndata is : " << item;
  148.     }
  149.     cout << "\n\nData of queue\n\n";
  150.     for (i = 0; i < size + 1; i++)
  151.     {
  152.         item = dqueue(&q);
  153.         if (item == -1)
  154.             cout << "\nqueue is empty";
  155.         else
  156.             cout << "\ndata is : " << item;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment