Saturday, July 4, 2009

Queue

Queue (แถว) เป็นโคงสร้างข้อมูลแบบ linear list ซึ่งข้อมูลจะถูกเพิ่มเข้าทางด้านท้ายของแถว (เรียกว่า rear) และจะลบข้อมูลออกจากด้านหัวแถว (เรียกว่า front) มีชื่อเรียกเรีกกระบวนการนี้ว่า first in – first out (FIFO)

1. Queue Operations
Queue มีคำสังพื้นฐานอยู่ 2 คำสั่งคือ

  • ใส่ข้อมูลลงใน queue ทางด้านท้ายแถว (enqueue)
  • ลบข้อมูลออกจาก queue ทางด้านหัวแถว (dequeue)

Enqueue
เป็นคำสั่งใส่ข้อมูลลงใน queue หลังจากใส่ข้อมูลลงใน queue แล้วข้อมูลตัวใหม่นี้จะไปอยู่ท้ายแถว (rear) ถ้าไม่มีที่สำหรับข้อมูลใหม่แล้วจะเรียกว่า queue อยู่ในสถานะ overflow

Enqueue inserts an element at the rear of the queue.

Dequeue
เป็นคำสั่งลบข้อมูลออกจาก queue โดยข้อมูลที่อยู่ด้านหัวแถวจะถูก return ไปยังผู้ใช้และถูกลบออกจาก queue

Dequeue deletes an element at the front of the queue.

2. Queue Implementation

2.1 Queue Array Implementation
สำหรับการ implement queue array นั้นจะเขียนโค้ดฟังก์ชัน enqueue และ dequeue ในรูปแบบของภาษา C ก่อนอื่นเเรามาดูการประกาศ header ของฟังก์ชันพร้อมทั้งทำความเข้าใจกับ parameter ของฟังก์ชันทั้งสองกันครับ

//Prototype Declarations
void enqueue (int queue[], int* front, int* rear, int maxElement, int data);
void dequeue (int queue[], int* front, int* rear, int maxElement, int* data);
เรามาทำความรู้จัก parameter แต่ละตัวกันนะครับว่ามีอะไรบ้าง
queue เป็น array 1 มิติ แทน queue


front เป็นตัวระบุตำแหน่งหัวแถว


rear เป็นตัวระบุตำแหน่งท้ายแถว


maxElement เป็นจำนวนสมาชิกของตัวแปร queue


data เป็นข้อมูล


*สำหรับ queue ที่ว่างเราจะกำหนด front = -1, rear = -1

หลังจากที่เราทราบ parameter ของทั้งสองฟังก์ชัน ต่อมาเรามาดูการนิยามฟังก์ชันทั้งสองกันครับ

Program 2.1-1 Enqueue



/*============ enqueue ================
This algorithm inserts data into a queue.
Pre queue has been created
Post data have been inserted
*/
void enqueue (int queue[], int* front, int* rear, int maxElement, int data)
{
if(*rear <= maxElement - 1)
{
*rear = *rear + 1; //Increment rear.
queue[*rear] = data; //Insert element.
if(*front == -1)
*front = 0; //First element.
}
else
printf("\nQueue overflow\n");

return ;
}// EnQueue

Program 2.1-1 Analysis


ในบรรทัดที่ 8 เราตรวจสอบว่า queue มีที่ว่างสำหรับใส่ข้อมูลใหม่หรือไม่ ถ้าไม่แสดงว่า queue เต็มหรือ overflow แต่ถ้ายังมีที่ว่าง เราจะชยับ rear ไปอีก 1 ตำแหน่งแล้ว insert ข้อมูลลงใน queue ต่อมาเรามาดูบรรทัดที่ 12 – 13 กันครับถ้า queue นี้ถูก insert ข้อมูลเป็นครั้งแรกเราจะต้องเปลี่ยน front จาก –1 เป็น 0 เพื่อบอกให้รู้ว่า queue นี้ไม่ใช่ queue ว่าง

