| stack คืออะไร ? | คือโคตรงสร้างข้อมูลแบบมีลำดับ และมีการทำงานแบบ LIFO โดยใช้คำสั่ง คือ push และ pop |
|---|---|
| new Stack() | ใช้สร้าง object stack |
| push() | ใช้เพิ่มข้อมูลใน stack โดยจะเรียงต่อไปเรื่อยๆ ในตำแหน่งท้ายสุด |
| pop() | ใช้คืนค่าข้อมูลใน stack จากตำแหน่ง top ออกมา และเลื่อน top ไปหนึ่งตำแหน่ง |
| peek() | ใช้คืนค่าข้อมูลใน stack จากตำแหน่ง top ออกมา |
| search() | ใช้ค้นหาข้อมูลใน stack หากพบข้อมูลจะคืนค่าตำแหน่งที่พบกลับมา หากไม่พบข้อมูลจะคืนค่า -1 กลับมา |
| empty() | ใช้ตรวจสอบว่าใน stack ไม่มีข้อมูล ใช่หรือไม่ |
subject:stack คืออะไร ?
content:stack คือโคตรงสร้างข้อมูลแบบมีลำดับ และมีการทำงานแบบ LIFO โดยใช้คำสั่ง ดังนี้ ( Last In First Out คือ ข้อมูลที่ใส่ทีหลังจะถูกนำมาออกมาก่อน - push คือ การนำข้อมูลไปเก็บไว้ในหน่วยความจำ stack - pop คือ การนำข้อมูลออกจากหน่วยความจำ stack
method:new Stack ();
return type:Stack
content:ใช้สร้าง object stack
example:Stack stack = new Stack();
method:push ( object );
return type:void
content:ใช้เพิ่มข้อมูลใน stack โดยจะเรียงต่อไปเรื่อยๆ ในตำแหน่งท้ายสุด
example:Stack stack = new Stack(); stack.push ( "data one" ); stack.push ( new int ( 15 ) );
method:pop ();
return type:Object
content:ใช้คืนค่าข้อมูลใน stack จากตำแหน่ง top ออกมา และเลื่อน top ไปหนึ่งตำแหน่ง ( ตำแหน่ง top คือ ข้อมูลตัวสุดท้ายที่เพิ่มเข้าไปใน stack )
example:Stack stack = new Stack(); stack.push ( "data one" ); stack.push ( new int ( 15 ) ); int number = ( int ) stack.pop ();
method:peek ();
return type:Object
content:ใช้คืนค่าข้อมูลใน stack จากตำแหน่ง top ออกมา ( แต่ไม่เลื่อนตำแหน่ง top ) ( ตำแหน่ง top คือ ข้อมูลตัวสุดท้ายที่เพิ่มเข้าไปใน stack )
example:Stack stack = new Stack(); stack.push ( "data one" ); stack.push ( new int ( 15 ) ); int number = ( int ) stack.peek ();
method:search ( object );
return type:int
content:ใช้ค้นหาข้อมูลใน stack หากพบข้อมูลจะคืนค่าตำแหน่งที่พบกลับมา หากไม่พบข้อมูลจะคืนค่า -1 กลับมา
example:Stack stack = new Stack(); stack.push ( "data one" ); stack.push ( new int ( 15 ) ); int position = stack.search ( new int ( 15 ) );
method:empty ();
return type:boolean
content:ใช้ตรวจสอบว่าใน stack ไม่มีข้อมูล ใช่หรือไม่
example:Stack stack = new Stack(); if ( stack.empty () ) { System.out.println ( "stack empty" ); }