0% found this document useful (0 votes)
8 views4 pages

Expp

The document contains two experiments demonstrating stack implementation in C: the first using an array and the second using a linked list. Each experiment includes source code for push, pop, and display operations, along with error handling for stack overflow and underflow. The main function in the linked list implementation allows user interaction for stack operations.

Uploaded by

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

Expp

The document contains two experiments demonstrating stack implementation in C: the first using an array and the second using a linked list. Each experiment includes source code for push, pop, and display operations, along with error handling for stack overflow and underflow. The main function in the linked list implementation allows user interaction for stack operations.

Uploaded by

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

Experiment No: 3

Aim: WAP to implement stack using array.

Source Code:
#include <stdio.h>
#define MAX 5
int stack[MAX], top = -1;
void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow! Cannot push %d\n", value);
} else {
stack[++top] = value;
printf("%d pushed into the stack.\n", value);
}
}
int pop() {
if (top == -1) {
printf("Stack Underflow! No elements to pop.\n");
return -1;
} else {
printf("%d popped from the stack.\n", stack[top]);
return stack[top--];
}
}
int peek() {
if (top == -1) {
printf("Stack is empty! No top element.\n");
return -1;
} else {
printf("Top element is: %d\n", stack[top]);
return stack[top];
}
}
void display() {
if (top == -1) {
printf("Stack is empty.\n");
} else {
printf("Stack elements: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
Experiment No: 4

Aim: WAP to implement array using linked list.

Source Code:
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

void push(struct Node** top, int value) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Heap overflow!\n");
return;
}
newNode->data = value;
newNode->next = *top;
*top = newNode;
printf("%d pushed onto the stack.\n", value);
}

void pop(struct Node** top) {


if (*top == NULL) {
printf("Stack underflow!\n");
return;
}
struct Node* temp = *top;
printf("%d popped from the stack.\n", temp->data);
*top = (*top)->next;
free(temp);
}

void display(struct Node* top) {


if (top == NULL) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
while (top) {
printf("%d ", top->data);
top = top->next;
}
printf("\n");
}

int main() {
struct Node* top = NULL;
int choice, value;

while (1) {
printf("\nStack Operations:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&top, value);
break;
case 2:
pop(&top);
break;
case 3:
display(top);
break;
case 4:
printf("Exiting...\n");
while (top) pop(&top);
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
Output:

You might also like