Data structures implementation in C

              C is a classical Programming Language which deals with Structured programming. Data structures are the foundation concepts in Programming.

When the classical Programming meets the foundations of data, here is the road map to follow to learn about the concepts.

The concepts are classified into 5 types of data structures.

·       Array and Strings

·       Stacks and Queues

·       Linked List

·       Graphs

·       Trees

Let us implement this one by one consequently.

Array Traversal:

              Array is a collection of Similar elements grouped under a common name.

Eg:

 ‘int a[5];’ Here ‘a’ is array which is of integer type. It has 5 elements.

Let us create a array and traverse it using C language.

Eg: C Program to traverse an Array

 #include <stdio.h>

int main() {

    // Array is declared here

    int a[10]; 

    int i,n;

    printf("Enter the 'n' value");

    scanf("%d",&n);

    printf("Enter the values");

    for(i=0;i < n; i++)

    {

        scanf("%d",&a[i]);

    }

    //Traversal

    printf("The Array elements are:\n");

    for(i = 0; i < n; i++) {           

        printf("%d ", a[i]);         

        }

    return 0;

}

Output:

Enter the 'n' value6

Enter the values

12

23

34

45

56

67

The Array elements are:

12 23 34 45 56 67

Yes. The traversal is done.

Try this program: To reverse the array elements.

C Program to reverse the array elements:

#include <stdio.h>

int main() {

    // Array is declared here

    int a[10]; 

    int i,n;

    printf("Enter the 'n' value");

    scanf("%d",&n);

    printf("Enter the values");

    for(i=0;i < n; i++)

    {

        scanf("%d",&a[i]);

    }

    //Traversal to print the reverse order

    printf("The Reversed Array elements are:\n");

    for(i = n-1; i >= 0; i--) {           

        printf("%d ", a[i]);         

        }

    return 0;

}

Output:

Enter the 'n' value4

Enter the values

23

34

67

54

80

The Reversed Array elements are:

80 54 67 34 23

Hope ,these  ‘C’ programming samples are useful to you. Keep Coding!!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

Symmetric Encryption in java

Java NIO examples to illustrate channels and buffers.