0% found this document useful (0 votes)
13 views3 pages

Stack C

Uploaded by

akshaya31032007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Stack C

Uploaded by

akshaya31032007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h> // For malloc and exit()

struct node {
int label;
struct node *next;
};

int main() {
int ch = 0;
struct node *h, *temp, *head;

// Stack head node (dummy node)


head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;

while (1) {
printf("\nStack using Linked List\n");
printf("1 -> Push\n");
printf("2 -> Pop\n");
printf("3 -> View\n");
printf("4 -> Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);

switch (ch) {
case 1: // Push
temp = (struct node *)malloc(sizeof(struct node));
if (!temp) {
printf("Memory allocation failed\n");
break;
}
printf("Enter label for new node: ");
scanf("%d", &temp->label);
temp->next = head->next;
head->next = temp;
printf("Node pushed successfully.\n");
break;

case 2: // Pop
if (head->next == NULL) {
printf("Stack Underflow\n");
} else {
temp = head->next;
head->next = temp->next;
printf("Node %d deleted\n", temp->label);
free(temp);
}
break;

case 3: // View
printf("\nHEAD -> ");
h = head->next;
while (h != NULL) {
printf("%d -> ", h->label);
h = h->next;
}
printf("NULL\n");
break;

case 4: // Exit
printf("Exiting...\n");
exit(0);

default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

You might also like