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

Class XI Computer Science Lesson 9 Stack Data Structure Session 2021-'22

The document provides an overview of the Stack Data Structure, explaining its definition, advantages, and basic operations. It highlights the Last In First Out (LIFO) principle of stacks, the implementation methods using arrays or linked lists, and the algorithms for push and pop operations. Additionally, it includes a worksheet exercise related to a class named WordPile that manages character elements using stack principles.

Uploaded by

vermarajveer160
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)
3 views6 pages

Class XI Computer Science Lesson 9 Stack Data Structure Session 2021-'22

The document provides an overview of the Stack Data Structure, explaining its definition, advantages, and basic operations. It highlights the Last In First Out (LIFO) principle of stacks, the implementation methods using arrays or linked lists, and the algorithms for push and pop operations. Additionally, it includes a worksheet exercise related to a class named WordPile that manages character elements using stack principles.

Uploaded by

vermarajveer160
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

`

Class XI
Computer Science
Lesson 9
Stack Data Structure
Session 2021-‘22

Video Tutorial:

• Introduction to data structures


• Introduction to stack

What is Data Structure?

A data structure is a way of storing and organizing data in a computer so that it can be used
efficiently. It provides a means to manage large amounts of data efficiently. And efficient
data structures are key to designing efficient algorithms. Some examples of Data Structures
are arrays, Linked List, Stack, Queue, etc.

Advantages of Data Structures

Efficiency: Efficiency of a program depends upon the choice of data structures. For example:
suppose, we have some data and we need to perform the search for a perticular record. In
that case, if we organize our data in an array, we will have to search sequentially element by
element. hence, using array may not be very efficient here. There are better data structures
which can make the search process efficient like ordered array or binary search tree.

Reusability: Data structures are reusable, i.e. once we have implemented a particular data
structure, we can use it at any other place.

Abstraction: Data structure is specified by the ADT which provides a level of abstraction.
The client program uses the data structure through interface only, without getting into the
implementation details.

Page 1 of 6
`
Class XI
Computer Science
Lesson 9
Stack Data Structure
Session 2021-‘22

Operations on data structure:

• Traversing
• Insertion
• Deletion
• Searching
• Sorting
• Merging

Page 2 of 6
`
Class XI
Computer Science
Lesson 9
Stack Data Structure
Session 2021-‘22

What is Stack Data Structure?

Stack is a linear data structure predefined capacity. It is a simple data structure that allows
adding and removing elements in a particular order. Every time an element is added, it goes
on the top of the stack and the only element that can be removed is the element that is at
the top of the stack, just like a pile of objects.

Stack of cards used to visually portray the stack data structure

Page 3 of 6
`
Class XI
Computer Science
Lesson 9
Stack Data Structure
Session 2021-‘22

Basic features of Stack:

1. Stack is an ordered list of similar data type.


2. Stack is a LIFO(Last in First out) structure.
3. push() function is used to insert new elements into the Stack and pop() function is
used to remove an element from the stack. Both insertion and removal are allowed
at only one end of Stack called Top.
4. Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty.

Implementation of Stack Data Structure:

Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but are
limited in size and Linked List requires overhead to allocate, link, unlink, and deallocate, but
is not limited in size. Here we will implement Stack using array.

Page 4 of 6
`
Class XI
Computer Science
Lesson 9
Stack Data Structure
Session 2021-‘22

Algorithm for PUSH operation

• Check if the stack is full or not.


• If the stack is full, then print error of overflow and exit the program.
• If the stack is not full, then increment the top and add the element.

Algorithm for POP operation

• Check if the stack is empty or not.


• If the stack is empty, then print error of underflow and exit the program.
• If the stack is not empty, then print the element at the top and decrement the top.

Analysis of Stack Operations

Below mentioned are the time complexities for various operations that can be performed
on the Stack data structure.
Push Operation : O(1)
Pop Operation : O(1)
The time complexities for push() and pop() functions are O(1) because we always have to
insert or remove the data from the top of the stack, which is a one step process.

Worksheet

Q1. WordPile is an entity which can hold maximum of 20 characters. The restriction is that a
character can be added or removed from one end only.Some of the members of the class
are given below:

Page 5 of 6
`
Class XI
Computer Science
Lesson 9
Stack Data Structure
Session 2021-‘22

Class name: WordPile


Data members/instance variables:

ch[]: character array to hold the character elements.


capacity: integer variable to store the maximum capacity.
top: to point to the index of the topmost element.

Methods/Member functions:

WordPile(int cap): constructor to initialize the data member capacity = cap, top = -1 and
create the WordPile.
void pushChar(char v): adds the character to the top of WordPile if possible, otherwise
output a message “WordPile is full”
char popChar(): returns the deleted character from the top of the WordPile if possible,
otherwise it returns ‘\\’.

Specify the class WordPile giving the details of the constructor, void pushChar(char) and
char popChar(). The main function and algorithm need not be written.

Page 6 of 6

You might also like