You are on page 1of 37

COMP6601 – Data Structures

Week 1 – Introduction to Data Structures


Textbooks
• Main Textbook
– Reema Thareja,. 2011.
Data Structures Using C. OXFORD. New Delhi.
ISBN:978-0-19-806544-9

• Additional Textbook
– Ellis Horowitz, Sartaj Sahni, Susan Anderson-Freed. 2007.
Fundamentals of Data Structures in C. COMSP. New
York. ISBN:978-0929306407
Course Description
• This course provides students with data structure basic
concept in which it will be frequently used in software
engineering and programming practices, concept of array,
structure, stack, queue, graph, and trees.

• By completing this course, students can do the


implementation of data structure concept in C programming
language and estimates its complexity in the usage.
Learning Outcomes
• LO1 : Explain the concept of data structure and its usage in
application
• LO2 : Demonstrate how to create any learned data structure
• LO3 : Analyze the usage of data structure in application
• LO4 : Design a proper data structure needed in application
Sub Topics
• Pointer and Array Review
• Types of Data Structures
• Abstract Data Type
• Structure Declaration
• Structure Assignments
• Nested Structure
• Array of Structure
Pointer Review
• Every variable in C has a name and a value associated with
it, and when a variable is declared, a specific block of
memory within the computer is allocated to hold the value
(the size depends of the data type).
• For example:
int x = 10;
• The size of integer may vary from one system to another.
• In 32 bit systems, an integer variable is allocated 4 bytes
while in 16 bit systems, it is allocated 2 bytes
Pointer Review

int x = 10;

• When the statement executes, the compiler sets aside 2


bytes of memory to hold the value 10 and it also sets up a
symbol table in which it adds the symbol x and the relative
address in the memory where those 2 bytes were set aside.
• Thus, every variable in C has a value and also a memory
location (address)
Pointer
• Pointer is a variable that contains the memory location of
another variable.

• Therefore, a pointer is a variable that represents the location of


a data item such as a variable or an array element.

• The two most important operators used with pointer type are:

& the address operator


* the dereferencing operator
Pointer
• Pointers are frequently used in C, as they have a number
of useful applications. Such as:
– Used to pass information back and forth between a function and
its reference point.
– Enable the programmers to return multiple data items from a
function via function arguments or to pass arrays and strings as
function arguments
– Provide an alternate way to access the individual elements of an
array
– Used to create complex data structures, such as trees, linked list,
linked stack, linked queue and graphs
– Used for the dynamic memory allocation of a variable
Pointer
• To declaring pointer variables can be given as below:

data_type *ptr_name;

• For example:
int *pnum;
char *pch;
Float *pfnum;

• In each of the above statements, a pointer variable is declared to point to a variable


of the specified data type.
• Although all these pointers point to different data types, they will occupy the same
amount of space in the memory (depends on the platform where the code is going
to run).
Pointer
• If we have the declaration: int x;
int *px;

• then x is an integer and px is a pointer to an integer.


• If we say:
px = &x;

• then &x returns the address of x and assigns it as the value of px.

• To assign a value of x we can say


x = 10; or *pi = 10;
Pointer
• What is the output of this program?

int a = 10;
int *p = &a;

printf( “%d\n”, *p );

a = 17;
*p = 20;

printf( “%d\n”, a );
Array
• A collection of similar data elements that have the same data type
(homogenous)
• The elements of the array are stored in consecutive memory locations and are
referenced by an index (subscript)
• In C, array index starts from zero
• Arrays are declared using the following syntax :

– data_type array_name [array_size];

 data type – what kind of values it can store


 array name – to identify the array
 array size – the maximum number of values that the array can hold
Array Declaration &
Accessing Array
• One Dimensional Array

• Declaration: Syntax:
int arr[5]; type name[size];

• Accessing: An array of size N have indexes


arr[0] = 7; from 0 to N-1.
arr[1] = 2;
arr[2] = 13;
arr[3] = 13;
arr[4] = 13;
Array Declaration &
Accessing Array
• Two Dimensional Array

Syntax:
• Declaration: type name[size1][size2];
int arr[3][6];
• The first index of array ranged from 0 to
size1 – 1.
• Accessing: • The second index of array ranged from 0
arr[0][2] = 2; to size2 – 1.
arr[2][1] = 9;
arr[1][5] = 13;
arr[2][4] = 10;
Array Declaration &
Accessing Array
• Multi Dimensional Array
Syntax:
type name[size1][size2][size3][...];
• Declaration:
• The first index of array ranged from 0 to
int arr[4][3][7][10]; size1 – 1.
• The second index of array ranged from 0 to
• Accessing: size2 – 1.
• The third index of array ranged from 0 to
arr[0][2][2][9]= 2; size3 – 1.
• and so on.
arr[2][1][6][0]= 9;
arr[3][0][0][6]= 13;
arr[2][1][3][8]= 10;
Storing Array Values

