0% found this document useful (0 votes)
19 views118 pages

Dsa Unit 1

The document provides an overview of data structures, their types, and operations, including arrays, linked lists, stacks, and queues. It also explains search algorithms such as linear and binary search, along with their complexities. Additionally, it covers sorting algorithms like bubble sort and discusses mathematical concepts related to algorithm analysis, including asymptotic notation and time complexity.

Uploaded by

uk8349209
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)
19 views118 pages

Dsa Unit 1

The document provides an overview of data structures, their types, and operations, including arrays, linked lists, stacks, and queues. It also explains search algorithms such as linear and binary search, along with their complexities. Additionally, it covers sorting algorithms like bubble sort and discusses mathematical concepts related to algorithm analysis, including asymptotic notation and time complexity.

Uploaded by

uk8349209
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

Introduction:

• Data
- Means?
- Purpose?
• Computer & Data
- Dependency Relationship
- Utilization
- Consequences
Basic Terminology
• Data / Datum
• Elementary Data
• Grouped Data
• Entity and Entity set
• Information
• Record
• File
Data Structures
• Definition

- The logical or mathematical model of organization of data

• Data Structure reflects the relationships among the collection of data.

• Data Structure allows processing of data efficiently.

• Data structure reflects organization of data in the memory of a computer.


Examples:
Types of Data Structure
Primitive Data Type
A[0]=20
A[1]=2
A[2]=.
.
.
..
.
d into Non Primitive Data Type
ar Data Structure – Array, Linked List, Stack, Queue

n-Linear Data Structure – Tree & Graph


ADT
• ADT means - Abstract Data Type

• Definition:
- It’s the logical representation of collection of data in a data structure
along with set of operations that are permitted over the data items in the
data structure.
- Example
- List ( Creation, Insert, Delete, Search )
-Stack (Push, Pop)
- Queue (Enqueue, Dequeue)
Data Structure Operations
• Array ADT
- Creation
- Definition and Declaration
- Insertion
- Deletion
- Sorting
-Searching
- Applications
Data Structure Operations
• Linked List
- Definition and Declaration (Structure)
- Creation (Head node)
- Insertion
- Deletion
- Search / Traverse
- Types (Single, Double, Multi, Circular)
- Applications
Data Structure Operations
• Stack
- Definition and Declaration (Array / List)
- Insertion (Push)
- Deletion (Pop)
- Searching
- Applications
Data Structure Operations
• Queue
- Definition and Declaration (Array / List)
- Insertion ( Enqueue )
- Deletion ( Dequeue)
- Types ( Linear, Circular )
- Applications
Data Structure Operations
• Tree
- Definition and Declaration (Array / List)
- Insertion
- Deletion
- Types ( Binary Tree, Binary Search Tree, AVL Tree, RB Tree )
- Applications
Introduction
• The process of determining the
What is value in a given list of values.
• it also finds the index of the
search? searched value.

• Linear Search
Types • Binary Search
Linear Search

uses brute force technique to find a given


item (element).

The time complexity of Linear search is


O(n)

n is total number of items in the list


Linear Search Algorithm:
• Step 1 - Take both input, set of numbers and search
element from the user.
• Step 2 - Match the search element with the staring element
in the list.
• Step 3 - If both the elements are matched, then display
"Given search item is found" and stop the function
• Step 4 - If both the elements are not matched, then
compare the search element with the next element in the
array list.
• Step 5 - Repeat steps 3 and 4 until search element is
compared with last element in the array list.
• Step 6 - If the end element in the list also doesn't match,
then display "Element is not found" and stop the function.
Example

consider the following list

60 20 15 52 30 12 10 28 1

Search element is 30
• Step 1:
List:

60 20 15 52 30 12 10 28 1

The first element and match element are not


same. So it moves with next element of the array.
• Step 2:

60 20 15 52 30 12 10 28 1
• Step 3:
List

60 20 15 52 30 12 10 28 1

Step 4:
List

60 20 15 52 30 12 10 28 1
• Step 5:
List

60 20 15 52 30 12 10 28 1

• Matched- the procedure has been stopped and


the function displays element found
• return the position of the element is 5.
( If array index starts by 1 otherwise the same
will return 4.)
Binary Search
Efficient than Linear search

Prerequisite: Sorted list in ascending order

