0% found this document useful (0 votes)
5 views10 pages

Array, Structure and Pointers in C - for DSA

The document provides an overview of arrays in data structures, detailing their definition, representation, and basic operations such as traversal, insertion, deletion, search, and update. It also discusses the use of pointers with structures in C, explaining how to define structures, declare structure pointers, and access structure members using both the dot and arrow operators. Examples of C code are included to illustrate these concepts in practice.

Uploaded by

rabinbruhh777
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views10 pages

Array, Structure and Pointers in C - for DSA

The document provides an overview of arrays in data structures, detailing their definition, representation, and basic operations such as traversal, insertion, deletion, search, and update. It also discusses the use of pointers with structures in C, explaining how to define structures, declare structure pointers, and access structure members using both the dot and arrow operators. Examples of C code are included to illustrate these concepts in practice.

Uploaded by

rabinbruhh777
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Data Structure - Array

Array is a container which can hold a fix number of items and these items should be of the same type. Most of
the data structures make use of arrays to implement their algorithms. Following are the important terms to
understand the concept of Array.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index, which is used to identify the
element.

Array Representation
Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.

Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.

As per the above illustration, following are the important points to be considered.
• Index starts with 0.
• Array length is 10 which mean it can store 10 elements.
• Each element can be accessed via its index. For example, we can fetch an element at index 6 as 27.

Basic Operations
Following are the basic operations supported by an array.
• Traverse − print all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or by the value.
• Update − Updates an element at the given index.

Traverse Operation
This operation is to traverse through the elements of an array.
Example
Following program traverses and prints the elements of an array:
#include <stdio.h>
#include<conio.h>
main() {
int LA[] = {1,3,5,7,8};
int n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8

Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can
be added at the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the end of the array −
Example
Following is the implementation of the above algorithm −
#include <stdio.h>
#include<conio.h>
main() {
int Arr[] = {11,33,55,77,22};
int item = 88, pos = 3, n = 5;
int i = 0, j = n;

printf("The original array elements are :\n");

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


printf("Arr[%d] = %d \n", i, Arr[i]);
}

n = n + 1;
while( j > pos) {
Arr[j] = Arr[j-1];
j = j - 1;
}

Arr[pos] = item;

printf("The array elements after insertion :\n");


for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, Arr[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 11
LA[1] = 33
LA[2] = 55
LA[3] = 77
LA[4] = 22

The array elements after insertion :


LA[0] = 11
LA[1] = 33
LA[2] = 55
LA[3] = 88
LA[4] = 77
LA[5] = 22

Program to Insert Array element at last Index:


#include<stdio.h>
#include<conio.h>
int main()
{
int arr[]={6,14,32,79,10};
int n=5,i;
for(i=0;i<n;i++)
{
printf("\nArray[%d]=%d",i,arr[i]);
}
printf("\nAfter new element inserted at end: ");
n=n+1;
arr[i]=15;
for(i=0;i<n;i++)
{
printf("\nArray[%d]=%d",i,arr[i]);
}
return 0;
}

Array: Insert Index at Beginning


#include<stdio.h>
#include<conio.h>
int main()
{
int arr[]={6,14,32,79,10};
int n=5,i,item;
for(i=0;i<n;i++)
{
printf("\nArray[%d]=%d",i,arr[i]);
}

n=n+1;
for(i=n;i>1;i--)
{
arr[i-1]=arr[i-2];
}
arr[0]=77;
printf("\nArray Element after insertion at First");
for(i=0;i<n;i++)
{
printf("\nArray[%d]=%d",i,arr[i]);
}
return 0;
}

Array: Insert Number at Middle/Any Index


#include<stdio.h>
#include<conio.h>
int main()
{
int arr[]={6,14,32,79,10};
int n=5,i,item,pos;
for(i=0;i<n;i++)
{
printf("\nArray[%d]=%d",i,arr[i]);
}

n=n+1;
printf("\nEnter the index position to enter element: ");
scanf("%d",&pos);
for(i=n;i>pos+1;i--)
{
arr[i-1]=arr[i-2];
}
arr[pos]=88;
printf("\nArray Element after insertion at First");
for(i=0;i<n;i++)
{
printf("\nArray[%d]=%d",i,arr[i]);
}
return 0;
}

Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of an array.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the
algorithm to delete an element available at the Kth position of LA.
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop

Example
Following is the implementation of the above algorithm −
#include <stdio.h>
#include<conio.h>

void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;

printf("The original array elements are :\n");

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


printf("LA[%d] = %d \n", i, LA[i]);
}

j = k;

while( j < n)
{
LA[j] = LA[j+1];
j = j + 1;
}

n = n -1;

printf("The array elements after deletion :\n");

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


printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8

The array elements after deletion :


LA[0] = 1
LA[1] = 3
LA[2] = 7
LA[3] = 8

Program to delete array element from given index position:


#include<stdio.h>
#include<conio.h>
int main()
{
int arr[]={6,14,32,79,10};
int n=5,i,item,pos;
for(i=0;i<n;i++)
{
printf("\nArray[%d]=%d",i,arr[i]);
}
printf("\nEnter the index position to delete element: ");
scanf("%d",&pos);
for(i=pos+1;i<n;i++)
{
arr[i-1]=arr[i];
}
n=n-1;
printf("\nArray Element after Deletion at given index: ");
for(i=0;i<n;i++)
{
printf("\nArray[%d]=%d",i,arr[i]);
}
return 0;
}

Search Operation
You can perform a search for an array element based on its value or its index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the
algorithm to find an element with a value of ITEM using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop

Example
Following is the implementation of the above algorithm −
#include <stdio.h>
#include<conio.h>
void main() {
int LA[] = {1,3,5,7,8};
int item = 7, n = 5;
int i = 0, j = 0;

printf("The original array elements are :\n");

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


printf("LA[%d] = %d \n", i, LA[i]);
}

while( j < n){


if( LA[j] == item ) {
break;
}

j = j + 1;
}

printf("Found element %d at position %d\n", item, j+1);


}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Found element 7 at position 4

