Algo lab/stack queue codes
รุ่นแก้ไขเมื่อ 02:32, 17 กันยายน 2563 โดย Jittat (คุย | มีส่วนร่วม) (สร้างหน้าด้วย "== Queue == <syntaxhighlight lang="c++"> typedef int ValueT; struct ListNode { ValueT val; ListNode* next; ListNode(ValueT val, ListNode* next=...")
Queue
typedef int ValueT;
struct ListNode {
ValueT val;
ListNode* next;
ListNode(ValueT val, ListNode* next=0)
: val(val), next(next) {}
};
ListNode* front;
ListNode* rear;
void init_queue()
{
front = rear = 0;
}
void insert_queue(ValueT v)
{
ListNode* new_node = new ListNode(v);
if(rear != 0) {
rear->next = new_node;
rear = new_node;
} else {
front = rear = new_node;
}
}
ValueT extract_queue()
{
if(front != 0) {
ValueT v = front->val;
ListNode* new_front = front->next;
delete front;
front = new_front;
if(front == 0) {
rear = 0;
}
return v;
} else {
throw "Error extract from empty queue";
}
}
bool is_empty()
{
return front == 0;
}