The time complexity of Linear search is O(logn)

n is total number of items in the list


Binary Search Algorithm:
• Step 1 - Take both the inputs, set of numbers and the
search element from the user.
• Step 2 - Find the middle element in the array of
elements (Constraint : list should be sorted list.
• Step 3 - Compare the search item with the middle
element in the array list.
• Step 4 - If both the elements are equal, then show that,
"The Given element is available in the list" and stop the
function.
• Step 5 - If both the elements are not matched, then
check whether the search element is lesser or greater
than the middle element.
Binary Search Algorithm:

• Step 6 - If the search element is lesser than the middle


element, repeat the steps 2, 3, 4 and 5 for the left partition
of sub array of the middle element.
• Step 7 - If the search element is greater than middle
element, repeat the steps 2, 3, 4 and 5 for the right
partition of sub array of the middle element.
• Step 8 - Repeat the same procedure until we find the search
element in the list or until sub array contains only one
element.
• Step 9 - If that element also doesn't match with the search
element, then display "The Element is not found in the list"
and stop the function.
Example

consider the following list


0 1 2 3 4 5 6 7 8

10 12 15 20 30 42 50 58 70

Search element is 20
Example

Step 1:
Find Middle Element = (Start index+ End index)/2
= (0+8)/2 =4 The middle element is (30)

0 1 2 3 4 5 6 7 8

10 12 15 20 30 42 50 58 70

Search element is 20
Example
Step 2:
The search element is compared with middle element and the it is smaller than
middle element. Searching starts with left sub array. ( 10,12, 15, 20)

Step 3:
The search element is compared with middle element (12). And it is greater than
middle element. search technique starts with right sub array of the middle
element. (15,20)

0 1 2 3 4 5 6 7 8

10 12 15 20 30 42 50 58 70
Example
Step 4:
• The search element is compared with middle element (15). And it is greater
than middle element. Now the search technique starts with right sub array of
the middle element. (20).

0 1 2 3 4 5 6 7 8

10 12 15 20 30 42 50 58 70

Middle

The element 20 found at index 3. And the procedure stopped.


1. FLOOR AND CEILING FUNCTIONS:

Floor Function: the greatest integer that


is less than or equal to x

Ceiling Function: the least integer that is


greater than or equal to x

Notation
2. Remainder function : Modular arithmetic

Let x be any integer and let M be a positive integer.


Then x ( mod M)

Eg:

13 (mod 5) =3

3. Integer and Absolute Value functions


INT (x) converts x into an integer by deleting the fractional part of the number
ABS (x) or |x| gives the greater of x or -x

Eg:

INT(3.14) =3
|15| = 15
|-0.33| = 0.33
4. Summation Symbol: sums

Summation symbol : Σ
This symbol (called Sigma) means "sum up"

5. Factorial Function

The factorial function (symbol: !) says to multiply all whole numbers from our
chosen number down to 1.

n! = 1.2.3…. (n-2).(n-1)

E.g: 2! = 1.2 = 2
6. Permutations

A permutation of a set of n elements is an arrangement


of the elements in a given order .

E.g: Permutations of the elements a, b and c arbe


abc, acb, bac, bca, cab, cba

7. Exponents and Logarithms

Exponents are also called Powers or Indices.


Example: 53 = 5 × 5 × 5 = 125

Logarithm of any positive number x to the base b, written as


logb(x)
=> y= logb(x)
=> by = x
Asymptotic Notation– Big, Omega , Theta

Asymptotic Notations refers to computing the running time of any


operation in mathematical units of computation.

•Ο Notation (Big)
•Ω Notation (Omega)
•θ Notation (Theta)
•Big Oh Notation, Ο
• It measures the worst case time complexity or longest amount of time an
algorithm can possibly take to complete.
O(g(n)) = { f(n): there exist positive constants c and n0 such that
0 <= f(n) <= cg(n) for all n >= n0}
•Ω Notation:
• Ω notation provides an asymptotic lower bound
•Θ Notation:

Θ((g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that
0 <= c1*g(n) <= f(n) <= c2*g(n) for all n >= n0}
Mathematical Analysis
• For Non-recursive Algorithms
• There are four rules to count the operations:
• Rule 1: for loops - the size of the loop times the running time of the body
• Find the running time of statements when executed only once
• Find how many times each statement is executed
• Rule 2 : Nested loops
• The product of the size of the loops times the running time of the body
• Rule 3: Consecutive program fragments
• The total running time is the maximum of the running time of the individual fragments
• Rule 4: If statement
• The running time is the maximum of the running times of if stmt and else stmt.
Exercise
c.
a.
sum = 0;
sum = 0;
for( i = 0; i < n; i++)
for( i = 0; i < n; i++)
for( j = 0; j < i*i; j++)
for( j = 0; j < n * n; j++)
for( k = 0; k < j; k++)
sum++;
sum++;
Ans : O(n^3)
Ans : O(n^5)

d.
b.
sum = 0;
sum = 0;
for( i = 0; i < n; i++)
for( i = 0; i < n; i++)
sum++;
for( j = 0; j < i; j++)
val = 1;
sum++;
for( j = 0; j < n*n; j++)
Ans : O(n^2)
val = val * j;
Ans : O(n^2)
Order of Growth Function
BUBBLE SORT
• Bubble Sort is the simplest sorting algorithm

• Works by repeatedly swapping the adjacent elements if they


are in wrong order.

• Easier to implement, but slower than Insertion sort


Example:
• Let us consider the input array of size, N=5
A[5] ={5,1,4,2,8}

First Pass:

• ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ),
• Here, algorithm compares the first two elements, and swaps since
5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ),
• Now, since these elements are already in order (8 > 5), algorithm
does not swap them
Second Pass
• ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
• Now, the array is already sorted, but the algorithm does not know if
it is completed.
• The algorithm needs one whole pass without any swap to know it is
sorted.

Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Pseudo-code: bubble sort
Bubble_sort(A , N)
Step 1: Repeat Step 2 for I = 0 to N-1
Step 2: Repeat for J=0 to N – I
Step 3: IF A[ J ] > A[ J + 1]
SWAP A[ J ] and A[ J + 1 ]
[END OF INNER LOOP]
[END OF OUTER LOOP]
Step 4: EXIT
Bubble sort program in C
for (c = 0 ; c < n - 1; c++)
/* Bubble sort code */ {
for (d = 0 ; d < n - c - 1; d++)
#include <stdio.h> {
if (array[d] > array[d+1])
int main( ) /* For decreasing order use ‘< ‘*/
{ {
int array[100], n, c, d, t, swap; swap = array[d];
array[d] = array[d+1];
printf("Enter number of elements:\n"); array[d+1] = swap;
scanf("%d", &n); }
}
printf("Enter %d integers\n", n); }
for (t = 0; t < n; t++)
{ printf("Sorted list in ascending order:\n");
scanf("%d", &array[t]); for (c = 0; c < n; c++) {
printf("%d\n", array[c]); }
} return 0;
}
Time Complexity:

