Queues
Java
Node for Queue
Node.java
public class Node {
int data;
Node next = null;
public Node(int d) {
data = d;
}
void appendToTail(int d) {
System.out.println("Adding " + d + "...");
Node n = this;
while (n.next != null) {
n = n.next;
}
n.next = new Node(d);
this.printList();
}
Node removeNode(int d) {
System.out.println("Removing " + d + "...");
Node n = this;
if (n.data == d) {
this.printList();
return this.next;
}
while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next;
this.printList();
return this;
}
n = n.next;
}
this.printList();
return null;
}
void printList() {
Node n = this;
while (n.next != null) {
System.out.print(n.data + " ");
n = n.next;
}
System.out.print(n.data);
System.out.println();
System.out.println();
}
}
Structure of Queue
Queue.java
public class Queue {
Node head, tail;
public Queue(int d) {
head = new Node(d);
tail = head;
}
void enqueue(int d) {
System.out.println("Enqueuing " + d + "...");
Node n = head;
if (head == null) {
head = new Node(d);
tail = head;
}
else {
tail.next = new Node(d);
tail = tail.next;
}
printQueue();
}
Node dequeue() {
System.out.println("Dequeuing...");
if (head == null) {
printQueue();
return null;
}
Node n = head;
head = head.next;
printQueue();
return n;
}
void printQueue() {
Node n = head;
while (n.next != null) {
System.out.print(n.data + " ");
n = n.next;
}
System.out.print(n.data);
System.out.println();
System.out.println();
}
}
Implementation of Queue
ImplementQueue.java
public class ImplementQueue {
public static void main(String[] args) {
System.out.println("Initializing Queue with " + 1);
Queue queue = new Queue(1);
queue.printQueue();
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
queue.dequeue();
queue.enqueue(6);
queue.enqueue(7);
queue.dequeue();
queue.enqueue(8);
}
}
C
Header File for Node
Node_c.h
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *next;
} Node;
void appendToTail(Node *head, int d);
Node* removeNode(Node *head, int d);
void printList(Node *head);
Node for Queue
Node.c
#include "Node_c.h"
void appendToTail(Node *head, int d) {
printf("Adding %d...\n", d);
Node *n = head;
while (n->next != NULL) {
n = n-> next;
}
Node *newNode = malloc(sizeof(Node));
newNode->data = d;
n->next = newNode;
printList(head);
}
Node* removeNode(Node *head, int d) {
printf("Removing %d...\n", d);
Node *n = head;
if (n->data == d) {
printList(head);
return head->next;
}
while (n->next != NULL) {
if (n->next->data == d) {
n->next = n->next->next;
printList(head);
return head;
}
n = n->next;
}
printList(head);
return NULL;
}
void printList(Node *head) {
Node *n = head;
while (n->next != NULL) {
printf("%d ", n->data);
n = n->next;
}
printf("%d", n->data);
printf("\n\n");
}
Header File for Queue
Queue_c.h
#include "Node_c.h"
void enqueue(Node **head, Node **tail, int d);
Node* dequeue(Node **head);
void printQueue(Node *head);
Structure of Queue
Queue.c
#include "Queue_c.h"
void enqueue(Node **head, Node **tail, int d) {
printf("Enqueuing %d...\n", d);
Node *n = *head;
if (*head == NULL) {
(*head)->data = d;
*tail = *head;
}
else {
Node *newNode = malloc(sizeof(Node));
newNode->data = d;
(*tail)->next = newNode;
*tail = (*tail)->next;
}
printQueue(*head);
}
Node* dequeue(Node **head) {
printf("Dequeuing...\n");
if (*head == NULL) {
printQueue(*head);
return NULL;
}
Node *n = *head;
*head = (*head)->next;
printQueue(*head);
return n;
}
void printQueue(Node *head) {
Node *n = head;
while (n->next != NULL) {
printf("%d ", n->data);
n = n->next;
}
printf("%d", n->data);
printf("\n\n");
}
Implementation of Queue
ImplementQueue.c
#include "Queue_c.h"
int main() {
printf("Initializing Queue with %d\n", 1);
Node *head = malloc(sizeof(Node));
head->data = 1;
Node *tail = head;
printQueue(head);
enqueue(&head, &tail, 2);
enqueue(&head, &tail, 3);
enqueue(&head, &tail, 4);
enqueue(&head, &tail, 5);
dequeue(&head);
enqueue(&head, &tail, 6);
enqueue(&head, &tail, 7);
dequeue(&head);
enqueue(&head, &tail, 8);
return 0;
}
C++
Header File for Node
Node.h
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next = NULL;
Node(int d) {
data = d;
}
~Node() {};
void appendToTail(int d);
Node* removeNode(int d);
void printList();
};
Node for Queue
Node.cpp
#include "Node.h"
void Node::appendToTail(int d) {
cout << "Adding " << d << "...\n";
Node *n = this;
while (n->next != NULL) {
n = n->next;
}
n->next = new Node(d);
this->printList();
}
Node* Node::removeNode(int d) {
cout << "Removing " << d << "...\n";
Node *n = this;
if (n->data == d) {
this->printList();
return this->next;
}
while (n->next != NULL) {
if (n->next->data == d) {
n->next = n->next->next;
this->printList();
return this;
}
n = n->next;
}
this->printList();
return NULL;
}
void Node::printList() {
Node *n = this;
while (n->next != NULL) {
cout << n->data << " ";
n = n->next;
}
cout << n->data;
cout << "\n\n";
}
Header File for Queue
Queue.h
#include "Node.h"
#include <iostream>
using namespace std;
class Queue {
private:
Node *head = NULL;
Node *tail = NULL;
public:
Queue(int d) {
head = new Node(d);
tail = head;
}
~Queue() {};
void enqueue(int d);
Node* dequeue();
void printQueue();
};
Structure of Queue
Queue.cpp
#include "Queue.h"
void Queue::enqueue(int d) {
cout << "Enqueuing " << d << "...\n";
Node *n = this->head;
if (this->head == NULL) {
this->head = new Node(d);
this->tail = this->head;
}
else {
this->tail->next = new Node(d);
this->tail = this->tail->next;
}
this->printQueue();
}
Node* Queue::dequeue() {
cout << "Dequeuing...\n";
if (this->head == NULL) {
this->printQueue();
return NULL;
}
Node *n = this->head;
this->head = this->head->next;
this->printQueue();
return n;
}
void Queue::printQueue() {
Node *n = this->head;
while (n->next != NULL) {
cout << n->data << " ";
n = n->next;
}
cout << n->data;
cout << "\n\n";
}
Implementation of Queue
ImplementQueue.cpp
#include "Queue.h"
int main() {
cout << "Initializing Queue with " << 1 << "\n";
Queue *queue = new Queue(1);
queue->printQueue();
queue->enqueue(2);
queue->enqueue(3);
queue->enqueue(4);
queue->enqueue(5);
queue->dequeue();
queue->enqueue(6);
queue->enqueue(7);
queue->dequeue();
queue->enqueue(8);
return 0;
}
C#
Node for Queue
Node.cs
using System;
public class Node
{
public int data;
public Node next = null;
public Node(int d)
{
data = d;
}
public void appendToTail(int d)
{
Console.WriteLine("Adding " + d + "...");
Node n = this;
while (n.next != null) { }
{
n = n.next;
}
n.next = new Node(d);
this.printList();
}
public Node removeNode(int d)
{
Console.WriteLine("Removing " + d + "...");
Node n = this;
if (n.data == d)
{
this.printList();
return this.next;
}
while (n.next != null)
{
if (n.next.data == d)
{
n.next = n.next.next;
this.printList();
return this;
}
n = n.next;
}
this.printList();
return null;
}
public void printList()
{
Node n = this;
while (n.next != null)
{
Console.WriteLine(n.data + " ");
n = n.next;
}
Console.WriteLine(n.data + "\n\n");
}
}
Structure of Queue
Queue.cs
using System;
public class Queue
{
Node head, tail;
public Queue(int d)
{
head = new Node(d);
tail = head;
}
public void enqueue(int d)
{
Console.WriteLine("Enqueuing " + d + "...");
Node n = head;
if (head == null)
{
head = new Node(d);
tail = head;
}
else
{
tail.next = new Node(d);
tail = tail.next;
}
printQueue();
}
public Node dequeue()
{
Console.WriteLine("Dequeuing...");
if (head == null)
{
printQueue();
return null;
}
Node n = head;
head = head.next;
printQueue();
return n;
}
public void printQueue()
{
Node n = head;
while (n.next != null)
{
Console.Write(n.data + " ");
n = n.next;
}
Console.Write(n.data + "\n\n");
}
}
Implementation of Queue
ImplementQueue.cs
using System;
public class ImplementQueue
{
public static void Main() {
Console.WriteLine("Initializing Queue with " + 1);
Queue queue = new Queue(1);
queue.printQueue();
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
queue.dequeue();
queue.enqueue(6);
queue.enqueue(7);
queue.dequeue();
queue.enqueue(8);
}
}
Python
Node for Queue
Node.py
import sys
class Node:
data = None
next = None
def __init__(self, d):
self.data = d;
def appendToTail(self, d):
print 'Adding ' + str(d) + '...'
n = self
while (n.next != None):
n = n.next
n.next = Node(d)
self.printList()
def removeNode(self, d):
print 'Removing ' + str(d) + '...'
n = self
if (n.data == d):
self.printList()
return self.next
while (n.next != None):
if (n.next.data == d):
n.next = n.next.next
self.printList()
return self
n = n.next
self.printList()
return None
def printList(self):
n = self
while (n.next != None):
sys.stdout.write(str(n.data) + ' ')
n = n.next
sys.stdout.write(str(n.data))
print '\n\n'
Structure of Queue
Queue.py
import Node
import sys
class Queue:
head = None
tail = None
def __init__(self, d):
self.head = Node.Node(d)
self.tail = self.head
def enqueue(self, d):
print 'Enqueuing ' + str(d) + '...'
n = self.head
if (self.head == None):
self.head = Node.Node(d)
self.tail = self.head
else:
self.tail.next = Node.Node(d)
self.tail = self.tail.next
self.printQueue()
def dequeue(self):
print 'Dequeuing...'
if (self.head == None):
self.printQueue()
return None
n = self.head
self.head = self.head.next
self.printQueue()
return n
def printQueue(self):
n = self.head
while (n.next != None):
sys.stdout.write(str(n.data) + ' ')
n = n.next
sys.stdout.write(str(n.data))
print '\n\n'
Implementation of Queue
ImplementQueue.py
import Queue
print 'Initializing Queue with ' + str(1)
queue = Queue.Queue(1)
queue.printQueue();
queue.enqueue(2)
queue.enqueue(3)
queue.enqueue(4)
queue.enqueue(5)
queue.dequeue()
queue.enqueue(6)
queue.enqueue(7)
queue.dequeue()
queue.enqueue(8)
Console Output
ImplementList.cpp
Initializing Queue with 1
1
Enqueuing 2...
1 2
Enqueuing 3...
1 2 3
Enqueuing 4...
1 2 3 4
Enqueuing 5...
1 2 3 4 5
Dequeuing...
2 3 4 5
Enqueuing 6...
2 3 4 5 6
Enqueuing 7...
2 3 4 5 6 7
Dequeuing...
3 4 5 6 7
Enqueuing 8...
3 4 5 6 7 8