0% found this document useful (0 votes)
10 views3 pages

Complete Data Structure Programs

The document provides algorithms and C programs for reversing a string using pointers, sorting a list of strings with bubble sort, and merging two sorted arrays. Each section includes a clear algorithm followed by the corresponding C code implementation. The examples demonstrate fundamental data structure operations in C programming.

Uploaded by

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

Complete Data Structure Programs

The document provides algorithms and C programs for reversing a string using pointers, sorting a list of strings with bubble sort, and merging two sorted arrays. Each section includes a clear algorithm followed by the corresponding C code implementation. The examples demonstrate fundamental data structure operations in C programming.

Uploaded by

rithusreekj005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Structure Programs Using C

Algorithm: Reverse a String Using Pointer


1. Start
2. Input string S.
3. Initialize two pointers:
a) left pointing to the first character.
b) right pointing to the last character.
4. While left < right:
a) Swap S[left] and S[right].
b) Move left forward and right backward.
5. Print the reversed string.
6. End

C Program: Reverse a String Using Pointer


#include <stdio.h>
#include <string.h>

void reverseString(char *str) {


char *left = str, *right = str + strlen(str) - 1;
char temp;

while (left < right) {


temp = *left;
*left = *right;
*right = temp;
left++;
right--;
}
}

int main() {
char str[100];

printf("Enter a string: ");


scanf("%s", str);

reverseString(str);
printf("Reversed string: %s\n", str);

return 0;
}

Algorithm: Sort a Given List of Strings


1. Start
2. Input N (number of strings).
3. Store N strings in LIST[N].
4. Use bubble sort:
a) Compare adjacent strings.
b) Swap if necessary.
5. Print the sorted list.
6. End

C Program: Sort a Given List of Strings


#include <stdio.h>
#include <string.h>

void sortStrings(char arr[][50], int n) {


char temp[50];

for (int i = 0; i < n - 1; i++) {


for (int j = i + 1; j < n; j++) {
if (strcmp(arr[i], arr[j]) > 0) {
strcpy(temp, arr[i]);
strcpy(arr[i], arr[j]);
strcpy(arr[j], temp);
}
}
}
}

int main() {
int n;
char arr[10][50];

printf("Enter number of strings: ");


scanf("%d", &n);
printf("Enter %d strings:\n", n);

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


scanf("%s", arr[i]);
}

sortStrings(arr, n);

printf("\nSorted strings:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", arr[i]);
}

return 0;
}

Algorithm: Merge Two Sorted Arrays


1. Start
2. Input two sorted arrays A[M] and B[N].
3. Initialize i = 0, j = 0, and k = 0.
4. While i < M and j < N:
a) If A[i] < B[j], store A[i] in C[k] and increment i.
b) Else, store B[j] in C[k] and increment j.
c) Increment k.
5. Copy remaining elements of A and B into C.
6. Print merged array C.
7. End

C Program: Merge Two Sorted Arrays


#include <stdio.h>

void mergeArrays(int A[], int B[], int C[], int M, int N) {


int i = 0, j = 0, k = 0;

while (i < M && j < N) {


if (A[i] < B[j])
C[k++] = A[i++];
else
C[k++] = B[j++];
}

while (i < M)
C[k++] = A[i++];

while (j < N)
C[k++] = B[j++];
}

int main() {
int A[5] = {1, 3, 5, 7, 9}, B[4] = {2, 4, 6, 8}, C[9];

mergeArrays(A, B, C, 5, 4);

printf("Merged sorted array: ");


for (int i = 0; i < 9; i++)
printf("%d ", C[i]);

return 0;
}

You might also like