• Worst and Average Case Time Complexity: O(n^2).


The worst-case occurs when an array is reversely sorted.
For example: input array to be sorted is 5,4,3,2,1

• Best Case Time Complexity: O(n).


The best-case occurs when an array is already sorted.
For example: input array to be sorted is 1,2,3,4,5
INSERTION SORT
• One of the simplest sorting algorithms that construct the
final sorted array by one element at a time.

• In-place comparison-based sorting algorithm.

• The array elements are compared with each other


sequentially and then arranged simultaneously in some
particular order
• Idea: like sorting a hand of playing cards
– Start with an empty left hand and the cards facing down
on the table.
– Remove one card at a time from the table, and insert it
into the correct position in the left hand
• compare it with each of the cards already in the hand,
from right to left
– The cards held in the left hand are sorted
• these cards were originally the top cards of the pile on
the table
• To insert 12, we need to make room for it by moving first 36
and then 24.

6 10 24 36

12
• First shift 36 to its right position

6 10 24 36

12
Insertion Sort
• Secondly 24 shifted to its right position. And 12 is placed in
its correct position.

6 10 24 3
6

12

6
Example:

Let us consider the input array of size, N=6


ARR[6] ={ 5 , 2 , 4 , 6 , 1 , 3}

At each iteration, the array is divided in two sub-arrays

left sub-array right sub-array


