NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package PQ;

import java.util.ArrayList;



public class PriorityQueue <T extends Comparable<T>> {

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main (String[] args)
{
PriorityQueue lista = new PriorityQueue();
lista.enqueue(10);
lista.enqueue(8);
lista.enqueue(30);
lista.enqueue(50);
lista.enqueue(5);

lista.enqueue(99);
lista.enqueue(40);

lista.print();

lista.dequeue();
lista.dequeue();

lista.print();

System.out.println("peek:" + lista.peek() + " size: " + lista.size() + " first: " +
lista.first + " sista: " + lista.last);

}

Node first;
Node last;
Integer nrOfElements;

public PriorityQueue ()
{
this.first = null;
this.last = null;
this.nrOfElements = 0;
}

@SuppressWarnings("unchecked")
void enqueue(T element)
{
if (this.first != null)
{
System.out.println(element.compareTo(this.first.data));
if(element.compareTo(this.first.data) < 0) //Om elementet vi stoppar in är mindre än första platsen
{
Node temp = this.first; //temp får värde på första platsen
//System.out.println(temp);
while(temp.next != null) //sålänge det finns något på andra platsen
{
temp = temp.next; //temp får värde på nästa plats
if((temp.data.compareTo(element) < 0 && temp.next.data.compareTo(element) >= 0))
{
Node tempNode = new Node(element, temp.next);
temp = new Node(temp.data, tempNode);
this.nrOfElements++;
break;
}
}
if (temp == this.last)
{
this.last.next = new Node(element, this.last.next.next);
//this.last = this.last.next;
this.nrOfElements++;
}
}
Node temp3 = this.first;
this.first = new Node(element, temp3);
}

else
{
this.first = new Node(element, this.first);
this.nrOfElements++;
}
}

void print() {
String result = "List:";
Node t = first;
while (t != null) {
result = result + ", " + t.data;
t = t.next;
}
System.out.println(result);
}

void dequeue()
{
this.first = this.first.next;
}

T peek()
{
return this.first.data;
}

int size()
{
return this.nrOfElements;
}

boolean isEmpty()
{
boolean returnValue;
if (this.nrOfElements == 0)
{
returnValue = true;
}
else
{
returnValue = false;
}
return returnValue;
}

private class Node
{
public T data;
public Node next;

public Node ()
{
this.data = null;
this.next = null;
}

public Node (T element) {
this.data = element;
this.next = null;
}

public Node (T element, Node nextNode)
{
this.data = element;
this.next = nextNode;
}
}

}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.