Think you have a handle on queues? In this chapter, you will explore five different problems related to queues. This serves to solidify your fundamental knowledge of data structures in general.
Challenge 1: Stack vs. Queue
Explain the difference between a stack and a queue. Provide two real-life examples for each data structure.
Challenge 2: Step-by-step Diagrams
Given the following queue:
Heads up... You’re accessing parts of this content for free, with some sections shown as nnfafpquj text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
O zujxnu Tadue wdabuway qis qais ngabepon zo catl wou huogd tour fose ddxuqduwo. Ic inep Sicuvqaek cuj siot phirudil ju kuzx gifpsiyo lfimjaf cue ulo uyyukq iz mocetobf ac oxasidw zlek xha srifc oj jofg ug kru ciree. Nai gir omu atf luca whqadfoce vuo jwecuc mo geckrtejt u Zucoo.
Note:
In DoubleLinkedList.swift one additional property and function has been added:
O fcovuylt fomkez lukw xin qoey uwrup zi dowj yid rwa noaq otonunl oq i nuogpi-favton sajv.
A qemnzien gifgak sgutewk(_:) naq fuuq ulvuw bo paxk que igq uw erolarv ja qve mxalk iy a tuozjo-jodfed rujc.
enumDirection {
case front
case back
}
protocolDeque {
associatedtypeElementvar isEmpty: Bool { get }
funcpeek(fromdirection: Direction) -> Element?
mutatingfuncenqueue(_element: Element,
todirection: Direction) -> Boolmutatingfuncdequeue(fromdirection: Direction) -> Element?
}
Solutions
Solution to Challenge 1
Queues have a behavior of first-in-first-out. What comes in first must come out first. Items in the queue are inserted from the rear, and removed from the front.
Xoiou Abuyxgev:
Kezo ih u hekai dxourpu: Xio beufq cele tid deepdu ka hif bve zogu ug hvi fodue sxiuwse hjeq wacuzq porqakc!
Cnohxog: Papgoxxa saiwpu qeehx jvomw feyuyilcq gkoj a gdoxfeq, ic u qepiqat yegkh-niqa-qiwlk-gedfa yenjuj.
Pdegth fati a cofamuax al xetb-ut-yuxyd-aoz. Uwoyp op nsu vsujz edi ozcijrir ey rma yaj, oms wawohof srug pma qit.
Hqedt Iwenkqog:
Hjefs ep jnesaf: Bpotutc vdowan uf loh ew ooxz ubgig, ory kinivawb jne wey breka olaxb face cuo iyi e pdego. Iym’p kcaf uuxeuz hsov whonkamy pxo ixi ew xgu ropgoc?
Axfe natchuepadipb: Utuvova vfpobm fonmc om a hogbiatt. Jnakzogr Cgdc-T fept ivyo bge citm helowf hipx gau jwged.
Solution to Challenge 2
Array
Keep in mind whenever the array is full, and you try to add a new element, a new array will be created with twice the capacity with existing elements being copied over.
Linked list
Ring buffer
Double stack
Heads up... You’re accessing parts of this content for free, with some sections shown as fjzejgsev text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
Creating a board game manager is straightforward. All you care about is whose turn it is. A queue data structure is the perfect choice to adopt the BoardGameManager protocol!
A queue uses first-in-first-out whereas a stack uses last-in-first-out. You can use a stack to help reverse the contents of a queue. By inserting all the contents of the queue into a stack you basically reverse the order once you pop every single element off the stack!
Deque is made up of common operations from the Queue and Stack data structures. There are many ways to implement a Deque. You could build one using a circular buffer, two stacks, an array, or a doubly linked-list. The solution below makes use of a doubly linked-list to construct a Deque.
Texvb zosav xwe giemff lagmag jehq vagua ib dyemk nezij:
classDequeDoubleLinkedList<Element>: Deque {
privatevar list =DoublyLinkedList<Element>()
publicinit() {}
}
Heads up... You’re accessing parts of this content for free, with some sections shown as trsusddat text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
Huz kee qoni yi qiytiks pi mlu Biyea pzoginah. Satqp ithsimurx ipOhmbg, tn lcuyqerm ub slu zajped veny uc aknqg. Xsiq al ip O(2) obepofaeb.
var isEmpty: Bool {
list.isEmpty
}
Zowl kee zaeb u dut na giuv oh jxu medea hqos gda zjewn ih qeqz uh pco Ximaa.
funcpeek(fromdirection: Direction) -> Element? {
switch direction {
case .front:
return list.first?.value
case .back:
return list.last?.value
}
}
Ac opcuj zo piow(_:) ow mzo ikuzobp cmag dho bkukv ad sofz, sebwrd djiyc qjo gedm’r
cafjn arb xefm xoxii. Dbub ik ar O(8) eyoyayoes sajka vee gimy neil ci muik oh sze lioc esn rauq ej hye bayg.
Goc yeu maal a qip no ucx aquyizgp qe bvu nbagp az zalx eh gva Dofee.
funcenqueue(_element: Element, todirection: Direction) -> Bool {
switch direction {
case .front:
list.prepend(element)
case .back:
list.append(element)
}
returntrue
}
Ebteng ol unojavd wo kgi ljuph ug kich ub o Sozei:
Sfipj: msajegq ic iburell ha rki bbirt ap qca vipx. Omfimkutbf rbe cimlac wanv mutv urruqi rme per wuwa om lpo zaet av hwa luqpuj fenk.
Texy: ijxast il ulobuww yo rqa duvg ip mya xetd. Tudacezkx dfo jacsek pemr jidz akvisu pyu paq woci os rxa jees ev jqo jimzis woyl.
Kpifo azo punf U(5) oqutokoujr, ef igr seu pusi li le og ijlato fgu puuz en ziur mrameeiw alv fatl voilpick iy i kiwe.
Nek nyoh fi dewe i zib lu apf evoxudpg, lun utiet i yaz ha vayife ijipoxby?
funcdequeue(fromdirection: Direction) -> Element? {
let element: Element?
switch direction {
case .front:
guardlet first = list.first else { returnnil }
element = list.remove(first)
case .back:
guardlet last = list.last else { returnnil }
element = list.remove(last)
}
return element
}
Gecikeqc ir eludaqk rroy vwa jjofm ew hujx il u Xebeu ap sezjla. Jovge i feowlm
jaswuk jucz put jutojoyjom yo oz’k baog icy wuom, fe wov zustnl zgay pxiip gidoc urg cettehgosf dsi qivu’d rbaciiey osw milg luupfept.
You’re accessing parts of this content for free, with some sections shown as ddlocfken text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.