You are on page 1of 21

Algorithms and Data

Structures
Principles of Data Structures using C and C++ by Vinu V Das
Course Contents
Trees

Graphs Linked
Introduction
to Data Lists
Structures

Introduction to
Algorithms Stacks
Arrays
Sorting and
Memory Queues Searching
Management
For Your Information
For Lectures and other course materials
 www.facebook.com/groups/pafkiet.ads.sp15

No Description Issue Date Submission Date Details

1 Quizzes Surprised - Expect it after end of every Chapter

2 days after issued


2 Assignments Periodic data Popup during Lectures
Introduction to Data
Structures
Chapter 1 - Principles of Data Structures using C by Vinu V Das
Contents
Classification
of Data
Structures
What is
Data

Non Linear

The Big Picture


What is Data
Structure Linear
Lecture 1
What is Data. What is Data Structure. The Big Picture.
What is Data
 Data is a value or a set of values.
 There are two types of data such as numeric and alphanumeric data.
 It recalls the concept of data types from the programming course.
 Data type tells two things..
• Set of values that can be stored in particular type of variable.
• Set of operations that can be performed on particular type of variable.

Example : int x
• Only numeric whole numbers can be stored in x, i.e. 0,1,2,3,4,5,6,7,8,9 etc.
• Only numeric operation can be performed on x, i.e. addition, subtraction etc.

 Some times primitive data types are not enough to solve all the problems.
• Like, Complex number arithmetic
 Need to implement new data type from primitive ones.

Instructor: Aatif Shahbaz 7


What is Data Structure
 The pattern in which data is stored in memory.
 The mathematical or logical model of particular data organization is called data structure.
• Data structure is a way of organizing data items by considering its relationship to each other.
• Data structure mainly specifies the structured organization of data, by providing accessing methods
with correct degree of associativity.
• Data structure can be represented in both main memory(RAM) and auxiliary memory(HDD).
• Data structure = Organized data + Operations (concept of Class in OOP)

Why Data Structure?


 To store and organize data in such a way that it provides better way to access and modify data.
 No single data structure work well for all the purposes.
 Each data structure has its limitations and strengths, choice depends on two considerations.
• It must be rich enough in representing real world object.
• It should be simple enough and provide effective data processing.

Instructor: Aatif Shahbaz 8


What is Data Structure
Example :
 Consider numeric type
 Two possible data structures
• integer
• float

integer S Magnitude

float S Exponent Mantissa(magnitude)

Instructor: Aatif Shahbaz 9


The Big Picture

Data describes Set of


Structure Values

Data
Program Type

Set of
Algorithm
Operations

Instructor: Aatif Shahbaz 10


Lecture 2
Classification of Data Structure. Linear Data Structures. Non-Linear Data Structures.
Classification of Data Structure
 Data structures are broadly divided into two :

Primitive Data Structures :


 These are the basic data structures and are directly operated upon by the machine instructions, which
is in a primitive level.
 These includes integers, floating point numbers, characters, string constants, pointers etc.
 These primitive data structures are the basis for the discussion of more sophisticated (non-primitive)
data structures.

Non-primitive Data Structures :


 It is a more sophisticated data structure emphasizing on structuring of a group of primitive structures.
 It may be consists of homogeneous (same type) or heterogeneous (different type) data items.
 Array, list, files, linked list, trees and graphs fall in this category.

Instructor: Aatif Shahbaz 12


Classification of Data Structure
Data
Structure

Non-
Primitive DS
Primitive DS

Integer Float Character Boolean Non-Linear


Linear DS
DS

Arrays Linked Lists Stacks Queues Trees Graphs

Instructor: Aatif Shahbaz 13


Linear Data Structures
Arrays
 It is the simplest form of linear data structure. The linear relationship between the element is
represented by means of sequential memory locations.
 It is a finite ordered set of homogenous element. Elements are stored in successive memory locations.
 Elements of array are referred respectively by an index set, consisting of N consecutive numbers.
 Linear arrays are also called 1-dimensional (1D) arrays, because each element is referred by one
subscript only.

Array Subscript:
 A value or expression enclosed in brackets [ ] after the array name, specifying which array element to access.

Example : char x[8];


 The above declaration instructs the compiler to associate eight memory cells with the name x ; these
memory cells will be adjacent to each other in memory.
 The subscripted variable x[0] may be used to reference the 1st element of the array x, x[1] the next
element, and x[7] the last element.

Instructor: Aatif Shahbaz 15


Linked Lists
 A linked list is a linear collection of data elements called nodes
 Linear order is maintained by means of a pointer.
Pointer:
 A memory cell that stores the address of a data item.

 Each node is divided into two parts;


• First part contains the information(actual data) of the data element.
• Second part called the linked field that contains the address of the next node in the list.

Example :
struct student
{
string name;
int s_id;
student * next;
}

Instructor: Aatif Shahbaz 16


Stacks
 Stack is simply a linear list that follows LIFO system while accessing data.
LIFO:
 Last in first out, it means element that is stored(write) last will be the first one to be access(read) when needed.

 Insertion and deletion can takes place only at one end of the stack, called top of stack (TOS)

Example :
 Consider a stack of dishes as shown in the figure.
 Note that new dishes are entered at the top of stack.
 And can be removed only from the top of stack.

Applications:
 Function calls
 Evaluating post-fix expressions

Instructor: Aatif Shahbaz 17


Queues
 Queue is simply a linear list that follows FIFO system while accessing data.
FIFO:
 First in first out, it means element that is stored(write) first will be the first one to be access(read) when needed.

 Insertion can takes place only at one end of the queue, called rear(back) of the queue.
 Deletion can takes place only at one end of the queue, called front of the queue.

Example :
 Consider a queue of peoples as shown in the figure.
 Note that new person can enter the line from the back.
 A person is only allowed to leave the line from front.

Applications:
 Jobs submitted to printer is on FIFO basis.
 Process scheduling in OS

Instructor: Aatif Shahbaz 18


Non Linear Data Structures
Trees
 A non-linear data structure in which data frequently contain a hierarchical relationship between
various elements.
 Also known as rooted tree graph

Example :
 Consider following employee data

Instructor: Aatif Shahbaz 20


Graphs
 A non-linear data structure in which data some times contain relationship between the pairs of
elements.
 This relationship is not necessarily hierarchical in nature.

Example :
 Suppose air-line only flies between the cities connected by line.

Instructor: Aatif Shahbaz 21

You might also like