Pointer and Array Review & Introduction To Data Structure
Pointer and Array Review & Introduction To Data Structure
Session 01
1
Learning Outcomes
At the end of this session, students will be able to:
LO 1: Explain the concept of data structures and its usage in
Computer Science
2
Outline
1. Pointer Review
2. Array
3. Storing Array Values
4. Accessing Array
5. Abstract Data Type
6. Struct
7. Nested Struct
8. Array of Struct
9. Data Structure
10. Types of Data Structure
3
Pointer
4
Pointer
• 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;
5
Pointer
6
Pointer
• Pointer is a data type whose value refers to another
value stored elsewhere in computer memory using its
address.
7
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
8
Pointer
• To declaring pointer variables can be given as below:
data_type *ptr_name;
• For example:
int *pnum;
char *pch;
float *pfnum;
• then &x returns the address of x and assigns it as the value of px.
10
Pointer Concept
11
Pointer to Pointer
• In C, you are also allowed to use pointers that point to
pointers. The pointers in turn point to data (or even to
other pointers). To declare pointers to pointers, just
add an asterisk * for each level of reference.
• For example:
int x=10;
int *px, **ppx;
px = &x;
ppx = &px;
Now if we write,
printf("\n %d", **ppx);
Then, it would print 10, the value of x
14
Linear List
• A linear list is an elementary abstract data type that allows
storage of elements of the same data type in the same
contiguous memory area.
• Most of programming languages support the implementation
of a linear list as an array.
15
Array
• A collection of similar data elements
• These data elements have the same data type
(homogenous)
• The elements of the array are stored in consecutive memory
locations and are referenced by an index
• Array index starts from zero
16
Array Declaration & Accessing Array
• One Dimensional Array
• Declaration:
• int arr[5];
• Accessing: Syntax:
• arr[0] = 7; type name[size];
• arr[1] = 2;
• arr[2] = 13; An array of size N have indexes
from 0 to N-1.
• arr[3] = 13;
• arr[4] = 13;
17
Array Declaration & Accessing Array
• Two Dimensional Array
• Declaration:
• int arr[3][6];
• Accessing: Syntax:
• arr[0][2] = 2; type name[size1][size2];
• arr[2][1] = 9;
• The first index of array ranged from
• arr[1][5] = 13;
0 to size1 – 1.
• arr[2][4] = 10; • The second index of array ranged
from 0 to size2 – 1.
18
Array Declaration & Accessing Array
• Multi Dimensional Array
• Declaration: Syntax:
• int arr[4][3][7][10]; type name[size1][size2];
19
Storing Array Values
20
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];
21
Operations in Array
• There are a number of operations that can be performed on
arrays. They are:
• Traversal
• Insertion
• Searching
• Deletion
• Merging
• Sorting
22
Introduction to Data Structure
23
Data Type
• Data Type is a collection of objects and a set of
operations that act on those objects.
24
Abstract Data Type
• Abstract Data Type (ADT) is an example of user-
defined data types.
• ADT is a collection of objects and a set of
operations that act on those objects.
• C/C++ has a concept called class and struct which
assist the programmer in implementing abstract
data type.
• The implementation view of an ADT is called a data
structure.
25
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.
26
Structure Declaration
structure name
struct tdata {
int age;
char name[100]; structure member
float score;
};
27
Structure Declaration
struct tdata {
int age;
char name[100];
float score;
};
28
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;
29
Structure Assignments
tdata x;
x.age = 17;
strcpy(x.name, “andi”);
x.score = 82.5;
30
Nested Structure
• You also can have a structure as a member of another structure
31
Array of Structure
• You also can have an array of structure.
32
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
33
Types of Data Structure
• Arrays
– A collection of similar data elements
– Data elements have the same data type
34
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
35
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
36
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)
37
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
38
References
• S. Sridhar. 2015. Design and Analysis of Algorithms. Oxford
University Press. New Delhi. ISBN: 9780198093695. Chapter 5
• Reema Thareja. 2014. Data structures using C. Oxford
University Press. New Delhi. ISBN:9780198099307. Chapter 1,
2, 3, & 5
• Arrays in C/C++ Programming,
http://www.mycplus.com/tutorials/data-structures/arrays-c-cp
p-programming
• Structures In C, http://www.asic-world.com/scripting/structs_c
.html
39