Arrays allow to define type of variables that can holds several data items of the same kind . similarly structure is another user defined data type available in c that allows to combine data items of different kinds.
Structures are used to represent a record. Like student name , age address id etc.
A way of doing this is able to making a different variable for each attribute, however when you need to store the information of multiple students that time, you need to create these multiple variables again for each student this is such a big headache to store information in way.
We also solve this problem simply by using structure. we are able to create a structure that has members for name ,id , address and age and then we are able to create the variables of this structure for every student.
Structure is collection of variables of different data type represent by a single name.
Declaration of structure
To declare structure you must use the keyword struct . the struct keyword defines a new data type .
Syntax
Struct structure –name
{
Data –type member 1;
Data –type member 2;
…
Data type member n;
} [one or more structure variable] ;
The structure tag is optional and each type member is a normal variable definition, such as int i; or float f; or any other valid variable definition. at the end of the structure `s definition ,before the final semicolon ,you can specify one or more structure variable but it is optional . Here is the way you would declare the book structure
#include<stdio.h>
#include<string.h>
Struct books
{
Char title [50];
Char author [50];
Char subject [100];
int book-id;
} book;
Int main()
{
Struct books book 1;
Struct books book 2;
Strcpy(book 1. Title , “c programming ”);
Strcpy (book 1. Author ,”sneha patel ”);
Strcpy(book1. Subject ,”basic programming”);
Book 1.book-id=3456087;
Strcpy(Book 2.title ,“java script”);
Strcpy(Book 2.author,” k.s mane” );
Strcpy(Book 2.subject,”java script”);
Book 2. Book-id =2789467;
Printf(“Book title :%s\n”,Book 1.title);
Printf(“Book 1 author: %s\n”,Book 1.author);
Printf(“Book 1 subject:%s\n”,Book 1 .subject);
Printf(“Book 1 book_id :%d\n”,Book 1.book_id);
Printf(“Book 2title :%s\n”,Book 2.title);
Printf(“Book 2 author: %s\n”Book 2.author);
Printf(“Book 2 subject:%s\n”,Book 2 .subject);
Printf(“Book 2 book_id :%d\n”,Book 2.book_id);
Return 0;
}
Output
Book 1 title:C Programming
Book 1author: sneha patel
Book 1 subject : basic programming
Book 1book-id: 3456087
Book 2 title: java script
Book 2 author :k.s mane
Book2 book_id 2789467
Array of structure
Structure is collection of different data types <variables>which are grouped together where as array of structures is nothing but collection of structures this is also known as structure.
Syntax
Stuct struct –name
{
Data –type var 1;
Data –type var 2;
…
Data- type var n;
};
Struct struct –name obj [size];
Eg.
#include <stdio.h>
#inclde<conio.h>
Struct result
{
Char name[100];
int rollno;
float cpi;
};
Void main()
{
Struct result r[66];
int i;
printf(“enter detail of student: ”);
for (i=0; I < 66; i++)
{
printf (“\n enter name ,roll no ,cpi”);
Scanf(“%s”,r[i] name);
Scanf(“%d%f”,&r[i] roll no,&r[i] cpi);
}
Printf(“\n detail of student:\n”);
for(i=0; i<66; i++);
{
Printf(“%s ”,r[i] name);
Printf(“\t%d”,r[i] roll no);
Printf(“\t %f\n”,r[i] cpi);
}
getch( );
}
Nested structure
A structure that contain another structure as a member variable is known as nested structure or structure within a structure .structure which is part of other structure must be declared before the structure in which it is used.
Syntax
{
Struct structure 1
…..
};
Struct structure 2
{
…..
Struct structure 1obj;
};
Eg.
#include<stdio.h>
Struct address
{
Char house no[25];
Char city [25];
Char pincode[25];
};
Stuct employee
{
Int id ;
Char name[25];
Float salary;
Struct address add;
};
Int main()
{
Int I;
Struct employee e;
Printf(“\n \t enter employee id :”);
Scanf(“%d”,&E.id);
Printf(“\n \t enter employee name:”);
Scanf(“%f”,&E.name);
Printf(“\n \t enter employee salary:”);
Scanf(“%f,&E.salary:”);
Printf(“\n \t enter employee house no:”);
Scanf(“%s,&E.add house no:”);
Printf(“\n \t enter employee city:”);
Scanf(“%d”,&E.add.city);
Printf(“\n \t enter employee pincode:”);
Scanf(“%s”,&E.add pincode);
Printf(“\n details of employees”);
Printf(“\n \t employee id:%d”,E.id);
Printf(“\n \t employee name:%s”,E.name);
Printf(“\n \t employee salary :%f”,E.salary);
Printf(“\n \t employee house no :%s ”,E.add.house no );
Printf(“\n \t employee city: %s”,E.add.city);
Printf(“\n \t employee pincode:%s\n”,E.add.pincode);
}
Output
Enter employee id:23
Enter employee name: kajol
Enter employee salary: 80999
Enter employee house no:4
Enter employee city :shahmer
Enter employee pincode:42450
Detail of employees
Employee id:23
Employee name :kajol
Employee salary : 80999
Employee house no : 4
Employee city :shahmer
Employee pin code :42450
Referential structures:
Self referential structure are those structure that have one or more pointers which point to the same type of structure ,as their member in other words, structures pointing to the same type of structure are self-referential in nature.
Format
Struct structure name
{
Data type structure member -1;
Data type structure member -2;
….
….
Data type structure member -n;
……
…….
Struct structure name *next;
};
In the above structure each node is pointing to the next node , this structure is called a linear linked list .node having two members where “info ”char type and “link ”pointer type ,which is pointing to the next node of same type.
The important point to consider is that the pointer should be initialized properly before accessing, as by default it contains garbage value.
Type of self referential structures
1 self referential structure with single link
2 self referential structure with multiple links
Self referential structure with single link
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.
#include<stdio.h>
Struct node
{
Int data 1;
Char data 2;
Struct node*link;
};
Int main()
{
Struct node ob1;
Ob1.link=null;
Ob1.data1=10;
Ob1.data.2=20;
Struct node ob2;
Ob2.link=null;
Ob2.data1=30;
Ob2.data2=40;
Ob1.link=&ob2;
Printf(“%d”,ob1.link->data1);
Pintf(“\n%d”,ob1.link->data2);
Return 0;
}
Output
30
40
Self referential structure with multiple links
Self referential structure with multiple links can have than one self-pointers.many complicated data structures can be easily constructed using these structures .such structure can easily connected to more than one nodes at a time .the following example shows one such structure with more than one links.
The connection made in the above example can be understood using the following
Eg
Include <stdio.h>
Struct node
{
Int data;
Struct node *prev-link;
Struct node *next-link;
};
Int main()
{
Struct node ob1;
Ob1.prev-link =null;
Ob1.data=10;
Struct node ob2;
Ob2.prev-link=null;
Ob2.next-link=null;
Ob2.data=20;
Struct node ob3;
Ob3.prev-link =null;
Ob3.next-link=null;
Ob3.data=30;
Ob1.next-link =&ob2;
Ob2.next-link=&ob3;
Ob2.prev-link=&ob1;
Ob3.prev-link=&ob2;
Printf(“%d\t”,ob1.data);
Printf(“%d\t”, ob1.next-link->data);
Printf(“%d\n”,ob1.next-link->next-link->data);
Printf(“%d\t”,ob2.prev-link ->data);
Printf(“%d\t”, ob2.data);
Printf(“%d\n”,ob2.next-link->data);
Printf(“%d\t”,ob3.prev-link->prev-link ->data);
Printf(“%d\t”, ob3.prev-link ->data);
Printf(“%d\n”,ob3.data);
Return 0;
}
Output
10 20 30
10 20 30
10 20 30
In this above example we can see that ob1, ob2,ob3 are three objects of the self referential structure node and they are connected using their links in such a way that any of them can easily access each other`s data this is the beauty of the self referential structures the connections can be manipulated according to the requirements of the programmer.
Self referential structures are very useful in creation of other complex data structure like Linked lists
Linked list
A linked list is a linear collection of data elements called nodes each pointing to the next node by means of a punter. It is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference (In other words ,link) to the next node in the sequence . this structure allows for efficient insertion or removal of elements from any position in the sequence during iteration . more complex variants add additional links , allowing efficient insertion or removal from arbitrary element references
Students should be able to sketch diagrams illustrating: adding a data item to linked list, deleting specified data item , modifying the data held in the linked list, searching for a given data item.
Circular linked list
It is a linked list where all nodes are connected to form a circle. There is no null at the end . a circular liked list can be a singly circular linked list or doubly circular linked list.
Any node can be a starting point we can traverse the whole list by starting from any point we just need to stop when the first visited node is visited again
Doubly linked list
A doubly linked list contains an extra pointer typically called previous pointer , together with next pointer and data which are there in singly linked list
A doubly linked linked list can be traversed in both forward and backward direction . the delete operation in doubly linked list is more efficient if pointer to the node to be deleted is given ,we can quickly insert a new node before a give node.
In singly linked list to delete a node pointer to the previous node is needed . to get this previous node , sometimes the list is traversed in doubly linked list we can get the previous node using previous pointer.
copyright©2021
programming tech.caps
0 Comments