Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

s03 Introduction To Data Structure

This document provides an overview of data structures and their usage in computer science. It defines key concepts like abstract data types (ADTs), structs, arrays of structs, and different types of data structures. The session outlines common data structures like arrays, linked lists, stacks, queues, binary trees and hash tables. It also explains how to declare and define structs, nested structs and arrays of structs in C/C++.

Uploaded by

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

s03 Introduction To Data Structure

This document provides an overview of data structures and their usage in computer science. It defines key concepts like abstract data types (ADTs), structs, arrays of structs, and different types of data structures. The session outlines common data structures like arrays, linked lists, stacks, queues, binary trees and hash tables. It also explains how to declare and define structs, nested structs and arrays of structs in C/C++.

Uploaded by

Odit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Course : Data Structures

Effective Period : February 2023

Introduction to Data Structure


Session 3

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. Abstract Data Type


2. Struct
3. Nested Struct
4. Array of Struct
5. Data Structure
6. Types of Data Structure

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

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

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

6
Structure Declaration

structure name

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

don’t forget a semicolon here!

7
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

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

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

10
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;
};

11
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”);

12
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

13
Types of Data Structure

• Arrays
– A collection of similar data elements
– Data elements have the same data type

14
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

15
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

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

17
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

18
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 2 & 5
• Structures In C, http://www.asic-
world.com/scripting/structs_c.html

19

You might also like