0% found this document useful (0 votes)
125 views30 pages

Lec01 AF

This document provides an overview of data structures and their implementation in C++/C#. It discusses how data structures prepare students for advanced courses and covers common structures like arrays, linked lists, stacks, queues, trees and graphs. It also outlines the grading breakdown and how to access course materials. Key points include: - Data structures store and organize data for efficient use by applications - Common structures include linear ones like arrays and linked lists, and non-linear ones like trees and graphs - Operations like traversing, searching, inserting and deleting are important for data structures

Uploaded by

syed asim najam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views30 pages

Lec01 AF

This document provides an overview of data structures and their implementation in C++/C#. It discusses how data structures prepare students for advanced courses and covers common structures like arrays, linked lists, stacks, queues, trees and graphs. It also outlines the grading breakdown and how to access course materials. Key points include: - Data structures store and organize data for efficient use by applications - Common structures include linear ones like arrays and linked lists, and non-linear ones like trees and graphs - Operations like traversing, searching, inserting and deleting are important for data structures

Uploaded by

syed asim najam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Lecture No.

01
Data Structures
M. Affan Aleem

Data Structures
Prepares the students for (and is a
prerequisite for) the more advanced
material students will encounter in later
courses.
Cover well-known data structures such as
arrays, linked lists, stacks, queues, tree
and graphs.
Implement data structures in C++/c#

Grading

Hourly 02 20%
Quizzes06 10%
Assignments 03 5%
Lab Exam 01 5%
Projects01 20%
Final Exam 01 40%

All Lectures Material Available at


//paf3//affanalim//datastructure//

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.

AL

Data Structures contd


Different kinds of data structures are
suited to different kinds of applications,
and some are highly specialized to specific
tasks.
For example, B-trees are particularly wellsuited for implementation of databases,
while compiler implementations usually
use hash tables to look up identifiers.
AL

Data Structure contd


Data structures provide a means to
manage huge amounts of data efficiently,
such as large databases
Data structures are generally based on
the ability of a computer to fetch and
store data at any place in its memory,
specified by an address.

AL

Types of Data Structures


Data structure are classified following two
categories:
i) Linear
ii) Non-Linear

AL

Linear Data Structures


Linear data structure is linear if elements
are adjacent to each other. It has exactly
two neighbors element to which it is
connected as its previous and next
member.

AL

Linear Data Structures contd


Example
i.
ii.
iii.
iv.

AL

Array
Linked List
Stack
Queue

Non-Linear Data Structures


Non-Linear data structure is that if one
element can be connected to more than
two adjacent elements then it is known as
non-linear data structure.

AL

10

Non-Linear Data Structures


contd

Example

i. Tree
ii. Graph

AL

11

Data Structure Operations


The following operations play a major role
in this text:
i.
ii.
iii.
iv.
v.
vi.
AL

Traversing
Searching
Inserting
Deleting
Sorting
Merging
12

Organizing Data
Any organization for a collection of records
that can be searched, processed in any
order, or modified.
The choice of data structure and algorithm
can make the difference between a
program running in a few seconds or many
days.

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.

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.

Goals of this Course


1. Reinforce the concept that costs and benefits
exist for every data structure.
2. Learn the commonly used data structures.

These form a programmer's basic data structure


toolkit.

3. Understand how to measure the cost of a data


structure or program.

These techniques also allow you to judge the merits


of new data structures that you or others might
invent.

Arrays
A linear array is a list of a finite number n
of homogenous data elements (i.e. data
elements of the same type) such that:
a) The elements of the array are

referenced respectively by an index set


consisting of n consecutive numbers.

AL

17

Arrays contd
b) The elements of the array are stored
respectively in successive memory
locations.
The number n of elements is called the
length or size of the array.
Length = UB LB + 1

AL

18

Example
Let DATA be a 4-element linear array of
integers such that
DATA[1] = 247,
DATA[3] = 249,

DATA[2] = 56,
DATA[4] = 87

Sometimes denoted such an array by


simply writing
1
AL

DATA

247

2
56

249

87
19

Arrays contd

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;
}

Array Layout
Array cells are
contiguous in
computer memory
The memory can be
thought of as an
array

x[0]
x[1]
x[2]
x[3]
x[4]
x[5]

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.

Dynamic Arrays contd


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

23

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.

Representation Of Linear Arrays In


Memory
Let LA be a linear array in memory. The
memory of the computer is simply a sequence
of addressed locations as:

LOC(LA[K]) = address of the element


LA[K] of the array LA

AL

25

Representation Of Linear Arrays In


Memory contd
The elements of LA stored in successive
memory cells. Accordingly, the computer
does not need to keep track of the address
of every elements of LA, but needs to keep
track only of the address of the first
element of LA, denoted by
Base(LA)
AL

26

Representation Of Linear Arrays In


Memory contd
The computer calculates the address of any
elements of LA by the following formula:

LOC(LA[K]) = Base(LA) + w(K lower


bound)
Where w is the number of words per
memory cell for the array LA.
AL

27

Example
Consider the array AUTO an ARRAY, which
records the number of automobiles, sold each year
from 1932 through 1984. Base(AUTO) = 200, and
w = 4 words per memory cell for AUTO,
Then LOC(AUTO[1932]) = 200,
LOC(AUTO[1933]) = 204 The address of the array
element for the year K = 1965 can be obtained

AL

28

Example contd
LOC(AUTO[1965]) = Base(AUTO) +
w(1965 lower bound)
LOC(AUTO[1965]) = 200 + 4(1965
1932)
LOC(AUTO[1965]) = 332

AL

29

Example
Consider an integer ARRAY A, which records
the integer numbers. The index of the array
from 1 to 62 and base(A) = 150. Find the
memory location of LOC(A[38]) ?

AL

30

You might also like