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

Data Structure & Algorithms: Prepared By: Kashafad-Dooja

This document provides information about a Data Structures and Algorithms course being taught at Lahore Garrison University. It includes the instructor contact details, course objectives, prerequisites, and contents to be covered throughout the semester. The document discusses the need for data structures to organize data efficiently and classify common data structures like arrays and lists. It also describes basic list operations like create, insert, remove, get, and more.

Uploaded by

ali raza
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Data Structure & Algorithms: Prepared By: Kashafad-Dooja

This document provides information about a Data Structures and Algorithms course being taught at Lahore Garrison University. It includes the instructor contact details, course objectives, prerequisites, and contents to be covered throughout the semester. The document discusses the need for data structures to organize data efficiently and classify common data structures like arrays and lists. It also describes basic list operations like create, insert, remove, get, and more.

Uploaded by

ali raza
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

1

Lahore Garrison University


CSC331- Data Structure & Algorithms
Week-1 Lecture -1& 2
Spring 2018

Prepared by:
kashafAd-Dooja
2
Instructor Contact Details

 Name: Kashaf Ad-Dooja


 Teaching Assistant: Ms. Kiran Amjad ( Lab # 51 )
 Office Location: CS – Faculty Room
 Email: [email protected]
 Students are strictly discouraged to contact the Instructor on any Social Media platform. In case of any
query please feel free to email or come to office in mentioned visiting hours.

Lahore Garrison University


3
Course Objectives

 Reinforce the concept that costs and benefits exist for every data structure.

 Learn the commonly used data structures.


These form a programmer's basic data structure “toolkit.”

 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.

Lahore Garrison University


4
Course Prerequisites

 The knowledge of C++ language is expected as strong background.

 Students should have taken the following courses in prior semesters.


 Programming Fundamentals
 Discrete Structures

Lahore Garrison University


5
Course Contents

 Following contents will be covered throughout the semester in week wise lectures:
Abstract data types, complexity analysis, Big Oh notation, Stacks (linked lists and array
implementations), Recursion and analyzing recursive algorithms, divide and conquer
algorithms, Sorting algorithms (selection, insertion, merge, quick, bubble, heap, shell, radix,
bucket), queue, dequeuer, priority queues (linked and array implementations of queues),
linked list & its various types, sorted linked list, searching an unsorted array, binary search for
sorted arrays, hashing and indexing, open addressing and chaining, trees and tree traversals,
binary search trees, heaps, M-way tress, balanced trees, graphs, breadth-first and depth-first
traversal, topological order, shortest path, adjacency matrix and adjacency list
implementations, memory management and garbage collection.

Lahore Garrison University


7
Need for Data Structures

 Data structures organize data  more efficient programs.

 More powerful computers  more complex applications.

 More complex applications demand more calculations.


8
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.
9
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.
10
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.
11
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?
12
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.
12
Classification of Data Structure

Lahore Garrison University


14
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;
}
15
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]
16
Array Layout

x[0]
Array cells are contiguous in
computer memory x[1]
The memory can be thought of as
an array x[2]

x[3]

x[4]

x[5]
17
What is Array Name?

 ‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.


 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;
18
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
19
The LIST Data Structure

 The List is among the most generic of data structures.

 Real life:

a. shopping list,
b. groceries list,
c. list of people to invite to dinner
d. List of presents to get
20
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
21
Lists

 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
22
List Operations

Useful operations

 createList(): 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.
23
List Operations

 We need to decide what is meant by “particular position”; we have used “?”


for this.

 There are two possibilities:

1. Use the actual index of element: insert after element 3, get element
number 6. This approach is taken by arrays
2. Use a “current” marker or pointer to refer to a particular position in the
list.
24
List Operations

 If we use the “current” marker, the following four methods would be useful:

 start(): moves to “current” pointer to the very first element.


 tail(): moves to “current” pointer to the very last element.
 next(): move the current position forward one element
 back(): move the current position backward one element
24
Implementing Lists

 We have designed the interface for the List; we now must consider how to
implement that interface.
 Implementing Lists using an array: for example, the list of integers (2, 6, 8,
7, 1) could be represented as:

current size
A 2 6 8 7 1
3 5
1 2 3 4 5

Lahore Garrison University


25
List Implementation

 add(9); current position is 3. The new list would thus be: (2, 6, 8, 9, 7, 1)
 We will need to shift everything to the right of 8 one place to the right to make place for the new
element ‘9’.

current size
step 1: A 2 6 8 7 1
3 5
1 2 3 4 5 6

current size
step 2: A 2 6 8 9 7 1
4 6
1 2 3 4 5 6

notice: current points


Lahore Garrison University to new element
26
Implementing Lists

 Next ():

current size
A 2 6 8 9 7 1
4 6
1 2 3 4 5 6 5

Lahore Garrison University


27
Implementing Lists

 There are special cases for positioning the current pointer:


a. past the last array cell
b. before the first cell

 We will have to worry about these when we write the actual code.

Lahore Garrison University


28
Implementing Lists

 remove(): removes the element at the current index

current size
Step 1: A 2 6 8 9 1
5 6
1 2 3 4 5 6 5

current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5

Lahore Garrison University


29
Implementing Lists

 remove(): removes the element at the current index

current size
Step 1: A 2 6 8 9 1
5 6
1 2 3 4 5 6 5

current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5

 We fill the blank spot left by the removal of 7 by


shifting the values to the right of position 5 over to the left one space.

Lahore Garrison University


30
Implementing Lists

find(X): traverse the array until X is located.

int find(int X)
{
int j;
for(j=1; j < size+1; j++ )
if( A[j] == X ) break;

if( j < size+1 ) { // found X


current = j; // current points to where X found
return 1; // 1 for true
}
return 0; // 0 (false) indicates not found
}
Lahore Garrison University
31
Implementing Lists

 Other operations:

get()  return A[current];


update(X)  A[current] = X;
length()  return size;
back()  current--;
start()  current = 1;
end()  current = size;

Lahore Garrison University


32
Analysis of Array Lists

 add
 we have to move every element to the right of current to make space for the
new element.
 Worst-case is when we insert at the beginning; we have to move every element
right one place.
 Average-case: on average we may have to move half of the elements

Lahore Garrison University


33
Analysis of Array Lists

 remove
 Worst-case: remove at the beginning, must shift all remaining elements to the left.
 Average-case: expect to move half of the elements.

 find
 Worst-case: may have to search the entire array
 Average-case: search at most half the array.

 Other operations are one-step.

Lahore Garrison University


34
Q &A

 Please feel free to ask any thing related to topic covered in these slides.

Lahore Garrison University


35
Reference Material

 Text Book:
Nell Dale : C++ Plus Data Structures, Latest Edition, Jones & Bartlett
 Reference Text Books:
Data Structure using C/C++ by Mark Alan.
Yadidyah Langsam, Moshe J. Augenstein, Aaron M. Tenenbaum: Data Structures
using C and C++.
 Handouts:
Dr. Sohail Aslam: Data Structures Handouts.

Lahore Garrison University

You might also like