Untitled

 avatar
unknown
plain_text
3 years ago
3.2 kB
1
Indexable
#include <stdio.h>
#include <stdlib.h>

void push();
void pop();
void display();
void top();
int isEmpty();
int size();

struct node
{
    int val;
    struct node *next;
};
struct node *head;

int main ()
{
    int choice = 100;
    printf("\n Stack operations\n");
    while(choice != 0)
    {
        int sz = 0, flag = 0;
        printf("\n\nMain menu:\n");
        printf("\n1.Push\n2.Pop\n3.Top\n4.Display\n5.Size\n6.isEmpty\n0.Exit\n");
        printf("\n Enter your choice : \n");
        fflush(stdin);
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            {   display();
                push();
                display();
                break;
            }
            case 2:
            {   display();
                pop();
                display();
                break;
            }
            case 3:
            {
                top();
                break;
            }
            case 4:
            {
                display();
                break;
            }
            case 5:
            {
                sz = size();
                printf("Size of stack is: %d \n", sz);
                break;
            }
            case 6:
            {
                flag = isEmpty();
                if(flag == 1)
                    printf("Stack is empty. \n");
                else
                    printf("Stack is not empty. \n");
                break;
            }
        }
    }
    return 0;
}

void push ()
{
    int val;
    struct node *ptr = (struct node*)malloc(sizeof(struct node));
    if(ptr == NULL)
    {
        printf("not able to push the element");
    }
    else
    {
        printf("Enter the value ");
        scanf("%d",&val);
        if(head==NULL)
        {
            ptr->val = val;
            ptr -> next = NULL;
            head=ptr;
        }
        else
        {
            ptr->val = val;
            ptr->next = head;
            head=ptr;

        }
        printf("Item pushed");

    }
}

void pop()
{
    int item;
    struct node *ptr;
    if (head == NULL)
    {
        printf("Underflow");
    }
    else
    {
        item = head->val;
        ptr = head;
        head = head->next;
        free(ptr);
        printf("Item popped");

    }
}
void display()
{
    int i;
    struct node *ptr;
    ptr=head;
    if(ptr == NULL)
    {
        printf("Stack is empty\n");
    }
    else
    {
        printf("Printing Stack elements \n");
        while(ptr!=NULL)
        {
            printf("%d\n",ptr->val);
            ptr = ptr->next;
        }
    }
}

void top()
{
    struct node * ptr;
    ptr = head;
        if(ptr == NULL)
        {
            printf("Stack is empty\n");
        }
        else
            printf("%d\n", ptr -> val);

}

int size()
{
    int cnt = 0;
    struct node* ptr;
    ptr = head;
    while(ptr != NULL)
    {
        cnt++;
        ptr = ptr -> next;
    }
    return cnt;
}

int isEmpty()
{
    int flag = 0;
    if(head == NULL)
        flag = 1;
    else
        flag = 0;
    return flag;

}