Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- #include"string"
- #define size 5
- using namespace std;
- struct node
- {
- int data;
- node* next;
- };
- struct stack
- {
- int data[size];
- int top;
- };
- struct queue
- {
- int data[size];
- int front, rear;
- };
- void add(node*& head, node*& tail, int item)
- {
- node* newNode = new node;
- newNode->data = item;
- newNode->next = NULL;
- if (head == NULL)
- {
- head = tail = newNode;
- return;
- }
- else
- {
- newNode->next = head;
- head = newNode;
- }
- }
- void enqueue(queue* q, int item)
- {
- if (q->rear > size - 1)
- {
- cout << "queue is full\n";
- return;
- }
- else
- {
- q->rear++;
- q->data[q->rear] = item;
- if (q->front == -1)
- q->front = 0;
- }
- }
- int dqueue(queue* q)
- {
- int item;
- if (q->front == -1)
- {
- return -1;
- }
- else if(q->front<q->rear)
- {
- item = q->data[q->front];
- q->front++;
- return item;
- }
- else if (q->front == q->rear)
- {
- item = q->data[q->front];
- q->front = q->rear = -1;
- return item;
- }
- }
- void push(stack *stk,int item)//push function for linked stack
- {
- if (stk->top > size-1)
- {
- cout << "stack is full";
- return;
- }
- else
- {
- stk->data[stk->top++] = item;
- }
- }
- int pop(stack* stk)
- {
- int item;
- if (stk->top == -1)
- return -1;
- else
- {
- item = stk->data[--stk->top];
- return item;
- }
- }
- void store(node*& head, stack* stk, queue* q)
- {
- node* temp;
- temp = head;
- if (head == NULL)
- {
- cout << "list is empty\n";
- return;
- }
- else
- {
- while (temp != NULL)
- {
- if (temp->data % 2 == 0)
- {
- push(stk, temp->data);
- }
- else if (temp->data % 2 == 1)
- {
- enqueue(q, temp->data);
- }
- temp = temp->next;
- }
- }
- }
- int main()
- {
- node* head, * tail;
- head = tail = NULL;
- stack stk;
- stk.top = -1;
- queue q;
- q.front = -1;
- q.rear = -1;
- int item, i;
- for (i = 0; i < 10; i++)
- {
- cout << "Enter item "; cin >> item;
- add(head, tail, item);
- }
- store(head, &stk, &q);
- cout << "\n\nData of stack\n\n";
- for (i = 0; i < size + 1; i++)
- {
- item = pop(&stk);
- if (item == -1)
- cout << "\nstack is empty";
- else
- cout << "\ndata is : " << item;
- }
- cout << "\n\nData of queue\n\n";
- for (i = 0; i < size + 1; i++)
- {
- item = dqueue(&q);
- if (item == -1)
- cout << "\nqueue is empty";
- else
- cout << "\ndata is : " << item;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment