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

Introduction to Data Structures Basics

Dynamic Array ppt

Uploaded by

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

Introduction to Data Structures Basics

Dynamic Array ppt

Uploaded by

kinshahra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Data Structures and Algorithms

Video Lecture

Lecture No. 1

Introduction
Engr. Rashid Farid Chishti
[Link]
[Link]
hti International Islamic University H-10, Islamabad, Pakistan
Introduction to Data Structures
 A data structure is a way of storing data in a computer so that it can be used
efficiently and it will allow the most efficient algorithm to be used.
 The choice of the data structure begins from the choice of an abstract data
type (ADT).
 A well-designed data structure allows a variety of critical operations to be
performed, using as few resources, both execution time and memory space, as
possible.
 Data structure introduction refers to a scheme for organizing data, or in other
words it is an arrangement of data in computer's memory in such a way that it
could make the data quickly available to the processor for required
calculations.
Introduction to Data Structures
 A data structure is a way of storing data in a computer so that it can be used
efficiently and it will allow the most efficient algorithm to be used.
 The choice of the data structure begins from the choice of an abstract data
type (ADT).
 A well-designed data structure allows a variety of critical operations to be
performed, using as few resources, both execution time and memory space, as
possible.
 Data structure introduction refers to a scheme for organizing data, or in other
words it is an arrangement of data in computer's memory in such a way that it
could make the data quickly available to the processor for required
calculations.
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
 dynamic arrays,
 linked lists,
 stacks,
 queues,
 Tree
 graphs.
 Searching
 Sorting
 Implement data structures in C++.
A Dynamic Array Example
Array vs Linked List
A Stack Example
A Queue Example
Trees
Graph
Grading Policy
 Midterm Exam 30 %
 Final Exam 50 %
 Assignments10 %
 Quizzes 10 %
Need for Data Structures
 Data structures help us to organize the data in the computer, resulting in more
efficient programs.
 An efficient program executes faster and helps minimize the usage of resources like
memory, disk.
 Computers are getting more powerful with the passage of time with the
increase in CPU speed. Therefore people have started solving more and more
complex problems. computer applications are becoming complex.
 More complex applications demand more calculations. This does not mean
that we should buy a new computer to make the application execute faster.
Our effort should be to ensue that the solution is achieved with the help of
programming, data structures and algorithm.
Organizing Data
 It means that the data should be arranged in a way that it is easily accessible.
 Suppose we have marks of students in computer and the programmer wants
to calculate the average, standard deviation etc.
 Or may be we have a list of names and want to search a particular name in it.
 To solve such problems, data structures and algorithm are used.
 Sometimes you may realize that the application is too slow and taking more
time. There are chances that it may be due to the data structure used, not due
to the CPU speed and memory.
 You may have two data structures and try to decide which one is more suitable
for the resolution of the problem.
Efficiency
 A solution is said to be efficient if it solves the problem within its resource
constraints.
 Space ( The size of RAM and hard disk needed)
 Time (The time CPU takes to complete the job)

 The cost of a solution is the amount of resources that the solution consumes.
 Suppose the data is too huge to be stored in disk. Here we have also the

problem of resources.
 There is always cost associated with these resources.
 We may need a faster and better CPU
 we may need to buy memory
 we will use the software i.e. data structures, algorithms and the recipes

through which the computer problems may be resolved with a faster


solution.
Selecting a Data Structure
Select a data structure as follows:
 Analyze the problem to determine the resource constraints a solution must

meet.
 Suppose, the data 1GB, while the disc space available is just 200 Mega bytes. This
problem can not be solved with programming. Rather, we will have to buy a new disk.
 Determine the basic operations that must be supported. Quantify the resource
constraints for each operation.
 Suppose you have to insert the data in the computer or database and have to search
some data item. Let’s take the example of telephone directory. Suppose there are eight
million names in the directory. Now someone asks you about the name of some
particular person. You want that this query should be answered as soon as possible. You
may add or delete some data. It will be advisable to consider all these operations when
you select some data structure.
Selecting a Data Structure
 Select the data structure that best meets these requirements.
 Without, sufficient experience, it will be difficult to determine which one is the best data
structure. We can get the help from internet, books or from someone whom you know
for already getting the problems solved. We may find a similar example and try to use it.
After this course, you will be familiar with the data structures and algorithms that are
used to solve the computer problems.
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?
 Let’s talk about the arrays and suppose you have an array of size 100. Data may be lying
in the first 50 locations of this array. Now you have to insert data in the start of this array.
What will you do? You have to move the existing data (fifty locations) to the right so that
we get space to insert new data.
 insert the data at 25th location ? first move the data from 26th to 50th locations;
 delete the data at 27th position ?
 We want to get the data, at first from 50th location and then from 13th. It means that
there is no order or sequence. We want to access the data randomly. Random access
means that we can’t say what will be the next position to get the data or insert the data.
Data Structure Philosophy
 Each data structure has costs and benefits.
 That can be computer resources or the time
 Rarely is one data structure better than another in all situations.
 you may think that the array is good enough for all the problems. Yet this is not
necessary. In different situations, different data structures will be suitable.
 If two different data structures are suitable for the problem. In such a case, you have to
choose the one that is more appropriate in that situation.
 A data structure requires:
 space for each data item it stores,
 time to perform each basic operation,
 programming effort.
Goals of this Course
 Reinforce the concept that costs and benefits exist for every data structure.
 Learn the commonly used data structures.
 In the previous course, you have learned how to form a loop, functions, use

of arrays, classes and how to write programs for different problems. In this
course, you will make use of data structures and have a feeling that there is
bag full of different data structures. In case of some problem, you will get a
data structure from the toolkit and use some suitable data structure.
 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.


 You will also see that some data structures are good for small data while the

others may suit to huge data. In this course we will do some mathematical
analysis and see which data structure is better one.
Arrays
 Elementary data structure that  Array declaration: int x[6];
exists as built-in in most  An array is collection of cells of the
programming languages. same type.
 The collection has the name ‘x’.
int main( int argc, char**  The cells are numbered with
argv ) consecutive integers.
{  To access a cell, use the array name
int x[6];
int j; and an index:
for(j=0; j < 6; j++) e.g. x[0], x[1], x[2], x[3], x[4], x[5]
x[j] = 2*j;
return 0;
}
Array Layout What is Array Name?
 Array cells are contiguous in  ‘x’ is an array name but there is no
computer memory variable x.
 The memory can be thought of
as an array  For example, if we have the code
x[0] int a, b;
x[1] then we can write
b = 2;
x[2]
a = b;
x[3] a = 5;
But we cannot write
x[4] 2 = a;
x[5]
Array Name
 ‘x’ is not a variable. It is an array name and it cannot be used on the left hand
side in an assignment statement.
 It can be assigned an address of a another variable.
 int x[6];
int n;

x[0] = 5;
x[1] = 2;

x = 3; // not allowed
x = a + b; // not allowed
x = &n; // not allowed
Example 1: Using Fixed Size Arrays
#include <iostream> for(int i=0 ; i<sz ; i++)
#include <stdlib.h> // for system(); cout <<" "<<my_data[i];
using namespace std; cout << endl;
int main(){ system("PAUSE");
const int sz = 5; return 0;
// int sz = 5; }
// cout<<"Enter the size of array:
";
// cin >> sz;
// double my_data[sz];
double my_data[sz];
cout <<"Please Enter "
<< sz <<" Numbers: ";

for(int i=0 ; i<sz ; i++)


cin >> my_data[i];

cout << "Here is your data: ";


1 23 2
Dynamic Array
 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:
 The new operator returns as address that is stored in y.

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


y[0] = 10;
y[1] = 15; // use is the same
Dynamic Array
 ‘y’ is a pointer that holds the starting address of 20 consecutive cells in
memory.
 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 Array
 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.
Example 2: Using Dynamic Array
#include <iostream> for(int i=0 ; i<sz ; i++)
#include <stdlib.h> // for system(); cout <<" "<<my_data[i];
using namespace std; cout << endl;
int main(){ system("PAUSE");
int sz; return 0;
cout << "Enter the size of an }
array: ";
cin >> sz;

double* my_data = new double[sz];

cout <<"Please Enter "<< sz


<<" Numbers: ";

for(int i=0 ; i<sz ; i++)


cin >> my_data[i];

cout << "Here is your data: ";


1 27 2
The LIST Data Structure
 The List is among the most generic of data structures.

 Real life:
 shopping list,
 groceries list,
 list of people to invite to dinner
 List of presents to get
Lists
 A list is collection of items that are all of the same type (grocery items,
integers, names)
 The items, or elements of the list, are stored in some particular order
It is possible to insert new elements into various positions in the list and
remove any element of the list

 List is a set of elements in a linear order.


For example, data values a1, a2, a3, a4 can be arranged in a list:
(a3, a1, a2, a4)
In this list, a3, is the first element, a1 is the second element, and so on
 The order is important here; this is not just a random collection of elements, it
is an ordered collection
List Operations
Useful operations
 create(): create a new list (presumably empty)
 copy(): set one list to be a copy of another
 clear(): clear a list (remove all elments)
 insert(X, ?): Insert element X at a particular position in the list
 remove(?): Remove element at some position in the list
 get(?): Get element at a given position
 update(X, ?): replace the element at a given position with X
 find(X): determine if the element X is in the list
 length(): return the length of the list.

You might also like