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
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 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
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 now
Download
Save lab2.q2 For Later
Carousel Previous
Carousel Next
Download
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
DS8.
PDF
No ratings yet
DS8.
3 pages
8
PDF
No ratings yet
8
2 pages
ds8
PDF
No ratings yet
ds8
3 pages
Stack
PDF
No ratings yet
Stack
4 pages
Ds New Input 5
PDF
No ratings yet
Ds New Input 5
3 pages
Stack Linked List
PDF
No ratings yet
Stack Linked List
4 pages
Stacks Program1 and 2 Explained
PDF
No ratings yet
Stacks Program1 and 2 Explained
5 pages
Ds Experiment 10
PDF
No ratings yet
Ds Experiment 10
4 pages
Data Structure Unit 3 PPT
PDF
No ratings yet
Data Structure Unit 3 PPT
61 pages
Practical DS 2024
PDF
No ratings yet
Practical DS 2024
7 pages
DSA Unit 2 Topic(9)
PDF
No ratings yet
DSA Unit 2 Topic(9)
5 pages
A69 DS Ass11
PDF
No ratings yet
A69 DS Ass11
6 pages
9842 DSA Exp4
PDF
No ratings yet
9842 DSA Exp4
8 pages
Queue & Stack
PDF
No ratings yet
Queue & Stack
12 pages
2.single_linked_list
PDF
No ratings yet
2.single_linked_list
4 pages
Q1
PDF
No ratings yet
Q1
3 pages
Stack Using Array
PDF
No ratings yet
Stack Using Array
2 pages
Stack Queue Using Linklist
PDF
No ratings yet
Stack Queue Using Linklist
6 pages
Adama Assignment
PDF
No ratings yet
Adama Assignment
7 pages
Write An Algorithm To Insert A New Node at The Beginning of A Singly Linked List Give Example
PDF
No ratings yet
Write An Algorithm To Insert A New Node at The Beginning of A Singly Linked List Give Example
5 pages
dsfile
PDF
No ratings yet
dsfile
11 pages
Array&LL Implementation of Stack
PDF
No ratings yet
Array&LL Implementation of Stack
7 pages
Chrisfred Assignment
PDF
No ratings yet
Chrisfred Assignment
7 pages
DSA lab
PDF
No ratings yet
DSA lab
24 pages
Linked List Implementation in C
PDF
No ratings yet
Linked List Implementation in C
17 pages
9
PDF
No ratings yet
9
4 pages
Linked List DSA
PDF
No ratings yet
Linked List DSA
63 pages
Practical 1
PDF
No ratings yet
Practical 1
9 pages
data_and_file_structure_implementation_and_theory
PDF
No ratings yet
data_and_file_structure_implementation_and_theory
34 pages
Algorithm and Program of Stacks in C
PDF
No ratings yet
Algorithm and Program of Stacks in C
11 pages
2024-Lab Exam Experiments BCA 3rd Sem Riya Updated (1)
PDF
No ratings yet
2024-Lab Exam Experiments BCA 3rd Sem Riya Updated (1)
36 pages
Stack
PDF
No ratings yet
Stack
58 pages
DS File
PDF
No ratings yet
DS File
21 pages
202411005_ CS162_2
PDF
No ratings yet
202411005_ CS162_2
21 pages
Unit II
PDF
No ratings yet
Unit II
32 pages
CodeNote_Stack_array1
PDF
No ratings yet
CodeNote_Stack_array1
4 pages
stacks
PDF
No ratings yet
stacks
2 pages
Stack Using Link List
PDF
No ratings yet
Stack Using Link List
2 pages
Introduction of Ddsu
PDF
No ratings yet
Introduction of Ddsu
29 pages
DSA Notes: Stack Algorithm For PUSH Operation
PDF
No ratings yet
DSA Notes: Stack Algorithm For PUSH Operation
29 pages
Stack in Linked List
PDF
No ratings yet
Stack in Linked List
8 pages
Stacks: EENG212 - Algorithms and Data Structures
PDF
No ratings yet
Stacks: EENG212 - Algorithms and Data Structures
21 pages
Record File - L 3
PDF
No ratings yet
Record File - L 3
9 pages
DS (KCS-301) Unit 2 CSE Stack (Till ST)
PDF
No ratings yet
DS (KCS-301) Unit 2 CSE Stack (Till ST)
15 pages
Lab 5
PDF
No ratings yet
Lab 5
4 pages
EENG212 - Algorithms & Data Structures: Stacks
PDF
No ratings yet
EENG212 - Algorithms & Data Structures: Stacks
5 pages
Stack Using Linked List
PDF
No ratings yet
Stack Using Linked List
2 pages
232-35-540_SE132_Spring25
PDF
No ratings yet
232-35-540_SE132_Spring25
10 pages
Stack
PDF
No ratings yet
Stack
33 pages
Linked Lists
PDF
No ratings yet
Linked Lists
11 pages
Lecture_8 4
PDF
No ratings yet
Lecture_8 4
2 pages
Week 3
PDF
No ratings yet
Week 3
19 pages
Experiment Report 3. 何娜娜
PDF
No ratings yet
Experiment Report 3. 何娜娜
14 pages
07_Stack
PDF
No ratings yet
07_Stack
3 pages
Kenny 230723 Data Structures Interview Questions
PDF
No ratings yet
Kenny 230723 Data Structures Interview Questions
16 pages
Experiment No.: 8: Implement A Stack Using A Single Linked List
PDF
No ratings yet
Experiment No.: 8: Implement A Stack Using A Single Linked List
11 pages
Practical Fil1 (Ankush Shukla 206320040)
PDF
No ratings yet
Practical Fil1 (Ankush Shukla 206320040)
32 pages
DS 2
PDF
No ratings yet
DS 2
63 pages
150+ C Pattern Programs
From Everand
150+ C Pattern Programs
Hernando Abella
No ratings yet