Header Ads Widget

Responsive Advertisement

Stack Push Pop Operation Using Linked List


#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int data;
    struct Node *next;
};

//struct Node* top = NULL;

void link_list_Traversal_print(struct Node *ptr)
{
    while (ptr != NULL)
    {
        printf("Element : %d\n", ptr->data);
        ptr = ptr->next;
    }
}

int isFull(struct Node *top)
{
    struct Node *p = (struct Node *)malloc(sizeof(struct Node));

    if (p == NULL)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int isEmpty(struct Node *top)
{
    if (top == NULL)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

struct Node* push(struct Node *top, int x)
{
    if (isFull(top))
    {
        printf("Stack Overflow\n");
    }
    else
    {
        struct Node *n = (struct Node *)malloc(sizeof(struct Node));
        n->data = x;
        n->next = top;
        top = n;
        return top;
    }
}
int pop(struct Node ** top)
{
    if (isEmpty(*top))
    {
        printf("Stack Underflow\n");
    }
    else
    {
        struct Node* n = *top;
       *top = (*top)-> next;
       int x = n-> data;
       free (n);
       return x;
     
    }
}



int main()
{
    struct Node* top = NULL;
    top = push(top,8);
    top = push(top,88);
    top = push(top,91);
    top = push(top,92);
    top = push(top,95);
    top = push(top,97);
    top = push(top,93);
    top = push(top,96);
    top = push(top,29);
    link_list_Traversal_print(top);
    int element = pop(&top);
   
    printf("The Popped ESlement is %d\n",element);
    link_list_Traversal_print(top);
    return 0;
}

 

Post a Comment

0 Comments