lecture11 of dsf

Upload: neeta-thune

Post on 08-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 lecture11 of dsf

    1/67

    Stacks and queues

  • 8/7/2019 lecture11 of dsf

    2/67

    stack

    A stack is theordered list inwhich all insertionand deletion willbe done at thesingle end.

    Typical example of

    stack is stack ofplates

    60

    50

    40

    3020

    10

    top

  • 8/7/2019 lecture11 of dsf

    3/67

    Primitive stack operations

    To create a stack

    To insert one element

    To delete one element

    To check which element is onto top

    To check whether the stack empty

    or not.

  • 8/7/2019 lecture11 of dsf

    4/67

    Stack using array

    #include #include #include #define size 5

    struct stack { int s[size]; int top; }st;

  • 8/7/2019 lecture11 of dsf

    5/67

    Stack using array

    int stfull()

    {

    if(st.top>=size-1)

    return 1;

    else

    return 0;

    }

  • 8/7/2019 lecture11 of dsf

    6/67

    void push(int item) { st.top++; st.s[st.top]=item; } int stempty() { if(st.top==-1) return 1; else return 0; }

  • 8/7/2019 lecture11 of dsf

    7/67

    int pop()

    {

    int item; item=st.s[st.top];

    st.top--;

    return(item); }

  • 8/7/2019 lecture11 of dsf

    8/67

    void display()

    {

    int i;

    if(stempty()) printf("\n Stack is empty!!");

    else

    {

    for(i=st.top;i>=0;i--)

    printf("\n%d",st.s[i]); }

    }

  • 8/7/2019 lecture11 of dsf

    9/67

    void main(void) { int item,choice; char ans;

    st.top=-1; clrscr(); printf("\n\t\t Implementation of stack"); do { printf("\n Main menu"); printf("\n1.Push\n2.Pop\n3.Display\n4.Exit"); printf("\nEnter your choice: "); scanf("%d",&choice);

  • 8/7/2019 lecture11 of dsf

    10/67

    switch(choice) { case 1:printf("\nEnter the item to be

    pushed: "); scanf("%d",&item); if(stfull()) printf("\nStack is full!"); else push(item); break;

  • 8/7/2019 lecture11 of dsf

    11/67

    case 2:if(stempty())

    printf("\nEmptyStack!Underflow!!");

    else

    {

    item=pop();

    printf("\nThe popped element

    is: %d",item); }

    break;

  • 8/7/2019 lecture11 of dsf

    12/67

    case 3:display(); break; case 4: exit(0);

    } printf("\nDo you want to continue?"); ans=getche(); }

    while(ans=='Y'||ans=='y'); getch(); }

  • 8/7/2019 lecture11 of dsf

    13/67

    STACK USING LINK LIST

    #include

    #include

    #include #include

    typedef struct stack{ int data;

    structstack*next;}node;

  • 8/7/2019 lecture11 of dsf

    14/67

    void main() { node*top; int val,item,choice; char ans,ch; void push(int ,node**); void display(node**); int pop(node**); int sempty(node*); clrscr(); top=NULL; printf("\nstack using linklist");

  • 8/7/2019 lecture11 of dsf

    15/67

    do { printf("\n1.push\n2.pop\n3.display\n4.exit"); printf("\nenter ur choice");

    scanf("%d",&choice); switch(choice) { case 1:printf("\nenter the data"); scanf("%d",&val);

    push(v

    al,&top); break; case 2:if (sempty(top)) printf("\nstack is empty");

  • 8/7/2019 lecture11 of dsf

    16/67

    else { item=pop(&top); printf("\npopped elementis%d",item); } break; case 3:display(&top); break; case 4:printf("do u want 2 quit"); ch=getche(); if(ch=='y') exit(0); else break;

  • 8/7/2019 lecture11 of dsf

    17/67

    default:printf("\nwrong choice");

    }

    printf("\ndo u want2 continue");

    ans=getche();

    }

    while(ans=='y');

    getch();

    }

  • 8/7/2019 lecture11 of dsf

    18/67

    void push(int item,node**top)

    {

    node*New; node*get_node(int);

    New=get_node(item);

    New->next=*top;

    *top=New;

    }

  • 8/7/2019 lecture11 of dsf

    19/67

    node*get_node(int item) { node*temp;

    temp=(node*)malloc(sizeof(node)); if(temp==NULL) printf("\n memory is not allocated"); temp->data=item;

    temp->next=NULL; return temp; }

  • 8/7/2019 lecture11 of dsf

    20/67

    int sempty(node*temp)

    {

    if(temp==NULL) return 1;

    else

    return 0; }

  • 8/7/2019 lecture11 of dsf

    21/67

    int pop(node**top) { int item;

    node*temp; item=(*top) ->data; temp=(*top); *top=(*top) ->next;

    free(temp); return (item); }

  • 8/7/2019 lecture11 of dsf

    22/67

    void display(node**head)

    {

    node*temp; temp=*head;

    if(sempty(temp))

    printf("\nstack is empty");

  • 8/7/2019 lecture11 of dsf

    23/67

    else

    {

    while(temp!=NULL)

    { printf("%d",temp->data);

    temp=temp->next;

    }

    } getch();

    }

  • 8/7/2019 lecture11 of dsf

    24/67

    Application of stack

    Various application of the stack

    Expression conversion

    Expression evaluation Parsing well formed parenthesis

    Decimal to binary conversion

    Reversing a string Storing function call

  • 8/7/2019 lecture11 of dsf

    25/67

    Concept ofinfix prefix and postfix

    expression

    Expression is the combination of theoperand and operator operand are

    numaricvalue and operator are oftwo type

    Unary + and

    Binary +,-,*,/ and exponential in

    general

    Three types of expression

  • 8/7/2019 lecture11 of dsf

    26/67

    Concept ofinfix prefix and postfix

    expression

    Infix expression (operand1 operatoroperand2)

    A+B

    (a+b)*(c-d) (a+b/e)*(d+f)

    PostfixExpression(operand1operand2opewrator)

    ab+ ab+cd-*

    ab+e/df+*

  • 8/7/2019 lecture11 of dsf

    27/67

    Prefix Expression

    Prefix=operatoroperand1operand2

    +ab

    *+ab-cd */+abe+df

  • 8/7/2019 lecture11 of dsf

    28/67

    Conversion of infix to postfix

    expression

    Algorithm

    Read the infix expression for left to rightone character at a time

    Initially push $ onto the stack for $instack set priority as -1 if input symbolread is( then push it on to the stack

    If the input symbol read is an opersndthen place it in postfix expression

    If the input symbol read is operator

  • 8/7/2019 lecture11 of dsf

    29/67

    a)Check if priority of operator in stack is greaterthan the priority of incoming operator then popthe operator from the stack and place itin thepostfix expression.repeat step 4 a) till we get the

    opertator from the stack which has greaterpriority then incoming operator.

    b)otherwise push the operator being read ontothe stack

    C) if we read input operator as the ) then pop the

    operator until we not get (

    Final pop all th content until stack becomesempty append them to postfix expression.

  • 8/7/2019 lecture11 of dsf

    30/67

  • 8/7/2019 lecture11 of dsf

    31/67

    operator Instackpriorities

    Incomingpriorities

    +or- 2 1

    *or/ 4 3

    ^ for anyexponential

    5 6

    ( 0 9

    Operand 8 7

    ) na 0

  • 8/7/2019 lecture11 of dsf

    32/67

    Program for Evaluation of postfix

    expression

    Algorithm

    Read the post fix expression from left toright

    If input symbol as operand push it ontothe stack

    If operator is read pop two operand andperform arithmatic operation

    Push the result onto the stack Repeat steps 1-4 until postfix expression

    not over,

  • 8/7/2019 lecture11 of dsf

    33/67

    program

    #include #include #include

    #define size 80

    struct stack {

    double s[size]; int top; }st;

  • 8/7/2019 lecture11 of dsf

    34/67

    void main() { char exp[size];

    int len; Double result; Double post(); clrscr();

    printf("\nEnter a postfix expression :");

  • 8/7/2019 lecture11 of dsf

    35/67

    scanf(%s, &exp);

    len=strlen(exp);

    exp[len]=$; result=post[exp];

    Printf(\n the value of theexpression is%f,result);

    getch(); }

  • 8/7/2019 lecture11 of dsf

    36/67

  • 8/7/2019 lecture11 of dsf

    37/67

    double post(char exp[]) { char ch, *type; Double result ,val,op1,op2;

    Void push (double);

    Double pop(); int i; St.top=-1; i=0; Ch=exp[i];

    While(ch!=$) {

  • 8/7/2019 lecture11 of dsf

    38/67

    if(ch>=0&& ch

  • 8/7/2019 lecture11 of dsf

    39/67

    Else if(strcmp(type,operator)==0) { op2=pop();

    op1=pop();

    switch(ch) { case '+': result=op1+op2; break; case '-:result=op1-op2; break;

  • 8/7/2019 lecture11 of dsf

    40/67

    case '*':result=op1*op2;

    break;

    case'/':result=op1/op2;break;

    case^':result=pow(op1,op2);break

  • 8/7/2019 lecture11 of dsf

    41/67

    }

    Push(result);

    }

    i++;

    Ch=exp[i];

    }

    Result=pop();

    Return result;

    }

  • 8/7/2019 lecture11 of dsf

    42/67

    Void push(double val)

    {

    If (st.top>=size-1) Printf(\n stack full);

    St.top++;

    St.s[st.top]=val; }

  • 8/7/2019 lecture11 of dsf

    43/67

    Double pop()

    {

    Double val;

    If(st.top==-1)

    Printf(\n stack is empty);

    Val=st.s[st.top];

    St.top--;

    Return val;

    }

  • 8/7/2019 lecture11 of dsf

    44/67

    queues

    The queue is defined as theordered collection of elements that

    has two ends front and rear fromthe front end one can remove theelement and from the rear end onecan delet the element.

    Ex people waiting for ticket counter.

  • 8/7/2019 lecture11 of dsf

    45/67

    Operation on queue

    The queue is called as FIFO,

    Operations like

    Queue overflow

    Insertion of the element into thequeue

    Queue underflow

    Deletion of the element Display of the queue

  • 8/7/2019 lecture11 of dsf

    46/67

    Operation on queue

    Before performing the insert operation we check thequeue overflow

    Before performing the deletion we check the queueis underflow

    10 20 30 40 50 60

    Rear end

    Frontend

  • 8/7/2019 lecture11 of dsf

    47/67

    queue using array

    #include

    #include

    #include

    #define size 5

    struct Queue

    {

    int que[size];

    int front,rear;

    }Q;

  • 8/7/2019 lecture11 of dsf

    48/67

    Int Qfull()

    {

    if(Q.rear>=size-1) return 1;

    else

    return 0; }

  • 8/7/2019 lecture11 of dsf

    49/67

    int insert(int item)

    {

    if(Q.front==-1) Q.front++;

    Q.que[++Q.rear]=item;

    return Q.rear; }

  • 8/7/2019 lecture11 of dsf

    50/67

    int Qempty()

    {

    if((Q.front==-1)||(Q.front>Q.rear)) return 1;

    else

    return 0; }

  • 8/7/2019 lecture11 of dsf

    51/67

  • 8/7/2019 lecture11 of dsf

    52/67

    void display()

    {

    int i; printf("\nThe elements are:- \n");

    for(i=Q.front;i

  • 8/7/2019 lecture11 of dsf

    53/67

    void main()

    {

    int choice,item;

    char ans; clrscr();

    Q.front=-1;

    Q.rear=-1;

    do {

  • 8/7/2019 lecture11 of dsf

    54/67

  • 8/7/2019 lecture11 of dsf

    55/67

    printf("\nQUEUE is FULL");

    else

    {

    printf("\nEnter the element tobe inserted:- ");

    scanf("%d",&item);

    insert(item); }

    break;

  • 8/7/2019 lecture11 of dsf

    56/67

    case 2:if(Qempty()) printf("\nQUEUE is EMPTY"); else

    item=delet(); break; case 3:if(Qempty()) printf("\nQUEUE is EMPTY"); else display(); break;

  • 8/7/2019 lecture11 of dsf

    57/67

  • 8/7/2019 lecture11 of dsf

    58/67

  • 8/7/2019 lecture11 of dsf

    59/67

    void main(void)

    {

    char ans;

    int ch; void insert();

    Q *delet();

    void display(Q *);

    front=NULL;

    rear=NULL; do

    {

  • 8/7/2019 lecture11 of dsf

    60/67

    printf("\n\tProgram for queue using link list\n");

    printf("\nMain Menu");

    printf("\n1.Insert\n2.Delete\n3.Display");

    printf("\nEnter Your Choice"); scanf("%d",&ch);

    switch(ch)

    {

    case 1:insert();

    break; case 2:front=delet();

  • 8/7/2019 lecture11 of dsf

    61/67

  • 8/7/2019 lecture11 of dsf

    62/67

    while(ans=='y'||ans=='Y'); getch(); clrscr();

    } Q *get_node(Q *temp) { temp=(Q *)malloc(sizeof(Q)); temp->next=NULL; return temp; }

  • 8/7/2019 lecture11 of dsf

    63/67

    void insert() { char ch; Q *temp; temp=get_node(temp); printf("\nInsert the elelment in the queue:\n"); scanf("%d",&temp->data); if(front==NULL) {

    front=temp; rear=temp; }

  • 8/7/2019 lecture11 of dsf

    64/67

    else {

    rear->next=temp;

    rear=rear->next;

    }

    } int Qempty(Q *front)

    {

    if(front==NULL)

    return 1;

    else

    return 0;

    }

  • 8/7/2019 lecture11 of dsf

    65/67

  • 8/7/2019 lecture11 of dsf

    66/67

    else { printf("\n\tThe deleted Element is

    %d",temp->data); front=front->next; temp->next=NULL; free(temp); } return front; }

  • 8/7/2019 lecture11 of dsf

    67/67