Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author stillmen
- */
- public class Tester{
- public static void main(String[]args){
- Scanner sc=new Scanner(System.in);
- QueueLL q=new QueueLL();
- int masukan=sc.nextInt();
- while(masukan!=-1){
- System.out.println(q.poll());
- System.out.println(q.peek());
- q.offer(masukan);
- masukan=sc.nextInt();
- }
- }
- }
- class QueueLL {
- protected MyLinkedList linkedList;
- public QueueLL(){
- linkedList=new MyLinkedList();
- }
- public boolean isEmpty(){
- if(linkedList.size()==0){
- return true;
- }
- else{
- return false;
- }
- }
- public boolean offer(Object newObj){
- linkedList.addLast(newObj);
- return true;
- }
- public Object poll(){
- if(!this.isEmpty()){
- return linkedList.removeFirst();
- }
- else{
- return null;
- }
- }
- public Object peek(){
- if(!this.isEmpty()){
- return linkedList.get(0);
- }
- else{
- return null;
- }
- }
- public int size(){
- return linkedList.size();
- }
- }
- class MyLinkedList{
- Node head;
- Node tail;
- int count;
- MyLinkedList(Node head){
- this.head=head;
- count++;
- }
- MyLinkedList(){
- this.head=null;
- }
- boolean empty(){
- if(head==null){
- return true;
- }
- else{
- return false;
- }
- }
- void addLast(Object info){
- Node newNode=new Node(info);
- if(head==null){
- this.head=newNode;
- this.tail=newNode;
- }
- else{
- this.tail.setNext(newNode);
- this.tail=newNode;
- }
- count++;
- }
- Object removeFirst(){
- if(head==null){
- return null;
- }
- else{
- count++;
- Node removedNode=head;
- head=head.getNext();
- if(head==null){
- tail=null;
- }
- return removedNode.getInfo();
- }
- }
- Object peek(){
- if(head==null){
- return "Stack is empty";
- }
- else{
- return head;
- }
- }
- int size(){
- return this.count;
- }
- }
- class Node{
- Object info;
- Node next;
- Node(Object info){
- this.info=info;
- }
- Object getInfo(){
- return this.info;
- }
- void setInfo(Object info){
- this.info=info;
- }
- Node getNext(){
- return this.next;
- }
- void setNext(Node next){
- this.next=next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement