DSA 3 DAYS Part 2
1. Introduction to Arrays
An array is a data structure used to store a collection of elements, typically of the same data type, in contiguous memory locations. Arrays allow efficient indexing and manipulation of data, making them a fundamental building block in programming and computer science.
2. Definition
An array is a data structure that stores a fixed-size, ordered collection of elements of the same data type in contiguous memory locations. This arrangement allows efficient access and manipulation using indices.
3. One-Dimensional Array and Multi-Dimensional Array
a. One-Dimensional Array
A one-dimensional array is a linear data structure that stores elements of the same data type in a sequential manner. These elements are accessed using a single index. It is often referred to as a list.
Example:
int arr[5] = {1, 2, 3, 4, 5};
b. Multi-Dimensional Array
A multi-dimensional array is a data structure that stores data in a tabular or grid-like format across two or more dimensions. Each element is accessed using multiple indices.
Example (Two-dimensional array):
int matrix[3][3];
4. Pointer
A pointer is a variable that stores the memory address of another variable. It is a core feature in programming, particularly in languages like C and C++, which allows direct access and manipulation of memory locations.
Syntax:
int a = 10;
int *ptr = &a;
printf("%p", ptr);
5. Pointer to Structure
In C, a pointer to a structure is used to store the address of a structure variable. This allows indirect access to the structure’s members, and is especially useful for dynamic memory allocation and passing structures to functions efficiently.
Example Code:
#include <stdio.h>
struct Person {
char name[50];
int age;
};
int main() {
struct Person p1 = {"Alice", 30};
struct Person *ptr = &p1;
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
return 0;
}
6. Various Programs for Array Operations
i. Insertion
a. Insert at Beginning
insert_first(Array, n, max, data)
Step 1: Start
Step 2: If n = max, show Overflow and exit
Step 3: Repeat Step 4 for i = n down to 0
Step 4: arr[i + 1] = arr[i]
Step 5: arr[0] = data
Step 6: n = n + 1
Step 7: End
b. Insert at Last
insert_last(Array, n, max, data)
Step 1: Start
Step 2: If n = max, show Overflow and exit
Step 3: arr[n] = data
Step 4: n = n + 1
Step 5: End
c. Insert at Specific Position
insert_specific(Array, n, max, data, k)
Step 1: Start
Step 2: If n = max, show Overflow and exit
Step 3: Repeat Step 4 for i = n down to k
Step 4: arr[i + 1] = arr[i]
Step 5: arr[k] = data
Step 6: n = n + 1
Step 7: End
ii. Deletion
a. Deletion at Beginning
delete_first(Array, n)
Step 1: Start
Step 2: If n = 0, show Underflow and exit
Step 3: Repeat Step 4 for i = 0 to n - 1
Step 4: arr[i] = arr[i + 1]
Step 5: n = n - 1
Step 6: End
b. Deletion at Last
delete_last(Array, n)
Step 1: Start
Step 2: If n = 0, show Underflow and exit
Step 3: n = n - 1
Step 4: End
c. Deletion at Specific Position
delete_specific(Array, n, k)
Step 1: Start
Step 2: If n = 0, show Underflow and exit
Step 3: Repeat Step 4 for i = k to n - 1
Step 4: arr[i] = arr[i + 1]
Step 5: n = n - 1
Step 6: End
7. Introduction to Strings
A string is a sequence of characters used to represent text in programming. In C, strings are implemented as arrays of characters, terminated by a special null character '\0'
, which indicates the end of the string.
String Library Functions
strcat()
Concatenates (appends) the source string (src
) to the destination string (dest
).
Syntax:
char *strcat(char *dest, const char *src);
strncat()
Concatenates at most n
characters from the source string (src
) to the destination string (dest
).
Syntax:
char *strncat(char *dest, const char *src, size_t n);
strlen()
Returns the length of a string (excluding the null terminator).
Syntax:
size_t strlen(const char *str);
strcpy()
Copies the source string (src
) to the destination string (dest
).
Syntax:
char *strcpy(char *dest, const char *src);
strncpy()
Copies at most n
characters from the source string (src
) to the destination string (dest
). If src
is shorter than n
, the rest of dest
is filled with null characters.
Syntax:
char *strncpy(char *dest, const char *src, size_t n);
strcmp()
Compares two strings lexicographically.
- Returns
0
if strings are equal.
- Returns a negative value if the first string is less than the second.
- Returns a positive value if the first string is greater than the second.
Syntax:
int strcmp(const char *str1, const char *str2);
strncmp()
Compares at most n
characters of two strings lexicographically.
Syntax:
int strncmp(const char *str1, const char *str2, size_t n);
strcat()
Concatenates (appends) the source string (
src
) to the destination string (dest
).Syntax:
char *strcat(char *dest, const char *src);
strncat()
Concatenates at most
n
characters from the source string (src
) to the destination string (dest
).Syntax:
char *strncat(char *dest, const char *src, size_t n);
strlen()
Returns the length of a string (excluding the null terminator).
Syntax:
size_t strlen(const char *str);
strcpy()
Copies the source string (
src
) to the destination string (dest
).Syntax:
char *strcpy(char *dest, const char *src);
strncpy()
Copies at most
n
characters from the source string (src
) to the destination string (dest
). If src
is shorter than n
, the rest of dest
is filled with null characters.Syntax:
char *strncpy(char *dest, const char *src, size_t n);
strcmp()
Compares two strings lexicographically.
0
if strings are equal.Syntax:
int strcmp(const char *str1, const char *str2);
strncmp()
Compares at most
n
characters of two strings lexicographically.Syntax:
int strncmp(const char *str1, const char *str2, size_t n);
Comments
Post a Comment