Stacks Son

Embed Size (px)

Citation preview

  • 8/3/2019 Stacks Son

    1/21

    EASTERN MEDITERRANEAN UNIVERSITY

    Stacks

    EENG212 Algorithms and Data Structures

  • 8/3/2019 Stacks Son

    2/21

    Stacks

    Outline

    Stacks

    Definition

    Basic Stack Operations

    Array Implementation of Stacks

  • 8/3/2019 Stacks Son

    3/21

    What is a stack?

    It is an ordered group of homogeneous items of elements.

    Elements are added to and removed from the top of the stack(the mostrecently added items are at the top of the stack).

    The last element to be added is the first to be removed (LIFO:Last In, First Out).

  • 8/3/2019 Stacks Son

    4/21

    BASIC STACK OPERATIONS

    Initialize the Stack.

    Pop an item off the top of the stack (delete anitem)

    Push an item onto the top of the stack (insertan item)

    Is the Stack empty?

    Is the Stack full? Clear the Stack

    Determine Stack Size

  • 8/3/2019 Stacks Son

    5/21

    Array Implementation of the Stacks

    The stacks can be implemented by the use ofarraysand linked lists.

    One way to implement the stack is to have a

    data structure where a variable called topkeeps the location of the elements in the stack(array)

    An array is used to store the elements in the

    stack

  • 8/3/2019 Stacks Son

    6/21

    Stack Definition

    struct STACK{

    int count; /* keeps the number of elements inthe stack */

    int top; /* indicates the location of the top ofthe stack*/

    int items[STACKSIZE]; /*array to store the

    stack elements*/}

  • 8/3/2019 Stacks Son

    7/21

    Stacks

  • 8/3/2019 Stacks Son

    8/21

    Stack Initialisation

    initialize the stack by assigning -1 to the toppointer to indicate that the array based stackis empty (initialized) as follows:

    You can write following lines in the mainprogram:

    :

    STACK s;

    s.top = -1;

    :

  • 8/3/2019 Stacks Son

    9/21

    Stack Initialisation

    Alternatively you can use the followingfunction:

    void StackInitialize(STACK *Sptr)

    {

    Sptr->top=-1;

    }

  • 8/3/2019 Stacks Son

    10/21

    Push Operation

    Push an item onto the top of the stack (insert an item)

  • 8/3/2019 Stacks Son

    11/21

    Void push (Stack *, type newItem)

    Function: Adds newItem to the top ofthe stack.

    Preconditions: Stack has beeninitialized and is not full.

    Postconditions: newItem is at the top ofthe stack.

  • 8/3/2019 Stacks Son

    12/21

    void push (STACK *, type newItem)

    void push(STACK *Sptr, int ps) /*pushes ps intostack*/

    {if(Sptr->top == STACKSIZE-1){

    printf("Stack is full\n");return; /*return back to main function*/

    }else {

    Sptr->top++;

    Sptr->items[Sptr->top]= ps;Sptr->count++;

    }}

  • 8/3/2019 Stacks Son

    13/21

    Pop operation

    Popan item off the top of the stack (deletean item)

  • 8/3/2019 Stacks Son

    14/21

    type pop (STACK *)

    Function: Removes topItem from stack andreturns with topItem

    Preconditions: Stack has been initialized and

    is not empty. Postconditions: Top element has been

    removed from stack and the function returnswith the top element.

  • 8/3/2019 Stacks Son

    15/21

    Type pop(STACK *Sptr)

    int pop(STACK *Sptr){int pp;if(Sptr->top == -1){

    printf("Stack is empty\n");

    return -1; /*exit from the function*/}else {

    pp = Sptr->items[Sptr->top];Sptr->top--;

    Sptr->count--;return pp;

    }}

  • 8/3/2019 Stacks Son

    16/21

    void pop(STACK *Sptr, int *pptr)

    void pop(STACK *Sptr, int *pptr){if(Sptr->top == -1){

    printf("Stack is empty\n");

    return;/*return back*/}else {

    *pptr = Sptr->items[Sptr->top];Sptr->top--;

    Sptr->count--;}}

  • 8/3/2019 Stacks Son

    17/21

    EXAMPLE

    The following program implements stack ofsize 10 using array representation.

    The 10 elements entered by the user are

    pushed into the stack and then the elementsare popped back and displayed.

  • 8/3/2019 Stacks Son

    18/21

    #include

    #include

    #define STACKSIZE 10

    typedef struct{int count;

    int top;

    int items[STACKSIZE]; /*stack can contain up to 10 integers*/

    }STACK;

    void push(STACK *, int);

    int pop(STACK *);

    int main()

    {

    int p, i;STACK s;

    s.top = -1; /*indicates that the stack is empty at the beginning*/

    s.count = 0; /* 0 items are in the stack*/

  • 8/3/2019 Stacks Son

    19/21

    /*reading and pushing items into the stack*/

    printf("Enter the %d stack items\n",STACKSIZE);

    for(i=0;i

  • 8/3/2019 Stacks Son

    20/21

    void push(STACK *Sptr, int ps) /*pushes ps into stack*/

    {

    if(Sptr->top == STACKSIZE-1){

    printf("Stack is full\n");

    printf("There are %d items in the stack\n", Sptr-

    >count);

    return; /*return back to main function*/

    }

    else {Sptr->top++;

    Sptr->items[Sptr->top]= ps;

    Sptr->count++;

    }

    }

  • 8/3/2019 Stacks Son

    21/21

    int pop(STACK *Sptr){

    int pp;

    if(Sptr->top == -1){

    printf("Stack is empty\n");return -1 /*exit from the function*/

    }

    else {

    pp = Sptr->items[Sptr->top];

    Sptr->top--;Sptr->count--;

    return pp;

    }

    }