0% found this document useful (0 votes)
106 views6 pages

Technical Report Writing For Ca2 Examination: Topic: Stack Implementation

This technical report provides an overview of stack data structures, including their key properties, common operations, implementations, use cases, and complexity analysis. Stacks follow the LIFO principle and support push and pop operations. They are commonly used to manage function calls, evaluate expressions, and implement undo/redo features. Stacks can be implemented using arrays or linked lists, with arrays typically being faster but having a fixed size. Understanding stacks is essential for computer science problems and programming.

Uploaded by

Aishee Dutta
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)
106 views6 pages

Technical Report Writing For Ca2 Examination: Topic: Stack Implementation

This technical report provides an overview of stack data structures, including their key properties, common operations, implementations, use cases, and complexity analysis. Stacks follow the LIFO principle and support push and pop operations. They are commonly used to manage function calls, evaluate expressions, and implement undo/redo features. Stacks can be implemented using arrays or linked lists, with arrays typically being faster but having a fixed size. Understanding stacks is essential for computer science problems and programming.

Uploaded by

Aishee Dutta
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/ 6

TECHNICAL REPORT WRITING FOR CA2 EXAMINATION

Topic: Stack Implementation

NAME: AISHEE DUTTA


UNIVERSITY ROLL NO.: 18730522017
REGISTRATION NO.: 221870110320
PAPER NAME: Data Structure and Algorithm
PAPER CODE: PCCCS301
STREAM: CSE (DATA SCIENCE)
BATCH: 2022-2026
INTRODUCTION

A stack is a fundamental data structure in computer science that follows the Last-In-First-Out
(LIFO) principle. It is used to store and manage a collection of items, allowing two main
operations: pushing (adding) an item onto the stack and popping (removing) the top item from
the stack. Stacks are widely used in various computer science applications, such as parsing
expressions, tracking function calls, and managing memory.

This technical report provides an in-depth overview of stack data structures, their
implementation in various programming languages, and common use cases.

Stacks are widely used in various computer algorithms and applications. They are essential for
managing function calls and storing temporary data in programs, as well as for solving problems
like evaluating expressions, parsing syntax, and tracking state changes in algorithms.

In this introduction, we'll explore the basic concepts of implementing a stack data structure and
how it works:

Operations on a Stack:

➢ Push: Adding an element to the top of the stack.


➢ Pop: Removing the top element from the stack.
➢ Peek (or Top): Viewing the top element without removing it.
➢ IsEmpty: Checking if the stack is empty.
➢ Size (or Count): Determining the number of elements in the stack.
Stack Data Structure

1. Properties
A stack has two primary properties:
LIFO Principle: The last item added to the stack is the first one to be removed. This principle
ensures that the most recently added item is always at the top of the stack.
Operations: Stacks support two main operations:
➢ Push: Adding an item to the top of the stack.
➢ Pop: Removing the top item from the stack.
➢ Peek: returns the top item without removing it.
➢ isEmpty and isFull check if the stack is empty or full, respectively.

2. Key Characteristics of a Stack:


o It operates on a "last-in, first-out" (LIFO) basis.
o Elements can only be added or removed from the top of the stack.
o It has a fixed capacity or can dynamically resize as needed.

3. Common Use Cases:


o Function call management (call stack): Storing function call information during program
execution.
o Expression evaluation: Evaluating mathematical expressions, such as postfix or infix
notation.
o Undo/Redo functionality in applications.
o Backtracking algorithms: Storing and retrieving states during recursive algorithms.
o Syntax parsing: Validating and parsing code syntax, like checking for balanced
parentheses.

4. Implementation Approaches:
o Stacks can be implemented using arrays or linked lists.
o Array-based stacks are generally faster but have a fixed size.
o Linked list-based stacks can grow dynamically but involve more memory overhead.
Stack Implementation in C:
#include <stdio.h>
#include <stdbool.h>
struct Stack {
int items [MAX_SIZE];
int top;
};
void initialize (struct Stack* stack) {
stack->top = -1;
}
bool isEmpty (struct Stack* stack) {
return stack->top == -1;
}
bool isFull (struct Stack* stack) {
return stack->top == MAX_SIZE - 1;
}
void push (struct Stack* stack, int item) {
if (isFull(stack)) {
printf ("Stack is full. Cannot push %d.\n", item);
return;
}
stack->items[++stack->top] = item;
}
int pop (struct Stack* stack) {
if (isEmpty(stack)) {
printf ("Stack is empty. Cannot pop.\n");
return -1;
}
return stack->items[stack->top--];
}
int peek (struct Stack* stack) {
if (isEmpty(stack)) {
printf ("Stack is empty. Cannot peek.\n");
return -1;
}
return stack->items[stack->top];
}
int main () {
struct Stack myStack;
initialize(&myStack);
push (&myStack, 10);
push (&myStack, 20);
push (&myStack, 30);
printf ("Top element: %d\n", peek(&myStack));
while (! isEmpty(&myStack)) {
printf ("Popped: %d\n", pop(&myStack));
}
return 0;
}

5.Complexity Analysis:
o The time complexity of basic stack operations (push, pop, peek) is typically O (1) for
both array and linked list implementations.
o The space complexity depends on the implementation and whether it uses a fixed or
dynamic size.
6. Error Handling:
o Stack overflow: Occurs when trying to push an element onto a full stack.
o Stack underflow: Occurs when trying to pop or peek an element from an empty stack.
CONCLUSION
Stacks are a fundamental data structure with many practical applications in computer science
and software development. They are relatively simple to implement and can be used in various
programming languages to solve a wide range of problems. Understanding stack principles and
their implementations is essential for any programmer or computer scientist.

In summary, understanding and implementing a stack is crucial for many aspects of computer
science and programming. It offers a simple yet powerful way to manage data and control flow
in various applications. Depending on the specific requirements and constraints of your project,
you can choose the most suitable stack implementation approach, whether it's array-based or
linked list-based, to efficiently solve problems and optimize your code.

REFERENCES
• Geegsforgeeks.com
• Wikipedia.com

You might also like