0% found this document useful (0 votes)
215 views38 pages

Introduction To Data Structure

This document provides an overview of data structures including arrays, pointers, structs, and nested structs. It discusses the main textbook for the course, describes the course as teaching basic data structure concepts and their implementation in C, and lists learning outcomes around explaining data structures and their usage. The outline covers introductions to arrays, pointers, structs, and nested structs.

Uploaded by

Sarah Safira
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
215 views38 pages

Introduction To Data Structure

This document provides an overview of data structures including arrays, pointers, structs, and nested structs. It discusses the main textbook for the course, describes the course as teaching basic data structure concepts and their implementation in C, and lists learning outcomes around explaining data structures and their usage. The outline covers introductions to arrays, pointers, structs, and nested structs.

Uploaded by

Sarah Safira
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 38

Data Structures

Week 1
Introduction to Data Structure
TextBooks
• Main Textbook
• Reema Thareja, 2014
Data Structures Using C. Oxfort. New Delhi.
ISBN: 978-0-19-809930-7
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
OUTLINE
1. Introduction
2. Array
3. Pointer and Array
4. Struct
5. Nested Struct
INTRODUCTION
Introduction
A good program is defined as a program that
• runs correctly
• is easy to read and understand
• is easy to debug and
• is easy to modify
A data structure is basically a group of data elements that are put
together under one name, and which defines a particular way of storing
and organizing data in a computer so that it can be used efficiently
Examples of data structures are arrays, linked lists, queues, stacks,
binary trees, and 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 Tree
 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
ARRAY
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 – 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:
arr[0] = 7;
arr[1] = 2;
arr[2] = 13;
arr[3] = 13;
arr[4] = 13;
Array Declaration & Accessing
Array
Two Dimensional Array

• Declaration: Syntax:
int arr[3][6]; type name[size1][size2];

• Accessing:
arr[0][2] = 2;
arr[2][1] = 9;
arr[1][5] = 13;
arr[2][4] = 10;
Array Declaration & Accessing
Array
Multi Dimensional Array

• Declaration: Syntax:
int arr[4][3][7][10]; type name[size1][size2][size3][...];

• Accessing:
arr[0][2][2][9]= 2;
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];
Operations in Array
There are a number of operations that can be performed on arrays.
They are:
 Insertion
 Deletion
 Searching
 Traversal
 Merging
 Sorting
POINTER
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
• 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 );
Struct
Struct
• 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.
Struct Declaration
Struct
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
Struct Declaration
• You also can define a structure as well as declare variables.
Struct 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 Struct
Nested Struct
• You also can have a structure as a member of another structure
Array of Struct
• You also can have an array of structure
REFERENCES
Thareja, R. (2014). Data Structures Using C (second). Oxford
University Press. https://doi.org/10.1136/adc.67.4.533

C – Arrays https://www.tutorialspoint.com/cprogramming/c_arrays.htm

Structures In C, http://asic-world.com/scripting/structs_c.html

You might also like