Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
13 views
2 pages
Stack Using Array
Uploaded by
Ayush Sharma
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download
Save
Save stack using array For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
13 views
2 pages
Stack Using Array
Uploaded by
Ayush Sharma
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save stack using array For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 2
Search
Fullscreen
#include <stdio.
h>
#include <stdlib.h>
#define MAX 100 // Maximum size of the stack
// Structure to represent a stack
struct Stack {
int arr[MAX]; // Array to store stack elements
int top; // Index of the top element
};
// Function to initialize the stack
void initStack(struct Stack* stack) {
stack->top = -1; // Set top to -1 indicating an empty stack
}
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
// Function to check if the stack is full
int isFull(struct Stack* stack) {
return stack->top == MAX - 1;
}
// Function to push an element onto the stack
void push(struct Stack* stack, int data) {
if (isFull(stack)) {
printf("Stack overflow! Cannot push %d\n", data);
return;
}
stack->arr[++stack->top] = data;
printf("%d pushed to stack\n", data);
}
// Function to pop an element from the stack
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow! Cannot pop\n");
return -1;
}
return stack->arr[stack->top--];
}
// Function to peek the top element of the stack
int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
return stack->arr[stack->top];
}
// Driver code to test the stack implementation
int main() {
struct Stack stack;
initStack(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printf("%d popped from stack\n", pop(&stack));
printf("Top element is %d\n", peek(&stack));
return 0;
}
You might also like
Ds - All Codes
PDF
No ratings yet
Ds - All Codes
18 pages
Data Structure
PDF
No ratings yet
Data Structure
10 pages
Peek Operation in Stack Using Array
PDF
No ratings yet
Peek Operation in Stack Using Array
6 pages
DataStructures Unit 2
PDF
No ratings yet
DataStructures Unit 2
26 pages
DS 2
PDF
No ratings yet
DS 2
63 pages
Experiment 1
PDF
No ratings yet
Experiment 1
6 pages
101
PDF
No ratings yet
101
4 pages
Stack Implementation
PDF
No ratings yet
Stack Implementation
5 pages
DSA - Exp1 - Vu4f2021081 - Sushant Patil
PDF
No ratings yet
DSA - Exp1 - Vu4f2021081 - Sushant Patil
5 pages
Ds Experiment 10
PDF
No ratings yet
Ds Experiment 10
4 pages
Stack Operations Based On The Provided Sources
PDF
No ratings yet
Stack Operations Based On The Provided Sources
3 pages
202411005_ CS162_2
PDF
No ratings yet
202411005_ CS162_2
21 pages
STACK DATA STRUCTURE
PDF
No ratings yet
STACK DATA STRUCTURE
18 pages
Dsa Lab 1
PDF
No ratings yet
Dsa Lab 1
3 pages
DS Programs
PDF
No ratings yet
DS Programs
4 pages
Stack operations
PDF
No ratings yet
Stack operations
3 pages
Ds New Input 5
PDF
No ratings yet
Ds New Input 5
3 pages
StackOperations
PDF
No ratings yet
StackOperations
2 pages
MODULE_2_DSC
PDF
No ratings yet
MODULE_2_DSC
17 pages
Stack
PDF
No ratings yet
Stack
4 pages
DS8.
PDF
No ratings yet
DS8.
3 pages
Implementing Stack in C
PDF
No ratings yet
Implementing Stack in C
6 pages
stacks
PDF
No ratings yet
stacks
2 pages
Stack in Linked List
PDF
No ratings yet
Stack in Linked List
8 pages
Q1
PDF
No ratings yet
Q1
3 pages
CBS FOR LAB PROGRAMMING IN C - CS3271
PDF
No ratings yet
CBS FOR LAB PROGRAMMING IN C - CS3271
6 pages
Chapter 1 dsa
PDF
No ratings yet
Chapter 1 dsa
25 pages
DS NEHA 8
PDF
No ratings yet
DS NEHA 8
4 pages
Stacks Program1 and 2 Explained
PDF
No ratings yet
Stacks Program1 and 2 Explained
5 pages
Stack Problems
PDF
No ratings yet
Stack Problems
15 pages
DSADARA cProgramee
PDF
No ratings yet
DSADARA cProgramee
1 page
Unit 2 Stacks and Queues
PDF
No ratings yet
Unit 2 Stacks and Queues
57 pages
Queue & Stack
PDF
No ratings yet
Queue & Stack
12 pages
StackUsingLL Tilak Jain H2 42
PDF
No ratings yet
StackUsingLL Tilak Jain H2 42
3 pages
CodeNote_Stack_array1
PDF
No ratings yet
CodeNote_Stack_array1
4 pages
Stack Implementation Using Arrays
PDF
No ratings yet
Stack Implementation Using Arrays
5 pages
DATA STRUCTURE CODES final
PDF
No ratings yet
DATA STRUCTURE CODES final
75 pages
Stacks_Tutorial
PDF
No ratings yet
Stacks_Tutorial
12 pages
DDS Practical
PDF
No ratings yet
DDS Practical
37 pages
Ds Assignment 7
PDF
No ratings yet
Ds Assignment 7
6 pages
Algorithm and Program of Stacks in C
PDF
No ratings yet
Algorithm and Program of Stacks in C
11 pages
9
PDF
No ratings yet
9
4 pages
lab2.q2
PDF
No ratings yet
lab2.q2
2 pages
DSA 1-6 exp (041)
PDF
No ratings yet
DSA 1-6 exp (041)
28 pages
DS Lab Work 4
PDF
No ratings yet
DS Lab Work 4
6 pages
Stack
PDF
No ratings yet
Stack
1 page
Stack Program
PDF
No ratings yet
Stack Program
2 pages
8
PDF
No ratings yet
8
2 pages
Stack Using Array
PDF
No ratings yet
Stack Using Array
3 pages
Stack programs (1)
PDF
No ratings yet
Stack programs (1)
12 pages
Stack
PDF
No ratings yet
Stack
4 pages
Experiment 3 DS Student
PDF
No ratings yet
Experiment 3 DS Student
10 pages
FALLSEM2023-24_ITA3002_ETH_VL2023240102705_2023-07-31_Reference-Material-I
PDF
No ratings yet
FALLSEM2023-24_ITA3002_ETH_VL2023240102705_2023-07-31_Reference-Material-I
6 pages
Implementing Stack in C
PDF
No ratings yet
Implementing Stack in C
2 pages
Lab Programs
PDF
No ratings yet
Lab Programs
29 pages
DSA UNIT-2
PDF
No ratings yet
DSA UNIT-2
78 pages
stack using array
PDF
No ratings yet
stack using array
7 pages
Practical 1
PDF
No ratings yet
Practical 1
9 pages
Computer Engineering Laboratory Solution Primer
From Everand
Computer Engineering Laboratory Solution Primer
Karan Bhandari
No ratings yet
150+ C Pattern Programs
From Everand
150+ C Pattern Programs
Hernando Abella
No ratings yet