Sorted Unsorted
Pseudo-code: Insertion sort
INSERTION SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 for J =1 to N - 1
Step 2: SET KEY = ARR[ J ]
Step 3: SET I = J – 1
Step 4: Repeat while ((I >= 0) and (ARR[ I ] > KEY ))
SET ARR[I + 1] = ARR[ I ]
SET I = I – 1
[END OF INNER LOOP]
Step 5: SET ARR[I + 1] = KEY
[END OF OUTER LOOP]
Step 6: EXIT
#include <stdio.h> // A utility function to print an array of size n

/* Function to sort an array using insertion sort*/ void printArray(int arr[], int n)
void insertionSort(int arr[], int n) {
{ int i;
int i, key, j; for (i = 0; i < n; i++)
for (j = 1; j < n-1; j++) { printf("%d ", arr[i]);
key = arr[j]; printf("\n");
i = j - 1; }

/* Move elements of arr[0..i-1], that are /* Driver program to test insertion sort */
greater than key, to one position ahead
of their current position */ int main()
while (i >= 0 && arr[i] > key) { {
arr[i + 1] = arr[i]; int arr[] = { 12, 11, 13, 5, 6 };
i = i - 1; int n = sizeof(arr) / sizeof(arr[0]);
}
arr[i + 1] = key; insertionSort(arr, n);
} printArray(arr, n);
} return 0;
}
Time Complexity:

• Worst and Average Case Time Complexity: O(n*n).


The worst-case occurs when an array is reversely sorted.
For example: input array to be sorted is 9,7,5,3,1

• Best Case Time Complexity: O(n).


The best-case occurs when an array is already sorted.
For example: input array to be sorted is 2,4,6,8,10
Data Structure
• Collecting and Organizing data
• Perform operations on data in effective way
• Data refers to a value or set of values.
• Data can be organized in many ways
– logical
- Mathematical
Types of Data Structures
Data Structures

Primitive Data Non-Primitive


Structures Data Structures

Integer Real Character Boolean Linear Data Non-Linear


Structures Data Structures
Arrays Trees
Linked List Graphs

Stack
Queue
Types of Data Structures
1. Primitive
- Integer
- Real
- Character
- Boolean

2. Non-Primitive
- Linear
- Non-Linear
Linear and Non-Linear Data Structures
• A data structure is considered as linear if the elements are arranged in
a linear fashion(elements form a sequence).
• Examples: Arrays, Linked List, Stacks and Queues
• A data structure is considered as Non-Linear if the elements not
arranged in a linear fashion(not in sequence).
• Examples: Trees, Graphs
Arrays

• Sequential collection of elements


• Similar data type.
• Adjacent memory locations.
Linked List
• Getter memory management
• Run time memory allocated
• No memory wastage
• Direct access to elements restricted
Stack
• LIFO - Last In First Out
• Elements inserted at last will be removed first.
• Application of Stack includes
- Solving Recursion
- Towers of Hanoi
- Evaluating Postfix Expression
- Depth First Search
Queue
• FIFO - First In First Out
• The element inserted first will be removed first.
• The variants of Queue includes
- Double Ended Queue
- Circular Queue
- Priority Queue.
• Application of Queue includes
- Shared Resource access
- Multiprogramming and
- Message Queue
Trees
• Hierarchical in nature.
• Root - Top element of tree
• It is an useful data structure to store hierarchical information.
• Example – Directory structure of file system
Graph

• Networked data structure.


• It consists of a collection of nodes(Vertices) connected by edges.
• An edge is a path/link between two nodes.
• The edge can be directed/undirected.
• If a path is directed then you can travel in one direction alone.
• If a path is undirected then you can travel in both directions.
1D Array Initialization and Accessing using
Pointers
• The pointer can be used to store address of arrays.
• Consider we have an array of integer then
Step1 : Declare the integer array
int a[] = {12,34,42,11,10};
Step 2: Declare integer pointer
int *p;
Step 3: Initialize pointer with base address of array of integers.
p = a; // return base address of array ‘a’(using array name)
or
p = &a[0]; // return base address of array ‘a’(using address of first
element of array )
1D Array Initialization and Accessing using
Pointers
Step 4: To access the array element we use *(p + n)
where n is the number of element.
i.e. *(p+0) returns a[0] value
*(p+1) returns a[1] value
.
.
.
*(p+n) returns a[n] value
1D Array Initialization and Accessing using
Pointers
Sample C Code:
#include <stdio.h>
int main() Output:
{ 12
int a[]={12,34,42,11,10}; 34
int *p, i; 42
p = a; //initialize pointer 11
for(i=0; i<5; i++) 10
printf("%d\n",*(p+i));
return 0;
}
2D Array Initialization and Accessing using
Pointers
• We can access an 2D array using pointers as *(*(a+i) + j)
• The expression *(a + i) provides the base address of ith 1D array.
• *(a + i) + j provides the address of jth element of ith 1D array
1 2 3
a

a+1 4 5 6

7 8 9
a+2

*(a+2) *(a+2)+3
2D Array Initialization and Accessing using
Pointers
Sample Code:
#include <stdio.h> Output:
int main() 11 10 31 40
{ int a[3][4] = { { 11, 10, 31, 40 }, 15 61 72 81
{ 15, 61, 72, 81 }, 9 1 1 2
{ 9, 1, 1, 2 } };
int k, l;
for (k = 0; k < 3; k++)
{ for (l = 0; l < 4; l++)
printf("%d ", *(*(a + k) + l));
printf("\n"); }
return 0; }
Sorting Techniques
Sorting
• A sorting algorithm is an algorithm used to generate the
ordered list in a certain order.

• Sorting is defined as ordering of given data based on


different sorting technique

Unsorted list

Sorted list
BASIC TYPES :

Internal Sorting

External Sorting
Sorting Techniques

✔ Bubble sort

✔ Insertion sort

✔ Select sort

✔ Quick sort

✔ Merge sort

✔ Bucket sort

✔ Radix sort
Bubble Sort

Bubble sort, is referred as sinking sort it is the simple sorting algorithm.


Swapping with adjustment element in the list. Until no further swapping is
needed which denoted all the elements as sorted in the list

The sorting order is small element to largest number like the bubble in the
given image
Algorithm

procedure bubbleSort( A : list of sortable items ) defined


as: do
swapped := false
for each i in 0 to length( A ) - 1
do: if A[ i ] > A[ i + 1 ] then
swap( A[ i ], A[ i + 1 ]
) swapped := true
end if
end for
while swapped
end procedure
Time Complexities

Cases Big – O

Best O(n)

Average O ( n2 )

Worst O ( n2 )
Insertion Sort
All the data items arranged in one at a time
using Insertion sort algorithm
Algorithm

insertionSort(array A)
for i = 1 to length[A]-1 do
begin
value =
A[i] j = i-1
while j >= 0 and A[j] > value do
begin
swap( A[j + 1], A[j]
) j = j-1
end
A[j+1] = value
end
Time Complexities

Cases Big – O

Best O(n)

Average O ( n2 )

