DATA STRUCTURES (DS) Arrays, Stacks & Queues Courtesy : Goutam M V.
Week 4 - Monday. What did we talk about last time? Queues Implementing queues with circular...
-
Upload
rolf-short -
Category
Documents
-
view
240 -
download
0
Transcript of Week 4 - Monday. What did we talk about last time? Queues Implementing queues with circular...
Assignment 1 issues
Most people messed up the add() method that takes an index
Most people didn't throw exceptions at quite the right times
There was a lot of inconsistent formatting I don't specify what your formatting should be
too closely, but it should be consistent Check out the coding standards for the course:▪ http://users.etown.edu/w/wittmanb/cs221/standards/
Specific Assignment 1 don'ts Don't create a new Node unless you have to:
Never look at the next thing unless you have to:
Node temp = new Node(); //wastes memorytemp = head;
public boolean contains(int element) {Node temp = head;while(temp.next != null){ //two different bugsif( temp.value == element )return true;temp = temp.next;}return false;
}
Specific Assignment 1 do's Use other methods to simplify code:
Except when it doesn't make sense Using get() in addAll() is inefficient
Keep it simple:
Test, test, test!
public boolean contains(int element) {return indexOf(element) != -1;
}
public boolean isEmpty() {return size == 0;
}
Linked lists
What is a linked list? Why not just use (dynamic) arrays
for everything?
X
head
23 47 58
Pros
Insert at front (or back) O(1)
Delete at front (or back) O(1)
Arbitrary amounts of storage with low overhead
Cons
Search O(n)
Go to index O(n)
Potentially significant memory overhead if data is small
Much easier to make pointer and memory errors (especially in C/C++)
Levels of flexibility
Class protecting nodes implementation
Generic class providing nodes with arbitrary type
Generic class with the addition of iterators
Wait, what's an iterator?
I'm glad you asked They allow a collection to be used in a foreach loop So, what's a foreach loop?
It allows you to read (but not change) each value in a list
public static int sum( int[] array ) {int total = 0;for( int value: array )
total += value;return total;
}
So what?
Foreach loops work for any iterable list of any type
public static double weigh(LinkedList<Wombat> list) {double total = 0.0;for( Wombat wombat: list )
total += wombat.getWeight();return total;
}
public static double weigh(ArrayList<Wombat> list) {double total = 0.0;for( Wombat wombat: list )
total += wombat.getWeight();return total;
}
public static double weigh(Wombat[] list) {double total = 0.0;for( Wombat wombat: list )
total += wombat.getWeight();return total;
}
Singly linked list
Node consists of data and a single next pointer
Advantages: fast and easy to implement
Disadvantages: forward movement only
X
head
23 47 58
Doubly linked list
Node consists of data, a next pointer, and a previous pointer
Advantages: bi-directional movement Disadvantages: slower, 4 pointers must
change for every insert/delete Xhead
23 47 58
X tail
Interview question
You are given a singly linked list It may have a loop in it, that is, a
node that points back to an earlier node in the list
If you try to visit every node in the list, you’ll be in an infinite loop
How can you see if there is a loop in a linked list?
Definition
Let’s try a simple definition for a linked list:public class LinkedList {
private static class Node {public int data;public Node next;public Node previous;
}
private Node head = null;private Node tail = null;…
}
Next time…
Continued implementation of a linked list
Circular linked lists and skip lists Implementing a stack with a linked
list Keep reading section 1.3