DS-(I) Assignment 7: Set A- b)

Init(S)

Create an empty stack and initializing top to NULL indicating the stack is empty

S=Push(S,x)

Adding an element x to the stack S requires creation of node containing x and putting it in front of the top node pointed by S. This changes the top node S and the function should return the changed value of S.

S=Pop(S)

Deletes top node from stack and returns. Next element will become top of stack and function returns the changed value of S.

X=Peek(S)

Returns topmost element from the stack S without deleting element.

isEmpty(S)

Checks and returns if the stack S is empty or not. Stack isempty when top==NULL

b) Implement a stack library (dystack.h) of integers using a dynamic (linked list) implementation of the stack and implementing the above five operations. Write a driver program that includes stack library and calls different stack operations..

Solution:
#include <stdio.h>
#include <stdlib.h>
#include "dystack.h"
int main()
{
    int n, ch;
    SNODE *S;
    S=Init(S);
    do
    {
        printf("\n\nStack Menu\n1. Push \n2. Pop\n3. Peek \n4. Display\n0. Exit");
        printf("\nEnter Choice 0-4? : ");
        scanf("%d", &ch);
        switch (ch)
        {
            case 1:
                printf("\nEnter number ");
                scanf("%d", &n);
                S=Push(S,n);
                break;
            case 2:
                if(!isEmpty(S))
                {
                    S=Pop(S);
                }
                else
                    printf("\nStack Underflow\n");
                break;
            case 3:
                if(!isEmpty(S))
                {
                    n=Peek(S);
                    printf("\n Peek Element = %d", n);
                }
                else
                    printf("\nStack Underflow\n");
                    break;
            case 4:
                Display(S);
                break;
        }
    }while (ch != 0);
}

Library Function ( .h file )
NOTE: save file name as 'dystack.h'.

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    int data;
    struct node *next;
}SNODE;
SNODE * Init(SNODE *);
SNODE * Push(SNODE *, int);
SNODE * Pop(SNODE *);
int Peek(SNODE *);
int isEmpty(SNODE *);
void Display(SNODE *);

SNODE *Init(SNODE *S)
{
    return(NULL);
}
SNODE *Push(SNODE *S,int item)
{
    SNODE *nptr = (SNODE*)malloc(sizeof(struct node));
    nptr->data = item;
    if(S==NULL)
    {
        nptr->next = NULL;
        S = nptr;
    }
    else
    {
        nptr->next = S;
        S = nptr;
    }
    return(S);
}
SNODE * Pop(SNODE *S)
{
    SNODE *temp;
    if (S == NULL)
    {
        printf("\n\nStack is empty ");
    }
    else
    {
        temp = S;
        S = S->next;
        printf("\n\n%d Deleted", temp->data);
        temp->next=NULL;
        free(temp);
    }
    return(S);
}
int Peek(SNODE * S)
{
    return(S->data);   
}

int isEmpty(SNODE * S)
{
    if(S==NULL)
        return(1);
    return(0);
}
void Display(SNODE * S)
{
    SNODE *temp;
    temp = S;
    while (temp != NULL)
    {
        printf("\n%d", temp->data);
        temp = temp->next;
    }
}

0 Comments:

Post a Comment