0% found this document useful (0 votes)
2 views

Exp_5_cs

The document outlines an experiment to implement a stack data structure using arrays in C++. It describes key operations such as push, pop, and display, along with the procedure for creating a stack class and handling overflow and underflow conditions. The implementation successfully demonstrates stack functionality and validates the LIFO principle with example scenarios.

Uploaded by

Aadarsh Raj1693
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Exp_5_cs

The document outlines an experiment to implement a stack data structure using arrays in C++. It describes key operations such as push, pop, and display, along with the procedure for creating a stack class and handling overflow and underflow conditions. The implementation successfully demonstrates stack functionality and validates the LIFO principle with example scenarios.

Uploaded by

Aadarsh Raj1693
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment-5: Implementation of Stack Using Arrays in C++

Aim of the Experiment:

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

Theory:

A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. The last
element added to the stack is the first one to be removed.

Key Operations:

1. Push: Adds an element to the top of the stack.


2. Pop: Removes the element from the top of the stack.
3. Display: Shows all elements in the stack.

Stack using Arrays:

 A fixed-size array is used to store stack elements.


 A variable top keeps track of the current position in the stack.
 The stack is full when top equals the maximum size of the array minus one.
 The stack is empty when top is -1.

Procedure:

1. Define the class:


o Create a class Stack with private data members: an array, a top variable, and the
maximum size.
o Provide methods for push, pop, and display.
2. Implement push operation:
o Check if the stack is full; if not, increment top and add the element to the array.
3. Implement pop operation:
o Check if the stack is empty; if not, decrement top.
4. Implement display operation:
o Traverse the array from top to 0 to display the elements.
5. Test the program:
o Create a menu-driven program to allow users to perform stack operations.
6. Analyze the results:
o Verify stack behavior for different input sequences.
Result:

The program successfully implements a stack using arrays, demonstrating push, pop, and display
operations while handling overflow and underflow conditions.

The program demonstrates stack functionality using arrays. It allows the user to add elements to the
stack, remove elements from it, and view the current stack contents .Overflow and underflow scenarios
are appropriately handled.

For example, with a stack size of 3, pushing 4 elements results in an overflow, and the elements are
displayed in the correct LIFO order (30 20 10). This validates the implementation of the stack.

You might also like