Skip to content

devdarji07/Cpp_Exp18_Stack-Implementation-using-Array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Cpp_Exp18_Stack-Implementation-using-Array

📘 Experiment: Stack Implementation Using Arrays in C++


🎯 AIM

To implement a stack data structure using arrays in C++ and perform basic operations such as push, pop, peek, and display.


📌 OBJECTIVES

  • To understand the concept of a stack as a linear data structure.
  • To learn how to use arrays and classes in C++ for implementing abstract data types.
  • To practice error handling for stack overflow and underflow conditions.
  • To develop logic-building skills by writing step-by-step algorithms.
  • To understand how LIFO (Last In, First Out) principle is applied in computing.

📖 THEORY

🔹 What is a Stack?

A stack is a linear data structure that follows LIFO (Last In, First Out):

  • The element inserted last is removed first.
  • Example: Stack of books – only the top book can be removed or added.

🔹 Basic Stack Operations

  • Push → Insert an element at the top.
  • Pop → Remove the top element.
  • Peek/Top → View the top element without removing it.
  • Display → Show all elements in the stack.

🔹 Real-Life Examples

  • Undo/Redo operations in editors.
  • Backtracking in puzzles (e.g., maze solving).
  • Function calls in programming (call stack).
  • Browser history (Back button).

🔹 Overflow and Underflow

  • Overflow → Stack is full and cannot insert new elements.
  • Underflow → Stack is empty and cannot remove/peek elements.

🔹 Comparison: Array vs Linked List Stack

Feature Array-based Stack Linked-list-based Stack
Memory Allocation Fixed size (MAX_SIZE) Dynamic, grows/shrinks
Speed Fast (direct indexing) Slightly slower (pointer ops)
Memory Usage May waste space Exact memory but extra for ptr
Implementation Simple and easy More complex

⚙️ ALGORITHMS

🔹 Stack Function (Array Approach)

push()

  1. Increment top by 1.
  2. Assign the value to array[top].
  3. Return the inserted element.

Main Program

  1. Initialize integer array array[5] and top = -1.
  2. Use a loop to push elements 5 times.
  3. Print inserted elements.

🔹 Stack Class Approach

pushElement(num)

  1. Check if topIndex == MAX_SIZE - 1.
  2. If yes → print STACK OVERFLOW.
  3. Else → increment topIndex and insert num.

popElement()

  1. If topIndex == -1 → print STACK UNDERFLOW.
  2. Else → return arr[topIndex] and decrement topIndex.

peekTop()

  1. If topIndex == -1 → print STACK UNDERFLOW.
  2. Else → return arr[topIndex].

displayStack()

  1. If topIndex == -1 → print STACK UNDERFLOW.
  2. Else → print all elements from index 0 to topIndex.

📝 PROGRAM DESCRIPTION

🔹 Stack Function

  • Uses array[5] and top to track the stack.
  • push() inserts value 77 at the top.
  • Main function calls push() 5 times.
  • Uses pass-by-reference for top to update the main stack.

🔹 Stack Class

  • Private data:
    • arr[MAX_SIZE] → Stores stack elements.
    • topIndex → Tracks top element.
  • Public functions:
    • pushElement(int num) → Insert element.
    • popElement() → Remove top element.
    • peekTop() → View top element.
    • displayStack() → Display all elements.
  • Main function demonstrates push, pop, peek, display operations.

🧩 CONCEPTS USED

  • Class & Object → Encapsulation of stack operations.
  • Array → Stores elements sequentially.
  • Encapsulation → Access stack via methods only.
  • Conditionals (if-else) → Handle overflow/underflow.
  • Loops (for loop) → Display stack elements.
  • Macros (#define) → For constants like MAX_SIZE and ERROR_CODE.

✅ CONCLUSION

  • Successfully implemented a stack using arrays in C++.
  • Demonstrated push, pop, peek, display operations.
  • Showed array-based vs linked-list-based stack differences.
  • Improved understanding of data structures, OOP, and error handling.
  • Stack is fundamental in compilers, operating systems, and problem-solving applications.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages