0% found this document useful (0 votes)
52 views19 pages

Unit - 2 Linear Data Structures - Array & Stack

The document summarizes linear data structures arrays and stacks. It describes that an array is a collection of homogeneous data items stored in contiguous memory locations that can be accessed using an index. A stack follows the LIFO principle and only allows insertions and deletions from one end called the top. Common stack operations include push to insert and pop to delete elements.

Uploaded by

Darshna Sharma
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)
52 views19 pages

Unit - 2 Linear Data Structures - Array & Stack

The document summarizes linear data structures arrays and stacks. It describes that an array is a collection of homogeneous data items stored in contiguous memory locations that can be accessed using an index. A stack follows the LIFO principle and only allows insertions and deletions from one end called the top. Common stack operations include push to insert and pop to delete elements.

Uploaded by

Darshna Sharma
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/ 19

Unit -2

Linear Data Structures –


Array & Stack
Array
• Array is a container which can hold a fix number of
items and these items should be of the same type.
• An array is a collection of homogeneous (same type)
data items stored in contiguous memory locations.

• 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.
• Why we need an array?
• Array is particularly useful when we are dealing
with lot of variables of the same type. For
example, lets say I need to store the marks in
math subject of 100 students. To solve this
particular problem, either I have to create the
100 variables of int type or create an array of int
type with the size 100.
• Obviously the second option is best, because
keeping track of all the 100 different variables is a
tedious task. On the other hand, dealing with
array is simple and easy, all 100 values can be
stored in the same array at different indexes (0 to
99).
Representation of an Array
• Arrays can be declared in various ways in
different languages. For illustration, let's take
C array declaration.
As per the above illustration, following are
the important points to be considered.
• Index starts with 0.
• Array length is 10 which means it can store
10 elements.
• Each element can be accessed via its index.
For example, we can fetch an element at
index 6 as 27.
Basic Operations
Following are the basic operations supported by an
array.
• Traverse − print all the array elements one by
one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given
index or by the value.
• Update − Updates an element at the given index.
Advantages :
• A convenient way to store large amounts of similar data.
• It is used to represent multiple items of similar nature
using a single name.
• It allows us to store data in multi-dimensional arrays. (
We will learn about them in a later section.)
• It is used to create other data structures like heaps, linked
lists, etc.
• You need not access elements sequentially, random
access is allowed.
• Since elements are stored in consecutive blocks hence
they are useful to use in iterations.
• Since the size of the array is known during compile-time,
there is no chance of memory run out for arrays.
• Disadvantages:
• Since it is a static data structure that is has a fixed
size we should know or determine array size at
compile time itself. No modifications can be done to
array size during runtime.
• Inserting and deleting elements from an array is a
tedious task, as it would involve shifting of some or
all the elements of the array which would also
involve managing memory space for it as well.
• Since we declare array at compile time itself with a
particular size, it is possible that a lot of memory
space might get wasted if only some address space is
used and occupied.
• Operations like insertion, deletion are time-
consuming tasks on arrays.
Applications of an array:
Apart from being widely used in programming,
arrays have additional applications as well:
• Used in mathematical problems like matrices etc.
• They are used in the implementation of other
data structures like linked lists etc.
• Database records are usually implemented as
arrays.
• Used in lookup tables by computer.
• It effectively executes memory addressing logic
wherein indices act as addresses to the one-
dimensional array of memory.
Stack
A Stack is a widely used linear data structure in modern
computers in which insertions and deletions of an element
can occur only at one end, i.e., top of the Stack. It is used in
all those applications in which data must be stored and
retrieved in the last.
An everyday analogy of a stack data structure is a stack of
books on a desk, Stack of plates, table tennis, Stack of
bootless, Undo or Redo mechanism in the Text Editors, etc.
• A Stack is a linear data structure that follows
the LIFO (Last-In-First-Out) principle. Stack
has one end.
• It contains only one pointer top
pointer pointing to the topmost element of
the stack.
• Whenever an element is added in the stack, it
is added on the top of the stack, and the
element can be deleted only from the stack.
• In other words, a stack can be defined as a
container in which insertion and deletion can be
done from the one end known as the top of the
stack.
Working of Stack
• Stack works on the LIFO pattern. As we can
observe in the below figure there are five
memory blocks in the stack; therefore, the size of
the stack is 5.
• Suppose we want to store the elements in a stack
and let's assume that stack is empty. We have
taken the stack of size 5 as shown below in which
we are pushing the elements one by one until
the stack becomes full.
• The following are some common operations
implemented on the stack:
• push(): When we insert an element in a stack then
the operation is known as a push. If the stack is full
then the overflow condition occurs.
• pop(): When we delete an element from the stack,
the operation is known as a pop. If the stack is
empty means that no element exists in the stack,
this state is known as an underflow state.
• isEmpty(): It determines whether the stack is empty
or not.
• isFull(): It determines whether the stack is full or not.
'
• peek(): It returns the element at the given
position.
• count(): It returns the total number of elements
available in a stack.
• change(): It changes the element at the given
position.
• display(): It prints all the elements available in
the stack.
PUSH operation
The steps involved in the PUSH operation is given below:
• Before inserting an element in a stack, we check
whether the stack is full.
• If we try to insert the element in a stack, and the stack
is full, then the overflow condition occurs.
• When we initialize a stack, we set the value of top as
-1 to check that the stack is empty.
• When the new element is pushed in a stack, first, the
value of the top gets incremented, i.e.,
top=top+1, and the element will be placed at the new
position of the top.
• The elements will be inserted until we reach
the max size of the stack.
• The process of putting a new data element onto
stack is known as a Push Operation. Push
operation involves a series of steps −
• Step 1 − Checks if the stack is full.
• Step 2 − If the stack is full, produces an error and
exit.
• Step 3 − If the stack is not full, increments top to
point next empty space.
• Step 4 − Adds data element to the stack location,
where top is pointing.
• Step 5 − Returns success.

You might also like