Link Liste Push Implementation Stack Program in C

Link Liste Push Implementation Stack Program in C

#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node* link;
} *top = NULL;


void push(int data)
{
    struct node* newNode;
    newNode = malloc(sizeof(newNode));
    if(newNode == NULL)
    {
        printf("Stack Overflow. ");
        exit(1);
    }
    newNode -> data = data;
    newNode -> link = NULL;
    newNode -> link = top;
    top = newNode;
}

void print()
{
    struct node* temp;
    temp = top;
    printf("the elements are : ");
    while(temp)
    {
        printf("%d", temp -> data);
        temp = temp -> link;

    }
    printf("\n");
}

int main()
{
    int choice, data;
    while(1){
        printf("1. Push\n");
        printf("2. print\n");
        printf("3. Quit \n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch(choice){
            case 1:
            printf("Enter the element to be pushed: ");
            scanf("%d", &data);
            push(data);
            break;

            case 2: print(); break;
            case 3: exit(1);
            defualt:
            printf("Wrong choice \n");
            break;
        }
    }
    return 0;
}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.