Update Operation
Update operation refers to updating an existing element from the array at a given index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the
algorithm to update an element available at the Kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. Stop
Example
Following is the implementation of the above algorithm −
#include <stdio.h>
#include<conio.h>

void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;

printf("The original array elements are :\n");

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


printf("LA[%d] = %d \n", i, LA[i]);
}

LA[k-1] = item;

printf("The array elements after updation :\n");

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


printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after updation :
LA[0] = 1
LA[1] = 3
LA[2] = 10
LA[3] = 7
LA[4] = 8

Pointers with Structures in C


C allows programmers to create user-defined data types by grouping data of different types together using struct
keywords, such data types are called structures. Like any other data type in C, variables of user-defined
structure occupy addresses in a memory block, and pointers can be used to point them. A pointer pointing to a
structure is called structure pointer. Structures and pointers in C together help in accessing structure members
efficiently.
Structure pointer points to the address of the structure variable in the memory block to which it points. This
pointer can be used to access and change the value of structure members. This way, structures and pointers in C
can be used to create and access user-defined data types conveniently.

Before understanding how to use structures and pointers in C together, let us understand how structures are
defined and accessed using the variable name.

Syntax to Define a Structure


struct structure_name {
data_type member_variable_1;
data_type member_variable_2;
// ...
data_type member_variable_N;
};

C struct keyword is used to create a new data type, followed by the structure name. We define different
members of the structure inside parenthesis. Once a structure is defined, its name structure_name can be used to
declare variables as

struct structure_name structure_variable;

Declare a Structure Pointer


Structure pointer in C is declared using the keyword struct followed by structure name to which the pointer will
point to followed by pointer name. A structure pointer can only hold the address of a variable of the same
structure type used in its declaration.

struct structure_name *structure_pointer;

This way structures and pointers in C are used together to create a pointer pointing to the structure.

Initialization of Structure Pointer


After a structure pointer is declared, we need to initialize it to a variable before using it. To initialize a variable,
we need to provide the address of the structure variable using the & operator.

structure_pointer = &structure_variable;

Also, the structure pointer can be initialized during the time of declaration.

struct structure_type *structure_pointer = &structure_variable;

Accessing Structure Member Using Pointer


There are two ways to access the values of structure members using pointers -

1. Using asterisk (*) and dot (.) operator with the structure pointer.
2. Using membership or arrow (->) operator.

Accessing structure members using the arrow operator


Another way to access structure members in C is using the (->) operator. Using this way, we don't need an
asterisk and dot operator with the pointer. To access members of the structure using (->) operator we write
pointer name with -> followed by the name of the member that is
pointer_name->member_name

#include<stdio.h>

struct Student {
char name[30];
int age;
int roll_number;
};

int main() {
struct Student student_1;
struct Student *sp = &student_1;

printf ("Enter the details of the Student (student_1)\n");


printf ("\tName: ");
scanf ("%s", sp->name);
printf ("\tAge: ");
scanf ("%d", &sp->age);
printf ("\tRoll Number: ");
scanf ("%d", &sp->roll_number);

printf ("\n Display the details of the student_1 using Structure Pointer\n");
printf ("\tName: %s\n", sp->name);
printf ("\tAge: %d\n", sp->age);
printf ("\tRoll Number: %d", sp->roll_number);

return 0;
}

You might also like