C Program Day-17

download C Program Day-17

of 15

Transcript of C Program Day-17

  • 7/31/2019 C Program Day-17

    1/15

  • 7/31/2019 C Program Day-17

    2/15

    Push and Pop Operation in a stack

    Define Enqueue & Dequeue method in a Queue

    Discuss LIFO Method

    Discuss FIFO Method

    Define Stack & Queue

  • 7/31/2019 C Program Day-17

    3/15

    Stack : (Pushdown list)

    It is a linear data structure. The insertion of element on to the stack

    is called as PUSH operation, and deletion of element from the stack iscalled as POP operation.

    Elements insertion or deletion can be done at one end. It follows

    Last-In First-Out technique or LIFO technique.

    Using TOP pointer Push or Pop operation is performed

    Stack Related Terms :

    Top : This pointer points to the top most element of the stack. The top of

    the stack indicates its door from where elements are entered or deleted.

    It is used to verify stacks current position.

  • 7/31/2019 C Program Day-17

    4/15

    Stack Underflow : When there is no element in the stack, the status of the stack is

    known as Stack Underflow. The TOP is present at the bottom of the stack.

    Stack Overflow : When the stack contains equal number of elements as per its

    capacity and no more elements can be added, the status of stack is known as Stack

    Overflow. In such position, the TOP rests at the highest position.

    Storage : The capacity of the stack is known as Storage.

    Representation of Stack : The stack is represented in various ways. The stack can be

    shown as completely empty or fully filled or filled with few elements.

    When stack has no elements, it is called as Empty Stack.

    The stack with completely filled elements is called as Filled Stack, and no further

    elements can be inserted.

    A top pointer maintains track of the top elements of the stack. For empty stack, top

    value is zero.

  • 7/31/2019 C Program Day-17

    5/15

    When stack has one element the value of top is one. Whenever an element is

    inserted in the stack the value of top is incremented by one (PUSH Operation)

    In the same manner, the value of top is reduced by one when an element is

    deleted from the stack (POP operation).

    The push operation can be applied to any stack, but before applying pop

    operation on the stack.

    Stack Implementation :

    (i) Static Implementation : It can be achieved using arrays. Once a size of an

    array is declared, its size cannot be modified during execution. It is also

    inefficient for utilization of memory. It cannot be expanded, when we store

    more elements. The vacant space of stack is wasted, when we store less

    elements.

  • 7/31/2019 C Program Day-17

    6/15

    (ii) Dynamic Implementation : Pointer can be used for implementation of

    stack. The link list is an example of this implementation. Using this

    implementation at runtime, there is no restriction on the number of

    elements. The stack may be expandable. Memory is efficiently utilized

    with pointers

    Operation on stack :

    The following fundamental operations on stack can be carried outthrough static implementation called array implementation.

    (i) creating a stack

    (ii) Checking stack either full or empty

    (iii) Initializing a stack

    (iv) Insert (PUSH) an element into the stack

    (v) Delete (POP) an element from the stack

    (vi) Display elements of stack.

  • 7/31/2019 C Program Day-17

    7/15

    Stack using Pointer : Pointer implementation of a stack can be carried out is

    similar to array implementation. This Dynamic Implementation, create

    NODE at run time, accordingly PUSH, POP operation can be done.

    (i) creating a stack

    (ii) Checking stack either full or empty

    (iii) Initializing a stack

    (iv) Insert (PUSH) an element into the stack

    (v) Delete (POP) an element from the stack

    (vi) Display elements of stack.

  • 7/31/2019 C Program Day-17

    8/15

    #include

    struct stacktype

    {

    int item[100];

    int top;

    };struct stacktype stack;

    int givendata,poppeddata;

    int choice;

    isstackfull()

    {if (stack.top == 100)

    return 1;

    else

    return 0;

    }

    isstackempty(){

    if (stack.top == 0)

    return 1;

    else

    return 0;

    }Continued

  • 7/31/2019 C Program Day-17

    9/15

    void createstack()

    {

    stack.top = 0;

    }

    void push()

    {

    stack.top = stack.top +1;

    stack.item[stack.top] = givendata;

    }

    void pop()

    {

    poppeddata = stack.item[stack.top];

    stack.top= stack.top -1;

    }

    void main()

    {

    clrscr();

    createstack(stack);Continued,

  • 7/31/2019 C Program Day-17

    10/15

    do

    {

    printf("\n1. Push");

    printf("\n2. Pop");

    printf("\n3. Show Top");

    printf("\n4. Quit\n");

    printf("\n Enter your Choice : ");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1:printf("\nEnter data to push : ");

    scanf("%d",&givendata);

    if (isstackfull(stack))

    printf("\nStack Full!!!,cannot push");

    elsepush();

    printf("Data pushed into stack\n");

    break;case 2:

    if (isstackempty(stack)== 1)

    Continued,

  • 7/31/2019 C Program Day-17

    11/15

    printf("\nStack Empty\n");

    else

    {

    pop();

    printf("\nData popped is %d\n",poppeddata);

    }

    break;

    case 3:

    if (isstackempty()== 1)

    printf("\nStack empty\n");

    elseprintf("\nTop most item is %d\n",stack.item[stack.top]);

    break;

    }

    }while(choice != 4);

    }

  • 7/31/2019 C Program Day-17

    12/15

    # include

    # include

    #define null 0

    struct list

    {

    int item;

    struct list *next;

    };

    typedef struct list node;node *start,*temp;

    void push()

    {

    node *new,*temp;int newitem;

    while(1) {

    printf("\nEnter new integer data [0 to exit]:");

    scanf("%d",&newitem);

    Continued,

  • 7/31/2019 C Program Day-17

    13/15

    if (newitem ==0)

    break;

    new = (node *)malloc(sizeof(node));

    new->item = newitem;

    new->next = start;

    start = new;

    }

    }

    void display()

    {node *temp;

    temp = start;

    if (start == null)

    printf("\nEmpty Stack");

    elsewhile(temp)

    {

    printf("%d ",temp->item);

    temp = temp->next;

    }

    } Continued,

  • 7/31/2019 C Program Day-17

    14/15

    void pop()

    {

    node *temp;

    if (start == null)

    printf("\n Empty Stack");

    else {

    printf("\n Popped item is %d",start->item);

    temp = start->next;

    free(start);

    start = temp;

    }}void main()

    {

    int choice;

    start = null;

    clrscr();do{

    printf("\n 1. Push");

    printf("\n 2. Pop");

    printf("\n 3. Display");

    Continued,

  • 7/31/2019 C Program Day-17

    15/15

    printf("\n 4. Exit");

    printf("\n Enter your choice : ");

    scanf("%d",&choice);

    switch(choice)

    {case 1:

    push();

    break;

    case 2:

    pop();break;

    case 3:

    display();

    break;

    }

    }while(choice != 4);

    }