Chapter+3c Part1 Stack Implementation

Embed Size (px)

Citation preview

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    1/33

    Data Structures and Algorithms C++ Implementation

    Ho Chi Minh City University of TechnologyFaculty of Computer Science and Engineering

    BKTP.HCM

    BKTP.HCM

    Hunh Tn t

    Email: [email protected] Page: http://www.cse.hcmut.edu.vn/~htdat/

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    2/33

    Stacks

    Basic stack operations

    Linked-list implementation

    Stack applications

    Array implementation

    Slide 2Faculty of Computer Science and Engineering HCMUT

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    3/33

    Linear List Concepts

    General list: no restrictions on where data can beinserted/deleted, and on which operations can be usedon the list

    Restricted list: data can be inserted/deleted ando erations are erformed onl at the ends of the list

    Slide 3Faculty of Computer Science and Engineering HCMUT

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    4/33

    Stack

    All insertions and deletions are restricted to one endcalled the top

    Last-In First-Out (LIFO) data structure

    Slide 4Faculty of Computer Science and Engineering HCMUT

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    5/33

    Basic Stack Operations

    Push

    Data

    Slide 5Faculty of Computer Science and Engineering HCMUT

    Top

    Stack Stack

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    6/33

    Basic Stack Operations

    Top

    Overflow

    Data

    Slide 6Faculty of Computer Science and Engineering HCMUT

    Stack

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    7/33

    Basic Stack Operations

    Pop

    Data

    Slide 7Faculty of Computer Science and Engineering HCMUT

    Top

    Stack

    Top

    Stack

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    8/33

    Basic Stack Operations

    Underflow

    Slide 8Faculty of Computer Science and Engineering HCMUT

    Top Stack

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    9/33

    Basic Stack Operations

    Stack Top

    Data

    Slide 9Faculty of Computer Science and Engineering HCMUT

    Top

    Stack

    Top

    Stack

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    10/33

    Linked-List Implementation

    Top

    Stack structure

    top5

    Slide 10Faculty of Computer Science and Engineering HCMUT

    Conceptual Physical

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    11/33

    Linked-List Implementation

    Stackstructure count top

    stackcount top

    end stack

    Slide 11Faculty of Computer Science and Engineering HCMUT

    Stack nodestructure

    data next

    nodedata

    next end node

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    12/33

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    13/33

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    14/33

    Create Stack

    Before After

    0? ?

    Slide 14Faculty of Computer Science and Engineering HCMUT

    coun op

    (no stack) (empty stack)

    coun op

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    15/33

    Create Stack

    Algorithm createStack (ref stack )

    Initializes metadata for a stack

    Pre stack is structure for metadataPost metadata initialized

    1 k. n =

    Slide 15Faculty of Computer Science and Engineering HCMUT

    2 stack.top = null

    3 return

    End createStack

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    16/33

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    17/33

    Push Stack

    Before

    red

    data next

    After

    red

    data nextstack stack

    pNew pNew

    Slide 17Faculty of Computer Science and Engineering HCMUT

    2

    count top

    blue

    data next

    green

    data next

    3

    count top

    blue

    data next

    green

    data next

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    18/33

    Push Stack

    Algorithm pushStack (ref stack ,

    val data )

    Inserts (pushes) one item into the stackPre stack is a metadata structure to a valid stack

    n in h in k

    Slide 18Faculty of Computer Science and Engineering HCMUT

    Post data have been pushed in stack

    Return true if successful; false if memory overflow

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    19/33

    Push Stack

    1 if (stack full)

    1 success = false

    2 else

    1 allocate (pNew)

    2 pNew -> data = data

    Before

    red

    data nextstack

    pNew

    Slide 19Faculty of Computer Science and Engineering HCMUT

    p ew -> nex = s ac . op4 stack.top = pNew

    5 stack.count = stack.count + 1

    6 success = true3 return success

    End pushStack

    count top data next

    green

    data next

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    20/33

    Push Stack

    template

    void Stack::Push

    (List_ItemType value){

    Node* pNew = newNode();

    pNew->data = value;

    Slide 20Faculty of Computer Science and Engineering HCMUT

    pNew->next = top;this->top = pNew;

    this->count++;

    }

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    21/33

    Pop Stack

    recycled

    Before

    red

    data next

    dltPtr

    After

    data next

    dltPtr

    stack stack

    Slide 21Faculty of Computer Science and Engineering HCMUT

    3

    count top

    blue

    data next

    green

    data next

    2

    count top

    blue

    data next

    green

    data next

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    22/33

    Pop Stack

    Algorithm popStack (ref stack ,

    ref dataOut )

    Pops the item on the top of the stack and returns it to callerPre stack is a metadata structure to a valid stack

    i r iv h

    Slide 22Faculty of Computer Science and Engineering HCMUT

    Post data have been returned to caller

    Return true if successful; false if underflow

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    23/33

    Pop Stack

    1 if (stack empty)

    1 success = false

    2 else

    1 dltPtr = stack.top

    2 dataOut = stack.top -> data

    Before

    red

    data next

    dltPtr

    stack

    Slide 23Faculty of Computer Science and Engineering HCMUT

    s ac . op = s ac . op -> nex

    4 stack.count = stack.count - 1

    5 recycle (dltPtr)

    6 success = true3 return success

    End popStack

    3

    count top

    blue

    data next

    green

    data next

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    24/33

    Pop Stacktemplate

    int Stack::Pop(List_ItemType

    &dataOut){

    if (count == 0)return 0;

    Node* dltPtr = this->top;

    dataOut = this->top->data;

    Slide 24Faculty of Computer Science and Engineering HCMUT

    this->top = this->top->next;this->count--;

    delete dltPtr;

    return 1;

    }

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    25/33

    Stack Top

    Algorithm stackTop (val stack ,

    ref dataOut )

    Retrieves the data from the top of the stack without

    changing the stack

    Pre stack is a metadata structure to a valid stack

    Slide 25Faculty of Computer Science and Engineering HCMUT

    Post data have been returned to caller

    Return true if successful; false

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    26/33

    Stack Top

    1 if (stack empty)

    1 success = false

    2 else

    1 dataOut = stack.top -> data

    2 success = true

    Slide 26Faculty of Computer Science and Engineering HCMUT

    re urn success

    End stackTop

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    27/33

    Stack Top

    template

    int Stack::GetStackTop

    (List_ItemType &dataOut){

    if (count == 0)return 0;

    dataOut = this->top->data;

    Slide 27Faculty of Computer Science and Engineering HCMUT

    return 1;}

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    28/33

    Destroy Stack

    Algorithm destroyStack (ref stack )

    Releases all nodes back to memory

    Pre stack is a metadata structure to a valid stack

    Post stack empty and all nodes recycled

    1 if (stack not empty)

    Slide 28Faculty of Computer Science and Engineering HCMUT

    .

    1 temp = stack.top

    2 stack.top = stack.top -> next

    3 recycle (temp)

    2 stack.count = 0

    3 return

    End destroyStack

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    29/33

    Destroy Stack

    template

    void Stack::Clear() {

    Node* temp;

    while (this->top != NULL){temp = this->top;

    this->top = this->top->next;

    Slide 29Faculty of Computer Science and Engineering HCMUT

    delete temp;}

    this->count = 0;

    }

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    30/33

    Stack Emptytemplate

    int Stack::IsEmpty() {

    return (count == 0);

    }

    template

    int Stack::GetSize() {

    return count;

    Slide 30Faculty of Computer Science and Engineering HCMUT

    }

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    31/33

    Print a stack

    template

    void Stack::Print2Console() {

    Node* p;

    p = this->top;while (p != NULL){

    printf("%d\t", p->data);

    Slide 31Faculty of Computer Science and Engineering HCMUT

    p = p->next;}

    printf("\n");

    }

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    32/33

    Using Stacks

    int main(int argc, char* argv[]){

    Stack *myStack = new Stack();

    int val;

    myStack->Push(7);myStack->Push(9);

    myStack->Push(10);

    Slide 32Faculty of Computer Science and Engineering HCMUT

    my ac - us ;

    myStack->Print2Console();

    myStack->Pop(val);

    myStack->Print2Console();

    delete myStack;return 0;

    }

  • 7/27/2019 Chapter+3c Part1 Stack Implementation

    33/33

    Exercises

    template

    Stack*

    Stack::Clone() {

    // ...}

    Slide 33Faculty of Computer Science and Engineering HCMUT