SlideShare a Scribd company logo
1 of 45
Debasis Samanta
Computer Science & Engineering
Indian Institute of Technology Kharagpur
Spring-2017
Programming and Data Structures
Lecture #09
Structures in C
Lecture #??: © DSamanta
CS 11001 : Programming and Data Structures 2
*Built-in data types
*User-defined data types
*Defining a Structure in C
*Operations with ADTs
*Some examples
*Complex numbers
*Binary Tree
*Linked List
*Other Examples
Today’s discussion…
CS 11001 : Programming and Data Structures 3 Lecture #??: © DSamanta
Built-in Data Type
CS 11001 : Programming and Data Structures 4 Lecture #??: © DSamanta
Built-in Data Type
•There are five basic data types associated with variables
int - integer: a whole number
float - floating point value: a number with a fractional part
double - a double-precision floating point value
char - a single character
void - valueless special purpose type
CS 11001 : Programming and Data Structures 5 Lecture #??: © DSamanta
Example: Built-in Data Type in C
CS 11001 : Programming and Data Structures 6 Lecture #??: © DSamanta
#include <stdio.h>
int main()
{
int a = 4000; // positive integer data type
float b = 5.2324; // float data type
char c = 'Z'; // char data type
long d = 41657; // long positive integer data type
long e = -21556; // long -ve integer data type
int f = -185; // -ve integer data type
short g = 130; // short +ve integer data type
short h = -130; // short -ve integer data type
double i = 4.1234567890; // double float data type
float j = -3.55; // float data type
}
Advantage of Built-in Data Type
•Simple
• Really simple! Only FIVE types of data!!
•Easy to handle
• Allocation of memory and operations are already defined
•Built-in support by programming language
• The C library are there to deal with them
CS 11001 : Programming and Data Structures 7 Lecture #??: © DSamanta
Disadvantage of Built-in Data Type
•There is a need for storing and handling variety of data types
• Image, text, video, etc.
•Limited range
•Waste of memory
•No flexibility
•Error prone programming
CS 11001 : Programming and Data Structures 8 Lecture #??: © DSamanta
User-Defined Data Type
CS 11001 : Programming and Data Structures 9 Lecture #??: © DSamanta
What is User Defined Data Type?
•User can define their own data type
• Also, called custom data type, abstract data type (ADT), etc.
• Examples
• Complex number: z = x + i y
• Matrices: 𝐴𝑚×𝑛
• Date: dd/mm/yy
... And many more
CS 11001 : Programming and Data Structures 10 Lecture #??: © DSamanta
Why Abstract?
•Because details of the implementation are hidden.
•When you do some operation on the list, say insert an element,
you just call a function.
•Details of how the list is implemented or how the insert function
is written is no longer required.
CS 11001 : Programming and Data Structures 11 Lecture #??: © DSamanta
Why User-Defined Data Types?
•It is always convenient for handling a group of logically related
data items.
•Examples:
•Student’s record
• name, roll number, and marks.
•Elements in a set
• Used in relational algebra, database, etc.
•A non non-trivial data structure becomes a trivial.
• Helps in organizing complex data in a more meaningful way.
CS 11001 : Programming and Data Structures 12 Lecture #??: © DSamanta
How User-Defined Data Types?
•All logically related data can be grouped into a form called
structure
•Each member into the group may be a built-in data type or
any other user defined data type
•No recursion, that is, a structure cannot include itself
•Example
• Date : {int dd, int mm, int yy}
CS 11001 : Programming and Data Structures 13 Lecture #??: © DSamanta
Structure Definition in C
CS 11001 : Programming and Data Structures 14 Lecture #??: © DSamanta
Defining a Structure
•The composition of a structure may be defined as
•struct is the required keyword
•tag is the name of the structure
•member 1,member 2,..are individual member declarations
CS 11001 : Programming and Data Structures 15 Lecture #??: © DSamanta
struct tag {
member 1;
member 2;
:
member m;
};
Defining a Structure
•Example
•Defining structure variables
A new data-type
CS 11001 : Programming and Data Structures 16 Lecture #??: © DSamanta
struct student {
char name[30];
int roll_number;
int total_marks;
char dob[10];
};
struct student s1, sList[100];
Point to be Noted
• The individual members can be ordinary variables, pointers, arrays, or other
structures.
• The member names within a particular structure must be distinct from one
another.
• A member name can be the same as the name of a variable defined outside
of the structure.
• Once a structure has been defined, the individual structure-type variables can
only be declared then.
• Each member in a structure can be accessed with (.) operator called scope
resolution operator
struct student s1, sList[100];
s1.name; sList[5].roll_number;
CS 11001 : Programming and Data Structures 17 Lecture #??: © DSamanta
Example: Complex Number Addition
CS 11001 : Programming and Data Structures 18 Lecture #??: © DSamanta
#include <stdio.h>
main()
{
struct complex
{
float real;
float complex;
} a, b, c;
scanf (“%f %f”, &a.real, &a.complex);
scanf (“%f %f”, &b.real, &b.complex);
c.real = a.real + b.real;
c.complex = a.complex + b.complex;
}
Scope
restricted
within
main()
Structure definition
and
Variable Declaration
Reading a member
variable
Accessing
members
Operations with ADTs
CS 11001 : Programming and Data Structures 19 Lecture #??: © DSamanta
ADT: Complex Number
CS 11001 : Programming and Data Structures 20 Lecture #??: © DSamanta
Complex
Number
add
print
multiplication
sub
read
division
Example: Complex Numbers
CS 11001 : Programming and Data Structures 21 Lecture #??: © DSamanta
struct typeX {
float re;
float im;
};
typedef struct typeX complex;
complex *add (complex a, complex b);
complex *sub (complex a, complex b);
complex *mul (complex a, complex b);
complex *div (complex a, complex b);
complex *read();
void print (complex a);
Function
prototypes
Structure
definition
ADT: Set
CS 11001 : Programming and Data Structures 22 Lecture #??: © DSamanta
Set
union
size
minus
intersection
delete
insert
Example: Set Manipulation
CS 11001 : Programming and Data Structures 23 Lecture #??: © DSamanta
struct nodeS {
int element;
struct nodeS *next;
};
typedef struct nodeS set;
set *union (set a, set b);
set *intersect (set a, set b);
set *minus (set a, set b);
void insert (set a, int x);
void delete (set a, int x);
int size (set a);
Function
prototypes
Structure
definition
Binary Tree
CS 11001 : Programming and Data Structures 24 Lecture #??: © DSamanta
Definition
•A data structure in which a record is linked to two successor
records, usually referred to as the left branch when greater and
the right when less than the previous record.
CS 11001 : Programming and Data Structures 25 Lecture #??: © DSamanta
Tree Terminology
• A binary tree is made of nodes, where each node contains a "left"
reference, a "right" reference, and a data element. The topmost node in
the tree is called the root
• Every node (excluding a root) in a tree is connected by a directed edge
from exactly one other node which is called a parent
• On the other hand, each node can be connected to arbitrary number of
nodes, called children
• Nodes with no children are called leaves or external nodes
• Nodes which are not leaves are called internal nodes
• Nodes with the same parent are called sibling
CS 11001 : Programming and Data Structures 26 Lecture #??: © DSamanta
More Tree Terminology
• The depth of a node is the number of edges from the root to the node.
• The height of a node is the number of edges from the node to the
deepest leaf.
• The height of a tree is a height of the root.
CS 11001 : Programming and Data Structures 27 Lecture #??: © DSamanta
Full & Complete Binary Tree
• A full binary tree is a binary tree in which each node has exactly zero
or two children.
• A complete binary tree is a binary tree, which is completely filled,
with the possible exception of the bottom level, which is filled from
left to right.
CS 11001 : Programming and Data Structures 28 Lecture #??: © DSamanta
Advantages of Trees
• Trees are so useful and frequently used, because they have some very
serious advantages:-
• Trees reflect structural relationships in the data
• Trees are used to represent hierarchies
• Trees provide an efficient insertion and searching
• Trees are very flexible data, allowing to move subtrees around with
minimum effort
CS 11001 : Programming and Data Structures 29 Lecture #??: © DSamanta
Declaring a Binary Tree in C
CS 11001 : Programming and Data Structures 30 Lecture #??: © DSamanta
struct node {
int value;
struct node * left;
struct node * right;
}
Traversals
• A traversal is a process that visits all the nodes in the tree. Since a tree
is a nonlinear data structure, there is no unique traversal. We will
consider several traversal algorithms with we group in the following
two kinds:-
• depth-first traversal
• breadth-first traversal
• There are three different types of depth-first traversals:-
• PreOrder traversal - visit the parent first and then left and right children;
• InOrder traversal - visit the left child, then the parent and the right child;
• PostOrder traversal - visit left child, then the right child and then the
parent;
CS 11001 : Programming and Data Structures 31 Lecture #??: © DSamanta
Example: Traversals
• As an example consider the following tree and its four traversals:
• PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3
• InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
• PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
• LevelOrder - 8, 5, 4, 9, 7, 11, 1, 12, 3, 2
CS 11001 : Programming and Data Structures 32 Lecture #??: © DSamanta
Linked List
CS 11001 : Programming and Data Structures 33 Lecture #??: © DSamanta
Definition
• A linked list is a sequence of data structures, which are connected
together via links.
• Linked list is a sequence of links which contains items.
• Each link contains a connection to another link.
• Important terms to understand the concept of linked list.
• Data − Each link of a linked list can store a data called an element
• Next − Each link of a linked list contains a link to the next link called Next
• List − A list contains the connection link to the first link called Head
CS 11001 : Programming and Data Structures 34 Lecture #??: © DSamanta
Linked List Representation
• Linked list can be visualized as a chain of nodes, where every node
points to the next node
• Important points to be considered:
• Linked list contains a link element called Head
• Each link carries a data field(s) and a link field called next
• Each link is linked with its next link using its next link
• Last link carries a link as null to mark the end of the list
CS 11001 : Programming and Data Structures 35 Lecture #??: © DSamanta
Defining A Linked List
CS 11001 : Programming and Data Structures 36 Lecture #??: © DSamanta
struct node {
int data;
struct node *next;
};
Data Next
Why Linked List?
• Arrays can be used to store linear data of similar types, but arrays have
following limitations.
• The size of the arrays is fixed
• Inserting a new element in an array of elements is expensive
• Advantages over arrays
• Dynamic size
• Ease of insertion/deletion
• Drawbacks of linked list
• Random access is not allowed. We cannot do binary search with linked lists.
• Extra memory space for a pointer is required with each element of the list.
CS 11001 : Programming and Data Structures 37 Lecture #??: © DSamanta
Other Examples
CS 11001 : Programming and Data Structures 38 Lecture #??: © DSamanta
Example: Comparing Dates
CS 11001 : Programming and Data Structures 39 Lecture #??: © DSamanta
#include <stdio.h>
struct date {
int dd, mm, yy;
} ;
int date_cmp(struct date d1, struct date d2);
void date_print(struct date d);
int main(){
struct date d1 = {7, 3, 2015};
struct date d2 = {24, 10, 2015};
int cmp = date_cmp(d1, d2);
date_print(d1);
if (cmp == 0)
printf(" is equal to");
else if (cmp > 0)
printf(" is greater, i.e., later than ");
else printf(" is smaller, i.e., earlier than");
date_print(d2);
return 0;
}
Comparing Dates
CS 11001 : Programming and Data Structures 40 Lecture #??: © DSamanta
/* compare given dates d1 and d2 */
int date_cmp(struct date d1, struct date d2) {
if (d1.dd == d2.dd && d1.mm == d2.mm && d1.yy == d2.yy)
return 0;
else if (d1.yy > d2.yy || d1.yy == d2.yy && d1.mm > d2.mm || d1.yy ==
d2.yy && d1.mm == d2.mm && d1.dd > d2.dd)
return 1;
else return -1;
}
/* print a given date */
void date_print(struct date d) {
printf("%d/%d/%d", d.dd, d.mm, d.yy);
}
OUTPUT
7/3/2015 is smaller, i.e., earlier than
24/10/2015
Example: Add Two Complex Numbers
CS 11001 : Programming and Data Structures 41 Lecture #??: © DSamanta
#include <stdio.h>
typedef struct complex
{
float real;
float imag;
} complex;
int main()
{
complex n1, n2, temp;
printf("For 1st complex number n");
printf(" Enter re & im part respectively:n");
scanf("%f %f", &n1.real, &n1.imag);
printf("nFor 2nd complex number n");
printf("Enter re & im part respectively:n");
scanf("%f %f", &n2.real, &n2.imag);
temp = add(n1, n2);
printf("Sum = %.1f + %.1fi", temp.real, temp.imag);
return 0;
}
Add Two Complex Numbers
CS 11001 : Programming and Data Structures 42 Lecture #??: © DSamanta
complex add(complex n1, complex n2)
{
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return(temp);
}
OUTPUT
For 1st complex number
Enter re & im part respectively: 2.3
4.5
For 2nd complex number
Enter re & im part respectively: 3.4
5
Sum = 5.7 + 9.5i
Any question?
You may post your question(s) at the “Discussion Forum”
maintained in the course Web page.
CS 11001 : Programming and Data Structures 43 Lecture #??: © DSamanta
Problems to ponder…
CS 11001 : Programming and Data Structures 44 Lecture #??: © DSamanta
Will be posted shortly…
Problems for practice…
CS 11001 : Programming and Data Structures 45 Lecture #??: © DSamanta
Will be posted shortly…