Sunday, June 21, 2009

Stack

Stack (กอง) เป็นโครงสร้างข้อมูลมีลักษณะการให้ข้อมูลเข้าออกคล้ายกับการวางของลงในกล่อง ของที่วางไว้ทีหลังจะอยู่บนสุด และจะถูกดึงออกมาก่อน ถ้าเป็นข้อมูลก็จะถูกดึงข้อมูลหลังสุดมาก่อน ถ้าเป็นการทำงานก็จะทำงานหลังสุดไล่ลงไปถึงงานแรกสุด มีชื่อเรียกกระบวนการนี้ว่า LIFO(Last in, First Out) เข้าทีหลังออกก่อน

Operations ของ stack
ที่สำคัญได้แก่


  • การนำข้อมูลเข้า (PUSH) เป็นการนำข้อมูลเข้าโดยข้อมูลที่เข้าทีหลังจะอยู่บนสุด (TOP)
  • การนำข้อมูลออก (POP) เป็นการนำข้อมูลที่อยู่บนสุดออกโดยข้อมูลที่ถูก Pop จะถูกลบออกจาก stack

ตัวอย่างการทำงานของ Operation Push และ Pop Implementation Stack การ implement Stack ทำได้ 2 วิธี คือ

1. Array Implementation

2. Linked List Implementation

Stack Array Implementation

------------------------------------------------------------ กำลังทำการปรับปรุง------------------------------------------------------ 
Stack Linklist Implementation

  • Stack Head ประกอบด้วย attributes 2 ตัวคือ 1. top (เป็น pointer ที่ชี้ไปยัง(เก็บ address) node ที่อยู่บนสุด) และ 2. count (เอาไว้นับจำนวน data ใน stack)
  • Stack Node ประกอบด้วย attributes 2 ตัวคือ 1. data (ตัวแปรที่ใช้เก็บข้อมูลที่เรา Push) และ 2. link (เป็น pointer ที่ชี้ไปยัง second node)

เพื่อให้เห็นภาพเรามาสร้าง Stack Node โดยใช้ struct ของภาษา C กัน
/* Stack Node */
typedef struct
{
void* dataPtr;
/* ใช้เก็บข้อมูล ที่ประกาศให้เป็น void เนื่องจากเราไม่รู้ว่าข้อมูลที่จะรับเข้ามานั้นเป็นข้อมูลชนิดไหน */

STRUCT node* link;
} STACK_NODE;


หลังจากสร้าง Stack Node แล้ว เรามาสร้าง Stack Head กัน
/* Stack Head */
typedef struct
{
int count;
STACK_NODE* top;
} STACK;


ต่อมาเรามาดู Stack's operations algorithm ที่เป็น pseudocode กัน


Create Stack

operation นี้ใช้สร้าง Stack Head โดย return address ของ stack head ซึ่งเวลาเรียกเราจะเรียกแบบนี้
STACK* stack;
stack = (STACK*) malloc(sizeof(STACK));


Algorithm createStack()
Creates and initializes metadata( stack head) structure
Pre : Nothing
Post: Structure created and initialized
Return: Stack Head

1. Allowcate memory for stack head
2. Set count to 0
3. Set top to null
4. return stack head

end createStack

Implement createStack with C
//========= createStack ============
STACK* createStack ()
{
STACK* stack;
stack = (STACK*) malloc(sizeof(STACK));
//Step 1.
stack->count = 0;
//Step 2.
stack->top = NULL; //Step 3.
return stack; //Step 4.

}//end createStack


