2014-09-21 4 views
0

this 큐 구현 예에서, 노드 클래스 즉 노드 내부에있는 내용을 이해할 수 없습니다. 우리가 현재 가지고있는 Node 클래스 객체이거나 다른 클래스일까요? deletefirst()는 첫 번째 요소를 제거하는 방법은 무엇입니까?linkedlist를 사용하는 큐 구현

import java.io.*; 
    class Node 
    { 
       public int data; 
       public Node next; 
       public Node(int x) 
       { 
        data=x; 
       } 
       public void displayNode() 
       { 
        System.out.print(data+"  "); 
       } 
    } 
    class LinkList 
    { 
       

private Node first; 
   private Node last; 
   public LinkList() 
   { 
    first=null; 
    last=null; 
   } 
   public void insertLast(int x) 
   { 
    Node newNode=new Node(x); 
    newNode.next=null; 
    if(isEmpty()) 
     first=newNode; 
    else 
     last.next=newNode; 
    last=newNode; 
   } 
   public int deleteFirst() 
   { 
    int t=first.data; 
    if(first.next==null) 
     last=null; 
    first=first.next; 
    return t; 
   } 
   public int peekFirst() 
   { 
    return(first.data); 
   } 
   public boolean isEmpty() 
   { 
    return(first==null); 
   } 
   public void displayList() 
   { 
    Node current=first; 
    while(current!=null) 
    { 
     current.displayNode(); 
     current=current.next; 
    } 
   } 
    } 

답변

0

그것은 대기열에서 다음 노드로의 포인터를 이동하여 소자를 제거

public int deleteFirst() 
{ 
    int t=first.data; // Gets data from first Node. 
    if(first.next==null) // Checks if next element is null. If true. Queue is empty so last element is null 
    last=null; 
    first=first.next; // Sets first to the next Node. <== This is where your magic happens. 
    return t; // Returns the removed item from your Queue 
}