Assignment 1 has been graded Everything in red
Assignment 1 has been graded
Everything in red: speak with Ehsan. Everything in blue: speak with the instructor. The grade entered on gradebook: speak with the instructor.
« Even though the trick requires less space and has the same time complexity, it cannot be realized by people immediately. The method with temporary variable is much easier to read. » Good algorithms cannot be found right away. Get used to it? « Even though a car requires less energy and has as much space, it cannot be built by people immediately. Transportation based on carts are much easier to design. »
Variations on Linked. List
The issue: going to the end Remember: to add an element, we have to go all the way to the end! null head tail One not just to have a shortcut? • Keep another variable pointing to the end of the list Variations on Linked. List
Adding an element using the tail void add(Object o){ if(head==null){ head = new Node(o); return; } Node last = new Node(o); if(tail==null) head. next = last; else tail. next = last; tail = last; } Case 1: Empty list Create a new head. Case 2: One element head tail Case 3: More than one element … tail Variations on Linked. List
It changes other functions too! If we remove something and it happens to be the tail, we should update. null current = current. next; head Linked. List tail Variations on Linked. List
It changes other functions too! If we remove something and it happens to be the tail, we should update. void remove(int i){ How would you solve it? if(i>=size){ System. err. println( « Out of bounds. » ); return; } if(i==0){ head = null; return ; } NO! if(i==size-1) tail = null; Node current = head; while(i>1){ current = current. next; i--; } current. next = current. next; } null if we remove the tail null. next: doesn’t make sense, crach. Variations on Linked. List
- Slides: 8