0% found this document useful (0 votes)
9 views15 pages

DSA Unit-1 (2nd)

The document provides an overview of data structures, focusing on arrays, pointers, and strings. It covers definitions, types of arrays, operations performed on arrays, and the relationship between arrays and pointers. Additionally, it discusses string representation and common library functions for string manipulation.

Uploaded by

niktannu12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

DSA Unit-1 (2nd)

The document provides an overview of data structures, focusing on arrays, pointers, and strings. It covers definitions, types of arrays, operations performed on arrays, and the relationship between arrays and pointers. Additionally, it discusses string representation and common library functions for string manipulation.

Uploaded by

niktannu12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

DATA

STRUCTURES
UNIT - I
Arrays, Pointers and Strings
CO1: Apply appropriate constructs of Programming language, coding standards
for application development
UNIT I: CONTENT
Introduction to Data Structures:

 Algorithms and Flowcharts, Basics Analysis on Algorithm, Complexity of Algorithm,

Introduction and Definition of Data Structure, Classification of Data, Arrays, Various types of
Data Structure, Static and Dynamic Memory Allocation, Function, Recursion. [CO5]

Arrays, Pointers and Strings:

 Introduction to Arrays, Definition, One Dimensional Array and MultiDimensional Arrays,

Pointer, Pointer to Structure, various Programs for Array and Pointer. Strings. Introduction to
Strings, Definition, Library Functions of Strings. [CO1]
Arrays, Pointers and Strings: Introduction to Arrays, Definition
 An array is a collection of items of the same variable type that are stored at contiguous
memory locations.
 It’s one of the most popular and simple data structures and is often used to implement other
data structures.
 Store collection of primitive data type.
 Each item in an array is indexed starting with 0 .
 Each element in an array is accessed through its index.
 C arrays are static
Need of Array Data Structures
Need of Array Data Structures
Arrays are a fundamental data structure in computer science. They are used in a wide variety of
applications, including:
• Storing data for processing
• Implementing data structures such as stacks and queues
• Representing data in tables and matrices
• Creating dynamic data structures such as linked lists and trees
Arrays, Pointers and Strings: One Dimensional Array and MultiDimensional Arrays

• One-dimensional arrays: These arrays store a single row of elements.


• Multidimensional arrays: These arrays store multiple rows of elements.

Array Operations: Common operations performed on arrays include:


• Traversal : Visiting each element of an array in a specific order (e.g.,
sequential, reverse).
• Insertion : Adding a new element to an array at a specific index.
• Deletion : Removing an element from an array at a specific index.
• Searching : Finding the index of an element in an array.
Array declaration
 data_type array_name [size];
 The C arrays are static in nature, i.e., they are

allocated memory at the compile time.


 data_type array_name[] = {1,2,3,4,5}; /*without size*/
 // C Program to illustrate the array  #include <stdio.h>
declaration
 #include <stdio.h>  int main()
{
 int main()
 {
 // array declaration and initialization
 int arr[5] = { 10, 20, 30, 40, 50 };
 // declaring array of integers
 // modifying element at index 2
 int arr_int[5];
 // declaring array of characters  arr[2] = 100;
 char arr_char[5];  // traversing array using for loop
 printf("Elements in Array: ");
 return 0;
 }  for (int i = 0; i < 5; i++) {
 printf("%d ", arr[i]);
 }
 return 0;
}
Array
Update array element: array_name[i] = new_value;
C Array traversal: for (int i = 0; i < N; i++) {array_name[i];}
 #include <stdio.h>
 int main()
 {
 // 1d array declaration
 int arr[5];
 // 1d array initialization using for loop
 for (int i = 0; i < 5; i++) {
 arr[i] = i * i - 2 * i + 1;
 }

 printf("Elements of Array: ");


 // printing 1d array by traversing using for loop
 for (int i = 0; i < 5; i++) {
 printf("%d ", arr[i]);
 }

 return 0;
 }
Arrays, Pointers and Strings: Pointer, Pointer to Structure
 Arrays and Pointers are closely related to each other such that we can use pointers to
perform all the possible operations of the array.
 The array name is a constant pointer to the first element of the array and the array
decays to the pointers when passed to the function.
Arrays, Pointers and Strings: various Programs for Array and Pointer
 #include <stdio.h>
 int main()
 {
 int arr[5] = { 10, 20, 30, 40, 50 };
 int* ptr = &arr[0];
 // comparing address of first element and address stored inside array name
 printf("Address Stored in Array name: %p\nAddress of "
 "1st Array Element: %p\n",
 arr, &arr[0]);

 // printing array elements using pointers


 printf("Array elements using pointer: ");
 for (int i = 0; i < 5; i++) {
 printf("%d ", *ptr++);
 }
 return 0;
 }
Arrays, Pointers and Strings: Introduction to Strings, Definition
 Strings are considered a data type in general and are typically
represented as arrays of bytes (or words) that store a sequence of
characters.
 Strings are defined as an array of characters. The difference between a
character array and a string is the string is terminated with a special
character ‘\0’
 In data structures, a string is a sequence of characters used to represent
text.
 Strings are commonly used for storing and manipulating textual data in
computer programs.
 They can be manipulated using various operations like concatenation,
substring extraction, and comparison.
Arrays, Pointers and Strings: Library Functions of Strings
 Strings support a wide range of operations, including concatenation, substring extraction,
length calculation, and more. These operations allow developers to manipulate and process
string data efficiently.
 Below are fundamental operations commonly performed on strings in programming.

• Concatenation: Combining two strings to create a new string.


• Length: Determining the number of characters in a string.
• Access: Accessing individual characters in a string by index.
• Substring: Extracting a portion of a string.
• Comparison: Comparing two strings to check for equality or order.
• Search: Finding the position of a specific substring within a string.
• Modification: Changing or replacing characters within a string.

You might also like