Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Implementing Stack in C. Using Linked List */
- #include<stdio.h>
- #include<stdlib.h>
- struct Node
- {
- int data;
- struct Node* next;
- }*top = NULL;
- void push(int value)
- {
- struct Node* newNode;
- newNode = (struct Node*)malloc(sizeof(struct Node));
- newNode->data = value;
- if (top == NULL)
- newNode->next = NULL;
- else
- newNode->next = top;
- top = newNode;
- printf("\nInsertion is Successful!!!\n");
- }
- void pop()
- {
- if (top == NULL)
- printf("\nStack is Empty!!!\n");
- else
- {
- struct Node* temp = top;
- printf("\n to beDeleted element: %d", temp->data);
- top = temp->next;
- free(temp);
- }
- }
- void print()
- {
- if (top == NULL)
- printf("\nStack is Empty!!!\n");
- else {
- struct Node* temp = top;
- while (temp->next != NULL) {
- printf("%d--->", temp->data);
- temp = temp->next;
- }
- printf("%d--->NULL", temp->data);
- }
- }
- int main()
- {
- int choice, value;
- printf("\n:: Stack using Linked List ::\n");
- while (1)
- {
- printf("\n****** MENU ******\n");
- printf("1. Push\n2. Pop\n3. Print\n4. Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1: printf("Enter the value to be insert: ");
- scanf("%d", &value);
- push(value);
- break;
- case 2: pop(); break;
- case 3: print(); break;
- case 4: exit(0);
- default: printf("\nWrong selection!!! Please try again!!!\n");
- }
- }
- return 0;
- }
- /*
- :: Stack using Linked List ::
- ****** MENU ******
- 1. Push
- 2. Pop
- 3. Print
- 4. Exit
- Enter your choice: 1
- Enter the value to be insert: 10
- Insertion is Successful!!!
- ****** MENU ******
- 1. Push
- 2. Pop
- 3. Print
- 4. Exit
- Enter your choice: 1
- Enter the value to be insert: 20
- Insertion is Successful!!!
- ****** MENU ******
- 1. Push
- 2. Pop
- 3. Print
- 4. Exit
- Enter your choice: 2
- to beDeleted element: 20
- ****** MENU ******
- 1. Push
- 2. Pop
- 3. Print
- 4. Exit
- Enter your choice: 3
- 10--->NULL
- ****** MENU ******
- 1. Push
- 2. Pop
- 3. Print
- 4. Exit
- Enter your choice: 4
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement