1 / 90

List, Stack, Queue

List, Stack, Queue. List. It is about putting things in a sequence. Example: A1,A2,A3,…,An. first. last. What can we do with a list. Find – find a specified member. Insert – insert a new member at a specified position. findKth – return the kth element.

dayb
Télécharger la présentation

List, Stack, Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. List, Stack, Queue

  2. List • It is about putting things in a sequence. • Example: A1,A2,A3,…,An first last

  3. What can we do with a list • Find – find a specified member. • Insert – insert a new member at a specified position. • findKth – return the kth element. • Remove – remove a specified element from list. • Head – return the first member. • Tail – return the list without its first member. • Append – combine 2 lists.

  4. What if we create a list from an array? • Do not forget that an array needs us to specify its length. • Find -> O(n) • Because we need to search from the first element of the list. • findKth –O(1) • We can use index to find the kth element directly. • Insert andremove may take a long time • Because all members may be shifted.

  5. A1 A3 A2 Linked list • Find -> O(n) • because we still need to start at the biginning of the list. • findKth(i) -> O(i) • Because we cant use array index any more. node

  6. A1 A3 A2 A1 A3 A2 Linked list (cont.) • Deletion is easier (no need to shift members) • We only need to pointreference over to the next node. The original list When A2 is removed.

  7. A1 A3 A2 x A1 A3 A2 The original list After inserting x between A2 and A3. Linked list (cont 2.) • Insertion is also similar.

  8. A1 A3 A2 x Insert -> a small problem • Inserting the first member is different from inserting others. • No other node pointing to x. • Need the former A1 reference to point to x. The code needs to be different from other insertions.

  9. A1 A3 A2 header Empty list header We can avoid such special case. • We have a dummy node (or header) at the front of the list. • With this solution, every node will have a node in front, therefore all codes will be the same.

  10. theElement theElement Code:node class ListNode { // Constructors ListNode( Object theElement ) { this( theElement, null ); } ListNode( Object theElement, ListNode n ) { element = theElement; next = n; } Point ton.

  11. // Friendly data; accessible by other package routines Object element; ListNode next; } Instance variables

  12. List iterator • Is an object pointing to a node we are interested in. • Why do we have to write this class separate from list? • We can keep an interested node in our list anyway, right? • It’s because • If we use iterator, we can keep a general form of list separate from any interested node.

  13. Code:iterator public class LinkedListItr { ListNode current; //interested position /** * @param theNode any node in the list */ LinkedListItr( ListNode theNode ) { current = theNode; }

  14. /**see if current has passed the last element of the list. * @return true ifcurrent isnull */ public boolean isPastEnd( ) { return current == null; } /** * @return item stored incurrent, or null if * current is not in a list. */ public Object retrieve( ) { return isPastEnd( ) ? null : current.element; }

  15. /** * movecurrent to the next position in the list. * Ifcurrent isnull, do nothing. */ public void advance( ) { if( !isPastEnd( ) ) current = current.next; } }

  16. Code:linked list public class LinkedList { private ListNode header; public LinkedList( ) { header = new ListNode( null ); } public boolean isEmpty( ) { return header.next == null; }

  17. /** make the list empty.*/ public void makeEmpty( ) { header.next = null; } /** * return iterator that points to the header node. */ public LinkedListItr zeroth( ) { return new LinkedListItr( header ); }

  18. /* returniterator that points to the node next toheader (can be null if the list is empty.)*/ public LinkedListItr first( ) { return new LinkedListItr( header.next ); } /** insert a new node following the position pointed to by p. * @param x item to be in the new node. * @param p iterator of the position before the new node. */ public void insert( Object x, LinkedListItr p ) { if( p != null && p.current != null ) p.current.next = new ListNode( x, p.current.next ); }

  19. /** * @param x object that we want to find. * @return iterator that points to the first node that has x. *If x is not in the list, the iterator points to null. * */ public LinkedListItr find( Object x ) { /* 1*/ ListNode itr = header.next; /* 2*/ while( itr != null && !itr.element.equals( x ) ) /* 3*/ itr = itr.next; /* 4*/ return new LinkedListItr( itr ); }

  20. /** * return iterator that points to a node before the first • *node that has x. If there is no x in the list, return iterator *that points to the last node in the list. */ public LinkedListItr findPrevious( Object x ) { /* 1*/ ListNode itr = header; /* 2*/ while( itr.next != null && !itr.next.element.equals( x ) ) /* 3*/ itr = itr.next; /* 4*/ return new LinkedListItr( itr ); }

  21. /** * remove the first node with x from the list. * @param x is theitem to be removed from the list. */ public void remove( Object x ) { LinkedListItr p = findPrevious( x ); if( p.current.next != null )//mean x is found because // p is not the last member. p.current.next = p.current.next.next; //movereference //over x }

  22. public static void printList( LinkedList theList ) { if( theList.isEmpty( ) ) System.out.print( "Empty list" ); else { LinkedListItr itr = theList.first( ); for( ; !itr.isPastEnd( ); itr.advance( ) ) System.out.print( itr.retrieve( ) + " " ); } System.out.println( ); }

  23. A1 A3 A2 Doubly linked list • Node has extrainstance variable: • Previous : point to the node in front. This works the same way as next, but pointing in a different direction. • We can search both ways. • Additional time to change pointers.

  24. A1 A3 A2 X P 2 3 4 1 newnode Insert:doubly linked list

  25. A1 A3 A2 Remove:doubly linked list

  26. A1 A3 A2 Circular Linked list • Last node points to the first node. • nodummy node needed. • We can even make it into a doubly linked list.

  27. a0 a1 a2 … … linked list example • Let us want to store a polynomial • We can use array,using index i to store the coefficient of

  28. a0 a0 a1 a1 a2 a2 … … … … Using array to storepolynomial When addingpolynomials • The answer comes from the addition of corresponding slots, as shown.

  29. a0 a0 a1 a1 a2 a2 … … … … Using array to storepolynomial (2) multiplyingtwo polynomial • Each slot multiplies every slot of the other polynomial, then all results are added. • If there are many terms with 0 coefficient, there will be so many multiplication with 0. • Waste of time.

  30. uselinked list instead • Reduce the number of 0. Save space. • Example: 5x75+11x125+8 5 75 11 125 8 0 header coefficient Power of x

  31. 8 5 15 2 6 122 Skip list • A node can have more than one “next” pointers. • The extra pointers point to other parts of the list. • In this example, every node has a pointer to next. • A node in a position that is divisible by two will also have a pointer pointing to the next node with that quality. • Same for a node in a position divisible by four.

  32. Skip list problem • Inserting and removing an element will cause all pointers structure to be changed. • Too hard to do. • Usually only the number of pointers for each level is enforced. • Example: a 20 node list. • Level 0–> 20 nodes • Level 1– > 10nodes • Level 2 -> 5 nodes • Level 3 -> 2 nodes

  33. Skip list problem (cont) • The number of nodes in the example: • Level 3 -> 2 nodes • Level 2 -> 5-2 = 3 nodes • Level 1 -> 10-2-3 = 5 nodes • Level 0 -> 20-5-3-2 = 10 nodes • When adding a new node, random a number between 1 and 20. • 1 to 10 -> add the node with link level 0. • 11 to 15 -> add the node with link level 1. • And so on.

  34. Self-Organizing List • Put the data just viewed in front of the list, or • Swap the node just viewed with a node in front, or • Putting elements according to access frequency, or • Use a specific ordering scheme, such as alphabetically ordered. • Good for searching. • If we cannot find an element within a certain number of steps, we will know that the element is not in a list.

  35. Self-Organizing List(cont.) • Adam Drozdek (he has a book on data structure) found that • Putting the most recently viewed data in front of a list yields the same speed as ordering the list by data access frequency. • Faster than • swapping the node just viewed with a node in front. • Using a specific ordering scheme.

  36. Multilist orSparce Matrix • Example: data of all students and all subjects taught by our university. We must be able to: • Find all subjects a particular student is taking. • Find all students enroll in a particular subject. • We can use a 2D array to create a table of students and subjects. • But there will be lots of empty spaces. • A medical student and an engineering student surely enroll in different subjects. • We can fix this problem by making a 2D list.

  37. Subj 1 2 3 3 2 Student 1

  38. E B A D C B C C D E A B A graph B A C E D B E A C B D E Sparce table Node directory

  39. stack • Are divided into levels. We can only insert and remove things one way. • (LIFO = last in, first out) • What can we do: • Push – put an object at the top. • Pop – remove the top most element. • Top – return the top element without removing anything. A B C ฏ

  40. Making astack from list • Push = insert new object next to header. • Pop = remove object next to header. • If the list is originally empty, we can • Do nothing, or • throw exception. • Top = return an element in the node next to header.

  41. Stack code • Modified fromlinked list • For simplicity, we do not have a header in this example. • We can make a stack even though we do not have header. public class StackFromLinkedList { private ListNode top; public StackFromLinkedList( ){ top = null; }

  42. /** * @return always return false. */ public boolean isFull( ) { return false; } /** * @return true if stack is empty, otherwise return false. */ public boolean isEmpty( ) { return top == null; }

  43. public void makeEmpty( ) { top = null; } /** * @return top of stack, or null if empty. */ public Object top( ) { if( isEmpty( ) ) return null; return top.element; } Top does not change stack. Can choose to throw exception.

  44. /** * remove element on top of stack. * @exception Underflow if stack is empty. */ public void pop( ) throws Underflow { if( isEmpty( ) ) throw new Underflow( ); top = top.next; } Can choose to do nothing. Just moving the pointer over.

  45. /** * top() and pop() * @return popped item, or null if the stack is empty. */ public Object topAndPop( ) { if( isEmpty( ) ) return null; Object topItem = top.element; top = top.next; return topItem; } Can choose to throw exception.

  46. /** * put new element on top ofstack. * @param x element we want to put in stack. */ public void push( Object x ) { top = new ListNode( x, top ); } Old node New node New top points to old top.

  47. Stack weakness • When popped out, an element disappear forever. • We need to keep elements in extra variables, or another stack.

  48. Creatingstack with array • Do not forget that we need to specify array size. • Let a stack have • arrayBody • topIndex – index of the top element. (-1 if the stack is empty.)

  49. Code: stack made from array public class StackFromArray { private Object [ ] arrayBody; private int topIndex; static final int DEFAULT_CAPACITY = 10; public StackFromArray( ) { this( DEFAULT_CAPACITY ); }

  50. /** * create a stack, specifying its capacity. * @param capacity number of elements the * stack can hold. */ public StackFromArray( int capacity ) { arrayBody = new Object[ capacity ]; topIndex = -1; }

More Related