You are on page 1of 21

Data Structure and Algorithm

Analysis

Lecture 4
Data
Data and information are an essential part in computer science, and various
implementations are being made to store in different ways.

Data are just a collection of facts and figures, or you can say data are values or a
set of values that are in a particular format.

Our data must be organized in a particular way, so that we can access and
manipulate it.
WHAT ARE DATA STRUCTURES?
In computer science, a data structure is a particular way of storing
and organizing data in a computer so that it can be used efficiently.

When we deal with data, How we store, organize and group our data
together matters for example: when trying to find a word in a
dictionary its quick and efficient because its sorted
Categories of Data Structures
The data structure can be subdivided into major types:

Linear Data Structure

Non-linear Data Structure


Linear Data Structure
Data elements are arranged or stored in sequential manner. There is order.
Common linear data structure are:
Array
Linked list
Stack
Queue
Non-Linear Data Structure
Data elements have no order. They are arranged in a hierarchical manner, where
one element is connected to one or more other elements.

Non-Linear Data Structures are listed below:

Graphs

Trees
Arrays
Array is a container which can hold a fixed number of items and these items should be of
the same type.
When compared to a List(dynamic Arrays), Python Arrays stores the similar type of
elements in it. While a Python List can store elements belonging to different data types in
it.
The following are important terms to understand the concept of Array:
Element − Each item stored in an array is called an element.
Index − Each location of an element in an array has a numerical index, which is used to
identify the element.
Example: [7,5,6,-5,50,800] array of integers
[“cat”, ‘’dog”, “sheep”] array of strings
To initialize a Python Array
1. Using for loop and Python range() function to initialize an array with a default
value.

[value for element in range(num)]

Python range() function accepts a number as argument and returns a


sequence of numbers which starts from 0 and ends by the specified number,
incrementing by 1 each time.

Python for loop would place 0(default-value) for every element in the array
between the range specified in the range() function.
To initialize a Python Array
myarray=[]

myarray= [0 for i in range(3)]

print(myarray)

We have created an array — ‘myarray’ and initialize it with 3 elements carrying a


default value (0).

Output:

[0, 0, 0]
To initialize a Python Array
2. Direct method to initialize a Python array
While declaring the array, we can initialize the data values using the following command:
array-name = [default-value]*size
myarray = [0] * 3
print(myarray)
Output : [0, 0, 0]
myarray = ['P'] * 5
print(myarray)
Output : ['P', 'P', 'P', 'P', 'P']
Stack
A stack is a linear data structure that stores items in a LAST IN FIRST OUT = LIFO

It means: the last element inserted is the first one to be removed.

Which is the first element to pick up?


LIFO Principle of Stack
In programming terms, putting an item on top of the stack is called push and
removing an item is called pop.
Stack Operations
There are some basic operations that allow us to perform different actions on a
stack.
Push: Add an element to the top of a stack
Pop: Remove an element from the top of a stack
IsEmpty: Check if the stack is empty
IsFull: Check if the stack is full
Peek: Get the value of the top element without removing it
Working of Stack Data Structure
The operations work as follows:
● A pointer called TOP is used to keep track of the top element in the stack.
● When initializing the stack, we set its value to -1 so that we can check if the
stack is empty by comparing TOP == -1.
● On pushing an element, we increase the value of TOP and place the new
element in the position pointed to by TOP.
● On popping an element, we return the element pointed to by TOP and reduce
its value.
● Before pushing, we check if the stack is already full
● Before popping, we check if the stack is already empty
Working of Stack Data Structure
Push Operation on Stack
Step 1: check if the stack is either full or not

Step 2: if the stack is full, produce an error and exit

Step 3: if the stack is not full, increment top by one

Step 4: add data elements to the stack, where the top is pointing

Step 5: success
Pop Operation on Stack
Step 1: check if the stack is either empty or not

Step 2: if the stack is empty, exit

Step 3: if the stack is not empty, approach the data element at which the top is
pointing

Step 4: decrease top by one

Step 5: success
Peek Operation on Stack
Step 1: check if the stack is either empty or not

Step 2: if the stack is empty, exit

Step 3: if the stack is not empty, approach the data element at which the top is
pointing

Step 4: decrease top by one

Step 5: success
Applications of Stack
Queue
Linked List

You might also like