More Related Content

Similar to 09 Structures in C.pptx

Similar to 09 Structures in C.pptx (20)

Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptx
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
OODPunit1.pdf
OODPunit1.pdfOODPunit1.pdf
OODPunit1.pdf
 
Pf cs102 programming-10 [structs]
Pf cs102 programming-10 [structs]Pf cs102 programming-10 [structs]
Pf cs102 programming-10 [structs]
 
cs8251 unit 1 ppt
cs8251 unit 1 pptcs8251 unit 1 ppt
cs8251 unit 1 ppt
 
DDS Tutorial -- Part I
DDS Tutorial -- Part IDDS Tutorial -- Part I
DDS Tutorial -- Part I
 
Taxonomy of Differential Compression
Taxonomy of Differential CompressionTaxonomy of Differential Compression
Taxonomy of Differential Compression
 
c++ ppt.ppt
c++ ppt.pptc++ ppt.ppt
c++ ppt.ppt
 
OOP
OOPOOP
OOP
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
Unit 3 rdbms study_materials-converted
Unit 3  rdbms study_materials-convertedUnit 3  rdbms study_materials-converted
Unit 3 rdbms study_materials-converted
 
Unit01 dbms 2
Unit01 dbms 2Unit01 dbms 2
Unit01 dbms 2
 
1. Introduction to Data Structure.pptx
1. Introduction to Data Structure.pptx1. Introduction to Data Structure.pptx
1. Introduction to Data Structure.pptx
 
data types.pptx
data types.pptxdata types.pptx
data types.pptx
 
data-structures_unit-01.pdf
data-structures_unit-01.pdfdata-structures_unit-01.pdf
data-structures_unit-01.pdf
 
Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)
 

More from MouDhara1

datacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptxdatacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptxMouDhara1
 
06 Recursion in C.pptx
06 Recursion in C.pptx06 Recursion in C.pptx
06 Recursion in C.pptxMouDhara1
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptxMouDhara1
 
lecture-i-trees.ppt
lecture-i-trees.pptlecture-i-trees.ppt
lecture-i-trees.pptMouDhara1
 
JSP-Servlet.ppt
JSP-Servlet.pptJSP-Servlet.ppt
JSP-Servlet.pptMouDhara1
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptMouDhara1
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.pptMouDhara1
 
javascript12.pptx
javascript12.pptxjavascript12.pptx
javascript12.pptxMouDhara1
 
Linked list.pptx
Linked list.pptxLinked list.pptx
Linked list.pptxMouDhara1
 
For Client vs.pptx
For Client vs.pptxFor Client vs.pptx
For Client vs.pptxMouDhara1
 
Form tag.pptx
Form tag.pptxForm tag.pptx
Form tag.pptxMouDhara1
 
Web technology.pptx
Web technology.pptxWeb technology.pptx
Web technology.pptxMouDhara1
 
web tech.pptx
web tech.pptxweb tech.pptx
web tech.pptxMouDhara1
 
Introduction to Data Structure.pptx
Introduction to Data Structure.pptxIntroduction to Data Structure.pptx
Introduction to Data Structure.pptxMouDhara1
 

More from MouDhara1 (20)

ppt_dcn.pdf
ppt_dcn.pdfppt_dcn.pdf
ppt_dcn.pdf
 
datacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptxdatacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptx
 
06 Recursion in C.pptx
06 Recursion in C.pptx06 Recursion in C.pptx
06 Recursion in C.pptx
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
lecture-i-trees.ppt
lecture-i-trees.pptlecture-i-trees.ppt
lecture-i-trees.ppt
 
JSP-Servlet.ppt
JSP-Servlet.pptJSP-Servlet.ppt
JSP-Servlet.ppt
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.ppt
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
DTD1.pptx
DTD1.pptxDTD1.pptx
DTD1.pptx
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.ppt
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
javascript12.pptx
javascript12.pptxjavascript12.pptx
javascript12.pptx
 
Linked list.pptx
Linked list.pptxLinked list.pptx
Linked list.pptx
 
For Client vs.pptx
For Client vs.pptxFor Client vs.pptx
For Client vs.pptx
 
Form tag.pptx
Form tag.pptxForm tag.pptx
Form tag.pptx
 
Web technology.pptx
Web technology.pptxWeb technology.pptx
Web technology.pptx
 
cse.pptx
cse.pptxcse.pptx
cse.pptx
 
web tech.pptx
web tech.pptxweb tech.pptx
web tech.pptx
 
Introduction to Data Structure.pptx
Introduction to Data Structure.pptxIntroduction to Data Structure.pptx
Introduction to Data Structure.pptx
 

Recently uploaded

UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSrknatarajan
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Christo Ananth
 

Recently uploaded (20)

UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 

09 Structures in C.pptx

  • 1. Debasis Samanta Computer Science & Engineering Indian Institute of Technology Kharagpur Spring-2017 Programming and Data Structures
  • 2. Lecture #09 Structures in C Lecture #??: © DSamanta CS 11001 : Programming and Data Structures 2
  • 3. *Built-in data types *User-defined data types *Defining a Structure in C *Operations with ADTs *Some examples *Complex numbers *Binary Tree *Linked List *Other Examples Today’s discussion… CS 11001 : Programming and Data Structures 3 Lecture #??: © DSamanta
  • 4. Built-in Data Type CS 11001 : Programming and Data Structures 4 Lecture #??: © DSamanta
  • 5. Built-in Data Type •There are five basic data types associated with variables int - integer: a whole number float - floating point value: a number with a fractional part double - a double-precision floating point value char - a single character void - valueless special purpose type CS 11001 : Programming and Data Structures 5 Lecture #??: © DSamanta
  • 6. Example: Built-in Data Type in C CS 11001 : Programming and Data Structures 6 Lecture #??: © DSamanta #include <stdio.h> int main() { int a = 4000; // positive integer data type float b = 5.2324; // float data type char c = 'Z'; // char data type long d = 41657; // long positive integer data type long e = -21556; // long -ve integer data type int f = -185; // -ve integer data type short g = 130; // short +ve integer data type short h = -130; // short -ve integer data type double i = 4.1234567890; // double float data type float j = -3.55; // float data type }
  • 7. Advantage of Built-in Data Type •Simple • Really simple! Only FIVE types of data!! •Easy to handle • Allocation of memory and operations are already defined •Built-in support by programming language • The C library are there to deal with them CS 11001 : Programming and Data Structures 7 Lecture #??: © DSamanta
  • 8. Disadvantage of Built-in Data Type •There is a need for storing and handling variety of data types • Image, text, video, etc. •Limited range •Waste of memory •No flexibility •Error prone programming CS 11001 : Programming and Data Structures 8 Lecture #??: © DSamanta
  • 9. User-Defined Data Type CS 11001 : Programming and Data Structures 9 Lecture #??: © DSamanta
  • 10. What is User Defined Data Type? •User can define their own data type • Also, called custom data type, abstract data type (ADT), etc. • Examples • Complex number: z = x + i y • Matrices: 𝐴𝑚×𝑛 • Date: dd/mm/yy ... And many more CS 11001 : Programming and Data Structures 10 Lecture #??: © DSamanta
  • 11. Why Abstract? •Because details of the implementation are hidden. •When you do some operation on the list, say insert an element, you just call a function. •Details of how the list is implemented or how the insert function is written is no longer required. CS 11001 : Programming and Data Structures 11 Lecture #??: © DSamanta
  • 12. Why User-Defined Data Types? •It is always convenient for handling a group of logically related data items. •Examples: •Student’s record • name, roll number, and marks. •Elements in a set • Used in relational algebra, database, etc. •A non non-trivial data structure becomes a trivial. • Helps in organizing complex data in a more meaningful way. CS 11001 : Programming and Data Structures 12 Lecture #??: © DSamanta
  • 13. How User-Defined Data Types? •All logically related data can be grouped into a form called structure •Each member into the group may be a built-in data type or any other user defined data type •No recursion, that is, a structure cannot include itself •Example • Date : {int dd, int mm, int yy} CS 11001 : Programming and Data Structures 13 Lecture #??: © DSamanta
  • 14. Structure Definition in C CS 11001 : Programming and Data Structures 14 Lecture #??: © DSamanta
  • 15. Defining a Structure •The composition of a structure may be defined as •struct is the required keyword •tag is the name of the structure •member 1,member 2,..are individual member declarations CS 11001 : Programming and Data Structures 15 Lecture #??: © DSamanta struct tag { member 1; member 2; : member m; };
  • 16. Defining a Structure •Example •Defining structure variables A new data-type CS 11001 : Programming and Data Structures 16 Lecture #??: © DSamanta struct student { char name[30]; int roll_number; int total_marks; char dob[10]; }; struct student s1, sList[100];
  • 17. Point to be Noted • The individual members can be ordinary variables, pointers, arrays, or other structures. • The member names within a particular structure must be distinct from one another. • A member name can be the same as the name of a variable defined outside of the structure. • Once a structure has been defined, the individual structure-type variables can only be declared then. • Each member in a structure can be accessed with (.) operator called scope resolution operator struct student s1, sList[100]; s1.name; sList[5].roll_number; CS 11001 : Programming and Data Structures 17 Lecture #??: © DSamanta
  • 18. Example: Complex Number Addition CS 11001 : Programming and Data Structures 18 Lecture #??: © DSamanta #include <stdio.h> main() { struct complex { float real; float complex; } a, b, c; scanf (“%f %f”, &a.real, &a.complex); scanf (“%f %f”, &b.real, &b.complex); c.real = a.real + b.real; c.complex = a.complex + b.complex; } Scope restricted within main() Structure definition and Variable Declaration Reading a member variable Accessing members
  • 19. Operations with ADTs CS 11001 : Programming and Data Structures 19 Lecture #??: © DSamanta
  • 20. ADT: Complex Number CS 11001 : Programming and Data Structures 20 Lecture #??: © DSamanta Complex Number add print multiplication sub read division
  • 21. Example: Complex Numbers CS 11001 : Programming and Data Structures 21 Lecture #??: © DSamanta struct typeX { float re; float im; }; typedef struct typeX complex; complex *add (complex a, complex b); complex *sub (complex a, complex b); complex *mul (complex a, complex b); complex *div (complex a, complex b); complex *read(); void print (complex a); Function prototypes Structure definition
  • 22. ADT: Set CS 11001 : Programming and Data Structures 22 Lecture #??: © DSamanta Set union size minus intersection delete insert
  • 23. Example: Set Manipulation CS 11001 : Programming and Data Structures 23 Lecture #??: © DSamanta struct nodeS { int element; struct nodeS *next; }; typedef struct nodeS set; set *union (set a, set b); set *intersect (set a, set b); set *minus (set a, set b); void insert (set a, int x); void delete (set a, int x); int size (set a); Function prototypes Structure definition
  • 24. Binary Tree CS 11001 : Programming and Data Structures 24 Lecture #??: © DSamanta
  • 25. Definition •A data structure in which a record is linked to two successor records, usually referred to as the left branch when greater and the right when less than the previous record. CS 11001 : Programming and Data Structures 25 Lecture #??: © DSamanta
  • 26. Tree Terminology • A binary tree is made of nodes, where each node contains a "left" reference, a "right" reference, and a data element. The topmost node in the tree is called the root • Every node (excluding a root) in a tree is connected by a directed edge from exactly one other node which is called a parent • On the other hand, each node can be connected to arbitrary number of nodes, called children • Nodes with no children are called leaves or external nodes • Nodes which are not leaves are called internal nodes • Nodes with the same parent are called sibling CS 11001 : Programming and Data Structures 26 Lecture #??: © DSamanta
  • 27. More Tree Terminology • The depth of a node is the number of edges from the root to the node. • The height of a node is the number of edges from the node to the deepest leaf. • The height of a tree is a height of the root. CS 11001 : Programming and Data Structures 27 Lecture #??: © DSamanta
  • 28. Full & Complete Binary Tree • A full binary tree is a binary tree in which each node has exactly zero or two children. • A complete binary tree is a binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right. CS 11001 : Programming and Data Structures 28 Lecture #??: © DSamanta
  • 29. Advantages of Trees • Trees are so useful and frequently used, because they have some very serious advantages:- • Trees reflect structural relationships in the data • Trees are used to represent hierarchies • Trees provide an efficient insertion and searching • Trees are very flexible data, allowing to move subtrees around with minimum effort CS 11001 : Programming and Data Structures 29 Lecture #??: © DSamanta
  • 30. Declaring a Binary Tree in C CS 11001 : Programming and Data Structures 30 Lecture #??: © DSamanta struct node { int value; struct node * left; struct node * right; }
  • 31. Traversals • A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there is no unique traversal. We will consider several traversal algorithms with we group in the following two kinds:- • depth-first traversal • breadth-first traversal • There are three different types of depth-first traversals:- • PreOrder traversal - visit the parent first and then left and right children; • InOrder traversal - visit the left child, then the parent and the right child; • PostOrder traversal - visit left child, then the right child and then the parent; CS 11001 : Programming and Data Structures 31 Lecture #??: © DSamanta
  • 32. Example: Traversals • As an example consider the following tree and its four traversals: • PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3 • InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11 • PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8 • LevelOrder - 8, 5, 4, 9, 7, 11, 1, 12, 3, 2 CS 11001 : Programming and Data Structures 32 Lecture #??: © DSamanta
  • 33. Linked List CS 11001 : Programming and Data Structures 33 Lecture #??: © DSamanta
  • 34. Definition • A linked list is a sequence of data structures, which are connected together via links. • Linked list is a sequence of links which contains items. • Each link contains a connection to another link. • Important terms to understand the concept of linked list. • Data − Each link of a linked list can store a data called an element • Next − Each link of a linked list contains a link to the next link called Next • List − A list contains the connection link to the first link called Head CS 11001 : Programming and Data Structures 34 Lecture #??: © DSamanta
  • 35. Linked List Representation • Linked list can be visualized as a chain of nodes, where every node points to the next node • Important points to be considered: • Linked list contains a link element called Head • Each link carries a data field(s) and a link field called next • Each link is linked with its next link using its next link • Last link carries a link as null to mark the end of the list CS 11001 : Programming and Data Structures 35 Lecture #??: © DSamanta
  • 36. Defining A Linked List CS 11001 : Programming and Data Structures 36 Lecture #??: © DSamanta struct node { int data; struct node *next; }; Data Next
  • 37. Why Linked List? • Arrays can be used to store linear data of similar types, but arrays have following limitations. • The size of the arrays is fixed • Inserting a new element in an array of elements is expensive • Advantages over arrays • Dynamic size • Ease of insertion/deletion • Drawbacks of linked list • Random access is not allowed. We cannot do binary search with linked lists. • Extra memory space for a pointer is required with each element of the list. CS 11001 : Programming and Data Structures 37 Lecture #??: © DSamanta
  • 38. Other Examples CS 11001 : Programming and Data Structures 38 Lecture #??: © DSamanta
  • 39. Example: Comparing Dates CS 11001 : Programming and Data Structures 39 Lecture #??: © DSamanta #include <stdio.h> struct date { int dd, mm, yy; } ; int date_cmp(struct date d1, struct date d2); void date_print(struct date d); int main(){ struct date d1 = {7, 3, 2015}; struct date d2 = {24, 10, 2015}; int cmp = date_cmp(d1, d2); date_print(d1); if (cmp == 0) printf(" is equal to"); else if (cmp > 0) printf(" is greater, i.e., later than "); else printf(" is smaller, i.e., earlier than"); date_print(d2); return 0; }
  • 40. Comparing Dates CS 11001 : Programming and Data Structures 40 Lecture #??: © DSamanta /* compare given dates d1 and d2 */ int date_cmp(struct date d1, struct date d2) { if (d1.dd == d2.dd && d1.mm == d2.mm && d1.yy == d2.yy) return 0; else if (d1.yy > d2.yy || d1.yy == d2.yy && d1.mm > d2.mm || d1.yy == d2.yy && d1.mm == d2.mm && d1.dd > d2.dd) return 1; else return -1; } /* print a given date */ void date_print(struct date d) { printf("%d/%d/%d", d.dd, d.mm, d.yy); } OUTPUT 7/3/2015 is smaller, i.e., earlier than 24/10/2015
  • 41. Example: Add Two Complex Numbers CS 11001 : Programming and Data Structures 41 Lecture #??: © DSamanta #include <stdio.h> typedef struct complex { float real; float imag; } complex; int main() { complex n1, n2, temp; printf("For 1st complex number n"); printf(" Enter re & im part respectively:n"); scanf("%f %f", &n1.real, &n1.imag); printf("nFor 2nd complex number n"); printf("Enter re & im part respectively:n"); scanf("%f %f", &n2.real, &n2.imag); temp = add(n1, n2); printf("Sum = %.1f + %.1fi", temp.real, temp.imag); return 0; }
  • 42. Add Two Complex Numbers CS 11001 : Programming and Data Structures 42 Lecture #??: © DSamanta complex add(complex n1, complex n2) { complex temp; temp.real = n1.real + n2.real; temp.imag = n1.imag + n2.imag; return(temp); } OUTPUT For 1st complex number Enter re & im part respectively: 2.3 4.5 For 2nd complex number Enter re & im part respectively: 3.4 5 Sum = 5.7 + 9.5i
  • 43. Any question? You may post your question(s) at the “Discussion Forum” maintained in the course Web page. CS 11001 : Programming and Data Structures 43 Lecture #??: © DSamanta
  • 44. Problems to ponder… CS 11001 : Programming and Data Structures 44 Lecture #??: © DSamanta Will be posted shortly…
  • 45. Problems for practice… CS 11001 : Programming and Data Structures 45 Lecture #??: © DSamanta Will be posted shortly…