Push Stack
ตัวดำเนินการ pushStack หรือ insert เป็นการนำข้อมูลเข้าใน stack โดยมีจะต้องตรวจสอบด้วยว่ามีที่ว่าง(หน่วยความจำ) พอที่จะ push หรือไม่ algorithm เขียนได้ดังนี้
Algorithm pushStack (stack, data)
Insert (push) one item into stack.
Pre: stack passed by reference, data contain data to be push into stack
Post: data have been pushed in stack
Return: true if success, false if overflow
1. allowcate new node
2. store data in new node
3. make current top node the second node
4. make new node the top node
5. increment stack count
end pushStack
Implement pushStack with C
//========== pushStack ==============
bool pushStack (STACK* stack, void* dataInPtr)
{
STACK_NODE* newNodePtr;
newNodePtr = (STACK_NODE*) malloc(sizeof(STACK_NODE)); //Step 1.
if(!newNodePtr) // If memory full return false
{
return false;
} // end if
newNodePtr->dataPtr = dataInPtr; //Step 2.
newNodePtr->link = stack->top; // Step 3.
stack->top = newNodePtr; // Step 4.
(stack->count)++; // Step 5.
return true;
} // end pushStack


Pop Stack
ตัวดำเนินการ popStack เป็นการนำข้อมูลที่อยู่บนสุดออก แล้วทำการลบข้อมูลที่ pop ออกจาก Stack
Algorithm popStack(stack)
This algorithm pop the item on the top of the stack and return it to user.
Pre: stack passed by reference
Post: return pointer data to user if successful, return null if underflow
1.if (stack empty)
____1.1 set dataOut to null
2.else
____2.1 set dataOut to data in top node
____2.2 make second node the top node
____2.3 decrement stack count
3. end if
4. return dataOut
end popStack

Implement popStack with C

//=========== popStack ============

void* popStack (STACK*)
{
STACK_NODE* temp;
void* dataOut;
if(stack->count == 0) // Step 1.
{
dataOut = NULL;
//Step 1.1
}
else // Step 2.
{
temp = stack->top;
dataOut = stack->top->dataPtr; // Step 2.1
stack->top = stack->top->link; // Step 2.2
free(temp);
(stack->count) --; // Step 2.3
} // end if
return dataOut;
}
// end popStack

เสร็จแล้วนะครับสำหรับการ สร้าง Stack โดยการใช้ Link List

Full List of stackADT.h

// stackADT.h

// Stack ADT Definitions
//Stack Node
typedef struct
{
void* dataPtr;
STRUCT node* link;
} STACK_NODE;
//end stack Node

//Stack Head
typedef struct
{
int count;
STACK_NODE* top;
} STACK;
//end stack Head

/*========= createStack =================
This algorithm creates an empty stack.
Pre: Nothing
Post: Returns pointer to a null stack
-or- NULL if overflow
*/
STACK* createStack ()
{
STACK* stack;

stack = (STACK*) malloc(sizeof (STACK));
stack->count = 0;
stack->top = NULL;

return stack;
}//end createStack

/*============= pushStack =================
This function push an item to the stack.
Pre: stack is a pointer to the stack
dataPtr pointer to data to be inserted
Post: Data inserted into stack
Return: true if successful
false if overflow
*/
bool pushStack (STACK* stack, void* dataInPtr)
{
STACK_NODE* newNodePtr;

newNodePtr = (STACK_NODE*) malloc(sizeof(STACK_NODE));

if(!newNodePtr) // If memory full return false
{
return false;
} // end if

newNodePtr->dataPtr = dataInPtr;
newNodePtr->link = stack->top;
stack->top = newNodePtr;
(stack->count)++;

return true;
} // end pushStack

/*=============== popStack ===============
This function pops item on the top of stack
Pre: stack is pointer to a stack
Post: Return pointer data if successful
NULL if underflow
*/
void* popStack (STACK*)
{
STACK_NODE* temp;
void* dataOut;

if(stack->count == 0)
{
dataOut = NULL;
}
else
{
temp = stack->top;
dataOut = stack->top->dataPtr;
stack->top = stack->top->link;
free(temp);
(stack->count) --;
} // end if

return dataOut;
}// end popStack














Post comments, questions & answers