1 / 23

CSE 246 Data Structures and Algorithms

CSE 246 Data Structures and Algorithms. Lecture # 6. ADT. Abstract data type is a specification of data types having some defined set of operations and which are independent of their implementation.

ulf
Télécharger la présentation

CSE 246 Data Structures and Algorithms

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. CSE 246Data Structures and Algorithms Lecture # 6

  2. ADT • Abstract data type is a specification of data types having some defined set of operations and which are independent of their implementation. • ADT is implementation independent. For example, it only describes what a data type List consists (data) and what are the operations it can perform(logical specification), but it has no information about how the List is actually implemented at hardware level. • The examples of an ADT are stack, queues, linked list etc. Quratulain

  3. Stack • Last-in, First-out (LIFO) structure • 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 • Sample uses • “Back” button of a browse • “Undo” operation • function/method calls • Arithmetic Expression handling PEZ® candy dispenser Quratulain

  4. Stack Operations • Elements are added to and removed from one designated end called the top. • Basic Operations • Push(), add element into the stack • pop(), remove & return topmost element • Other Operation • Empty() • Top() • Size() Quratulain

  5. Array Implementation of a Stack public class MyStack { intstackArray[]; int top; intMAX = 100; public MyStack() { stackArray= new int[MAX]; top = -1; } // ... } 3 top ... 1 4 32 7 Quratulain

  6. ArrayStack class, continued public class MyStack { // ... public boolean empty() { return (top == -1); } public void push(intvalue) { if (top < MAX-1) stackArray[++top] = value; } // ... } // in the code of main // function that uses // the stack … MyStack S1=new MyStack(); … S1.push( 95 ); top 4 4 ... 1 4 32 7 95 Quratulain

  7. ArrayStack class, continued // in the code that uses // the stack … int x = S1.pop(); public class MyStack { // ... public int pop() throws Exception { if ( empty() ) throw new Exception(); else return stackArray[top--]; } } // x gets 95, // slot 4 is now free Note: Store[4] still contains 95, but it’s now considered “free”. top 4 5 ... 12 24 37 17 95 Quratulain

  8. Problems with Array Implementation • MAX (size of array) needs to be specified • Consequences • stack may fill up (when top == MAX) • memory is wasted if actual stack consumption is below maximum • Need a more “dynamic” implementation • The array implementation of a stack is simple and efficient for known size of list. • Time complexity of all stack operations is O(1). Quratulain

  9. Stack Using Link list • Linklist can avoid the size limitation of a STACK. However the use of array are much faster in PUSH and POP operation because array indexes can be access directly. Quratulain

  10. For a singly-linked list with head and tail pointers: • insert at start or end takes constant time O(1). • Also, removing an element at the start or end is constant time. • However if tail is not specified then insert and delete at end takes O(n) • Make sense to place stack elements at the start of the list because insert and removal are constant time. Quratulain

  11. Linked List Implementation • use a singly linked list to implement the stack ADT. • Stack as a sequence of nodes public class StackNode<T> { <T> info; StackNode next; public StackNode(){} public StackNode(<T> v) { info = v; Next=null; } } Quratulain

  12. Linked List as a Data Structure • Linked List Implementation of a Stack: • STACK is an example of a data structure implemented through another data structure (as here linked list). • Operations on STACK using linked list. • public void PUSH(<T> value); • public <T> POP (); • public booleanisEmpty(); • public <T> onTop(); Quratulain

  13. LinkedStack Class public class LinkedStack// this class use StackNode class { StackNodetop; public LinkedStack() { top = null; } public boolean empty() { return (top == null); } // ... } top 1 3 5 2 null Quratulain

  14. Push Operation using a List public class LinkedStack { // ... public void push( <T> entry ) { StackNodetemp = new StackNode(); temp.info= entry; temp.next=null;// if insert first top=temp; } // … } // in the code // that uses the // stack Mystack.push( 7 ); temp top X 2 null 1 7 5 3 Quratulain

  15. temp Pop Operation using a List public class LinkedStack { // ... public int pop() throws Exception { if ( empty() ) { throw new Exception(); } else { intvalue = temp.info(); top = temp.next(); return value; } } } // in the code // that uses the // stack int x = MyStack.pop(); top 7 X Garbage Collected 1 3 7 5 2 null Quratulain

  16. Stack using Linked List • The time complexity of all operations is O(1) except destructor, which takes O(n) time. • For applications in which the maximum stack size is known ahead of time, an array is suitable • If the maximum stack size is not known beforehand, we can use a linked list Quratulain

  17. Since both implementations support stack operations in constant time, any reason to choose one over the other? See below: • Allocating and deallocating memory for list nodes does take more time than preallocated array. • List uses only as much memory as required by the nodes; array requires allocation ahead of time. • List pointers (head, next) require extra memory. • Array has an upper limit; List is limited by dynamic memory allocation. Quratulain

  18. Stack Application • Parenthesis validation of arithmetic expressions • Infix to postfix conversion • Function call Quratulain

  19. Parenthesis Validation using stack A mathematical expression: 7 – ((X * ((X+Y) / (J-3)) + Y) / (4-2.5)) Ensure parenthesis nested correctly • There are an equal number of right and left parentheses. • Every right parenthesis is preceded by a matching left parenthesis. E.G ((A+B) or A+B( …. Violate condition 1 )A+B(-C or (A+B))-(C+D … Violate condition 2 Quratulain

  20. Solution for parenthesis • The parenthesis count at the end of the expression is 0. this implies that no scope have en left open or that exactly as many right parentheses as left parentheses have been found. • The parenthesis count at each point in the expression is nonnegative. This implies that no right parenthesis is encountered for which a matching left parenthesis had not previously been encountered. 7 – ( ( X * ( ( X + Y ) / ( J – 3 ) ) + Y ) / ( 4 - 2.5 ) ) 0 0 1 2 3 4 3 4 3 2 1 2 1 0 Quratulain

  21. Again problem ) A + B ( - C -1 0 • There are three different type of scope delimiters exits {}, [], () • The stack may be used to keep track of the types of scopes encountered Quratulain

  22. Algorithm for parenthesis Validation Valid = true; S= the empty stack While (we have not read the entire string) { Read the next symbol (symb) of the string; If (symb == ‘(‘ || symb == ‘{‘ || symb == ‘[‘) Push (s, symb); If (symb == ‘)‘ || symb == ‘}‘ || symb == ‘]‘) if (empty(s)) valid =false; else { i=pop(s); if (I is not the matching operand of symb) valid=false; If (valid) Print(“valid String”); Else Print(“Not valid String”); Quratulain

  23. Arithmetic Expression validate • Pushing an item on to stack correspond to opening a scope, and popping an item from the stack corresponds to closing a scope. • When the stack is empty and scope ender encountered, so the parenthesis pattern is invalid. Quratulain

More Related