Download presentation
Presentation is loading. Please wait.
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
What other operations might be useful?
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
List Property: Sequence First()=pos Value(pos)=item Kth(integer)=item Next(pos)=pos Length()=integer SetKth(item,integer) Insert(item,pos) Delete(pos) Find(item)=position Stack Property: LIFO Push(item) Pop()=item IsEmpty()=true/false Queue Property: FIFO Enqueue(item) Dequeue()=item IsEmpty()=true/false Vector Property: random access Kth(int) = item SetKth(item,integer)
6
Implementation Hierarchy
List Complexity: 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
8 To create a node node p = new node(); node q = new node(1,null);
p.next = q; 1 p q
9
9 (1) To insert a node after P
10 20 p q 10 15 p 20 q p.next = new node(15,q); (2) To insert a node containing 2 at the beginning of a list 10 p 20 q p 10 20 q p= new node(2,p);
10
10 To remove a node pointed by q
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
12 The method that builds a linked list
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());} }
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
14 (2) Stacks LIFO – Last In First Out.
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: a0 a1 a2 . an-1 an Top 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
16 The 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
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. a b c d front rear
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 ^ ^ * * Remarks: The order of operands remains unchanged
22
22 Algorithm 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) if ( s == ‘(‘ ) push(s, stack) if ( s == ‘)’) while ( top(stack) != ‘(‘ ) EnQ (pop(stack)) pop (stack); while Priority(s) ≤ Priority(top) EnQ (pop (stack)); push (s, stack) } while ( not the end of input) Priority: ** 3 * / 2 (, space (end of expression)
23
23 Trace Example: infix: 7 – ( 2 * 3 + 5 ) + 6
Input (Bottom)Stack Queue 7-(2*3+5)+6 -(2*3+5) (2*3+5) 2*3+5)+6 -( 7 *3+5)+6 -( 3+5)+6 -(* +5)+6 -(* 5)+6 -( * )+6 -( * 5 * 5 + * 5 + - * 7 2 3 * Remarks: a queue is used to store the output
24
24 Evaluate postfix expressions
example: ^ 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) 2 1 Input: 2 1 1 Input: ‘-’ 1 5 2 Input: 5 2 1 25 Input: ‘^’ 1 25 2 Input: 2 2 1 25 4 Input: ^ 1 25 4 Input: 6 3 6 3 1 25 4 Input: ‘*’ 18
25
25 Evaluate postfix expressions
example: ^ 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) 1 25 -14 Input: ‘-’ 1 11 Input: ‘+’ 11 Input: ‘*’
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.