1 / 25

Understanding Elementary Data Structures: Lists, Stacks, & More

Learn about Abstract Data Types, Concrete Data Types, List ADT operations, and implementation hierarchy in Java with linked lists.

jfilip
Download Presentation

Understanding Elementary Data Structures: Lists, Stacks, & More

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. Elementary Data Structures Based on slides by Harry Zhou and Dan Suciu Read Cormen et al, Chapter 10

  2. Lists • We will first describe them as ADTs = Abstract Data Types

  3. Abstract vs. Concrete Data Types • Abstract Data Type (ADT) • Mathematical description of an object and the set of operations on the object • List, Stack, Tree, Heap, Graph, … • One ADT may specialize another ADT • One ADT may implement another ADT • Concrete Data Type • Implementation of an ADT using some set of primitive data types and operations of known complexity • Primitives: integers, arrays, pointers or references • Object-oriented programming languages (Java, C++) let you explicitly define new concrete data types that correspond to ADT’s.

  4. List ADT ( A1 A2 … An-1 An ) length = n • Mathematical description: a sequence of items • Ai precedes Ai+1 for 1  i < n • Operations • First() = position • Value(position) = item • Next(position) = position • Length() = integer • Insert(item,position) • Delete(position) What other operations might be useful? Kth(integer)=item SetKth(item,integer) Find(item)=position

  5. Specialization Hierarchy ListProperty: Sequence First()=pos Value(pos)=item Kth(integer)=itemNext(pos)=pos Length()=integer SetKth(item,integer)Insert(item,pos) Delete(pos) Find(item)=position StackProperty: LIFO Push(item)Pop()=itemIsEmpty()=true/false QueueProperty: FIFO Enqueue(item)Dequeue()=itemIsEmpty()=true/false VectorProperty: random access Kth(int) = itemSetKth(item,integer)

  6. Implementation Hierarchy ListComplexity: Unspecified First()=pos Value(pos)=item GetKth(integer)Next(pos)=pos Length()=integer SetKth(item,integer)Insert(item,pos) Delete(pos) Find(item)=position Linked List(1) for: (n) for: Array(1) for: (n) for:

  7. 7 Linear Data Structures - Implementations • Linked lists and pointer implementations in Java • A linked list consists of dynamically allocated nodes. • Java uses class objects to implement pointers • To declare a node structure • public class node • { • int data; • node next; • public node() • {data = 0; next = null;} • public node(int x, node n) • {data = x; next =n;} • }

  8. 0 1 q p 8 To create a node node p = new node(); node q = new node(1,null); p.next = q;

  9. 10 20 10 15 20 p q p q 0 10 20 10 20 p q p q 9 (1) To insert a node after P p.next = new node(15,q); (2) To insert a node containing 2 at the beginning of a list p= new node(2,p);

  10. 10 To remove a node pointed by q 10 20 30 q p p.next = q.next;

  11. 11 Class linkedlist public class linkedlist { private static node head; public void Displaylist(node q) {…..} public void Buildlist() {..} public static void main(String[] args) {linkedlist mylist = new linkedlist(); mylist.Buildlist(); mylist.Displaylist(); } }

  12. public void BuildList() {node q = new node(0,null); head = q; String oneLine; try{BufferedReader indata = new BufferedReader(new InputStreamReader(System.in)); // read data from terminals System.out.println("How many nodes?\n"); oneLine = indata.readLine(); // always need the following two lines to read data head.data = Integer.parseInt(oneLine); for (int i=1; i<=head.data; i++) {System.out.println("A new value please\n"); oneLine = indata.readLine(); int num = Integer.parseInt(oneLine); node p = new node(num,null); q.next = p; q = p;} }catch(Exception e) { System.out.println("Error --" + e.toString());} } 12 The method that builds a linked list

  13. 13 To print a linked list node by node public void DisplayList(node q) {if (q != null) { System.out.println(q.data); DisplayList(q.next);} }

  14. Top a0 a1 a2 . an-1 an 14 (2) Stacks A stack is a linear data structure in which elements are added to and removed from one designated end called the top LIFO – Last In First Out. Given a stack S = (a0, a1, … an-1, an), we say that a0 is the bottom element, an is the top element if they are added in the order of a0,a1, .. and an, shown below: an abstract view of a stack of n elements Thus, an is the first one to be removed and a0 is the last one to be removed. The restrictions on the stack imply no other elements except an can be seen and accessed.

  15. 15 Stack Operations BASIC OPERATIONS: Push: place an element at the top of the stack Pop: remove the top element from the stack Top: retrieve the value of the top element, and the stack remains unchanged OTHER OPERATIONS: IsEmpty: return TRUE if the stack is empty IsFull: return TRUE if the stack is full MakeEmpty Java implementations (using linked lists) public class StackNode{ object info; StackNode link; public StackNode(){} public StackNode(Object j, StackNode p){info = j; link = p;} }

  16. 16The class Stack public class StackList {private StackNode top; public void push(Object item){ top = new StackNode (item,top);} public Object pop( ) { StackNode oldTop = top; Object item = top(); top = top.link; oldTop.link = null; return item;} public Object top() { if (isEmpty()) throw new NullPointerException(); return top.info;}}

  17. 17 The class stack( using vectors) public class StackVector extends Vector { public void push(Object item){addElement(item);} public Object pop() { Object item; item = top(); removeElementAt(size()-1); return item;} public Object top() { if(isEmpty()) throw new NullPointerException(); return elementAt(size()-1);} public boolean isEmpty() {return (size() == 0);} }

  18. b c d a front rear 18 Queue • A queue is an ordered collection of items from which elements may be deleted at the front of the queue and into which elements may be inserted at the rear of the queue. FIFO - First-In, First-Out behavior. • The Queue ADT: • Organization: • A queue is a sequence of elements of the same type, maintained in • first-in / first-out order so that only the element at the front of the queue can be removed. • Given a queue Q = (1st, 2nd, 3rd, .. nth), we say that the 1st element is the one placed into the queue first and therefore, will be removed first from the queue.

  19. 19 Queue implementation Queue operations: Peek - retrieve the first element but without removing it Enq - insert the element at the end of the queue Deq - remove the element at the front of the queue IsEmpty IsFull Public class QueueNode{ Object info; QueueNode link; public QueueNode() { }; public QueueNode()(Object i, QueueNode j) { info = i; link = j;} }

  20. 20 Applications of stacks and queues (1) Check the parentheses –uses a stack Algorithm: error = false; while ( not end of input) && not error { sym = input.nextChar(); // input is a Scanner object if ( sym == ‘(‘ ) push (sym, stack); if (sym == “)” ) if (stack == empty) error = true; else pop (sym, stack); } if ((error == false) and (IsEmpty(stack)) System.out.println ( “Formatted correctly” ) Example: - (2*(5*2+(2/10))+4*(7-5)) correct - (2*(5*2-(2/10)))+(((7-5)) incorrect

  21. 21(2) Infix to postfix conversion Why postfix expressions? - no need to have parentheses Examples: Infix expression: (2-1) * (52 + (22-6 * 3)) Postfix expression 2 1 - 5 2 ^ 2 2 ^ 6 3 * - + * Remarks: The order of operands remains unchanged

  22. 22Algorithm of conversion from infix to postfix stack – here we keep the operations that are pending queue – here we write the output do { read(s); if s is a number EnQ (s, queue) else if ( s == ‘(‘ ) push(s, stack) else if ( s == ‘)’) while ( top(stack) != ‘(‘ ) EnQ (pop(stack)) pop (stack); else // s is an operation while Priority(s) ≤ Priority(top) EnQ (pop (stack)); push (s, stack) } while ( not the end of input) Priority: ** 3 * / 2 + - 1 (, space (end of expression) 0

  23. Example: infix: 7 – ( 2 * 3 + 5 ) + 6 Input (Bottom)Stack Queue 7-(2*3+5)+6 -(2*3+5)+6 7 (2*3+5)+6 - 7 2*3+5)+6 -( 7 *3+5)+6 -( 7 2 3+5)+6 -(* 7 2 +5)+6 -(* 7 2 3 5)+6 -(+ 7 2 3 * )+6 -(+ 7 2 3 * 5 +6 - 7 2 3 * 5 + 6 + 7 2 3 * 5 + - + 7 2 3 * 5 + - 6 7 2 3 * 5 + - 6 + Remarks: a queue is used to store the output 23Trace

  24. 2 1 Input: 2 1 1 Input: ‘-’ 1 5 2 Input: 5 2 1 25 Input: ‘^’ 1 25 2 2 Input: 2 2 1 25 4 Input: ^ 1 25 4 6 3 Input: 6 3 1 25 4 18 Input: ‘*’ 24 Evaluate postfix expressions example: 2 1 - 5 2 ^ 2 2 ^ 6 3 * - + * Input: a postfix expression. Output: a value while not end of input read(t) if t is a number push(t,stack) else top = pop(stack) next=pop(stack) ans = next t top push(ans,stack) ans = pop(stack)

  25. 1 25 -14 Input: ‘-’ 1 11 Input: ‘+’ 11 Input: ‘*’ 25 Evaluate postfix expressions example: 2 1 - 5 2 ^ 2 2 ^ 6 3 * - + * Input: a postfix expression. Output: a value while not end of input read(t) if t is a number push(t,stack) else top = pop(stack) next=pop(stack) ans = next t top push(ans,stack) ans = pop(stack)

More Related