Cod sursa(job #1226519)

Utilizator vevuiocsaIocsa Valeriu Ionut vevuiocsa Data 5 septembrie 2014 20:46:00
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <iostream>
#include <fstream>

#include <queue>

using namespace std;

struct Node
{
    int v;
    Node *next, *prev;
};

struct Queue
{
    Node *head, *tail;

    void Push(int v)
    {
        if (tail == NULL)
        {
            head = tail = new Node;
            head->v = tail->v = v;
            head->next = tail->next = NULL;
            head->prev = tail->prev = NULL;
        }
        else
        {
            tail->next = new Node;
            tail->next->v = v;
            tail->next->next = NULL;
            tail->next->prev = tail;
            tail = tail->next;
        }
    }

    void Pop()
    {
        if (head == NULL)
            return;
        if (head->next == NULL)
        {
            head = tail = NULL;
            return;
        }
        head->next->prev = NULL;
        head = head->next;
    }

    int Front()
    {
        if (head == NULL)
            return -1;
        else
            return head->v;
    }
};

int main()
{
    Queue q;
    q.head = q.tail = NULL;
    q.Push(5);
    q.Push(4);
    cout << q.Front() << "\n";
    q.Pop();
    cout << q.Front() << "\n";
    q.Pop();
    q.Push(3);
    q.Push(2);
    q.Push(1);
    cout << q.Front() << "\n";
    return 0;
}