List Stack Queue 20201127 1 node class List

  • Slides: 116
Download presentation
List, Stack, Queue 2020/11/27 �. ����� 1

List, Stack, Queue 2020/11/27 �. ����� 1

������� node class List. Node { // Constructors List. Node( Object the. Element )

������� node class List. Node { // Constructors List. Node( Object the. Element ) { this( the. Element, null ); } the. Element List. Node( Object the. Element, List. Node n ) { the. Element element = the. Element; next = n; } 2020/11/27 �. ����� ��� 10

// Friendly data; accessible by other package routines Object element; List. Node next; Instance

// Friendly data; accessible by other package routines Object element; List. Node next; Instance variables } 2020/11/27 �. ����� 11

������� iterator public class Linked. List. Itr { List. Node current; // ��������� /**

������� iterator public class Linked. List. Itr { List. Node current; // ��������� /** * @param the. Node ��������� */ Linked. List. Itr( List. Node the. Node ) { current = the. Node; } 2020/11/27 �. ����� 13

/** ��� �� current ���������� * @return true ��� current ���� null */ public

/** ��� �� current ���������� * @return true ��� current ���� null */ public boolean is. Past. End( ) { return current == null; } /** * @return item ������ current ����� null ������� * current ��������� */ public Object retrieve( ) { return is. Past. End( ) ? �null : current. element; 2020/11/27. ����� 14 }

������� linked list public class Linked. List { private List. Node header; public Linked.

������� linked list public class Linked. List { private List. Node header; public Linked. List( ) { header = new List. Node( null ); } public boolean is. Empty( ) { return header. next == null; } 2020/11/27 �. ����� 16

/** ���������� */ public void make. Empty( ) { header. next = null; }

/** ���������� */ public void make. Empty( ) { header. next = null; } /** * �� ����� �iterator ���� header node. */ public Linked. List. Itr zeroth( ) { return new Linked. List. Itr( header ); } 2020/11/27 �. ����� 17

/* �� ����� �iterator ���� node ������ header )���� null ��������� )*/ public Linked.

/* �� ����� �iterator ���� node ������ header )���� null ��������� )*/ public Linked. List. Itr first( ) { return new Linked. List. Itr( header. next ); } /** ���������� p * @param x item ������ * @param p ���� iterator ����������� */ public void insert( Object x, Linked. List. Itr p ) { != null ) 2020/11/27 if( p != null && p. current �. ����� 18

/** * @param x ��� ������������. * @return iterator �������� x ������������ null ���

/** * @param x ��� ������������. * @return iterator �������� x ������������ null ��� x ������� */ public Linked. List. Itr find( Object x ) { /* 1*/ List. Node itr = header. next; /* 2*/ /* 3*/ while( itr != null && !itr. element. equals( x ) ) itr = itr. next; /* 4*/ return new Linked. List. Itr( itr ); } 2020/11/27 �. ����� 19

/** * �� ����� �iterator ������ ��� ������ x * ���� x �������� �iterator

/** * �� ����� �iterator ������ ��� ������ x * ���� x �������� �iterator ��������� */ public Linked. List. Itr find. Previous( Object x ) { /* 1*/ List. Node itr = header; /* 2*/ /* 3*/ while( itr. next != null && !itr. next. element. equals( x ) ) itr = itr. next; /* 4*/ return new Linked. List. Itr( itr ); } 2020/11/27 �. ����� 20

public static void print. List( Linked. List the. List ) { if( the. List.

public static void print. List( Linked. List the. List ) { if( the. List. is. Empty( ) ) System. out. print( "Empty list" ); else { Linked. List. Itr itr = the. List. first( ); for( ; !itr. is. Past. End( ); itr. advance( ) ) System. out. print( itr. retrieve( ) + " " ); } System. out. println( ); } 2020/11/27 �. ����� 22

Insert �� doubly linked list P A 1 A 2 A 3 2 3

Insert �� doubly linked list P A 1 A 2 A 3 2 3 4 newnode 2020/11/27 1 X �. ����� 24

Remove �� doubly linked list A 1 2020/11/27 A 2 A 3 �. �����

Remove �� doubly linked list A 1 2020/11/27 A 2 A 3 �. ����� 25

���� B A A C B C D E A E B D C

���� B A A C B C D E A E B D C D E A �� B �� E B �� A �� C C 2020/11/27 �� B �� D �� E Sparce table Node directory �. ����� 38

class Cursor. Node { // Constructors Cursor. Node( Object the. Element ) { this(

class Cursor. Node { // Constructors Cursor. Node( Object the. Element ) { this( the. Element, 0 ); } Node Cursor. Node( Object the. Element, int n ) { element = the. Element; next = n; ������ } index ��� pointer 2020/11/27 �. ����� 40

// Friendly data; accessible by other package routines Object element; int next; } 2020/11/27

// Friendly data; accessible by other package routines Object element; int next; } 2020/11/27 �. ����� 41

public class Cursor. List. Itr { /** * ����� list iterator * @param the.

public class Cursor. List. Itr { /** * ����� list iterator * @param the. Node �������� iterator ����� */ ���� pointe Cursor. List. Itr( int the. Node ) { current = the. Node; } Iterator 2020/11/27 �. ����� 42

/** * �� ����� �item stored in the current position. * @return item �������

/** * �� ����� �item stored in the current position. * @return item ������� current ������ �null * ��� current ������� */ public Object retrieve( ) { return is. Past. End( ) ? null : Cursor. List. cursor. Space[ current ]. element; } �������� 2020/11/27 �. ����� 44

/** * ����� current ������� * ��� current ������������ */ public void advance( )

/** * ����� current ������� * ��� current ������������ */ public void advance( ) { if( !is. Past. End( ) ) current = Cursor. List. cursor. Space[ current ]. next; } int current; 2020/11/27 // Current position �. ����� 45

public class Cursor. List { ����� new ������� ����� private static int alloc( )

public class Cursor. List { ����� new ������� ����� private static int alloc( ) ������ { int p =cursor. Space ]0. [ next; cursor. Space ]0. [ next =cursor. Space ]p. [next; if )p ==0( throw new Out. Of. Memory. Error( ); return p; } ���������� null 1 2 2020/11/27 null 2 null 3 null 4 null 0 ��������� use list �. ����� 47

private static void free )int p ( { cursor. Space ]p. [element =null; cursor.

private static void free )int p ( { cursor. Space ]p. [element =null; cursor. Space ]p. [next =cursor. Space ]0. [next; cursor. Space ]0. [ next =p; } ������ garbage collection ������� p ��� free list (����� p �������� Use list �� ������ use list ���������� index 1 ��� 2� null 3 2 2020/11/27 x 2 y 0 null 3 null 4 null �. ����� 0 48

public Cursor. List( ) { header =alloc( ); cursor. Space ]header. [next =0; }

public Cursor. List( ) { header =alloc( ); cursor. Space ]header. [next =0; } null 1 2 2020/11/27 null 2 0 null 3 null 4 null 0 �������� use list ������ 49

public boolean is. Empty( ) { return cursor. Space[ header ]. next == 0;

public boolean is. Empty( ) { return cursor. Space[ header ]. next == 0; } public void make. Empty( ) { while( !is. Empty( ) ) remove( first( ). retrieve( ) ); } 2020/11/27 �. ����� 50

public Cursor. List. Itr zeroth( ) { return new Cursor. List. Itr )header (;

public Cursor. List. Itr zeroth( ) { return new Cursor. List. Itr )header (; } public Cursor. List. Itr first( ) { return new Cursor. List. Itr )cursor. Space ] header. [next (; } 2020/11/27 �. ����� 51

public void insert )Object x, Cursor. List. Itr p ( { if )p =!null

public void insert )Object x, Cursor. List. Itr p ( { if )p =!null && p. current =!0( { ����� x int pos =p. current; ����� p int tmp =alloc( ); ���� p cursor. Space ]tmp. [element =x; ����� use cursor. Space ]tmp. [next =cursor. Space ]pos. [next; list ���� cursor. Space ]pos. [next =tmp; } } 2020/11/27 �. ����� 52

����� x 2���������� 2 pos == p. current == 2 null 2 x 1

����� x 2���������� 2 pos == p. current == 2 null 2 x 1 0 null 3 null 4 null 0 tmp == alloc() ==2 null 3 x 1 0 null 3 null 4 null 0 null 3 x 1 2 x 2 0 null 4 null 0 Use list 2020/11/27 �. ����� 53

public Cursor. List. Itr find )Object x ( { */1 /*int itr =cursor. Space

public Cursor. List. Itr find )Object x ( { */1 /*int itr =cursor. Space ]header. [next; */2 */3 /*while )itr =!0 && cursor. Space]itr. [element. equals )x ( ( /*itr =cursor. Space ]itr. [next; */4 /*return new Cursor. List. Itr )itr (; ! } 2020/11/27 �. ����� 54

public Cursor. List. Itr find. Previous )Object x ( { */1 /*int itr =header;

public Cursor. List. Itr find. Previous )Object x ( { */1 /*int itr =header; */2 /*while )cursor. Space ]itr. [next =!0 && ! cursor. Space ]itr. [next. [element. equals )x ( ( */3 /*itr =cursor. Space ]itr. [next; */4 } 2020/11/27 /*return new Cursor. List. Itr )itr (; �. ����� 55

public void remove )Object x ( ������ { ��� x Cursor. List. Itr p

public void remove )Object x ( ������ { ��� x Cursor. List. Itr p =find. Previous )x (; int pos =p. current; if )cursor. Space ]pos. [next =!0 ( // ����� � x ��� //������� { int tmp =cursor. Space ]pos. [next; cursor. Space ]pos. [next =cursor. Space ]tmp. [next; free )tmp (; } 2020/11/27 �. ����� 56 }

Remove x 2 null p= find. Previous() , pos =1 3 x 1 2

Remove x 2 null p= find. Previous() , pos =1 3 x 1 2 x 2 0 null 4 null 0 3 x 1 0 x 2 0 null 4 null 0 tmp = 2 null ����� use list ����� free(tmp) null 2020/11/27 3 2 x 0 x 2 0 null 3 null 4 �. ����� null 0 57

private int header; static Cursor. Node [ ]cursor. Space; variables private static final int

private int header; static Cursor. Node [ ]cursor. Space; variables private static final int SPACE_SIZE =100; initialization static { cursor. Space =new Cursor. Node ]SPACE_SIZE [; for )int i =0; i < SPACE_SIZE; i( ++ cursor. Space ]i = [new Cursor. Node )null, i +1( ; cursor. Space ]SPACE_SIZE -1. [ next =0; } 2020/11/27 �. ����� 58

������� stack ������ linked list u �������� header ������� u ���� header ������� stack���

������� stack ������ linked list u �������� header ������� u ���� header ������� stack��� public class Stack. From. Linked. List { private List. Node top; public Stack. From. Linked. List( ){ top = null; } 2020/11/27 �. ����� n 61

/** * @return false ��������� stack ���� */ public boolean is. Full( ) {

/** * @return false ��������� stack ���� */ public boolean is. Full( ) { return false; } /** * @return true if stack ������������ false */ public boolean is. Empty( ) { return top == null; 2020/11/27 �. ����� } 62

public void make. Empty( ) { top = null; } /** * @return ������������

public void make. Empty( ) { top = null; } /** * @return ������������ stack ��������� �null */ public Object top( ) ����� top() ����� { if( is. Empty( ) ) Throw exception ����� return null; return top. element; } 2020/11/27 �. ����� 63

/** * ������� stack ��������������� * @exception Underflow ��� stack ������� */ public void

/** * ������� stack ��������������� * @exception Underflow ��� stack ������� */ public void pop( ) throws Underflow { if( is. Empty( ) ) throw new Underflow( ); top = top. next; �������� pointer ��� } 2020/11/27 �. ����� 64

/** * ������ top() ��� pop() * @return item ������ pop �������� ����� �null

/** * ������ top() ��� pop() * @return item ������ pop �������� ����� �null ������� */ public Object top. And. Pop( ) { Throw exception ����� if( is. Empty( ) ) return null; ����� Object top. Item = top. element; ������ pointer ���� top = top. next; return top. Item; } 2020/11/27 �. ����� 65

���� stack ������ public class Stack. From. Array { private Object [ ] array.

���� stack ������ public class Stack. From. Array { private Object [ ] array. Body; private int top. Index; static final int DEFAULT_CAPACITY = 10; public Stack. From. Array( ) { this( DEFAULT_CAPACITY ); } 2020/11/27 �. ����� 70

/** * ��������� * @param capacity ������ ���. */ public Stack. From. Array( int

/** * ��������� * @param capacity ������ ���. */ public Stack. From. Array( int capacity ) { array. Body = new Object[ capacity ]; top. Index = -1; } 2020/11/27 �. ����� 71

public boolean is. Empty( ) { return top. Index == -1; } public boolean

public boolean is. Empty( ) { return top. Index == -1; } public boolean is. Full( ) { return top. Index == array. Body. length - 1; } public void make. Empty( ) { top. Index = -1; } 2020/11/27 �. ����� 72

public Object top( ) { if( is. Empty( ) ) return null; return array.

public Object top( ) { if( is. Empty( ) ) return null; return array. Body[ top. Index ]; } Throw exception ����� /** * ������� top ����� ��� ������ null. * @exception Underflow ������������. */ Set ����� nul public void pop( ) throws Underflow ����� index { if( is. Empty( ) ) throw new Underflow( ); array. Body[ top. Index-- ] = null; } ��������� pop � 2020/11/27 �. ����� 73

/** * ��������� �� ����������� * @param x ������� * @exception Overflow ��������� */

/** * ��������� �� ����������� * @param x ������� * @exception Overflow ��������� */ public void push( Object x ) throws Overflow { if( is. Full( ) ) throw new Overflow( ); ������ index ������� array. Body[ ++top. Index ] = x; } } 2020/11/27 �. ����� 75

method 1(){ method 2(); } method 2(){ method 3(); } method 3(){ … }

method 1(){ method 2(); } method 2(){ method 3(); } method 3(){ … } top method 3’s info method 2’s info method 1’s info main (){ method 1(); } 2020/11/27 �. ����� 94

��� enqueue ��� dequeue ������� (1) n enqueue(x) size++ u back++ u the. Array[back]

��� enqueue ��� dequeue ������� (1) n enqueue(x) size++ u back++ u the. Array[back] = x u n dequeue() size- u front++ u 2020/11/27 �. ����� 98

public class Queue. Array{ private Object [ ] the. Array; private int size; private

public class Queue. Array{ private Object [ ] the. Array; private int size; private int front; private int back; static final int DEFAULT_CAPACITY = 10; public Queue. Array( ) { this( DEFAULT_CAPACITY ); } public Queue. Array( int capacity ) { the. Array = new Object[ capacity ]; make. Empty( ); } 2020/11/27 �. ����� 101

public boolean is. Empty( ) { return size == 0; } public boolean is.

public boolean is. Empty( ) { return size == 0; } public boolean is. Full( ) { return size == the. Array. length; } public void make. Empty( ) { size = 0; front = 0; back = -1; } 2020/11/27 �. ����� 102

public Object get. Front( ) { if( is. Empty( ) ) return null; return

public Object get. Front( ) { if( is. Empty( ) ) return null; return the. Array[ front ]; } Throw exception ����� /*�� ��������������� ����� �null */ public Object dequeue( ){ Throw exception ����� if( is. Empty( ) ) return null; size--; Object front. Item = the. Array[ front ]; the. Array[ front ] = null; front = increment( front ); 2020/11/27 return front. Item; �. ����� 103

/** * ������ * @param x ������ * @exception Overflow �������� */ public void

/** * ������ * @param x ������ * @exception Overflow �������� */ public void enqueue( Object x ) throws Overflow { if( is. Full( ) ) throw new Overflow( ); back = increment( back ); the. Array[ back ] = x; size++; } 2020/11/27 �. ����� 104

����� size() is. Empty() top() last() push(x) insert. Last(x) pop() remove. Last() 2020/11/27 �.

����� size() is. Empty() top() last() push(x) insert. Last(x) pop() remove. Last() 2020/11/27 �. ����� 107

��� ����� size() is. Empty() get. Front() first() enqueue(x) insert. Last(x) dequeue() remove. First()

��� ����� size() is. Empty() get. Front() first() enqueue(x) insert. Last(x) dequeue() remove. First() 2020/11/27 �. ����� 108

class Simulation{ static int INFINITY = 1000; static int SERVE_TIME = 60; //������� int

class Simulation{ static int INFINITY = 1000; static int SERVE_TIME = 60; //������� int next. Arrival. Time; //��������� int current. Time; int next. Departure. Time; //�������� int number. Of. Customers; int waiting. Time; int sum. Of. Waiting. Time; Queue. List customer. Queue; 2020/11/27 �. ����� 112

public void process. Arrival(){ current. Time = next. Arrival. Time; number. Of. Customers; ++

public void process. Arrival(){ current. Time = next. Arrival. Time; number. Of. Customers; ++ if(next. Departure. Time == INFINITY}( next. Departure. Time = current. Time + SERVE_TIME; { else} customer. Queue. enqueue(new Customer(next. Arrival. Time; (( { 2020/11/27 �. ����� 113

public void process. Departure(){ current. Time = next. Departure. Time; if(!customer. Queue. is. Empty}(()

public void process. Departure(){ current. Time = next. Departure. Time; if(!customer. Queue. is. Empty}(() Customer c = ) Customer)customer. Queue. dequeue; () waiting. Time = current. Time - c. get. Arrival. Time; () sum. Of. Waiting. Time += waiting. Time; next. Departure. Time = current. Time + SERVE_TIME; { else } // �� ��������� waiting. Time = 0; next. Departure. Time = INFINITY; { 2020/11/27 �. ����� 114

public void process(int time){ next. Arrival. Time = time; while(next. Departure. Time <= next.

public void process(int time){ next. Arrival. Time = time; while(next. Departure. Time <= next. Arrival. Time( // �������������� process. Departure; () process. Arrival ; () { public void calculate. Mean. Waiting. Time}() double r = (double)sum. Of. Waiting. Time / number. Of. Customers; System. out. println(r; ( { 2020/11/27 �. ����� 115

public static void main(String[] args){ Simulation a = new Simulation; () while(true}( if(�������������� (

public static void main(String[] args){ Simulation a = new Simulation; () while(true}( if(�������������� ( break; a. process(������������ ; ( { // while(a. next. Departure. Time < INFINITY}( ������������� a. process. Departure; () { // ������������������������������ a. calculate. Mean. Waiting. Time; () 2020/11/27 �. ����� 116