Hi Brian, Another awesome tutorial! Thank you!!! I have two questions: 1. Could you give one or two practical examples when you might use this stack/linked list pattern when developing an app? It seems like it could come in handy in certain scenarios. 2. When you create the Node class could you make the Node a struct instead? I understand the copy value vs. copy reference difference between the two. Just wondering what your thinking was here for choosing class. Thank you again for creating such awesome tutorials!!!!!!
When you popped 3, you only returned the value 3 and modified the current top. The Value 3 wasn't really removed. Does the ARC do this for us as you have no way of referencing the value 3 after you pop it and deallocate the memory?
Thank you very much! I love your videos :) Could you please show, how to programmatically add constraints for different size classes(compact, regular) like we can do using storyboards? For example, for regular&compact constraint constant is 40, but for regular®ular it will be 70.
I'd advise simply not using Apple's size classes. Their classes are far too broad (and meaningless) when you're trying to to work across iPhones to iPads. And Storyboard/IB support is woeful; buggy, inconsistent. Apple really dropped the ball with the whole concept of Size Classes. Instead, I'd get the device current dimensions, and work your constraints dynamically from those dimensions. For example, if the device width is _currently_ below a certain threshold, you'd use one set of attributes (constraints, views, nibs, whatever), and if the width is above it, you'd use a different set of attributes. This way, you can make your own "size class" system, but with a much greater degree of accuracy, customised to exactly what you need. You would update your metrics when you get notified of a size transition.
Hi thank you for this video. Im not sure if you've done this in 2022... or earlier, but your Mic sounds kinda weird, there's like this frequency coming from it.
HI, useful but I have a question. When you make oldTop = top, as top is a class it is a reference to the top object. When you then re-initialize top = Node(etc) surely the oldtop which is referencing top is set to the same object ? ie a new Node ? Or have I completely misunderstood the reference type of class compared to struct ?
noob question: why does push have "(_ value: Int)" and pop have "-> Int"? In other words, why are they formatted differently even if they are both functions?
because . when u push or add , u need something to add , and the pop is for returning something. example . func addMoneyToMyCash ( _ money : Int){ . // u need to know the quantity u will add to MyCash,, (_ money : int ) is a parameter, myCash = myCash + money } func showMyCash()->Int { . //u dont need anything, u will just show/or return how much cash u have return myCash } i hope u understand :S
what you put in the parenthesis are the parameters you pass into the function. when you push, you have to tell it what you are adding or "pushing" to the stack. there's no "-> Int" in push because the arrow denotes what you return from the function. in push, you don't return anything, you just add to the stack. pop has nothing in the parenthesis because you don't have to tell it what you are popping, it just removes the top item from the stack. it has something after the arrow because it returns the item it pops.
are stacks recommended to be implemented using linkedlists? For example, if I get asked in an interview to implement a stack should i use a linkedlist over another implementation? great vid btw.
I wouldn't use a linked list for a stack in this day and age. Just use a Swift array. It already has functions for adding and removing items from the end. And it's easier to understand than a custom coded linked list. Bjarne Stroustrup did a talk on why rolling your own linked list solution is generally slower than simply using a built in collection type. And whilst he was using C++ to demonstrate it, the arguments apply equally to Swift. If your application performance relies on the stack, you might want to benchmark it when it comes time to look at optimisation. But certainly for a first pass, just use a Swift array.
Correct, nonetheless its a good idea to understand the implementation behind stacks. You'll never know when you run into these age old questions during an interview!
I think a good challenge after this tutorial is to implement a Queue, supporting enqueue and dequeue operations! Let me know if I can make any improvements to my one below :) class Node { var val: T var next: Node? init(val: T) { self.val = val } } class Queue { var head: Node? var tail: Node? // Add to the tail func enqueue(_ val: T) { if tail == nil { tail = Node(val: val) head = tail // Make head point to tail } else { tail?.next = Node(val: val) tail = tail?.next } } // Remove from the head func dequeue() -> T? { let currentHead = head head = head?.next if head == nil { tail = nil // Need to do this because of ARC } return currentHead?.val } }
Brian, can you think of anytime you needed a good algorithm to solve a real problem in an app? I develop a lot of apps for both android and iOS, but can't remember a time I needed algorithms.
You need algorithms all the time. Even the basic types you use (e.g. NSArray/Array, NSDictionary/Dictionary) use very sophisticated, fast algorithms. Not only that, but they implement different algorithms depending on what type of data you're feeding them, and how much data. An obvious example of needing to write your own algorithms is any time you are doing something that required performance. If, for example, you need to display 10,000 images, you need to load them asynchronously, parse them, cache them, build the equivalent of mipmaps, look them up efficiently, etc. How efficient your caching, sorting, lookup algos are will determine how fluid, useful, attractive the app will be.
I don't know if it's just me or everyone. I think your tutorials has intrinsic value and being so unique !!!
I am one of those who learn with practical examples which It never goes out of my head. Not theory type person
Great explanation about Generics!
Generics are really powerfull.. Thanks Brian
Was a bit confused about Generics implementation back in my Data Structures class, but this explained it so much better! Thanks Brian Voong!
Excellent example for swift generics.... Thanks for sharing.... Appreciate your effort on your videos
Love your algorithm videos! :)
Awesome i love these ! Cool to see these algorithms in swift. I just did the past two in school, except c++
Love watching your Friday algorithm shows! Keep up the amazing content!
I really appreciate this one Brian! This will help me a lot.
Excellent, these stack questions are always fun to go through.
keep doing the good work brian !!!
keep up the excellent algorithm videos!!
U are Awesome Instructor very easy to understand thank you :)
Excellent, programming is super easy and fun to understand once you get the basics down.
Another great video, thank you.
I like the video very much. However, do you have about Queue video?
Data Structures And Algorithms yayy!!
can you tell me why don't you just use an array and push to the end and pop from the end?
What will be size of array? This put limit to stack, that how much items you can push on stack. More you can search about linklists.
Great tutorial. Loved it.
Can you give some best examples where I can use Stack?
Thanks
Hi Brian, Another awesome tutorial! Thank you!!!
I have two questions: 1. Could you give one or two practical examples when you might use this stack/linked list pattern when developing an app? It seems like it could come in handy in certain scenarios.
2. When you create the Node class could you make the Node a struct instead? I understand the copy value vs. copy reference difference between the two. Just wondering what your thinking was here for choosing class.
Thank you again for creating such awesome tutorials!!!!!!
Trees use a linked list pattern when we keep a reference to the next child
or when you reading a byte buffer
Why can't we use orderedset instead of creating a stack
When you popped 3, you only returned the value 3 and modified the current top. The Value 3 wasn't really removed. Does the ARC do this for us as you have no way of referencing the value 3 after you pop it and deallocate the memory?
Amazing question, should be answered brain.
Excellent!
Thank you very much! I love your videos :)
Could you please show, how to programmatically add constraints for different size classes(compact, regular) like we can do using storyboards?
For example, for regular&compact constraint constant is 40, but for regular®ular it will be 70.
I'd advise simply not using Apple's size classes. Their classes are far too broad (and meaningless) when you're trying to to work across iPhones to iPads. And Storyboard/IB support is woeful; buggy, inconsistent. Apple really dropped the ball with the whole concept of Size Classes. Instead, I'd get the device current dimensions, and work your constraints dynamically from those dimensions. For example, if the device width is _currently_ below a certain threshold, you'd use one set of attributes (constraints, views, nibs, whatever), and if the width is above it, you'd use a different set of attributes. This way, you can make your own "size class" system, but with a much greater degree of accuracy, customised to exactly what you need. You would update your metrics when you get notified of a size transition.
Hi thank you for this video. Im not sure if you've done this in 2022... or earlier, but your Mic sounds kinda weird, there's like this frequency coming from it.
Opps! Is the last element or first element in Stack is considered "top"? I thought index 0 is the first pop, no?
why is the stack represented as a class instead of a struct?
Really helpful
HI, useful but I have a question. When you make oldTop = top, as top is a class it is a reference to the top object. When you then re-initialize top = Node(etc) surely the oldtop which is referencing top is set to the same object ? ie a new Node ? Or have I completely misunderstood the reference type of class compared to struct ?
Good question and you'll get the most out of this exercise but running through it yourself with breakpoints or print statements. Good luck.
Nice vid!
The questions comes from 2017 :)
Can we use struct instead of class ?
In this case it’s better to use Struct for Stack since we’re just holding values.
Can do.
noob question: why does push have "(_ value: Int)" and pop have "-> Int"? In other words, why are they formatted differently even if they are both functions?
because . when u push or add , u need something to add , and the pop is for returning something.
example .
func addMoneyToMyCash ( _ money : Int){ . // u need to know the quantity u will add to MyCash,, (_ money : int ) is a parameter,
myCash = myCash + money
}
func showMyCash()->Int { . //u dont need anything, u will just show/or return how much cash u have
return myCash
}
i hope u understand :S
what you put in the parenthesis are the parameters you pass into the function. when you push, you have to tell it what you are adding or "pushing" to the stack. there's no "-> Int" in push because the arrow denotes what you return from the function. in push, you don't return anything, you just add to the stack.
pop has nothing in the parenthesis because you don't have to tell it what you are popping, it just removes the top item from the stack. it has something after the arrow because it returns the item it pops.
are stacks recommended to be implemented using linkedlists? For example, if I get asked in an interview to implement a stack should i use a linkedlist over another implementation? great vid btw.
I love these videos
你好,鄙人英语不太好,能否推出中文的iOS 开发教程?谢谢,非常期待您的回复!
I wouldn't use a linked list for a stack in this day and age. Just use a Swift array. It already has functions for adding and removing items from the end. And it's easier to understand than a custom coded linked list. Bjarne Stroustrup did a talk on why rolling your own linked list solution is generally slower than simply using a built in collection type. And whilst he was using C++ to demonstrate it, the arguments apply equally to Swift.
If your application performance relies on the stack, you might want to benchmark it when it comes time to look at optimisation. But certainly for a first pass, just use a Swift array.
Correct, nonetheless its a good idea to understand the implementation behind stacks. You'll never know when you run into these age old questions during an interview!
I think a good challenge after this tutorial is to implement a Queue, supporting enqueue and dequeue operations! Let me know if I can make any improvements to my one below :)
class Node {
var val: T
var next: Node?
init(val: T) {
self.val = val
}
}
class Queue {
var head: Node?
var tail: Node?
// Add to the tail
func enqueue(_ val: T) {
if tail == nil {
tail = Node(val: val)
head = tail // Make head point to tail
}
else {
tail?.next = Node(val: val)
tail = tail?.next
}
}
// Remove from the head
func dequeue() -> T? {
let currentHead = head
head = head?.next
if head == nil {
tail = nil // Need to do this because of ARC
}
return currentHead?.val
}
}
awesome I bow ,good algorithm
Osthir !!! means Awesome :) :)
I can't resist my laughter :D :D :D
What if we change all the "Int"'-s to "Any"-s, instead of using Generics?
+Ika Javakhishvili generics give you a huge advantage because you tell the compiler what the type is, I suggest researching more on generics
Brian, can you think of anytime you needed a good algorithm to solve a real problem in an app? I develop a lot of apps for both android and iOS, but can't remember a time I needed algorithms.
You need algorithms all the time. Even the basic types you use (e.g. NSArray/Array, NSDictionary/Dictionary) use very sophisticated, fast algorithms. Not only that, but they implement different algorithms depending on what type of data you're feeding them, and how much data.
An obvious example of needing to write your own algorithms is any time you are doing something that required performance. If, for example, you need to display 10,000 images, you need to load them asynchronously, parse them, cache them, build the equivalent of mipmaps, look them up efficiently, etc. How efficient your caching, sorting, lookup algos are will determine how fluid, useful, attractive the app will be.
a simple function is considered an algorithm. I think what you mean is the BigO notation or how efficient an algorithm is
Well if your are programming you are using algorithms, so I think you should probably be more specific.
thanks
You are the best
Thanks.
very good videos and explanation!) but for people who don't speak english well, very difficult to get itttt) any way thank you!
+ Letsbuildthatapp is that Instagram I see? Is there going to be a series on creating Instagram anytime soon? Love to see this!
Owen Moore I'm waiting for this series
Samuel BIO would be awesome 👌🏽
not Enough Clear here Pleasr explain with graphical
Thanks :-)