Worst O ( n2 )
Pointer to structure declaration
Syntax
struct structname {
data type member1;
data type member2;
}variable1, *ptr_variable1
Accessing structure variables
Syntax 1 Syntax 2
(*ptrvariable).member1; *ptrvariable −> member1;
Example -1
struct book ptr = &b1 ;
{ printf ( "\n%s %s %d", [Link], [Link],
char name[25] ; [Link] ) ;
char author[25] ; printf ("\n%s %s %d", ptr->name,
int callno ; ptr->author, ptr->callno ) ;
};
struct book b1 = { "Let us C",
“ABC", 101 } ;
struct book *ptr ;
Example - 2
struct dog my_dog = {"tyke", "Bulldog", 5, "white"};
struct dog struct dog *ptr_dog;
{ ptr_dog = &my_dog;

char name[10]; printf("Dog's name: %s\n", ptr_dog->name);


char breed[10]; printf("Dog's breed: %s\n", ptr_dog->breed);
printf("Dog's age: %d\n", ptr_dog->age);
int age;
printf("Dog's color: %s\n", ptr_dog->color);
char color[10];
}; strcpy(ptr_dog->name, "jack");
ptr_dog->age++;
printf("Dog's new name is: %s\n", ptr_dog->name);
printf("Dog's age is: %d\n", ptr_dog->age);
Example -3
#include <stdio.h> int total;
int main() printf("Enter the Name of the student : ");
{ scanf("%s", (*studptr).name);
struct student{ printf("\nEnter sub1 marks : ");
char name[40]; scanf("%d",&studptr->avg.sub1);
struct avg{ printf("\nEnter sub2 marks : ");
int sub1, sub2, sub3; scanf("%d",&studptr->avg.sub2);
float average; printf("\nEnter sub3 marks : ");
}avg; scanf("%d",&studptr->avg.sub3);
}stud1, *studptr = &stud1; total = (studptr->avg.sub1 + studptr->avg.sub2 +
//Pointer variable stores the
address of structure variable studptr->avg.sub3);
Example -3 continues
studptr->[Link] = total / 3;
printf("\n-------Student Details-------\n ");
printf("Name: %s", studptr->name);
printf("\nsub1: %d ", studptr->avg.sub1);
printf("\nsub2: %d ", studptr->avg.sub2);
printf("\nsub3: %d ", studptr->avg.sub3);
printf("\nAverage: %f ", studptr->[Link]);
return 0;
}
Structure Declaration
The general form of a structure declaration statement is
given below:
struct <structure name>
{
datatype element 1 ;
datatype element 2 ;
datatype element 3 ;
......
......
};
Example of structure declaration
The following statement declares the
structure type:
struct book
{
char name[20] ;
float price ;
int pages ;
};
Example of structure declaration
Once the new structure data type has been defined one or more
variables can be declared to be of that type. For example the variables
b1, b2, b3 can be declared to be of the type struct book, as,
struct book b1, b2, b3;
Example of structure declaration
struct book struct book
Struct
{ {
{
char name[20] ; char name [20] ;
char name [20] ;
float price ; float price ;
float price ;
int pages ; int pages ;
int pages ;
}; } b1, b2, b3 ;
} b1, b2, b3 ;
struct book b1, b2, b3 ; is same as …
is same as...
Accessing structures
To refer to pages we would use,
[Link]
Similarly, to refer to price we would use,
[Link]
Array of Structures
Declaring array of structures
struct book
{
char name [20] ;
float price ;
int pages ;
};
struct book b[100] ;
Array of Structures
Declaring array of structures Accessing array of structures
struct book int i ;
for ( i = 0 ; i <= 99 ; i++ )
{
{
char name [20] ; scanf ( "%c %f %d", &b[i].name, &b[i].price,&b[i].pages) ;
float price ; }
int pages ; for ( i = 0 ; i <= 99 ; i++ )
}; printf ( "\n%c %f %d", b[i].name, b[i].price, b[i].pages);

struct book b[100] ;


Implement structures using Pointers
struct book ptr = &b1 ;
{ printf ( "\n%s %s %d", [Link], [Link],
char name[25] ; [Link] ) ;
char author[25] ; printf ("\n%s %s %d", ptr->name,
int callno ; ptr->author, ptr->callno ) ;
};
struct book b1 = { "Let us C",
“ABC", 101 } ;
struct book *ptr ;
Implement structures using Pointers
struct dog my_dog = {"tyke", "Bulldog", 5, "white"};
struct dog struct dog *ptr_dog;
{ ptr_dog = &my_dog;

char name[10]; printf("Dog's name: %s\n", ptr_dog->name);


char breed[10]; printf("Dog's breed: %s\n", ptr_dog->breed);
printf("Dog's age: %d\n", ptr_dog->age);
int age;
printf("Dog's color: %s\n", ptr_dog->color);
char color[10];
}; strcpy(ptr_dog->name, "jack");
ptr_dog->age++;
printf("Dog's new name is: %s\n", ptr_dog->name);
printf("Dog's age is: %d\n", ptr_dog->age);
Passing structure to a function
int main() {
#include <stdio.h> struct student s1;
struct student { scanf("%s", [Link]);
char name[50]; scanf("%d", &[Link]);
display(s1); // passing struct
int age;
}; return 0;
}

void display(struct student s); void display(struct student s) {


printf("Name: %s", [Link]);
printf("\nAge: %d", [Link]);
}
Return structure from a function
int main(){
struct student s;
#include <stdio.h>
s = getInformation();
struct student printf("Name: %s", [Link]);
{ printf("\nRoll: %d", [Link]);
return 0;
char name[50];
}
int age; struct student getInformation(){
}; struct student s1;
struct student getInformation(); scanf ("%s", [Link]);
scanf("%d", &[Link]);
return s1;
}

You might also like