0% found this document useful (0 votes)
48 views13 pages

C. Structure

The document contains code snippets demonstrating different data structures and algorithms in C programming: 1) A struct is defined to store student data including name, ID, and age. Main initializes a struct variable and prints the member values. 2) An array is accessed using a pointer that points to the first element. A for loop prints each element value using pointer arithmetic. 3) A linear search function returns the index of the target value in an array if found, else -1. Main calls this to search an integer array. 4) Binary search recursively searches a sorted array using divide and conquer. It returns the index if found, else prints not found. 5) Bubble sort algorithm sorts an

Uploaded by

ArafaT
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)
48 views13 pages

C. Structure

The document contains code snippets demonstrating different data structures and algorithms in C programming: 1) A struct is defined to store student data including name, ID, and age. Main initializes a struct variable and prints the member values. 2) An array is accessed using a pointer that points to the first element. A for loop prints each element value using pointer arithmetic. 3) A linear search function returns the index of the target value in an array if found, else -1. Main calls this to search an integer array. 4) Binary search recursively searches a sorted array using divide and conquer. It returns the index if found, else prints not found. 5) Bubble sort algorithm sorts an

Uploaded by

ArafaT
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
You are on page 1/ 13

C.

Structure :
#include <stdio.h>

struct StudentData

char *student_name;

int student_id;

int student_age;

};

int main()

/* student is the variable of structure StudentData*/

struct StudentData student;

/*Assigning the values of each struct member here*/

student.student_name = "Yeasir Ahnaf";

student.student_id = 20428151;

student.student_age = 20;

/* Displaying the values of struct members */

printf("Student Name is: %s", student.student_name);

printf("\nStudent Id is: %d", student.student_id);

printf("\nStudent Age is: %d", student.student_age);

return 0;

}
D. Array By Pointer :
#include<stdio.h>

void main()

int data[]={1,2,3,4,5,6,7,8,9,10,11,12,13};

int *p,i,n;

p=&data[0];

n=sizeof(data)/sizeof(data[0]);

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

printf("%d\n", *(p+i));

Description: In this program, we have a pointer p that points to the 0th element of the array. Similarly,
we can also declare a pointer that can point to whole array instead of only one element of the array.
This pointer is useful when talking about multidimensional arrays.

E. Linear Search:
// C code to linearly search x in arr[]. If x

// is present then return its location, otherwise

// return -1

#include <stdio.h>

int search(int arr[], int n, int x)


{

int i;

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

if (arr[i] == x)

return i;

return -1;

// Driver code

int main(void)

int arr[] = { 2, 3, 4, 10, 40 };

int x = 10;

int n = sizeof(arr) / sizeof(arr[0]);

// Function call

int result = search(arr, n, x);

(result == -1)

? printf("Element is not present in array")

: printf("Element is present at index %d", result);

return 0;

F: Binary Search:
#include <stdio.h>

int main()

int c, first, last, middle, n, search, array[100];


printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

printf("Enter value to find\n");

scanf("%d", &search);

first = 0;

last = n - 1;

middle = (first+last)/2;

while (first <= last) {

if (array[middle] < search)

first = middle + 1;

else if (array[middle] == search) {

printf("%d found at location %d.\n", search, middle+1);

break;

else

last = middle - 1;

middle = (first + last)/2;

if (first > last)


printf("Not found! %d isn't present in the list.\n", search);

return 0;

G. Bubble Sort:
#include <stdio.h>

int main()

int array[100], n, c, d, swap;

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)

for (d = 0 ; d < n - c - 1; d++)

if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */

swap = array[d];
array[d] = array[d+1];

array[d+1] = swap;

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)

printf("%d\n", array[c]);

return 0;

H. Infix to postfix:
#include<stdio.h>

#define SIZE 50 /* Size of Stack */

#include <ctype.h>

char s[SIZE];

int top = -1; /* Global declarations */


push(char elem) { /* Function for PUSH operation */

s[++top] = elem;

char pop() { /* Function for POP operation */

return (s[top--]);

int pr(char elem) { /* Function for precedence */

switch (elem) {

case '#':

return 0;

case '(':

return 1;

case '+':
case '-':

return 2;

case '*':

case '/':

return 3;

main() { /* Main Program */

char infx[50], pofx[50], ch, elem;

int i = 0, k = 0;

printf("\n\nRead the Infix Expression ? ");

scanf("%s", infx);

push('#');
while ((ch = infx[i++]) != '\0') {

if (ch == '(')

push(ch);

else if (isalnum(ch))

pofx[k++] = ch;

else if (ch == ')') {

while (s[top] != '(')

pofx[k++] = pop();

elem = pop(); /* Remove ( */

} else { /* Operator */

while (pr(s[top]) >= pr(ch))

pofx[k++] = pop();

push(ch);

}
while (s[top] != '#') /* Pop from stack till empty */

pofx[k++] = pop();

pofx[k] = '\0'; /* Make pofx as valid string */

printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);

I. Postfix Evalution:
#include<stdio.h>

#define SIZE 50 /* Size of Stack */

#include <ctype.h>

int s[SIZE];

int top=-1; /* Global declarations */

push(int elem)

{ /* Function for PUSH operation */


s[++top]=elem;

int pop()

{ /* Function for POP operation */

return(s[top--]);

main()

{ /* Main Program */

char pofx[50],ch;

int i=0,op1,op2;

printf("\n\nRead the Postfix Expression ? ");

scanf("%s",pofx);
while( (ch=pofx[i++]) != '\0')

if(isdigit(ch)) push(ch-'0'); /* Push the operand */

else

{ /* Operator,pop two operands */

op2=pop();

op1=pop();

switch(ch)

case '+':push(op1+op2);break;

case '-':push(op1-op2);break;

case '*':push(op1*op2);break;

case '/':push(op1/op2);break;

}
}

printf("\n Given Postfix Expn: %s\n",pofx);

printf("\n Result after Evaluation: %d\n",s[top]);

You might also like