0% found this document useful (0 votes)
32 views

DSA - Introduction To Data Structures

The document discusses different data structures and their uses. It begins by defining data structures as organized collections of data that can be searched, processed, and modified. It then discusses the need for data structures to organize data efficiently and handle complex applications. The rest of the document describes various linear data structures like arrays and linked lists, non-linear structures like trees and graphs, and how to select the appropriate data structure based on an analysis of resource constraints and operations needed.

Uploaded by

Niba Ilyas
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

DSA - Introduction To Data Structures

The document discusses different data structures and their uses. It begins by defining data structures as organized collections of data that can be searched, processed, and modified. It then discusses the need for data structures to organize data efficiently and handle complex applications. The rest of the document describes various linear data structures like arrays and linked lists, non-linear structures like trees and graphs, and how to select the appropriate data structure based on an analysis of resource constraints and operations needed.

Uploaded by

Niba Ilyas
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Lecture No.

01

Data Structures

Anum Tariq
Data Structures
Organizing Data
Any organization/arrangement for a collection of
records that can be searched, processed in any order, or
modified.
It is a logical way of storing data and it also define
mechanism to retrieve data.
The choice of data structure and algorithm can make
the difference between a program running in a few
seconds or many days.
Need for Data Structures
 Data structures organize data  more efficient
programs.
 More powerful computers  more complex
applications.
 More complex applications demand more
calculations.
Efficiency
 A solution is said to be efficient if it solves the
problem within its resource constraints.
– Space
– Time

 The cost of a solution is the amount of


resources that the solution consumes.
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the
resource constraints a solution must meet.
2. Determine the basic operations that must be
supported. Quantify the resource constraints
for each operation.
3. Select the data structure that best meets these
requirements.
Some Questions to Ask
• Are all data inserted into the data structure at
the beginning, or are insertions interspersed
with other operations?
• Can data be deleted?
• Are all data processed in some well-defined
order, or is random access allowed?
Data Structure Philosophy
 Each data structure has costs and benefits.
 Rarely is one data structure better than another
in all situations.
 A data structure requires:
– space for each data item it stores,
– time to perform each basic operation,
– programming effort.
Data Structure Types

8
Data Structures and Algorithms Fall 2017
Linear Data Structures:

• A data structure is said to be linear if the elements


form a sequence.
• Two ways of representation:
– By means of Sequential memory Locations
– By means of Links
1.Array
2.Stack
3.Queue
4.Linked List

9
Data Structures and Algorithms Fall 2017
Non Linear Data Structures:

• Don’t form a direct sequence among elements.


1.Tree
2.Graph

10
Data Structures and Algorithms Fall 2017
Operations on Data Structures
• Traversing: Accessing each record exactly once so that certain item in
the record may be processed.
• Searching: finding the location of the record with a given key value .

• Insertion : add a new record to the structure


• Deletion : removing a record from the structure

11
Data Structures and Algorithms Fall 2017
1: Arrays

• An array is a collection of homogeneous type of data


elements.
• An array is consisting of a collection of elements

12
Data Structures and Algorithms Fall 2017
Operations performed on arrays

1.Traversing
2.Search
3.Insertion
4.Deletion
5.Sorting
6.Merging

13
Data Structures and Algorithms Fall 2017
Limitations of Arrays

• Size of array is fixed.


• Inserting/Deleting an element is expensive

14
Data Structures and Algorithms Fall 2017
2. Linked List
• A Linked list is a linear collection of data elements .It has two part one is
info and other is link part.info part gives information and link part is
address of next node

15
Data Structures and Algorithms Fall 2017
2. Linked List
• A Linked list is a linear collection of data elements .It has two part one is
info and other is link part.info part gives information and link part is
address of next node

16
Data Structures and Algorithms Fall 2017
Advantage of Linked List over Arrays

• Dynamic Size
• Ease of Insertion and Deletion

Drawbacks:
• Random access is not allowed

17
Data Structures and Algorithms Fall 2017
3. Stacks(LIFO)

• A Stack is a list of elements in which an element may be


inserted or deleted at one end which is known as TOP of the
stack.
• Operation on LIFO:
– Push: add an element in stack
– Pop: remove an element in stack
• Example: Operating System stacks for addresses of programs.

