Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct queue_node
- {
- queue_node* next;
- int data;
- };
- typedef queue_node* queue;
- void InitQueue (queue &q)
- {
- q = nullptr; // q = NULL;
- }
- void PutQ (queue &q, int data)
- {
- queue new_node = (queue)malloc(sizeof(queue_node));
- new_node->data = data;
- if (q == nullptr)
- {
- new_node->next = new_node;
- }
- else
- {
- queue head = q->next;
- q->next = new_node;
- new_node->next = head;
- }
- q = new_node;
- }
- int GetQ(queue &q)
- {
- if (q != nullptr)
- {
- queue p = q->next;
- int data = p->data;
- if (p == q) q = nullptr;
- else
- {
- q->next = p->next;
- }
- free(p);
- return data;
- }
- else
- {
- cout << "Queue is empty" << endl;
- return -1;
- }
- }
- int main()
- {
- queue q;
- InitQueue(q);
- PutQ(q, 1);
- PutQ(q, 2);
- PutQ(q, 3);
- cout << GetQ(q) << ' ' << GetQ(q) << ' ' << GetQ(q) << ' ' << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment