Ch. 7 Stack 도입을 위한 예 “←” in keyboard input line

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Stacks, Queues, and Linked Lists
Stacks Chapter 11.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
CMPT 225 Stacks-part2.
CMPT 225 Stacks.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
1 Stacks (Walls & Mirrors - Chapter 6). 2 Overview The ADT Stack Array Implementation of a Stack Linked-List Implementation of a Stack Application Domain:
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Stack Queue.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Chapter 5 Linked Lists II
CS Data Structures I Chapter 7 Queue I. 2 Topics Introduction Queue Application Implementation Linked List Array ADT List.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
6/12/20161 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Stacks The Stack ADT stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) scheme. The last item placed.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
CENG 213 Data Structures Stacks 5/15/2018 CENG 213.
Stacks Chapter 6.
CS2006- Data Structures I Chapter 5 Linked Lists I.
CC 215 Data Structures Stack ADT
Abstract Data Types (ADTs)
Week 3 - Friday CS221.
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stack and Queue APURBO DATTA.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 6 Stacks.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Ch. 8 Queue Stack Queue milk queue stack
COMPUTER 2430 Object Oriented Programming and Data Structures I
Ch. 8 Queue Stack Queue milk queue stack
Stacks CS-240 Dick Steflik.
Abstract Data Types (ADTs)
Figure 6.1 Stack of cafeteria dishes. Figure 6.1 Stack of cafeteria dishes.
Chapters 6 and 7 Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Ch. 7 Stack 도입을 위한 예 “←” in keyboard input line E.g., abcd←←efgh←←←ij←km← abeik 한 character를 읽어 ‘←’ 이 아니면 저장하고 ‘←’ 이면 최근에 저장된 character를 제거한다.

A Stack Example 최근에 쌓은 접시를 꺼낸다 Stack of cafeteria dishes

ADT Stack Operations Create an empty stack Determine whether a stack is empty Add a new item on top of the stack Remove from the stack the most-recently added item Retrieve from the stack the most-recently added item Remove all the items from the stack

Notable fact The only access to the stack is the most-recently added item

Example: Checking Balance of Braces

Example: Checking Symmetric Language L = {w$w' : w is a possibly empty string w/o $, w' = reverse(w) } do { ch = string.getNextCharacter( ); stack.push(ch); } while (ch is not ‘$’) stack.pop( ); if (string.noMoreCharacter( )) { ←의미는 느낌으로 받아들일 것 if (stack.isEmpty( )) return true; else return false; } else { if (stack.isEmpty( )) return false; stackTop = stack.pop( ); } } while (ch == stackTop) return false; 설명의 편의상 error 처리 생략

Stack Implementation public boolean isEmpty( ); public interface StackInterface { public boolean isEmpty( ); public void push(Object newItem); public Object pop( ); public void popAll ( ); public Object peek( ); }

