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

Pointer and Array Review & Introduction To Data Structure

Session 1 Data Structures of Binus University Computer Science: Pointer and Array Review & Introduction to Data Structure

Uploaded by

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

Pointer and Array Review & Introduction To Data Structure

Session 1 Data Structures of Binus University Computer Science: Pointer and Array Review & Introduction to Data Structure

Uploaded by

bryan chandra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Course : Data Structures

Effective Period : February 2019

Pointer and Array Review &


Introduction to Data Structure (L)

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;

• 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

5
Pointer

• 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)

6
Pointer
• Pointer is a data type whose value refers to another
value stored elsewhere in computer memory using its
address.

• The two most important operators used with pointer type


are:
& the address operator
* the dereferencing operator

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;

• 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).
9
Pointer Variables
• 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;

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;

COMP6048 - Data Structure 12


Pointer to Pointer (cont.)
• Let us assume, the memory location of these
variables are as shown in figure.

Now if we write,
printf("\n %d", **ppx);
Then, it would print 10, the value of x

COMP6048 - Data Structure 13


Array

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];

• Accessing: • The first index of array ranged


• arr[0][2][2][9]= 2; from 0 to size1 – 1.
• arr[2][1][6][0]= 9; • The second index of array ranged
• arr[3][0][0][6]= 13; from 0 to size2 – 1.
• arr[2][1][3][8]= 10;

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.

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

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.

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

26
Structure Declaration
structure name

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

don’t forget a semicolon here!

27
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

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;

• You can use operator . (dot) to access member of 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

struct profile { student x;


int age;
char name[100]; x.score = 92;
}; x.grade = ‘A’;
x.p.age = 20;
struct student { strcpy(x.p.name,”budi”);
struct profile p;
int score;
char grade;
};

31
Array of Structure
• You also can have an array of structure.

struct profile { student arr[10];


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

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

You might also like