Desenvolva uma classe chamada Dequeue
para representar o tipo abstrato de dados deque, ou seja, onde os elementos podem ser inseridos e/ou removidos em ambas as extremidades da lista. Os elementos devem ser armazenados numa lista encadeada (singly linked list) genérica. A classe deve possuir um construtor que receba como parâmetro a quantidade máxima de elementos a serem armazenados na lista encadeada. Estenda a classe Dequeue
para implementar as classes Stack
e Queue
, para representar os tipos abstratos de dados pilha e fila, respectivamente (Goodrich, 2007). Forneça métodos public
para cada um dos itens a seguir da classe Queue
: a) inserir um elemento na fila, retornando o sucesso ou a falha na operação; b) remover e retornar o elemento inserido na fila; c) retornar o elemento inserido na fila, sem removê-lo; d) retornar o número de elementos inseridos na fila; e) retornar se há ou não elementos inseridos na fila. Forneça métodos public
para cada um dos itens a seguir da classe Stack
: a) inserir um elemento na pilha, retornando o sucesso ou a falha na operação; b) remover e retornar o elemento inserido na pilha; c) retornar o elemento inserido na pilha, sem removê-lo; d) retornar o número de elementos inseridos na pilha; e) retornar se há ou não elementos inseridos na pilha.
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial04.exercicio06;
/**
* Classe responsavel pela representacao do nodo
*
* @param <T> tipo generico
*/
public class Node<T>
{
/**
* Elemento do nodo
*/
private T element;
/**
* Proximo nodo
*/
private Node<T> next;
/**
* Constructor
*
* @param element elemento do nodo
*/
public Node(T element)
{
this(element, null);
}
/**
* Constructor
*
* @param element elemento do nodo
* @param next proximo nodo
*/
public Node(T element, Node<T> next)
{
this.element = element;
setNext(next);
}
/**
* Retornar o elemento do nodo
*
* @return elemento do nodo
*/
public T getElement()
{
return element;
}
/**
* Retornar o proximo nodo
*
* @return proximo nodo
*/
public Node<T> getNext()
{
return next;
}
/**
* Configurar o proximo nodo
*
* @param next proximo nodo
*/
public void setNext(Node<T> next)
{
this.next = next;
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial04.exercicio06;
/**
* Interface responsavel pela especificacao do deque
*
* @param <T> tipo generico
*/
public interface Dequeue<T>
{
/**
* Verificar se o deque esta vazio
*
* @return true se o deque esta vazio, ou false caso contrario
*/
public boolean empty();
/**
* Inserir o elemento especificado no inicio do deque
*
* @param element elemento especificado
*/
public void offerFirst(T element);
/**
* Inserir o elemento especificado no final do deque
*
* @param element elemento especificado
*/
public void offerLast(T element);
/**
* Recuperar o primeiro elemento do deque,
* ou retornar null se o deque estiver vazio
*
* @return primeiro elemento do deque, ou null se o deque estiver vazio
*/
public T peekFirst();
/**
* Recuperar o ultimo elemento do deque,
* ou retornar null se o deque estiver vazio
*
* @return ultimo elemento do deque, ou null se o deque estiver vazio
*/
public T peekLast();
/**
* Recuperar e remover o primeiro elemento do deque,
* ou retornar null se o deque estiver vazio
*
* @return primeiro elemento do deque, ou null se o deque estiver vazio
*/
public T pollFirst();
/**
* Recuperar e remover o ultimo elemento do deque,
* ou retornar null se o deque estiver vazio
*
* @return ultimo elemento do deque, ou null se o deque estiver vazio
*/
public T pollLast();
/**
* Retornar o numero de elementos do deque
*
* @return numero de elementos do deque
*/
public int size();
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial04.exercicio06;
/**
* Classe responsavel pela implementacao do deque,
* utilizando uma Singly Linked List
*
* @param <T> tipo generico
*/
public class DequeueSinglyLinkedList<T> implements Dequeue<T>
{
/**
* Primeiro nodo do deque
*/
private Node<T> header;
/**
* Limite maximo de nodos suportado
*/
private int maximum;
/**
* Inicializar o deque
*
* @param maximum limite maximo de nodos suportado
*/
public DequeueSinglyLinkedList(int maximum)
{
header = null;
if(maximum > 0)
{
this.maximum = maximum;
}
else
{
this.maximum = 0;
}
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Dequeue#empty()
*/
@Override
public boolean empty()
{
return header == null;
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Dequeue#offerFirst(java.lang.Object)
*/
@Override
public void offerFirst(T element)
{
if(empty())
{
header = new Node<T>(element);
}
else if(size() < maximum)
{
header = new Node<T>(element, header);
}
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Dequeue#offerLast(java.lang.Object)
*/
@Override
public void offerLast(T element)
{
if(empty())
{
header = new Node<T>(element);
}
else if(size() < maximum)
{
Node<T> trailer = header;
while(trailer.getNext() != null)
{
trailer = trailer.getNext();
}
Node<T> node = new Node<T>(element);
trailer.setNext(node);
}
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Dequeue#peekFirst()
*/
@Override
public T peekFirst()
{
if(!empty())
{
return header.getElement();
}
return null;
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Dequeue#peekLast()
*/
@Override
public T peekLast()
{
if(!empty())
{
Node<T> trailer = header;
while(trailer.getNext() != null)
{
trailer = trailer.getNext();
}
return trailer.getElement();
}
return null;
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Dequeue#pollFirst()
*/
@Override
public T pollFirst()
{
if(!empty())
{
Node<T> node = header;
header = header.getNext();
node.setNext(null);
return node.getElement();
}
return null;
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Dequeue#pollLast()
*/
@Override
public T pollLast()
{
if(!empty())
{
Node<T> trailer = header;
Node<T> node = null;
while(trailer.getNext() != null)
{
node = trailer;
trailer = trailer.getNext();
}
if(node != null)
{
node.setNext(null);
}
else
{
header = null;
}
return trailer.getElement();
}
return null;
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Dequeue#size()
*/
@Override
public int size()
{
Node<T> node = header;
int count = 0;
while(node != null)
{
node = node.getNext();
count = count + 1;
}
return count;
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial04.exercicio06;
/**
* Interface responsavel pela especificacao da fila
*
* @param <T> tipo generico
*/
public interface Queue<T>
{
/**
* Verificar se a fila esta vazia
*
* @return true se a fila esta vazia, ou false caso contrario
*/
public boolean empty();
/**
* Inserir o elemento especificado no fim da fila
*
* @param element elemento especificado
*/
public void offer(T element);
/**
* Recuperar o elemento no inicio da fila,
* ou retornar null se a fila estiver vazia
*
* @return elemento no inicio da fila, ou null se a fila estiver vazia
*/
public T peek();
/**
* Recuperar e remover o elemento no inicio da fila,
* ou retornar null se a fila estiver vazia
*
* @return elemento no inicio da fila, ou null se a fila estiver vazia
*/
public T poll();
/**
* Retornar o numero de elementos da fila
*
* @return numero de elementos da fila
*/
public int size();
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial04.exercicio06;
/**
* Classe responsavel pela implementacao da fila,
* utilizando uma Singly Linked List
*
* @param <T> tipo generico
*/
public class QueueSinglyLinkedList<T> extends DequeueSinglyLinkedList<T>
implements Queue<T>
{
/**
* Inicializar a fila
*
* @param maximum limite maximo de nodos suportado
*/
public QueueSinglyLinkedList(int maximum)
{
super(maximum);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Queue#offer(java.lang.Object)
*/
@Override
public void offer(T element)
{
offerLast(element);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Queue#peek()
*/
@Override
public T peek()
{
return peekFirst();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Queue#poll()
*/
@Override
public T poll()
{
return pollFirst();
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial04.exercicio06;
/**
* Interface responsavel pela especificacao da pilha
*
* @param <T> tipo generico
*/
public interface Stack<T>
{
/**
* Verificar se a pilha esta vazia
*
* @return true se a pilha esta vazia, false em caso contrario
*/
public boolean empty();
/**
* Recuperar o elemento no topo da pilha,
* ou retornar null se a pilha estiver vazia
*
* @return elemento no topo da pilha, ou null se a pilha estiver vazia
*/
public T peek();
/**
* Recuperar e remover o elemento no topo da pilha,
* ou retornar null se a pilha estiver vazia
*
* @return elemento no topo da pilha, ou null se a pilha estiver vazia
*/
public T pop();
/**
* Inserir o elemento especificado no topo da pilha
*
* @param element elemento especificado
*/
public void push(T element);
/**
* Retornar o numero de elementos na pilha
*
* @return numero de elementos na pilha
*/
public int size();
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial04.exercicio06;
/**
* Classe responsavel pela implementacao da pilha,
* utilizando uma Singly Linked List
*
* @param <T> tipo generico
*/
public class StackSinglyLinkedList<T> extends DequeueSinglyLinkedList<T>
implements Stack<T>
{
/**
* Inicializar a pilha
*
* @param maximum limite maximo de nodos suportado
*/
public StackSinglyLinkedList(int maximum)
{
super(maximum);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Stack#peek()
*/
@Override
public T peek()
{
return peekFirst();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Stack#pop()
*/
@Override
public T pop()
{
return pollFirst();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial04.exercicio06.Stack#push(java.lang.Object)
*/
@Override
public void push(T element)
{
offerFirst(element);
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial04.exercicio06;
/**
* Classe responsavel pela execucao da aplicacao
*/
public class Application
{
/**
* Construtor padrao
*/
private Application()
{
}
/**
* Metodo principal da linguagem de programacao Java
*
* @param args argumentos da linha de comando (nao utilizado)
*/
public static void main(String[] args)
{
System.out.println("Queue Test");
Queue<Integer> queue = new QueueSinglyLinkedList<Integer>(2);
System.out.println("empty: " + queue.empty());
System.out.println("offer: 9");
queue.offer(9);
System.out.println("empty: " + queue.empty());
System.out.println("poll : " + queue.poll());
System.out.println("offer: 8");
queue.offer(8);
System.out.println("peek : " + queue.peek());
System.out.println("offer: 7");
queue.offer(7);
System.out.println("offer: 6");
queue.offer(6);
System.out.println("size : " + queue.size());
System.out.println("poll : " + queue.poll());
System.out.println("poll : " + queue.poll());
System.out.println("poll : " + queue.poll());
System.out.println("empty: " + queue.empty());
System.out.println();
System.out.println("Stack Test");
Stack<Integer> stack = new StackSinglyLinkedList<Integer>(2);
System.out.println("empty: " + stack.empty());
System.out.println("push : 9");
stack.push(9);
System.out.println("empty: " + stack.empty());
System.out.println("pop : " + stack.pop());
System.out.println("push : 8");
stack.push(8);
System.out.println("peek : " + stack.peek());
System.out.println("push : 7");
stack.push(7);
System.out.println("push : 6");
stack.push(6);
System.out.println("size : " + stack.size());
System.out.println("pop : " + stack.pop());
System.out.println("pop : " + stack.pop());
System.out.println("pop : " + stack.pop());
System.out.println("empty: " + stack.empty());
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005 *
*************************************************************************/
#ifndef NODE_HPP_
#define NODE_HPP_
#include <cstdlib>
/**
* Classe responsavel pela representacao do nodo
*
* @param <T> tipo generico
*/
template <class T> class Node
{
public:
/**
* Constructor
*
* @param element elemento do nodo
*/
Node(const T element)
{
Node::element = element;
setNext(NULL);
}
/**
* Constructor
*
* @param element elemento do nodo
* @param next proximo nodo
*/
Node(const T element, Node<T>* next)
{
Node::element = element;
setNext(next);
}
/**
* Copy constructor
*/
Node(const Node<T> &other)
{
element = other.element;
next = other.next;
}
/**
* Destructor
*/
virtual ~Node()
{
setNext(NULL);
}
/**
* Retornar o elemento do nodo
*
* @return elemento do nodo
*/
T getElement() const
{
return element;
}
/**
* Retornar o proximo nodo
*
* @return proximo nodo
*/
Node<T>* getNext() const
{
return next;
}
/**
* Configurar o proximo nodo
*
* @param next proximo nodo
*/
void setNext(Node<T>* next)
{
Node::next = next;
}
private:
/**
* Elemento do nodo
*/
T element;
/**
* Proximo nodo
*/
Node<T>* next;
};
#endif
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005 *
*************************************************************************/
#ifndef DEQUEUE_HPP_
#define DEQUEUE_HPP_
#include "Node.hpp"
/**
* Interface responsavel pela especificacao do deque
*
* @param <T> tipo generico
*/
template <class T> class Dequeue
{
public:
/**
* Inicializar o deque
*
* @param maximum limite maximo de nodos suportado
*/
Dequeue(int maximum)
{
header = NULL;
if(maximum > 0)
{
Dequeue::maximum = maximum;
}
else
{
Dequeue::maximum = 0;
}
}
/**
* Copy constructor
*/
Dequeue(const Dequeue<T> &other, int maximum)
{
header = other.header;
Dequeue::maximum = maximum;
}
/**
* Destructor
*/
virtual ~Dequeue()
{
while(header != NULL)
{
Node<T>* node = header;
header = header->getNext();
free(node);
}
}
/**
* Verificar se o deque esta vazio
*
* @return true (1) se o deque esta vazio, ou false (0) caso contrario
*/
bool empty() const
{
return header == NULL;
}
/**
* Inserir o elemento especificado no inicio do deque
*
* @param element elemento especificado
*/
void offerFirst(const T element)
{
if(empty())
{
header = new Node<T>(element);
}
else if(size() < maximum)
{
header = new Node<T>(element, header);
}
}
/**
* Inserir o elemento especificado no final do deque
*
* @param element elemento especificado
*/
void offerLast(const T element)
{
if(empty())
{
header = new Node<T>(element);
}
else if(size() < maximum)
{
Node<T>* trailer = header;
while(trailer->getNext() != NULL)
{
trailer = trailer->getNext();
}
Node<T>* node = new Node<T>(element);
trailer->setNext(node);
}
}
/**
* Recuperar o primeiro elemento do deque,
* ou retornar zero se o deque estiver vazio
*
* @return primeiro elemento do deque, ou zero se o deque estiver vazio
*/
T peekFirst() const
{
if(!empty())
{
return header->getElement();
}
return 0;
}
/**
* Recuperar o ultimo elemento do deque,
* ou retornar zero se o deque estiver vazio
*
* @return ultimo elemento do deque, ou zero se o deque estiver vazio
*/
T peekLast() const
{
if(!empty())
{
Node<T>* trailer = header;
while(trailer->getNext() != NULL)
{
trailer = trailer->getNext();
}
return trailer->getElement();
}
return 0;
}
/**
* Recuperar e remover o primeiro elemento do deque,
* ou retornar zero se o deque estiver vazio
*
* @return primeiro elemento do deque, ou zero se o deque estiver vazio
*/
T pollFirst()
{
T element = 0;
if(!empty())
{
Node<T>* node = header;
header = header->getNext();
element = node->getElement();
free(node);
}
return element;
}
/**
* Recuperar e remover o ultimo elemento do deque,
* ou retornar zero se o deque estiver vazio
*
* @return ultimo elemento do deque, ou zero se o deque estiver vazio
*/
T pollLast()
{
T element = 0;
if(!empty())
{
Node<T>* trailer = header;
Node<T>* node = NULL;
while(trailer->getNext() != NULL)
{
node = trailer;
trailer = trailer->getNext();
}
node->setNext(NULL);
element = trailer->getElement();
free(trailer);
}
return element;
}
/**
* Retornar o numero de elementos do deque
*
* @return numero de elementos do deque
*/
int size() const
{
Node<T>* node = header;
int count = 0;
while(node != NULL)
{
node = node->getNext();
count = count + 1;
}
return count;
}
private:
/**
* Primeiro nodo do deque
*/
Node<T>* header;
/**
* Limite maximo de nodos suportado
*/
int maximum;
};
#endif
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005 *
*************************************************************************/
#ifndef QUEUE_HPP_
#define QUEUE_HPP_
#include "Dequeue.hpp"
/**
* Interface responsavel pela especificacao da fila
*
* @param <T> tipo generico
*/
template <class T> class Queue : private Dequeue<T>
{
public:
/**
* Inicializar a fila
*
* @param maximum limite maximo de nodos suportado
*/
Queue(int maximum) : Dequeue<T>(maximum)
{
}
/**
* Destructor
*/
virtual ~Queue()
{
}
/**
* Verificar se a fila esta vazia
*
* @return true (1) se a fila esta vazia, ou false (0) caso contrario
*/
bool empty() const
{
return Dequeue<T>::empty();
}
/**
* Inserir o elemento especificado no fim da fila
*
* @param element elemento especificado
*/
void offer(const T element)
{
Dequeue<T>::offerLast(element);
}
/**
* Recuperar o elemento no inicio da fila,
* ou retornar zero se a fila estiver vazia
*
* @return elemento no inicio da fila, ou zero se a fila estiver vazia
*/
T peek() const
{
return Dequeue<T>::peekFirst();
}
/**
* Recuperar e remover o elemento no inicio da fila,
* ou retornar zero se a fila estiver vazia
*
* @return elemento no inicio da fila, ou zero se a fila estiver vazia
*/
T poll()
{
return Dequeue<T>::pollFirst();
}
/**
* Retornar o numero de elementos da fila
*
* @return numero de elementos da fila
*/
int size() const
{
return Dequeue<T>::size();
}
};
#endif
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005 *
*************************************************************************/
#ifndef STACK_HPP_
#define STACK_HPP_
#include "Dequeue.hpp"
/**
* Interface responsavel pela especificacao da pilha
*
* @param <T> tipo generico
*/
template <class T> class Stack : private Dequeue<T>
{
public:
/**
* Inicializar a pilha
*
* @param maximum limite maximo de nodos suportado
*/
Stack(int maximum) : Dequeue<T>(maximum)
{
}
/**
* Destructor
*/
virtual ~Stack()
{
}
/**
* Verificar se a pilha esta vazia
*
* @return true (1) se a pilha esta vazia, false (0) em caso contrario
*/
bool empty() const
{
return Dequeue<T>::empty();
}
/**
* Recuperar o elemento no topo da pilha,
* ou retornar zero se a pilha estiver vazia
*
* @return elemento no topo da pilha, ou zero se a pilha estiver vazia
*/
T peek() const
{
return Dequeue<T>::peekFirst();
}
/**
* Recuperar e remover o elemento no topo da pilha,
* ou retornar zero se a pilha estiver vazia
*
* @return elemento no topo da pilha, ou zero se a pilha estiver vazia
*/
T pop()
{
return Dequeue<T>::pollFirst();
}
/**
* Inserir o elemento especificado no topo da pilha
*
* @param element elemento especificado
*/
void push(const T element)
{
Dequeue<T>::offerFirst(element);
}
/**
* Retornar o numero de elementos na pilha
*
* @return numero de elementos na pilha
*/
int size() const
{
return Dequeue<T>::size();
}
};
#endif
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005 *
*************************************************************************/
#include <iostream>
#include "Queue.hpp"
#include "Stack.hpp"
using namespace std;
/**
* Metodo principal da linguagem de programacao C++
*
* @param argc quantidade de argumentos na linha de comando (nao utilizado)
* @param argv argumentos da linha de comando (nao utilizado)
*/
int main(int argc, char** argv)
{
cout << "Queue Test" << endl;
Queue<int>* queue = new Queue<int>(2);
cout << "empty: " << queue->empty() << endl;
cout << "offer: 9" << endl;
queue->offer(9);
cout << "empty: " << queue->empty() << endl;
cout << "poll : " << queue->poll() << endl;
cout << "offer: 8" << endl;
queue->offer(8);
cout << "peek : " << queue->peek() << endl;
cout << "offer: 7" << endl;
queue->offer(7);
cout << "offer: 6" << endl;
queue->offer(6);
cout << "size : " << queue->size() << endl;
cout << "poll : " << queue->poll() << endl;
cout << "poll : " << queue->poll() << endl;
cout << "poll : " << queue->poll() << endl;
cout << "empty: " << queue->empty() << endl;
delete queue;
cout << endl;
cout << "Stack Test" << endl;
Stack<int>* stack = new Stack<int>(2);
cout << "empty: " << stack->empty() << endl;
cout << "push : 9" << endl;
stack->push(9);
cout << "empty: " << stack->empty() << endl;
cout << "pop : " << stack->pop() << endl;
cout << "push : 8" << endl;
stack->push(8);
cout << "peek : " << stack->peek() << endl;
cout << "push : 7" << endl;
stack->push(7);
cout << "push : 6" << endl;
stack->push(6);
cout << "size : " << stack->size() << endl;
cout << "pop : " << stack->pop() << endl;
cout << "pop : " << stack->pop() << endl;
cout << "pop : " << stack->pop() << endl;
cout << "empty: " << stack->empty() << endl;
delete stack;
return 0;
}
##########################################################################
# Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) #
# Ybadoo - Solucoes em Software Livre (ybadoo.com.br) #
# #
# Permission is granted to copy, distribute and/or modify this document #
# under the terms of the GNU Free Documentation License, Version 1.3 or #
# any later version published by the Free Software Foundation; with no #
# Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A #
# A copy of the license is included in the section entitled "GNU Free #
# Documentation License". #
# #
# Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) #
# gcc/g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005 #
##########################################################################
g++ -o Application.o -c Application.cpp
g++ -o application Application.o