Array-Based Implementation 0 1 2 3 4 … MAX_STACK–1 –1 top Items[ ] public class StackArrayBased implements StackInterface { final int MAX_STACK = 50; private Object items[ ]; private int top; // index for stack top public StackArrayBased( ) { items = new Object[MAX_STACK]; top = –1; }

public boolean isEmpty( ) { return (top < 0); } public boolean isFull ( ) { return (top == MAX_STACK–1); public void push(Object newItem) { if (!isFull( )) items[++top] = newItem; else {exception 처리}

if (!isEmpty( )) return items[top--]; else {exception 처리} } public Object pop( ) { if (!isEmpty( )) return items[top--]; else {exception 처리} } public void popAll( ) { items = new Object[MAX_STACK]; top = –1; public Object peek( ) { if (!isEmpty( )) return items[top]; } // end class StackArrayBased ~3/26/2018(제7강)

Reference-Based Implementation public class StackReferenceBased implements StackInterface{ private Node top; public StackReferenceBased( ) { top = null; } Class Node: Ch.5, 강의 노트 P.8 참조

Remind: class Node (Ch. 5) public class Node { private Object item; private Node next; public Node(Object newItem) { item = newItem; next = null; } public Node(Object newItem, Node nextNode) { next = nextNode; public Object getItem( ) { return item; // setItem, setNext, getNext … •

public boolean isEmpty( ) { return (top == null); } 5 public boolean isEmpty( ) { return (top == null); } public void push(Object newItem) { top = new Node(newItem, top); public Object pop( ) { if (!isEmpty( )) { Node temp = top; top = top.getNext( ); return temp.getItem( ); } else {exception 처리}

public void popAll ( ) { top = null; } public Object peek( ) { if (!isEmpty( )) return top.getItem( ); else {exception 처리} } // end class StackReferenceBased

ADT List-Based Implementation public class StackListBased implements StackInterface { private ListInterface list; public StackListBased( ) { list = new ListReferenceBased( ); } Class ListReferenceBased: Ch.5, 강의 노트 P.13 참조

Remind: class ListReferenceBased (Ch. 5) public class ListReferenceBased implements ListInterface { private Node head; private int numItems; // constructor public ListReferenceBased( ) { numItems = 0; head = null; } // operations public Object get(int index) { …

public boolean isEmpty( ) { return list.isEmpty( ); } public void push(Object newItem) { list.add(1, newItem); public Object pop( ) { if (!list.isEmpty( )) { Object temp = list.get(1); list.remove(1); return temp; } else {exception 처리}

public void popAll ( ) { list.removeAll( ); } public Object peek( ) { if (!isEmpty( )) return list.get(1); else {exception 처리} } // end class StackListBased

isEmpty( ); “ADT list”-based version reuses the class ListReferenceBased. The reuse made the code simple. Reuse 비교 isEmpty( ); push(Object newItem); Object pop( ); popAll ( ); peek( ); public Object pop( ) { if (!list.isEmpty( )) { Object temp = list.get(1); list.remove(1); return temp; } else {exception 처리} } public Object peek( ) { if (!isEmpty( )) return list.get(1); else {exception 처리} } public boolean isEmpty( ) { return list.isEmpty( ); } public void popAll ( ) { list.removeAll( ); } public void push(Object newItem) { list.add(1, newItem); }

Evaluating Postfix Expressions Stack Application Evaluating Postfix Expressions Postfix expression: 2 3 4 + *

for (each character ch in the expression) { if (ch is an operand) stack.push(ch); else { // ch is an operator named op operand2 = stack.pop( ); operand1 = stack.pop( ); result = operarand1 op operand2; stack.push(result); }

A Search Problem in Flight Map Stack Application A Search Problem in Flight Map : direct flight Is there a path from city A to city B?

searchS(originCity, destinationCity) { // All cities are marked UNVISITED in the beginning stack.push(originCity); mark[originCity] = VISITED; while (!stack.isEmpty( ) && destinationCity is not at the top of stack) { if (no flight exists from the stack-top city to unvisited cities) temp ← stack.pop( ); // backtracking else { Select an unvisited city C that is directly reachable from the stack-top city; stack.push(C); mark[C] ← VISITED; } if (stack.isEmpty( )) // There is no path return false; else // A path found return true;

Equivalently searchS(originCity, destinationCity) { // All cities are marked UNVISITED in the beginning stack.push(originCity); mark[originCity] = VISITED; while (!stack.isEmpty( )) { if (no flight exists from the stack-top city to unvisited cities) temp ← stack.pop( ); // backtracking else { Select an unvisited city C that is directly reachable from the stack-top city; if (C = destinationCity) return true; stack.push(C); mark[C] ← VISITED; } // There is no path return false;

Recursive version searchS(originCity, destinationCity) { if (originCity = destinationCity) return true; else { mark[originCity] ← VISITED; arrived ← false; for each x ∈ L(originCity) ▷ L(originCity) : originCity에 인접한 도시들 if (mark[x] = UNVISITED) { arrived ← searchS(x , destinationCity); if (arrived= true) return true; } return false;

Equivalently searchS(originCity, destinationCity) { if (originCity = destinationCity) return true else { mark[originCity] ← VISITED; for each x ∈ L(originCity) { ▷ L(originCity) : originCity에 인접한 도시들 if (mark[x] = UNVISITED and searchS(x , destinationCity) = true) return true; } return false;

The stack of cities as you travel from P b) to R c) to X d) back to R e) back to P f) to W

A trace of the search algorithm, given the flight map

Memory 분할과 Stack Stack active 상태의 함수 관련 parameter와 variable들 Dynamic 수행중에 사용자가 할당받는 메모리. 예; Java의 new, C의 malloc() Heap program이 끝날 때까지 없어지지 않는 데이터. Global, static variables Data Static Code Program Virtual memory (OS라는 벽을 통해 보는 메모리)