Stack, Queue and Linked Lists
Stack, queue and Linked lists are basic data structures. They appear in our daily life. For example, stacks of intermodal containers at the port, waiting in line(queue) to ride a roller coaster and the film The Human Centipede,if you know it.
Stack
Intro
Stack is like a stack of intermodal containers, or a stack of book. Every time you want to put a new item, you put above the original stack, not between or below. Every time you want to remove a item, you need to start from the top, remove the item which is latest added. Hence, there is a principle called “LIFO”, Last In, First Out.
Common operations:
push | pop |
---|---|
Array Implementation
Usually we use the first item in the array array[0]
as the bottom of the stack, create index max-index
to store the index of the top item, in other words, the index of the last item in the stack.
Push: max_index += 1
, and at the index of max_index add the item
Pop: delete the item at the index max_index
, max_index -= 1
Linked List Implementation
Using singly linked list, use head
to store the bottom of the stack, each item has next
pointing at the next item. The top item’s next
points to NULL
, size
stores the size of the stack (the length of the linked list).
push: change the next
variable of the top item in stack(last item in list) to the new item, let next
of the new item points to NULL
, size += 1
pop: delete the top item in stack (last item in list), size -= 1
, set next
of the new last item to NULL
C++ STL - stack
|
|
Queue
Intro
Queue is like the queue in our life, wait in line to check out. Each new item add to the end of a queue, and the item at the front goes out first. Hence, Queue has the principle of “FIFO” (First In, First Out).
Common operations:
enqueue | dequeue |
---|---|
Linked List Implementation
Using singly linked list, use head
to track the head of a queue, each item has a variable next
pointing to the next item, next
of the last item in the queue points to NULL
, size
tracks the length of the queue.
Enqueue: append the new item at the end of the queue/list, next
of the old last item points to the new item just appended, size += 1
Dequeue: Using next
to navigate to the second item, head
points to the second item, delete the first item, size -= 1
C++ STL - queue
Almost the same as stack
!
queue
uses front
and back
to get the first and last item respectively.
|
|
Priority Queue
Priority Queue is similar as queue, however each item has a priority. The first item to be dequeued maybe not the most front of the queue, it is now which the item has the highest priority. It’s like someone who have VIP cards, or someone is a queue-jumper, not fair for these items waiting, right? sigh.
In C++ STL, if you use int
as items in a priority queue, it is default that the smaller the integer is, the less priority it has. If you want the opposite, which is larger integer, less priority, you could use great<int>
, fully declarationstd::priority_queue<int, std::vector<int>, std::greater<int> q_name
. If you want other custom compare methods, you might need to custom cmp
function to complete priority comparison.
front()
to get the first item, NOT top()
.Linked List
Intro
Linked list is like linked paper clips, have you tired before? Linked list have several structures: singly linked list, doubly linked list, multiply linked list and others. We only show singly linked list here as others can be implemented with the understanding of singly linked list.
Common operations:
- add nodes
- delete nodes
- next node
C++ Struct Implementation
|
|