Initialize the elements

Store values in the array Input values for the elements

Assign values for the elements


Storing Array Values
• Initialization of Arrays
Example: int marks[5] = {90, 82, 78, 95, 88};

• Inputting Values
Example: int i, marks[10];
for (i=0; i<10; i++)
scanf(“%d”, &marks[i]);

• Assigning Values
Example: int i, arr1[10], arr2[10];
for(i=0; i<10; i++)
arr2[i] = arr1[i];
Calculating the Address
of Array Elements
• The array name is a symbolic reference for the address to the first byte
of the array.
• When we use the array name, we are actually referring to the first byte
of the array.
• Since an array stores all its data elements in consecutive memory
location, storing just the base address (the first element in the array) is
sufficient.
• The formula to perform this calculation is:
A[k] = BA (A) + w * k
 A is the array
 k is the index of the element
 BA is the base address of the array A
 w is the word size of one element in memory (example, size of int is 4)
Operations in Array
• There are a number of operations that can be performed on
arrays.
• They are:
 Traversal
 Insertion
 Searching
 Deletion
 Merging
 Sorting
Data Structure
• A data structure is an arrangement of data, either in the
computer’s memory or on the disk storage.
• Some common examples of data structures include:
 Arrays
 Linked lists
 Queues
 Stacks
 Binary trees
 Hash tables
Types of Data
Structure
• Arrays
 A collection of similar data elements
 Data elements have the same data type
Types of Data
Structure
• Linked Lists
 A very dynamic data structure in which the elements can be
added to or deleted from anywhere at will
 Each element is called a node

node
Types of Data
Structure
• Queue
 The element that was inserted first is the first one to be taken
out
 The elements in a queue are added at one end called the rear
and removed from the other end called the front
Types of Data
Structure
• Stacks
 Stacks can be represented as a linear array
 Every stack has a variable TOP associated with it
 LIFO (Last In First Out) / FILO (First In Last Out)
Types of Data
Structure
• Binary Trees
 A data structure which is defined as a collection of elements
called the nodes
 Every node contains a left pointer, a right pointer, and a data
element
Data Type
• Data Type is a collection of objects and a set of operations that
act on those objects.

• For example, the data type int consists of:


 objects : 0, +1, -1, +2, -2, etc
 operations : +, -, *, /, %, etc

• Example of predefined data types are int, char, float.


Abstract Data Type
• Abstract Data Type (ADT) is a data type that is organized in
such a way that the specification of the objects and the
specification of the operations on the objects is separated from
the representation of the objects and the implementation of the
operations.

• C/C++ has a concept called class and struct which assist the
programmer in implementing abstract data type.
Example of ADT
• Supposed we want to create an ADT of natural number which has
integer number as objects and several functions as operation.
• structure Number is
objects : an integer x
functions :
bool is_zero() if ( x == 0 ) return TRUE else return FALSE
bool equal(y) if ( x == y ) return TRUE else return FALSE
void set(y) x = y
void add(y) x = x + y
int get () return x
Structure
• Structure is basically a user-defined data type that can store
related information (even of different data types) together,
while an array can store only entities of same data types.

• It is a collection of variables under a single name.

• The variables within a structure are of different data types


and each has a name that is used to select it from the
structure.
Structure
Declaration
structure name

struct tdata {
int age;
char name[100]; structure member
float score;
};

don’t forget a semicolon here!


Structure
Declaration
struct tdata {
int age;
char name[100];
float score;
};

• The code above defines a structure named tdata which has three
members: age (int), name (char[]) and score (float).
• Creating a variable of structure is similar to create a variable of
primitive data type.
• tdata x; // a variable of tdata
• tdata arr[100];// an array of tdata
Structure
Declaration
• You also can define a structure as well as declare variables.

The code on the left is equal to:

struct tdata { struct tdata {


int age; int age;
char name[100];
char name[100];
float score;
float score; };
} a, b;
tdata a;
tdata b;
Structure
Assignments
tdata x;

• You can use operator . (dot) to access member of x

x.age = 17;
strcpy(x.name, “andi”);
x.score = 82.5;
Nested Structure
• You also can have a structure as a member of another structure

struct profile {
student x;
int age;
char name[100]; x.score = 92;
}; x.grade = ‘A’;
x.p.age = 20;
strcpy(x.p.name,”budi”);
struct student {
struct profile p;
int score;
char grade;
};
Array of Structure
• You also can have an array of structure.

struct profile { student arr[10];


int age;
arr[0].score = 92;
char name[100];
arr[0].grade = ‘A’;
}; arr[0].p.age = 20;
strcpy(arr[0].p.name,”budi”);
struct student {
struct profile p; arr[1].score = 83;
int score; arr[1].grade = ‘b’;
arr[1].p.age = 19;
char grade;
strcpy(arr[1].p.name,”chandra”);
};
Thank You

You might also like