DS Notes Class: B.Tech.
CSE 4th Sem
Introduction to Data Structures
While developing a program of an algorithm, we need some collection of data having certain kind
of structure or organization. So, it can be said that:
Algorithm + Data structure = Program
Definition: Data Structure is representation of data items in primary memory for performing
storage & retrieval operations efficiently in terms of time and space.
A data structure is said to be linear if its elements form a sequence or a linear list. The linear data
structures like an array, stacks, queues and linked lists organize data in linear order.
A data structure is said to be non linear if its elements form a hierarchical pattern where, data items
appear at various levels. Trees and Graphs are widely used non-linear data structures, which
represents hierarchial relationship between individual data elements.
Data structures are divided into two types:
• Primitive data structures.
• Non-primitive data structures.
Primitive Data Structures are the basic data structures that directly operate upon the machine
instructions. They have different representations on different computers. Integers, floating point
numbers, character constants, string constants and pointers come under this category.
Non-primitive data structures are derived from primitive data structures and are more
complicated data structures. They emphasize on grouping same or different data items. Arrays, lists
and files come under this category. Following Figure shows the classification of data structures.
Provided By: Shipra Swati, PSCET, Bihar
DS Notes Class: B.Tech. CSE 4th Sem
As per the memory organization, data structure can be classified as:
• Contiguous
• Non-Contiguous
In contiguous structures, terms of data are kept together in memory (either RAM or in a file). An
array is an example of a contiguous structure.
In contrast, items in a non-contiguous structure are scattered in memory, but we linked to each
other in some way. A linked list is an example of a non-contiguous data structure.
Abstract Data Type (ADT): An abstract data type in a theoretical construct that deals with
memory organization of data structures as well as the way data will be accessed and processed i.e.
expected operations on the data.
For example, a stack is an abstract data type, which stores items in the form of list, Items in a stack
can only be added and removed in certain order – the last item added is the first item removed
(LIFO). These operations are called: push and pop.
Provided By: Shipra Swati, PSCET, Bihar
DS Notes Class: B.Tech. CSE 4th Sem
Unit-II
[Linear Data Structures and their Sequential Representation: Array, stack, queue, circular queue and
their operations and applications.]
Array
Students have already studied this concept in earlier semesters. So, They are expected to revise
following questions and answers.
1. Define array and types of array.
An array is collection of homogeneous elements stored in consecutive memory locations.
Array is declared as:
type arrayName [ arraySize ];
A specific element in an array is accessed by an index, which ranges from 0 to 'arraySize-1'.
Types of array:
➢ Single Dimensional Array: Single or One Dimensional array is used to represent
and store data in a linear form. It has only one subscript variable and it is also called
Linear Array.
Ex: int a[5] = {4, 7, 8, 9, 6};
➢ Multi Dimensional Array: Array having more than one subscript variable is called
Multi-Dimensional array. It is also called as Matrix.
Ex: int a[2][2] = {4, 5, 6, 8};
Provided By: Shipra Swati, PSCET, Bihar
DS Notes Class: B.Tech. CSE 4th Sem
2. What are the advantages and disadvantages of an Array ?
Following are main advantages of using an array :
1. Arrays are the most convenient way of storing the fixed amount of data that can
be accessed in an unpredictable fashion
2. Another advantage of array is that iterating through an array is more faster than
compared to other data structure types like linked list because of its good
locality of reference.
Following are the main disadvantages of using an Array :
1. The size of the array should be know up priori i.e. before the compile time.
2. C language does not have a checking mechanism for the array sizes.
3. How can we use pointers for accessing array elements?
Consider an array:
int arr[4];
In C programming, name of the array always points to address of the first element of an
array. In the above example, arr and &arr[0] points to the address of the first element.
➢ &arr[0] is equivalent to arr and arr[0] is equivalent to *arr (value of an address of
the pointer)
Similarly,
➢ &arr[1] is equivalent to (arr + 1) AND, arr[1] is equivalent to *(arr + 1).
➢ &arr[2] is equivalent to (arr + 2) AND, arr[2] is equivalent to *(arr + 2).
➢ &arr[3] is equivalent to (arr + 3) AND, arr[3] is equivalent to *(arr + 3)
..............
..............
➢ &arr[i] is equivalent to (arr + i) AND, arr[i] is equivalent to *(arr + i).
Provided By: Shipra Swati, PSCET, Bihar
DS Notes Class: B.Tech. CSE 4th Sem
4. How to pass array as function arguments in C?
The array is passed to a function using its name, which denotes its base address. But the
formal parameters for the corresponding array arguments can be one of the followings:
➢ Pointers
➢ Array (sized or unsized)
Example: Find average of the numbers entered in an array:
Using Pointers:
/* Program to find average of the numbers entered in an array */
#include <stdio.h>
/* function declaration */
float getAverage(int arr[], int size);
void main () {
int arr[50], size, i;
float avg;
/* ask user for size of array */
printf("\nEnter the size of array.. \t");
scanf("%d", &size);
/* ask user for elements of array */
printf("Enter the elements of array:\t");
for(i = 0; i < size; i++){
scanf("%d", &arr[i]);
}
/* display the array elements */
printf("Array elements are:\t\t[ ");
for(i = 0; i < size; i++){
printf("%d ", arr[i]);
}
printf("]\n");
/* pass pointer to the array as an argument */
avg = getAverage( arr, size ) ;
/* output the returned value */
printf( "\nAverage value is: %f \n", avg );
}
/* function definition */
float getAverage(int arr[], int size) {
int i;
float avg;
float sum = 0;
for (i = 0; i < size; i++) {
sum = sum + arr[i];
}
avg = sum / size;
return avg;
}
Provided By: Shipra Swati, PSCET, Bihar
DS Notes Class: B.Tech. CSE 4th Sem
Using array:
/* Program to find average of the numbers entered in an array */
#include <stdio.h>
/* function declaration */
float getAverage(int *arr, int size);
void main () {
int arr[50], size, i;
float avg;
/* ask user for size of array */
printf("\nEnter the size of array.. \t");
scanf("%d", &size);
/* ask user for elements of array */
printf("Enter the elements of array:\t");
for(i = 0; i < size; i++){
scanf("%d", &arr[i]);
}
/* display the array elements */
printf("Array elements are:\t\t[ ");
for(i = 0; i < size; i++){
printf("%d ", arr[i]);
}
printf("]\n");
/* pass pointer to the array as an argument */
avg = getAverage( arr, size ) ;
/* output the returned value */
printf( "\nAverage value is: %f \n", avg );
}
/* function definition */
float getAverage(int *arr, int size) {
int i;
float avg;
float sum = 0;
for (i = 0; i < size; i++) {
sum = sum + *(arr + i);
//sum = sum + arr[i];
}
avg = sum / size;
return avg;
}
OUTPUT
Provided By: Shipra Swati, PSCET, Bihar
DS Notes Class: B.Tech. CSE 4th Sem
5. Write a C program to search an element in the 2-D array using linear search.
/* Search user input key in 2D array */
#include <stdio.h>
/* Function Definition */
void searchKeyIn2D(int *p, int k, int r, int c){
int i, j, flag=0;
/* Display the 2D array */
printf("The 2D array: \n");
for(i=0; i<r; i++){
for(j=0; j<c; j++){
printf("%d \t", (*p+i*c+j));
}
printf("\n");
}
/* Searching the entered key */
for(i=0; i<r; i++){
for(j=0; j<c; j++){
if (k == (*p+i*c+j)){
printf("\nSearch Key = %d is found at row = %d and col =
%d \n", k, i+1, j+1);
flag = 1;
break;
}
}
}
if (flag == 0){
printf("\nSearch Key = %d is not found in the given 2D array\n",k);
}
}
void main(){
int arr[50][50], i, j, row, col, key, flag = 0;
printf("\nEnter the number of rows and columns for 2D array:\t");
scanf("%d %d", &row, &col);
printf("Enter the elements of the 2D array:\t");
/* Enter the 2D array Elements */
for(i=0; i<row; i++){
for(j=0; j<col; j++){
scanf("%d", &arr[i][j]);
}
}
/* Display the 2D array */
printf("The 2D array: \n");
for(i=0; i<row; i++){
for(j=0; j<col; j++){
printf("%d \t", arr[i][j]);
Provided By: Shipra Swati, PSCET, Bihar
DS Notes Class: B.Tech. CSE 4th Sem
//printf("%d \t", *(*(arr+i) + j));
//printf("%d \t", *(arr[i] + j));
//printf("%d \t", (*(arr + i))[j]);
}
printf("\n");
}
/* ask user to enter the search key */
printf("Enter the key you want to search: \t");
scanf("%d", &key);
/* Call the function to search elements linearly */
searchKeyIn2D(arr, key, row, col);
}
OUTPUT
Assignments on Array
1. Write a C program to delete an element from an array at specified position.
2. Write a C program to find sum of main diagonal elements of a matrix.
3. Write a C program to find second largest element in an array using function.
4. Write a C program to merge two character array to third array.
5. Write a C program to copy all elements from an array to another array.
Provided By: Shipra Swati, PSCET, Bihar