0% found this document useful (0 votes)
12 views2 pages

Dsu 17

Uploaded by

Nirav Parmar
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)
12 views2 pages

Dsu 17

Uploaded by

Nirav Parmar
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/ 2

Practical No.

17: * Write a 'C' Program to perform PUSH and POP Operations on


Stack using Linked List.
CODE :
#include <stdio.h> int poppedValue = temp->data;

#include <stdlib.h> *top = (*top)->next;

struct StackNode { free(temp);

int data; return poppedValue;

struct StackNode* next; }

}; }

struct StackNode* createNode(int value) void display(struct StackNode* top) {


{
if (isEmpty(top)) {
struct StackNode* newNode = (struct
StackNode*)malloc(sizeof(struct printf("Stack is empty.\n");
StackNode)); return;
newNode->data = value; }
newNode->next = NULL; printf("Stack elements: ");
return newNode; struct StackNode* temp = top;
} while (temp != NULL) {
int isEmpty(struct StackNode* top) { printf("%d ", temp->data);
return top == NULL; temp = temp->next;
} }
void push(struct StackNode** top, int printf("\n");
value) {
}
struct StackNode* newNode =
createNode(value); int main() {

newNode->next = *top; struct StackNode* top = NULL;

*top = newNode; int choice, value;

printf("%d pushed onto stack.\n", while (1) {


value);
printf("\nStack Operations Menu:\
} n");

int pop(struct StackNode** top) { printf("1. Push\n");

if (isEmpty(*top)) { printf("2. Pop\n");

printf("Stack Underflow! Cannot pop printf("3. Display Stack\n");


from an empty stack.\n");
printf("4. Exit\n");
return -1;
printf("Enter your choice (1-4): ");
} else {
scanf("%d", &choice);
struct StackNode* temp = *top;
switch (choice) { case 3:

case 1: display(top);

printf("Enter the value to push: break;


");
case 4:
scanf("%d", &value);
printf("Exiting program...\n");
push(&top, value);
return 0;
break;
default:
case 2:
printf("Invalid choice! Please
value = pop(&top); enter a valid choice (1-4).\n");

if (value != -1) { }

printf("Popped value: %d\n", }


value);
return 0;
}
}
break;

OUTPUT :

You might also like