- Uncategorized

Data Structures using C language

      Data structures is a set of algorithms, used for organizing  data so that it can be used efficiently.  There different data structure algorithms for different applications. Stacks, Queues, Linked lists, Trees etc., are important concepts of data structures. To understand Data structures, one should know about structures and pointers. So, lets glance concept of pointers and structures today.
    Pointer in a program is a variable that stores the address of another variable with same data type.
Let us understand with an example.

  1. #include<stdio.h>

  2. int main()

  3. {

  4. int i; // Declaration of variable

  5. int *p; // Declaration of pointer variable

  6. i=2; // Initialization of variable i

  7. printf("n Value stored in 'i' %d",i); // This printf statement prints 2

  8. p=&i; // Initialization of pointer

  9. printf("n Value stored in 'p' %d",p); // This printf prints address of variable i

  10. printf(n Value in 'address' stored in p %d",*p);//This printf prints value of i where p is pointing

  11. return 0;

  12. }


Note: Some compilers may give warning at line number 9, because addresses are stored in hexadecimal values in computer memory. 
You should remember that,
data type *variable name;  is syntax for declaring pointer variable.
pointer variable = &variable; is the syntax for  initializing pointer variable.
If ‘p’ is a pointer variable, ” p ” gives the value stored in pointer variable, whereas ” *p ” value stored in memory location where is pointer ‘p’ is pointing. You can do mathematical and logical operations using pointers (try out your own programs). It must be noted that, program execution time reduces when we use pointers in place of normal variables.

Structures in c is a derived data type, in simple words structure variable is a group of different data types stored in single variable name.(click here for textbook definition of structure).
Let us understand with an example
#include<stdio.h>
struct student // Structure variable declaration
{
int rollNumber;
char name[10];
float percentage;
};
int main()
{
struct student data; // variable declaration of 'struct student' type
printf("n Enter student roll number: ");
scanf("%d",&data.rollNumber); // Stores roll number entered, in rollNumber of data variable
printf("n Enter student name: ");
scanf("%s",data.name); // Stores name entered, in name[10] of data variable
printf("n Enter student percentage: ");
scanf("%f",&data.percentage);// Stores roll percentage entered, in percentage of data variable
printf("n Roll number of student: %dn Name of student: %sn Percentage of student: %fn",data.rollNumber,data.name,data.percentage);// prints data stored in respective variables
return 0;
}

You should remember that,

  • struct variable name
     { data type variable1;
      data type variable2;
      .
      .
      .
     }; is syntax for declaring a structure

  • struct variable structureVariableName;
    is syntax of declaring a structure variable.
  • structureVariableName.variable; is used to refer to variables of structure.
Structure variables are used in arithmetic and logical operations like normal variables. 
Structures can also have pointers, as shown below.
  1. #include<stdio.h>
  2. struct student  // Structure variable declaration
  3. {
  4.      int rollNumber;
  5.      char name[10];
  6.      float percentage;
  7. };
  8. int  main()
  9. {
  10.     struct student *data; // pointer declaration of ‘struct student’ type
  11.     printf(“n Enter student roll number: “); 
  12.     scanf(“%d”,&data->rollNumber); // Stores roll number entered, in rollNumber of data pointer
  13.     printf(“n Enter student name: “);
  14.     scanf(“%s”,data->name); // Stores name entered, in name[10] of data pointer
  15.     printf(“n Enter student percentage: “);
  16.     scanf(“%f”,&data->percentage);// Stores roll percentage entered, in percentage of data pointer
  17.     printf(“n Roll  number of student: %dn Name of student: %sn Percentage of student: %fn”,data->rollNumber,data->name,data->percentage);// prints data stored in respective variables
  18.    return 0
  19. }
Note here, that
  • struct variable name *structurepointer; is used for declaration of pointer to structure.
  • structrepointer->variable is used to refer variables of structure.

Hope you understood about pointers and structures, please ask your queries in comment section.  Next post will be of Stack in Data Structures.

Sujay K

2 thoughts on “Data Structures using C language

Leave a Reply