0% found this document useful (0 votes)
29 views18 pages

Cs2x1 Dsa Spring 2023 l1

This document provides an overview of data structures and algorithms. It begins by classifying data structures as primitive or non-primitive, and lists common linear and non-linear data structures. It then defines key concepts like abstract data types, arrays, stacks, and queues. The document explains stacks in detail, covering common operations like push and pop, applications like parenthesis checking, and examples of stack problems.

Uploaded by

220120025
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)
29 views18 pages

Cs2x1 Dsa Spring 2023 l1

This document provides an overview of data structures and algorithms. It begins by classifying data structures as primitive or non-primitive, and lists common linear and non-linear data structures. It then defines key concepts like abstract data types, arrays, stacks, and queues. The document explains stacks in detail, covering common operations like push and pop, applications like parenthesis checking, and examples of stack problems.

Uploaded by

220120025
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/ 18

CS2x1:Data Structures and Algorithms

Koteswararao Kondepu
[email protected]
Outline

• Classifications of Data Structures


• Data Structures Overview
• Abstract Data Structures
o Stack
o Queue
o Linked List
Data Type
• Why do we need data types?

• Data type is an attribute of data, which tells the compiler (or interpreter) how
the programmer intended to use the data.

• Is it really helps to reduce the coding effort?

• Is the data type determines how the computation is performed in underlying


hardware?
Classification of Data Structures
Data
Structures

Primitive Data Non-Primitive


Structures Data Structures

Integer Real Character Boolean Linear Data Non-Linear Data


Structures Structures

Linked
Arrays Stack Queues Trees Graphs
List
Define: Data Structures

• Data structure is a special format for organizing and strong data

• Data structure is used to denote a particular way of organizing data for


particular type of operations

• Data structure is a data organization, management, and storage format


that enables efficient access and modification.

• Data structure is collection of data values, the relationships among them,


and the functions or operations that can be applied to the data.
Abstraction
• A real life example: Lets consider a program that manages the student records, which
allows to check if the student are opting a particular course or not.

• What student information is need for the record?


o Name, DoB, ID, email, mailing address, transcripts, hobbies, etc …

• IS all the above properties are necessary to solve the given problem?

• Student Properties: TYPE OF DATA


o Name ➢ No information
o ID about, how to
• Operations performed by this program: OPERATIONS ON THE DATA store this data in
o ADD (to add a student to the class) computer memory
o SEARCH (if the particular student enrolled to the class or not) and how to
o DELETE (if the student dropped the course) implement them.
Define: Abstract Data Type (ADT)
• Abstract data type defines the logical form of the data type → data and
operations

• Abstract data type contains functions/operations that operate on its data


(e.g., add, search and delete)

• Abstract data type describes the expected behaviour associated with a


concrete data structure.

• Abstract data type: Data structures + their operations

• What are the commonly used abstract data types?


o Stack, Queue and Lists
Arrays

• Arrays are the collection of same data type (homogeneous) item

a:
Index 0 1 2 3 4 5

int a[6] = {15, 20, 25, 30, 5, 10}


Array: Operations
Array Data Structures

Create Insert Delete Search Traversal

• Operations: Insertions, deletion, searching, sorting and traversal (visiting each


element)

• Why do arrays fail?


• Cannot grow size of array dynamically Can we solve these problems by
• Allocation of larger size --- wasting space using any other data structures?
Define: Stacks
• Stack is one-side open and the other side is closed

• Last-In-First-Out (LIFO) or First-In-Last-Out (FILO)


• Insertion and deletion are done at one end, called TOP

• Major Operations:
• Why we have only two operations?
• An element is inserted in a stack --- PUSH
• An element is removed from the stack --- POP

• Exceptions:
• Underflow – Trying to POP from an empty stack
• Overflow – Trying to PUSH an element in a full stack
Stack: PUSH Examples
PUSH (5) PUSH (8)
S
5 5
5
4 4
4
3 3
3
2 2 2
1 1 1 S [1] = 8
0 TOP = 0 0 S [0] = 5 0 5 TOP = 1 + 1
Stack is empty TOP = 0 + 1
PUSH (15)
5 PUSH (S, TOP, Val)
4 begin
3 S[TOP] = Val;
2 S [2] = 15 TOP = TOP + 1;
1 8 TOP = 2 + 1 end
0 5
Stack: POP Examples
S = POP() S = POP()
S = POP() 5 5
5 4 4
4 3 3
TOP = 2 - 1
3 TOP = 3 - 1 2 return S[1] 2
2 15 return S[2] 1 8 1 TOP = 1 - 1
1 0 0
8 5 5 return S[0]
0
5

POP (S, TOP, Val)


begin
TOP = TOP - 1;
return S[TOP]
//return S[TOP--]
end
Stack: Applications
• Page-visited history in a web browser [Back Buttons]
• Balancing of symbols (e.g., ( , { or [ )
• Infix to Postfix conversion
• Evaluation of postfix expression
• Implementation of function calls
• Matching Tags in HTML and XML
Stack: Applications – Parenthesis checking
• {23 + 4 * (4 + 6) + [4 + 6]}

• {23 + 4 * (4 + 6) + [4 + 6}] if fun () {


}
} //wrong: error don’t
➢ Push start brace into the stack have opening brace

➢ When you see the close brace, just pop its open brace from the stack
For every open brace,
➢ No braces then ignore other things there should be a close
brace { () [] }
Stack: Applications – Examples
• Following sequence of operations is performed on the stack
push (20), push (30), pop(), push (20), push (30), pop(), pop(),
pop(),push(30), pop(). Sequence of popped elements:

a) 30, 20, 30, 20, 30 b) 30, 30, 20, 20, 30 c) 20, 30, 30, 20, 30
Stack: Applications – Examples
• Following sequence of operations is performed on the stack
push (50), push (40), pop(), push (40), push (-30), pop(), pop(),
t=pop(),push(-10), t=pop(). What is the value of t+20?

a) -10 b) 10 c) 60 d) 70
Stack: Applications – Examples (1)
• What is the output of the program for the following input?
63*443+*+

a) 38 b) 46 c) 40 d) 42
thank you!
email:
[email protected]

NEXT Class: 20/04/2023

You might also like