0% found this document useful (0 votes)
9 views36 pages

DS Lect2

The document provides an overview of data structures, categorizing them into linear (e.g., arrays, stacks) and non-linear (e.g., trees, graphs). It explains operations on arrays and stacks, including insertion, deletion, and searching, while also defining abstract data types and algorithms. Additionally, it covers stack operations, conditions for overflow and underflow, and includes a homework section with questions related to stack operations.

Uploaded by

B43 VEDANT KADAM
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)
9 views36 pages

DS Lect2

The document provides an overview of data structures, categorizing them into linear (e.g., arrays, stacks) and non-linear (e.g., trees, graphs). It explains operations on arrays and stacks, including insertion, deletion, and searching, while also defining abstract data types and algorithms. Additionally, it covers stack operations, conditions for overflow and underflow, and includes a homework section with questions related to stack operations.

Uploaded by

B43 VEDANT KADAM
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/ 36

Data Structure

(CSC303)
Lecture 2
Shilpa Ingoley
• Linear Data Structure:
• A data structure is said to be linear if its elements form sequence
or linear list
• Eg Arrays, linked list, stack, queue

• Non Linear Data Structure:

• Non Linear Data Structure is that if one element can be


connected to more than one adjacent element then it is non
linear data structure.
• One-many or many-many relationships are handled through
nonlinear data structure.
• Eg tree, graph.
Linear Data Structures : Arrays
• An array is defined as a set of finite number of
homogeneous elements or same data items.
• It means an array can contain one type of data
only, either all integer, all float-point number or all
character.
• Types: 1 D array , 2 D Array … etc
Operations on Arrays
• Searching a element
• Insertion of new element
• Deletion of required element
• Modification of an element
• Merging of arrays
One Dimensional array representation
Array having maximum size as max
Searching
Insertion Operation
Deletion Operation
Abstract Data Type:
• An ADT is defined as a mathematical model of data
objects that make up a data type as well as the
functions that operate on these objects.
• ADT is the structure considered without regard to
its implementation.
• It can be thought of as a description of the data in
the structure with a list of operations that can be
performed on the data within that structure.
Algorithm:
• Algorithm: An algorithm is a sequence of instructions that act on
some input data to produce some output in a finite number of
steps.
An algorithm must have the following properties:
• Input: An algorithm must receive some input data supplied
externally.
• Output: An algorithm must produce at least one output as the
result.
• Finiteness: No matter what is the input, the algorithm must
terminate after a finite number of steps. The procedure which
goes on performing a series of steps infinitely is not an algorithm.
• Definiteness: The steps to be performed in the algorithm must be
clear and unambiguous.
• Effectiveness: One must be able to perform steps in algorithm-
Practically should be possible without applying any intelligence.
Example:
Algorithm to find largest of 2 numbers:
• Step 1: Read 2 numbers A & B
• Step 2: If A>B then A is largest and go to step 4
• Step 3: If A<B then B is largest and go to step 4
• Step 4: Stop
Stack
Linear DS : Stack
• A stack is also an ordered collection of elements
like arrays, but it has a special feature that deletion
and insertion of elements can be done only from
one end called the top of the stack (TOP)

• Due to this property it is also called as last in first


out type of data structure (LIFO).
Stack representation in memory using Array:
Stack

• Stack is a linear data structure in which addition of


new element or deletion of an existing element
always take place at the same end which is known as
top of the stack.
• Example: stack of books, coins, etc
• As the elements in the stack can be added or
removed from the top, it means the last element to
be added to stack is the first one to be removed.
• Therefore stack are also called as last in first out(LIFO)
or push down list or FILO
Operations for a stack:
• Create stack
• Push stack
• Pop stack
• Peek stack
• Empty stack
• Full stack
• Stack count
• Destroy stack
• Stack contains an ordered collection of elements and
array is used to store ordered list of elements.
• Hence it would be very easy to manage stack using an
array.
• In array we can push elements one by one from 0th
position till (n-1)th position.
• In array any element can be added or deleted at any
place but in stack we want to push or pop only from
the top of stack so we can take a variable top which
keeps position of the top element in array.
• If top=-1 or top=NULL will indicate that the stack is
empty.
• Variable MAXSTACK will give the maximum number of
elements held by the stack.
• A stack of elements of any particular type is finite
sequence of elements of that type together with
following operations:
• Initialize stack to be empty
• Determine whether stack is empty or not
• Determine if stack is full or not.
• If stack is not full, then insert new element at one end of
stack called top. This operation is called as push.
• If stack is not empty then retrieve the elements from its
top.
• If stack is not empty then delete the elements from its
top. This operation is called as pop.
Fig shows array representation of stack:
Stack Operations:
Push operation on stack
push(5)
Push(10)
Push(12)
Push(15)
Push Operation:
• It means adding a new element in the stack.
• For pushing or adding the new element on the stack, first we should
check the condition of overflow i.e. stack is full or not and if not then
only we can push the element as:
• if(top==max-1) {
• printf(“\n Stack overflow”); }
else
• {
top=top+1;
stack[top]=element
}

• Top pointer will always point to last added element of stack.


POP Operation

Fig: POP Operation


POP Operation
• Pop means removing an element from stack.
• For pop operation on stack first we should check the condition of
underflow i.e. whether the stack is empty or not and if not then
pop the element as:

if(top==-1)
printf(“\n Stack underflow”)
else
{
printf(“Popped element is %d”,arr[top]);
top=top-1;
}
Stack Full/ Stack Overflow:
Stack Full/ Stack Overflow:

• It might be possible that a condition arises in stack


when there is no place for adding the new element
in the array since it reaches to its maxsize.
• This condition is known as overflow condition.
• When top==max-1 then we can say that stack
overflow condition arises.
if(top==max-1)
printf(“\n Stack overflow”);
Stack Empty/ Stack Underflow:
• When the stack is empty and we are trying to
perform delete operation i.e. removing an element
from stack then that condition is known as stack
underflow condition
• If there is no element present in stack the value of
top=-1
if(top==-1)
printf(“\n Stack underflow”);
Stack Empty/ Stack Underflow:
Homework: Application of Stack
Reversing a list:
• A list of numbers can be reversed by reading each
number from the array starting from the first index
and pushing it on stack.
• Once all the numbers have been read, the numbers
can be popped one at a time and then stored in the
array starting from the first index.
1)Process of inserting an element in stack is called
a)create
b)push
c)pop
d)none of the above

2)Process of removing an element from stack is called_______


a) pop
b) push
c) delete
d) None of the above
3. In a stack, if a user tries to remove an element from empty
stack it is called _____
a) Empty collection
b) Underflow
c) Overflow
d) Garbage Collection

4.Pushing an element into stack already having five elements


and stack size of 5, then stack becomes
a) crash
b) Overflow
c) underflow
d) user flow

You might also like