1.3.4 Structure
1.3.4 Structure
2
Mission of the Department
4
Program Educational Objectives
PEO1 Demonstrate analytical and design skills including the ability to generate creative solutions and foster
team-oriented professionalism through effective communication in their careers.
PEO2 Expertise in successful careers based on their understanding of formal and practical methods of
application development using the concept of computer programming languages and design principles in
accordance to industry 4.0
PEO3 Exhibit the growth of the nation and society by implementing and acquiring knowledge of upliftment of
health, safety and other societal issues.
PEO4 Implement their exhibiting critical thinking and problem- solving skills in professional practices or tackle
social, technical and business challenges.
5
Program Specific Outcomes
6
Topic To be Covered
• Structure (CO2)
• Self Referential Structure (CO2)
• Types of Self Referential Structure (CO2)
Structure
• A structure is a composite data type that defines a grouped list of variables that are to be
placed under one name in a block of memory. It allows different variables to be accessed by
using a single pointer to the structure.
• Syntax
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
Advantages
• To create a structure, use the struct keyword and declare each of its
members inside curly braces.
• After the declaration, specify the name of the structure variable
(myStructure in the example below):
struct { // Structure declaration
int myNum; // Member (int variable)
string myString; // Member (string variable)
} myStructure; // Structure variable
Access Structure Members
To access members of a structure, use the dot syntax (.):
// Declare a structure named "car" // Create another car structure and store it in myCar2;
struct car { car myCar2;
int main() { cout << myCar1.brand << " " << myCar1.model << " " <<
myCar1.year << "\n";
// Create a car structure and store it in myCar1; cout << myCar2.brand << " " << myCar2.model << " " <<
car myCar1; myCar2.year << "\n";
myCar1.brand = "BMW";
return 0;
myCar1.model = "X5";
}
myCar1.year = 1999;
Self Referential Structures
• Self Referential structures are those structures that have one or more
pointers which point to the same type of structure, as their member.
Example:
struct node {
int data1;
char data2;
struct node* link;
};
int main()
{
struct node ob;
return 0;
}
• In the above example ‘link’ is a pointer to a structure of type ‘node’. Hence, the structure ‘node’ is a self-referential
structure with ‘link’ as the referencing pointer.
An important point to consider is that the pointer should be initialized properly before accessing, as by default it
contains garbage value.
Types of Self Referential Structures
• These structures can have only one self-pointer as their member. The
following example will show us how to connect the objects of a self-
referential structure with the single link and access the corresponding
data members. The connection formed is shown in the following
figure.
Implementation
#include <stdio.h> // Initialization
struct node { ob2.link = NULL;
int data1; ob2.data1 = 30;
char data2; ob2.data2 = 40;
struct node* link;
};
// Linking ob1 and ob2
int main()
ob1.link = &ob2;
{
struct node ob1; // Node1
// Initialization // Accessing data members of ob2 using ob1
ob1.link = NULL; printf("%d", ob1.link->data1);
ob1.data1 = 10; printf("\n%d", ob1.link->data2);
ob1.data2 = 20; return 0;
struct node ob2; // Node2 }
Self Referential Structure with Multiple
Links:
• Self referential structures with multiple links can have more than one
self-pointers. Many complicated data structures can be easily
constructed using these structures. Such structures can easily connect
to more than one nodes at a time. The following example shows one
such structure with more than one links.
The connections made in the above example can be understood using
the following figure.
Implementation: struct node ob3; // Node3
// Initialization
#include <stdio.h>
ob3.prev_link = NULL;
struct node { ob3.next_link = NULL;
int data; ob3.data = 30;
ob2.next_link = &ob3;
};
int main()
// Backward links
{ ob2.prev_link = &ob1;
struct node ob1; // Node1 ob3.prev_link = &ob2; // Accessing data of ob1, ob2 and ob3 by ob1
printf("%d\n", ob1.next_link->next_link->data);
ob1.next_link = NULL;
// Accessing data of ob1, ob2 and ob3 by ob2
ob1.data = 10;
printf("%d\t", ob2.prev_link->data);
struct node ob2; // Node2 printf("%d\t", ob2.data);
// Initialization
printf("%d\n", ob2.next_link->data); // Accessing data of ob1, ob2 and ob3 by ob3
ob2.prev_link = NULL; printf("%d\t", ob3.prev_link->prev_link->data);
printf("%d", ob3.data);
ob2.data = 20;
return 0;
}
Book References
TEXT BOOKS
• Seymour Lipchitz, Schaum's Outlines Series Data Structures TMH. J.P. Hayes,
Computer Organization and Architecture, Third Edition, TMH.
• Data Structure Theory Problem and Algorithms, R.S. Salaria, Khanna Book
Publishing Company, Delhi.
REFERENCE BOOKS
• Introduction to Data Structures Applications, Trembley&Soreson, Second
Edition, Pearson Education Robert L. Britton, MIPS Assembly Language
Programming, Pearson Prentice Hall.
• A. Tanenbaum, Y. Lanhgsam and A. J. Augenstein, Data Structures Using C++,
Prentice Hall of India, 1990
Video Links
https://www.youtube.com/watch?v=XBFGsKQX21s
THANK YOU