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)
3 views
lab2.q2
Uploaded by
Milica Markovic
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download now
Download
Save lab2.q2 For Later
Download
Save
Save lab2.q2 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
3 views
lab2.q2
Uploaded by
Milica Markovic
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download now
Download
Save lab2.q2 For Later
Carousel Previous
Carousel Next
Save
Save lab2.q2 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>
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct Stack {
Node* top;
int size;
} Stack;
// Function to initialize a stack
Stack* initStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
if (stack == NULL) {
printf("Memory allocation error\n");
exit(EXIT_FAILURE);
}
stack->top = NULL;
stack->size = 0;
return stack;
}
// Function to push an element onto the stack
void push(Stack* stack, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation error\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
stack->size++;
}
// Function to pop an element from the stack
void pop(Stack* stack) {
if (stack->size == 0) {
printf("Stack is empty. Cannot pop.\n");
return;
}
Node* temp = stack->top;
stack->top = stack->top->next;
free(temp);
stack->size--;
}
// Function to get the top element of the stack
int top(Stack* stack) {
if (stack->size == 0) {
printf("Stack is empty.\n");
exit(EXIT_FAILURE);
}
return stack->top->data;
}
// Function to get the size of the stack
int stackSize(Stack* stack) {
return stack->size;
}
int main() {
Stack* stack = initStack();
// Push elements onto the stack
push(stack, 10);
push(stack, 20);
push(stack, 30);
printf("Stack size: %d\n", stackSize(stack));
printf("Top element: %d\n", top(stack));
// Pop elements from the stack
pop(stack);
printf("Popped an element.\n");
printf("Stack size after pop: %d\n", stackSize(stack));
printf("Top element after pop: %d\n", top(stack));
// Free allocated memory for the stack
Node* current = stack->top;
Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
free(stack);
return 0;
}
You might also like
DS Lab Work 4
PDF
No ratings yet
DS Lab Work 4
6 pages
Stack Operation
PDF
No ratings yet
Stack Operation
5 pages
Ds 12
PDF
No ratings yet
Ds 12
3 pages
8
PDF
No ratings yet
8
2 pages
Max - Size
PDF
No ratings yet
Max - Size
7 pages
DATA STRUCTURE CODES final
PDF
No ratings yet
DATA STRUCTURE CODES final
75 pages
CodeNote_Stack_array1
PDF
No ratings yet
CodeNote_Stack_array1
4 pages
Stack Using Array
PDF
No ratings yet
Stack Using Array
2 pages
Implementing Stack in C
PDF
No ratings yet
Implementing Stack in C
6 pages
Ds Experiment 10
PDF
No ratings yet
Ds Experiment 10
4 pages
Week 3
PDF
No ratings yet
Week 3
19 pages
Stack Queue Using Linklist
PDF
No ratings yet
Stack Queue Using Linklist
6 pages
23Z433 Sem Dsa
PDF
No ratings yet
23Z433 Sem Dsa
7 pages
Experiment No 3
PDF
No ratings yet
Experiment No 3
8 pages
maths
PDF
No ratings yet
maths
20 pages
Data Structures Assignment Step 2-22N238
PDF
No ratings yet
Data Structures Assignment Step 2-22N238
35 pages
Self Study 04 Abdul Rafay B23F0389CS154 CS Red
PDF
No ratings yet
Self Study 04 Abdul Rafay B23F0389CS154 CS Red
4 pages
9
PDF
No ratings yet
9
4 pages
Assn Week-2 Ds
PDF
No ratings yet
Assn Week-2 Ds
35 pages
Lab04 2380125
PDF
No ratings yet
Lab04 2380125
11 pages
Ds New Input 5
PDF
No ratings yet
Ds New Input 5
3 pages
Lab Program No. 3
PDF
No ratings yet
Lab Program No. 3
2 pages
3 RD
PDF
No ratings yet
3 RD
3 pages
DSA LAB Programs
PDF
No ratings yet
DSA LAB Programs
59 pages
Data Structure Lab 5 Name: Roll No: Section: Teacher: Task 1
PDF
No ratings yet
Data Structure Lab 5 Name: Roll No: Section: Teacher: Task 1
10 pages
LAB NO 04
PDF
No ratings yet
LAB NO 04
10 pages
lab2q4
PDF
No ratings yet
lab2q4
2 pages
Stack
PDF
No ratings yet
Stack
3 pages
remainderdump
PDF
No ratings yet
remainderdump
28 pages
Stack (Array Implementation)
PDF
No ratings yet
Stack (Array Implementation)
3 pages
Import Public Class Public Static Void: //creating An Object of Stack Class
PDF
No ratings yet
Import Public Class Public Static Void: //creating An Object of Stack Class
2 pages
LabAssign1
PDF
No ratings yet
LabAssign1
2 pages
Stack
PDF
No ratings yet
Stack
2 pages
Queue & Stack
PDF
No ratings yet
Queue & Stack
12 pages
Program 2
PDF
No ratings yet
Program 2
2 pages
DS Print No 7 To 15
PDF
No ratings yet
DS Print No 7 To 15
33 pages
DSA LAB Task 1
PDF
No ratings yet
DSA LAB Task 1
7 pages
3
PDF
No ratings yet
3
2 pages
Program 19
PDF
No ratings yet
Program 19
4 pages
Java Lab Manual
PDF
No ratings yet
Java Lab Manual
42 pages
It2305 Java Programming Lab
PDF
No ratings yet
It2305 Java Programming Lab
42 pages
Lecture 4
PDF
No ratings yet
Lecture 4
9 pages
pfile
PDF
No ratings yet
pfile
41 pages
Dsa Lab Questions
PDF
No ratings yet
Dsa Lab Questions
37 pages
DSA_UNIT-3
PDF
No ratings yet
DSA_UNIT-3
149 pages
StackOperations
PDF
No ratings yet
StackOperations
2 pages
Middel Element
PDF
No ratings yet
Middel Element
5 pages
Stack
PDF
No ratings yet
Stack
2 pages
Stack in Linked List
PDF
No ratings yet
Stack in Linked List
8 pages
Stack Problems
PDF
No ratings yet
Stack Problems
15 pages
Implement Queue Using Link List
PDF
No ratings yet
Implement Queue Using Link List
2 pages
pathakDS
PDF
No ratings yet
pathakDS
38 pages
DS Practical
PDF
No ratings yet
DS Practical
17 pages
WAP To Using Recursion
PDF
No ratings yet
WAP To Using Recursion
46 pages
Name-Anshu Biswas 2005647 DSA Lab 6
PDF
No ratings yet
Name-Anshu Biswas 2005647 DSA Lab 6
22 pages
Assignment 3 Answers Dsa
PDF
No ratings yet
Assignment 3 Answers Dsa
15 pages
DSU Micro Project
PDF
No ratings yet
DSU Micro Project
5 pages
3. Stack - Sample Program
PDF
No ratings yet
3. Stack - Sample Program
5 pages
DSA Lab 06
PDF
No ratings yet
DSA Lab 06
5 pages
150+ C Pattern Programs
From Everand
150+ C Pattern Programs
Hernando Abella
No ratings yet