From 2c8c5d405d692b57868c2ee64f88b994d3191e2f Mon Sep 17 00:00:00 2001 From: Priyesh Raj Singh <65354440+priyesh-raj-singh@users.noreply.github.com> Date: Fri, 16 Oct 2020 15:51:25 +0530 Subject: [PATCH] stack_using_linked list Program for implementing stack with help of linked list . hacktoberfest --- stack_using_linked list | 119 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 stack_using_linked list diff --git a/stack_using_linked list b/stack_using_linked list new file mode 100644 index 0000000..cf851e6 --- /dev/null +++ b/stack_using_linked list @@ -0,0 +1,119 @@ +#include +#include +void push(); +void pop(); +void display(); +struct node +{ +int val; +struct node *next; +}; +struct node *head; + +void main () +{ + int choice=0; + printf("\nStack operations using linked list ------\n"); + + while(choice != 4) + { + printf("\n\nChose one from the below options...\n"); + printf("\n1.Push\n2.Pop\n3.Show\n4.Exit"); + printf("\n Enter your choice \n"); + scanf("%d",&choice); + switch(choice) + { + case 1: + { + push(); + break; + } + case 2: + { + pop(); + break; + } + case 3: + { + display(); + break; + } + case 4: + { + printf("Exiting...."); + break; + } + default: + { + printf("Please Enter valid choice "); + } + }; +} +} +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; + } + } +}