Tìm hiểu về Stack

31 1.1K 4
Tìm hiểu về Stack

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Tìm hiểu về Stack

Chapter 3 - STACKDefinition of StackSpecifications for StackImplementations of StackLinked StackContiguous StackApplications of Stack1 Linear List ConceptsLIFO(Stack)2 Stack ADTDEFINITION: A Stack of elements of type T is a finite sequence of elements of T, in which all insertions and deletions are restricted to one end, called the top. Stack is a Last In - First Out (LIFO) data structure.Basic operations:• Construct a stack, leaving it empty.• Push an element. • Pop an element. • Top an element.3 Basic operation of Stack (Push)Before Afterpush datapush data(Stack remains unchanged)toptoptoptopa) Successful operation: function returns successb) Unsuccessful operation: function returns overflow4 Basic operation of Stack (Pop)Before Afterpop datapop data(Stack remains unchanged)toptopa) Successful operation: function returns successb) Unsuccessful operation: function returns underflow5 Before AfterReceived data: Stack remains unchangedBasic operation of Stack (Top)top datatop data(Stack remains unchanged)toptopXXa) Successful operation: function returns successb) Unsuccessful operation: function returns underflowX6 Stack ADT (cont.)Extended operations:• Determine whether the stack is empty or not.• Determine whether the stack is full or not.• Find the size of the stack.• Clear the stack to make it empty.• Determine the total number of elements that have ever been placed in the stack.• Determine the average number of elements processed through the stack in a given period.• …7 Specifications for Stack ADT<void> Create()<ErrorCode> Push (val DataIn <DataType>)<ErrorCode> Pop ()<ErrorCode> Top (ref DataOut <DataType>)<boolean> isEmpty ()<boolean> isFull ()<integer> Size () // the current number of elements in the stack.Variants of similar methods:ErrorCode Pop (ref DataOut <DataType>)…8 Built a Stack ADTStack may be fully inhirited from a List ADT, inside its operations calling List’s operations.Ex.:<ErrorCode> Push (val DataIn <DataType>)// Call List::InsertHead(DataIn) or// Call List::Insert(DataIn, 0) // 0: insert to the 1stpositionend Push<ErrorCode> Pop ()// Call List::RemoveHead() end PopOther operations of Stack are similar …9 Built a List ADT from Stack ADTIf the Stack ADT has been built first, List ADT may be inhirited from the Stack. Some of its operations call Stack’s operations; the others will be added.10 [...]... Push 17 pNew count top n+1 X … count top 1 pNew Stack ADT (cont.) Extended operations: • Determine whether the stack is empty or not. • Determine whether the stack is full or not. • Find the size of the stack. • Clear the stack to make it empty. • Determine the total number of elements that have ever been placed in the stack. • Determine the average number of elements processed through the stack in a given period. • … 7 Basic operation of Stack. .. // the current number of elements in the stack. Variants of similar methods: ErrorCode Pop (ref DataOut <DataType>) … 8 Linear List Concepts LIFO (Stack) 2 Built a List ADT from Stack ADT If the Stack ADT has been built first, List ADT may be inhirited from the Stack. Some of its operations call Stack s operations; the others will be added. 10 Top Stack <ErrorCode> Top(ref DataOut <DataType>) //... for Linked Stack 1. if (count = 0) 1. return underflow 2. else 1. DataOut = data[top] 2. return success end Top 30 Create Stack <void> Create() // Specifications here are similar to specifications for Linked Stack 1. count = 0 2. top = -1 end Create 27 isFull Linked Stack <boolean> isFull() Determines if the stack is full. Pre none Post return stack status Return TRUE if the stack is full,... from the top of the stack Pre none Post If the stack is not empty, the element on the top has been removed; otherwise, the stack remains unchanged. Return success or underflow. 20 Linked Stack a) Conceptual b) Physical Node Data <DataType> link <pointer> end Node Stack top <pointer> count <integer> end Stack 4 count top top 12 Before After Received data: Stack remains unchanged Basic... underflow. // For Linked Stack 1. If (count > 0) 1. DataOut = top->data 2. Return success 2. Else 1. Return underflow 3. End Top 22 isEmpty Linked Stack <boolean> isEmpty() Determines if the stack is empty. Pre none Post return stack status Return TRUE if the stack is empty, FALSE otherwise 1. if (count = 0) 1. Return TRUE 2. else 1. Return FALSE end isEmpty 23 Basic operation of Stack (Push) Before... Create Linked Stack <void> Create () Creates an empty linked stack. Pre none Post An empty linked stack has been created. 1. top = NULL 2. count = 0 3. return end Create 13 top count = ? ? top count = 0 top = NULL count = 0 Implementations of Stack Contiguous Implementation: use an array. (May be Automatically or Dynamically Allocated Array) Linked Implementation: linked stack. 11 Pop Stack <ErrorCode>... stack. 2. top points to the next element. 3. Recycle pDel. Decrease count by 1. top … pDel count n top … pDel count n-1 X top = pDel->link recycle pDel count = count -1 18 Top Algorithm (cont.) <ErrorCode> Top (ref DataOut <DataType>) Retrieves data on the top of the stack without changing the stack. Pre none. Post if the stack is not empty, DataOut receives data on its top. The stack. .. return success end Push 28 Stack ADT DEFINITION: A Stack of elements of type T is a finite sequence of elements of T, in which all insertions and deletions are restricted to one end, called the top. Stack is a Last In - First Out (LIFO) data structure. Basic operations: • Construct a stack, leaving it empty. • Push an element. • Pop an element. • Top an element. 3 Pop Linked Stack 1. pDel holds the... After pop data pop data (Stack remains unchanged) top top a) Successful operation: function returns success b) Unsuccessful operation: function returns underflow 5 Push Algorithm (cont.) <ErrorCode> Push (val DataIn <DataType>) Pushes new data into the stack. Pre DataIn contains data to be pushed. Post If stack is not full, DataIn has been pushed in; otherwise, stack remains unchanged. Return... <integer> end Stack 4 count top top 12 Before After Received data: Stack remains unchanged Basic operation of Stack (Top) top data top data (Stack remains unchanged) top top XX a) Successful operation: function returns success b) Unsuccessful operation: function returns underflow X 6 Specifications for Stack ADT <void> Create() <ErrorCode> Push (val DataIn <DataType>) <ErrorCode> . Chapter 3 - STACK Definition of Stack Specifications for Stack Implementations of Stack Linked Stack Contiguous Stack Applications of Stack1 Linear List. operations of Stack are similar …9 Built a List ADT from Stack ADTIf the Stack ADT has been built first, List ADT may be inhirited from the Stack. Some of

Ngày đăng: 20/08/2012, 12:07

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan