DS_Lecture_05.1_05.2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Structure

Course Code: CSC 2106 Course Title: Data Structure (Theory)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 2.2 Week No: 2 Semester:


Lecturer: Name & email
Lecture Outline

2. Structure
i. Definition
ii. Defining Structure in C++
iii. Declaring Variable of Structure
iv. Access Structure Member
v. Initializing Structure Variable
vi. Some Facts about Structure
vii. Nested Structure
viii. Self-referential Structure
3. Books
4. References
Structure
Definition

 The array takes simple data types like int, char or double and organizes them
into a linear array of elements all of the same type.

 Now, consider a record card which records name, age and salary. The name would
have to be stored as a string, the age could be int and salary could be float. As
this record is about one person, it would be best if they are all stored under one
variable.

 At the moment the only way we can work with this collection of data is as separate
variables. This isn't as convenient as a single data structure using a single name and
so the C language provides structure.

 A structure is an aggregate data type built using elements of other types.


Defining Structure in C++

 In general “structure” in C++ is defined as follows:


struct name{
Here struct is the key word, name is an identifier
list of component variables defining the structure name, list of component
}; variables declares as much different type of variables
as needed. The structure name works as the new data
type defined by the user. Definition ends with a semicolon.
 For example, suppose we need to store a name, age and salary as a single
structure. You would first define the new data type using:
struct EmployeeRecord{
char name[5]; So EmployeeRecord is the new user defined data type and a
variable b of type EmployeeRecord can hold total 22 bytes of
int age;
information [5 consecutive characters (5*2=10 bytes), followed by an
float salary; integer (4 bytes ), and a floating point number (8 bytes)].
};
Just like when we say int is a compiler defined data type and a
variable x of type int can hold 4 bytes of integer number.
Declaring Variable of a Structure

 As we can declare variables for compiler defined data types (example: int a, b, *c, d[50];), we can do the
same for user defined data type created using struct.
struct EmployeeRecord{ Variable a takes –
char name[5]; 5*sizeof(char)+1*sizeof(int)+1*sizeof(float)
int age; = 5*2+1*4+1*8 = 22 bytes in the memory.
float salary;
}a; 22 bytes will be distributed sequentially to the structure
members name, age, and salary.
EmployeeRecord b, *c, d[5];
c = &a

char name[5]
a int age
float salary
Main Memory
Declaring Variable of a Structure

 As we can declare variables for compiler defined data types (example: int a, b, *c, d[50];), we can do the
same for user defined data type created using struct.
struct EmployeeRecord{ Each index
contains
char name[5]; 22 bytes
int age;
float salary;
d[0] d[1] d[2] d[3] d[4]
}a;
EmployeeRecord b, *c, d[5]; Each 22 bytes contains the followings

name[5] age salary


Accessing Structure Member

 The dot (.) or combination of dash-line and greater-than sign (->) is used as operator to refer to
members of struct.
struct EmployeeRecord{  Here, variable x is of type EmployeeRecord.
char name[5];  x.age is of type int.
int age;  x.salary is of type float.
float salary;  x.name is of type char[5].
};  y[2].age is of type int where y[2] is of type
EmployeeRecord and represents the 3rd element.
EmployeeRecord x, y[5], *p;
 p->age is of type int where p is a pointer
x.age = 22; pointing to variable x of type EmplyeeRecord.
x.salary = 1234.56; Operator (->) is used for pointer variable of
struct instead of (.).
strcpy(x.name, "Sam");
 p->age can be represented as (*p).age also.
y[2].age = 22;
p = &x;
p->age = 22;

 Member variables can be used in any manner appropriate for their data type.
Initializing Structure Variable

 To initialize a struct variable, follow the struct variable name with an equal sign,
followed by a list of initializers enclosed in braces in sequential order of definition.

struct EmployeeRecord{
char name[5];
int age;
float salary;
};

EmployeeRecord x = {"Sam", 22, 1234.56};

 "Sam" is copied to the member name referred as x.xame.


 22 is copied to the member age referred as x.age.
 1234.56 is copied to the member salary referred as x.salary.

Mushfiqur Rahman
Some Facts About Structure
 No memory (as data) is allocated for defining struct.
 Memory is allocated when its instances/variables are created.
 Hence struct stand alone is a template… and it cannot be initialized. You need to
declare a variable of type struct.
 No arithmetic or logical operation is possible on the struct variables unless defined
by the operator overloading. Example:

EmployeeRecord a, b;
Any expression like a == b or a + b etc. is not possible.
But a.age = b.age is possible as both of these are of type int.

 Only assignment operation works. i.e. a = b;


 Call-by-value, call-by-reference, return-with-value, return-with-reference, array-as-
parameter – all works with a struct variable as same as the normal variable concept
using function in C++.
Nested Structure
 As any number and type of variables declared inside
a structure, another structure can also be
Example 1:
declared/defined inside another structure. struct Appointment{
 Example 1: struct AppDate{
 int day, month, year;
AppDate and AppTime is defined inside the }dt;
structure Appointment.
 dt is a variable for the structure AppDate. struct AppTime{
int minute, hour;
 tm is a variable for the structure AppTime. }tm;
 Both dt and tm is declared inside the structure
Appointment. char venue[100];
};

 Example 2:
Example 2: struct DateOfBirth{
 dob is a variable for the structure int day, month, year;
};
DateOfBirth.
 dob is declared inside structure Employee. struct Employee{
char EmpName[100];
DateOfBirth dob;
};
Self-referential Structure
 Structure member cannot be instance of enclosing struct

 Structure member can be pointer to instance of enclosing struct (self-


referential structure)
 Used for linked lists, queues, stacks and trees

 Example: Every person may have a child who is also a person.

struct Person{
char Name[30];
Person *Child;
};

 Here, Person contains a pointer variable Child of type Person.


Self-referential Structure

struct Person{  Mr. Arif has two children –


char Name[30]; Rahim and Sara.
 Mr. Rahim has one child –
Person *Child;
Karim.
};  Ms. Sara has no child.
Person P, *C;
strcpy(P.Name, "Arif"); P Person Person
C = P.Child = new Person[2]; Name Child Name Child
strcpy(C[0].Name, "Sara"); Arif Rahim
C[0].Child = NULL; C

strcpy(C[1].Name, "Rahim");
Person C Person
C = C[1].Child = new Person;
Name Child Name Child
strcpy(C->Name, "Karim");
Sara Ø Karim Ø
C->Child = NULL;
Books

 “Schaum's Outline of Data Structures with C++”. By John R. Hubbard


 “Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
 “Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
 “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
 “Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
 “C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
 “Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,
References

1. http://www.cplusplus.com/doc/tutorial/pointers/
2. http://www.cplusplus.com/doc/tutorial/structures/

You might also like