18
Data Structures and Algorithms Fall 2017
Stack Representation

• A stack is an ordered collection of items, generally implemented with only


two principle operations, called Push and Pop.

19
Data Structures and Algorithms Fall 2017
Stack Applications

• Solving Recursion - recursive calls are placed onto a


stack and removed from there once they are
processed.
• Evaluating post-fix expressions
• Solving Towers of Hanoi
• Backtracking
• Depth-first search
• Converting a decimal number into a binary number
20
Data Structures and Algorithms Fall 2017
4. Queues(FIFO)
• A queue is defined as a special type of data structure where the
elements are inserted from one end and elements are deleted from
the other end.
– The end from where the elements are inserted is called REAR end.
– The end from where the elements are deleted is called FRONT end.
• Queue is organized as First In First Out (FIFO)Data Structure.
– Insertion : add a new element in queue (Enque)
– Deletion: Removing an element in queue (Deque)
• Examples: Queues in Operating Systems

21
Data Structures and Algorithms Fall 2017
4. Queues(FIFO) Applications

• Access to shared resources (e.g., printer)


• Multiprogramming
• Message queue

22
Data Structures and Algorithms Fall 2017
5. Trees
• Tree is a hierarchical data structure.
• The very top element of a tree is called the root of the tree.
• Except the root element every element in a tree has a parent
element, and zero or more children elements.
• All elements in the left sub-tree come before the root in sorting
order, and all those in the right sub-tree come after the root.
• Tree is the most useful data structure when you have hierarchical
information to store.
• For example, directory structure of a file system; there are many
variants of tree you will come across. Some of them are Red-black
tree, threaded binary tree, AVL tree, etc.
23
Data Structures and Algorithms Fall 2017
5. Tree’s Applications
• Genealogical tree
– pictures a person's descendants and ancestors
• Game trees
– shows configurations possible in a game such as the Towers of Hanoi
problem
• Parse trees
– used by compiler to check syntax and meaning of expressions such as
2*(3+4)

24
Data Structures and Algorithms Fall 2017
6. Graphs
• Graph is a networked data structure that connects a collection
of nodes called vertices, by connections, called edges.
• An edge can be seen as a path or communication link between
two nodes.
• These edges can be either directed or undirected.
• If a path is directed then you can move in one direction only,
while in an undirected path the movement is possible in both
directions.

25
Data Structures and Algorithms Fall 2017
Arrays
 Elementary data structure that exists as built-in in
most programming languages.

main( int argc, char** argv )


{
int x[6];
int j;
for(j=0; j < 6; j++)
x[j] = 2*j;
}
Arrays
 Array declaration: int x[6];
 An array is collection of cells of the same type.
 The collection has the name ‘x’.
 The cells are numbered with consecutive integers.
 To access a cell, use the array name and an index:
x[0], x[1], x[2], x[3], x[4], x[5]
Array Layout
x[0]
Array cells are
x[1]
contiguous in
computer memory x[2]

The memory can be x[3]


thought of as an
x[4]
array
x[5]
What is Array Name?
 ‘x’ is an array name but there is no variable x. ‘x’ is not an
lvalue. (lvalue simply means an object that has an
identifiable location in memory (i.e. having an address))
 For example, if we have the code
int a, b;
then we can write
b = 2;
a = b;
a = 5;
But we cannot write
2 = a;
Array Name
 ‘x’ is not an lvalue
int x[6];
int n;
x[0] = 5;
x[1] = 2;
x = 3; // not allowed
x = a + b; // not allowed
x = &n; // not allowed
Dynamic Arrays
 You would like to use an array data structure but
you do not know the size of the array at compile
time.
 You find out when the program executes that you
need an integer array of size n=20.
 Allocate an array using the new operator:

int* y = new int[20]; // or int* y = new int[n]


y[0] = 10;
y[1] = 15; // use is the same
Dynamic Arrays
 ‘y’ is a lvalue; it is a pointer that holds the address
of 20 consecutive cells in memory.
 It can be assigned a value. The new operator returns
as address that is stored in y.
 We can write:

y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Dynamic Arrays
 We must free the memory we got using the new
operator once we are done with the y array.

delete[ ] y;

 We would not do this to the x array because we did


not use new to create it.

You might also like