0% found this document useful (0 votes)
6 views

Data Structure

The document provides an overview of data structures, emphasizing their importance in efficiently storing and organizing data. It covers various types of data structures, including primitive and linear/non-linear data structures, as well as specific implementations like arrays and sorting algorithms. Additionally, it discusses operations performed on data structures such as searching, sorting, inserting, deleting, and updating.

Uploaded by

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

Data Structure

The document provides an overview of data structures, emphasizing their importance in efficiently storing and organizing data. It covers various types of data structures, including primitive and linear/non-linear data structures, as well as specific implementations like arrays and sorting algorithms. Additionally, it discusses operations performed on data structures such as searching, sorting, inserting, deleting, and updating.

Uploaded by

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

Data

Structure

Syllabus:
Data Structure

Ds is a Way to store and organize


data so that it can be used
efficiently.

Why ?
Want to store : 1-5
If store: {4,1,2,3,5} not useful

If store: { 1,2,3,4,5}
{5,4,3,2,1} is useful

Point To Remember:
How to store Data ?
What are the different methods use
to store this DS?
Data Structure Divided into
following Categories

Primitive :
Int,float,char,Double,Pointer
If any keywords founds in datatype
is called primitive DS (Predefine
reserved words)
Operation Performed in DS
1) Searching
2) Sorting
3) Inserting 12345
4) Deletion 1 345
5) Updating 1345
6) Traverse
LDS:
A DS is called Linear if all of its
elements are arranged in the linear
order.
1-2-3-4-
5.1====2===3===4====5
NLDS:
This DS does not form a sequence
i.e. each item or element is
connected with two or more items
in non-linear arrangement.
Multivalued Cycle

Adv:
1) Efficient
2) Reusability

Day 2
Chap 2 Linear Data Structure

Array:
An array is finite ordered collection
of homogeneous data element
which provide random access to
the element .

Int a[5];
Int ---------data type
a------------array name
5 ----------------size
a
0 1 2
3 4
10 20 30 40 50
Application Of Array
Stored list of value
Matrix operation
Search algorithm
Sorting algorithm
Data structure
Cpu scheduling

Array initialization and


Representation

At the time declaration

Int x[5]={ 14,15,16,17,18}


Float a[3]={ 5.2,3.2,6.1}
Char x[3]={‘a’,’b’,’c’}
Index0 1 2 3
4
14 15 16 17 18
Address100 200 300 400
500

Index 0 1 2
5.2 3.2 6.1
Address 200 300 400

Int==========2 or 4 bytes
Float ============ 4 bytes
Char =============1 byte
Day 3

Type of array
1) One dimensional
2) Multidimentional
a)two Dimensional
b) n- Dimensional
One DA
A1
2byt
es
A2
-
-
-
An
Q1 : if int a[5] is an array of 5
elements. Each integer is of 2 bytes
and base address is 100,then find
the address of 3rd element.
0 1 2
3 4
1stele 2ndele ?3 ele 4 ele 5 ele
100 ?

Formal:
Address ith element=Base address
+ (offset of the ith element from
the base address)
A[i]=base address+(offset x size of
datatype)
A[2]= 100+(2x2)
=100+4
=104
4th element
A[3]=100+(3x2)
=100+6
=106
5th element
A[]= ? 108

Two DA
Int A[i][j]
1) i========row
2) j========col

Q1: TO calculate address of num[2]


[3] of interger array of size 3x4
with base address 1000
A[row][col]
Int A[3][4]
0 1 2
3
0a[0] A[0] A[0] A[0][3]
[0] [1] [2]
1a[1] A[1] A[1] A[1][3]
[0] [1] [2]
2a[2] A[2] A[2] A[2]
[0] [1] [2] [3]=?
1000
Formula:
A[i][j]= base address+[(i x
no_of_col+j) x size_of_data_type)]

num[2][3]= 1000+[(2 x4+3) x 2]


= 1000+[(8+3) x 2]
=1000+[11 x 2]
=1000+22
=1022

Num[2][1]=?
1018

H.W
A[2][3]=101+[(2 x 3+3) x 2]
=101 +[(6+3) x2]
=101 + [9 x 2]
=101 + 18
=119

Q2: To calculate address of A(1,2)


of integer array of size 2 x 3 with
base address 100.

11 july 2024
Sorting :
Sorting refers to the operation of
arranging data in some given
sequence such as ascending order
or descending order .
For eg: 30 36 50 49 22
AO: 22 30 36 49 50
DO: 50 49 36 30 22
Sorting stability

1) stable sort:bubble sort,merge


sort , and insertion sort.
2) Unstable sort :heap sort and
quick sort

Ex:
A Ab O s p R
75 90 80 85 85 82
ss
Ab S P r o a
90 85 85 82 80 75

Us_s
Ab P S r o a
90 85 85 82 80 75

Sorting Techniques
Two main categories
1) Internal sort: use primary
memories (main memories)
Ex: Bubble sort, insertion sort,
Quick sort
2) External sort : use secondary
memories(Hard disk,magnetic
tape)
Ex: merge sort
Sorting Methods :
1. Bubble sort
2. Insertion sort
3. Merge sort
4. Quick sort
5. Selection sort
Swapping
A=1 B=2
Swapping: A=2 B=1
Temp=A temp= 1 a=?
B=2 temp=1
A=BA=2 A=2 B=?
temp=1
B=TempB= 1 A=2 B=1
Swapping

A=3 B= 2 ====== A=2


B=3
T=A;
A=B;
B=T;

Bubble Sort
In Bubble Sort ,each element is
compared its adjacent element.if
the first element is larger thanthe
second one then the position of the
element are
interchanged ,otherwise it is no
changed.

13 11 14 15 19 9
Sort using Bubble sort
Process:

Pass 1:
13 11 14 15 19 9 13 > 11
true
11 13 14 15 19 9 13 >
14 false
11 13 14 15 19 9 14>15
false
11 13 14 15 19 9
15>19 flase
11 13 14 15 19 9 19>9
true
11 13 14 15 9 19

Pass 2:
11 13 14 15 9 19
11 13 14 15 9 19
11 13 14 15 9 19
11 13 14 15 9 19
11 13 14 9 15 19
11 13 14 9 15 19

Pass 3:
11 13 14 9 15 19
11 13 14 9 15 19
11 13 14 9 15 19
11 13 9 14 15 19
11 13 9 14 15 19
11 13 9 14 15 19

Pass 4:
11 13 9 14 15 19
11 13 9 14 15 19
11 9 13 14 15 19
11 9 13 14 15 19
11 9 13 14 15 19
11 9 13 14 15 19
Pass 5:
11 9 13 14 15 19
9 11 13 14 15 19
9 11 13 14 15 19
9 11 13 14 15 19
9 11 13 14 15 19
9 11 13 14 15 19
======= sorted array
Algorithm: (arranging the elements
in ascending order)
1) Read N,number of elements in
the list.
2) Read array a[0],a[1],------a[n-1]
3) For i=0 to n-1
For j=0 to n-i-1
If (a[j] >a[j+1]) then
Temp= a[j]
A[j]=a[j+1]
A[j+1]=Temp
4) Stop

1) N=5
8 1 7 2 5

2) A[0] A[1] A[2]


A[3] a[4]
8 1 7 2 5

3) i=0 to N-1
j=0 to n-i-1( 5-0-1)
if(a[j]0 >a[j+1]
1]======a[0] >a[1]
8>1
a[j]=a a[j+1]= b
Temp=a[j]
Temp=a
A[j]=a[j+1]
A=b
A[j+1]=temp
B=temp
===================
===========

13 july 2024
Q1:Write a program for Bubble
sort using C

Call by value and call by reference


Actual parameters: the parameters
passed
To function
Formal parameters: the parameters
received by function
Add(m,n) int add(int a,
int b) //formal P//Actual P
{

Return(a+b);
}
Call by value
Here values of actual parameter
will be copied to formal parameters
and these two different store value
in different location.

Int x= 10 ,y=20; int


fun(int x ,int y)
Fun(x,y);
Print(“x=%,y=%d”,x,y)
Int x= 10 ,y=20; int fun(int x ,int
Fun(x,y); y)
Print(“x=%,y= {
%d”,x,y)
X=20;
Y=10;
}
Output:
X=10 y=20

x y
xy 2 1
0 0
10 20

Call of reference

Int a=10, b=20; Int fun(int *p1,int


fun(&a,&b) *p1)
{
*p1=20;
Print(“x=%d,y= *p2=10;
%d”x,y);

}
Output:
X=20 , y=10

P1 p2
100 200
0 0
X y
20 10

#include<stdio.h>
Int main(void)
{
Int a[ ]={1,3,7,9,0,2,4,5,8,6};
Int length=10;
For(int i=0;i<length;i++)
{
For(j=0; j<(length-1);j++)
{
If(a[j]>a[j+1])
{
Temp= a[j];
A[j]=a[j+1];

A[j+1]=temp;
}
}
}
For(int i=0;i<length;i++)
Printf(“a[%d]=%d \n”,I,a[i]);
Return 0;
}

15 th july 2024

Insertion sort
Insertion sort arrange N elements
of array by inserting particular item
in a particular place such a way
that the item are in sorted order.
16 15 4 13 2 1
A[0] A[1] A[2] A[3]
A[4] A[5]
Max= 6
15<16 true
15 16 4 13 2 1
A[0] A[1] A[2] A[3]
A[4] A[5]

4< 15 true
4 15 16 13 2 1
A[0] A[1] A[2] A[3]
A[4] A[5]
13<4 false
13 <15 true (shift)
4 13 15 16 2 1
A[4]
2<4 true (shift)
2 4 13 15 16 1
A[5]
1< 2 true (shift)
1 2 4 13 15 16

Algorithm
Step 1: set i=1 set i=1
Step2: key= A[i] key=A[1]
key=15
Step3: set j=i-1 set j=i-1
j=1-1=0
Step4:if key< A[j] if
15<A[0]16
A[j+1]=A[j];
A[0+1]=A[0]A[1]=16
J=j-1;j=0-1=-1
Repeat step 2 until j>= 0
Step 5:A[j+1]=key; A[-
1+1]=15 A[0]=15
i=i+1;i=1+1=2
Step 6: Repeat step from 2 to 5 till
i<N
Step 7:stop

For(i=1;i<n;i++)
Insertion sort: {
Temp=a[i];
Sorted sub list J=i-1;
and unsorted While(j>=0 &&
sub list a[j]>temp)
Sorted unsorted
{
5 4 1 1 6 2
A[j+1]=a[j];
0
0 1 2 3 4
J--;
5 }
N=6 A[j+1]=temp;
4 5 10 1 6 2 }

Temp=4

4 5 10 1 6 2
0 1 2 3
4 5 Temp=10

1 4 5 10 6 2
0 1 2 3
4 5
Temp=1
1 4 5 6 10 2
Tem=6
1 2 4 5 6 10
Temp=2

#include<stdio.h>
Void main()
{
Int i , j,temp,a[10],n;
Printf(\n enter the number of
elements”);
Scanf(“%d”,&n);
Printf(“\n enter n element);
For(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
}
Insertion_sort(n,a);
Print(\n the sorted element are:”)
For(i=0;i<n;i++)
{
Printf(“%d”,a[i]);
}
}

Void insertion_sort(int n,int a[])


{
Int i,j,temp;
For(i=1;i<=n:i++)
{
Temp=a[i];
J=i-1;
While(temp<a[j] && j>=0)
{
A[j+1]=a[j];
J=j-1;
}
A[j+1]=temp;
}
}

18 july 2024
Quick sort:

ke
y
=pivot(any element from array,it
can be first element,last element or
random element
n
Partition piv Partition 2(right)
1(left) ot
Values <pivotpivot<value
Small
large

20th July 2024

Quick sort:

0 1 2 3 4 5 6 Partition:
7 8 9 1) i=low
2 4 3 9 1 4 8 7 5 6 2) j=hight
3) i++ until
Pivot
L S

2 4 3 9 1 4 8 7 5 6 element >
Pivotijj

i= large element i++ pivot is


j=small element j- - found
4) j—until
interchange1 &4, i>pivot, J<=pivot
element<=
2 1 3 9 4 4 8 7 5 6 pivot is
j i j found
5) swap a[i]
j<i &a[j] and
repeat step
1 2 3 9 4 4 8 7 5 4 and 5
6 until j<i
1, 2 {3,9,4,4,8,7,5,6} 6) swap pivot
1,2, {3,9,4,4,8,7,5,6}
I j & a[j].
1,2,3 {9,4,4,8,7,5,6}
I
i/j
1,2,3,{6,4,4,8,7,5},9}
|
|
|
1,2,3,4,4,5,6,7,8,9

23 july 2024

Merge sort (Divide, conquer and


combined)
42 84 75 20 60 10 5 30
0 1 2 3 4
5 6 7
42 84 75 20 60 10 5 30

LB=0 n=8 0+8=8/2=4


4 8 7 2 6 1 5 3
2 4 5 0 0 0 0
4 8 7 2 6 1 5 3
2 4 5 0 0 0 0

42 84 20 75 10
60 5 30
20 42 75 84 5
10 30 60
5 10 20 30 42 60 75 84

4,2,6,1,8,3

1,8,3
4,2,6

4,2 6 1,8
3

4 2 1 8
2 4 6 18
3
246 138
123468

25 July 2024
Searching
Searching is a process to finding an
element within the list of an
element stores in any order.

Searching

Linear Search(Sequential Binary Search


Search)

Linear Search:
2 3 5 1 6 7

6
Binary Search:
1 2 3 5 6 7
Pass 1pass2
Mid = 0+5/2=2.5

Linear Search :
We access each element of an
array/list one by one sequentially
and see whether it is desired
element or not.
2 4 6 3 1 5
0 1 2 3
4 5
Find: x=3, given n=6

Algorithm 1) i=0 x=3


1) i=0 if 0>6 (false)
2) if i>n goto step 7 if a[i]=3= 2=3
3) if A[i]=x goto 6 false
4) i=i+1 i=0+1=1
5) go to step 2 2) if 1>6
6) print element x a[1]=34=3
fount at I th pos i=1+1=2
7) print element not 3) if 2>6
found a[i]=3=6=3
8) exit i=2+1=3
4) If 3>6
A[i]=33=3
true
3 3

Binary Search :
This search technique the given
item in minimum possible
comparison.In this searching first
we had to sort array element then
following operation perform
1 first find the middle element of
the array.
2compare the mid element with
an item.
3There are three cases
a) if it is desired element then
search is
complete .
b) if its less than then desired
item then search only the
first half of th array.
c) If its greater than the desired
item then search in the
second half of the array.
Pass-1Midpass-2
2 3 4 5 6 7
0 1 2 3
4 5
Mid=[(0+5)/2]=5/2=[2.5]=2
Search=6

Algorithm: 2 3 5 8 9
1) Set beg=LB and 0 1 23
end=UP and 4
mid=int(beg+en
Beg=0 end=4 mid=
d/2)
2) Repeat step 3 0+4/2=2
and 4 while 0<=4 and a[2]!=85!
beg<=end and =8 true
a[mid]!=item 8<5 false
3) If item<a[mid] Else
then
Set end=mid-1 Beg=2+1=3
Else End=4
Set beg=mid+1
Mid=int
4) set
mid=int(beg+en (3+4/2)=7/2=int(3.5)
d/2) =3
Goto step 2 A[mid]!=item
5) If a[mid]=item
A[3]!=8
then
Set loc=mid 8!=8 flase
Else 88
Set loc=null
6)exit

// write a c program for Linear


Search
# include<stdio.h>
int linear_search( int a[],int n ,int
item);
int main()
{
Int a[10],item,c ,n,position;
Printf(“enter the no of element\n”);
Scanf((“%d”,&n);
Printf(enter %d number \n”,n);
For(i=0;i<n;i++)
Scanf(“%d”,&a[i]);
Printf(“enter no to search\n”);
Scanf(“%d”,&item);
Position=linear_search(a,n,item);
If(position==-1)
Printf(“%d is not present in array \
n,item);
Else
Printf(“%d is present at location
%d\n,item,position+1 );
Return 0;
}
Int linear_search(int a[],int n,int item)
{
Int i;
For(i=0;i<n;i++)
{
If (a[i]==item)
Return i;
}
Return -1;
}

Date :30/07/24
Chap 3: Linked List

Def:
A linked list or one-way list is a
linear collection of data elements
call nodes.Each node contain two
parts
1) Data
2) Successor

Array/list
1 3 6 8

Link list
1

3
Characteristics
1) Place element anywhere in the
memory
2) Dynamic allocation
3) Insertion and deletion od data
not required any data element ,
4) Each element is a collection of
data and successor.
5) Linked needs pointers and
dynamic memory allocation.
6) Required Head node .

7) Two field
Data field and pointer field.
8) Data field consist of information.
9) The link list must always have at
least one pointer pointing to first
node call head
10) Head=null
11) Pointer of the last node node of
the linked list contain a value null.
12) Every node must contain at least
one link field.

Primitive operation
a) Create
b) Traverse.
c) Insert node
d) Delete node
e) Searching a node.
f) Update a node.
g) Counting length.
h) Reverse the linked list.
i) Sort the linked list
j) Concatenate two linked list.
k) Merge two sorted linked list
31/07/24
Types of Linked list
1) Single linked list: Navigation
forwards only
2) Doubly: forwards and backward
navigation
3) Circular linked list: last element is
linked to the first element.

What is Single linked list ?


A single linked list is a list made up of
nodes that consist of two part:
a) Data b) Link

Data Link
Node
Representation SLL
Head
10
0

23 20 54 30 78 40 9 NUL
0 0 0 0 L
100 200 300
400

Create Single Linked List

Self referential Structure : its is structure


which contain a pointer to structure of
the same type
Struct abc
{
Int a;
Char b;
Struct abc *self
};

Node Representation in C
Node & link/successor/next
Data Link

Exam: Syntax:
Struct node Struct node
{ {
Int data; Data_type member1;
Struct node *link; Data_type member2;
}: .
.
.
.
.
Struct node *link;
}

// write a c program to create node using single Linked list


# include <stdio.h>
#include <stdlib.h>
Struct node
{
Int data;
Struct node *link;
};
int main()
{
Head
100

Struct node *head= null;


Head=(struct node *)malloc(size of( Struct node));
Headdata=45; node
4 nul
5 l
100
Headlink=null

Printf(“%d”,headdata);
Return 0;
}

Output
45

Create single linked list

Struct node
{
Int data;
Struct node *link;
};
Method 1
Int main()
{
Struct node *head=malloc(size of(struct node));
Headdata=45;
Headlink=null;
Struct node *current=malloc(size of(struct node));
currentdata=98;
currentlink=null;
Headlink=current;
Struct node *current1=malloc(size of(struct node));
Current1data=3;
Currentlink=null;
Currentlink=current1;
Return 0;
}
Method 2

Two methods to add 3rd node before we need to understand following


concenpt

Method -1

Method-2
#include <stdio.h>
#include<stdlib.h>
Struct node
{
Int data;
Struct node *next;
};
Int main();
Int i, item, n;
Struct node *head,*p,*q;
Printf(“enter the no of node”);
Scanf(“%d”, &n);
Printf(“enter the value of the head node”);
Scanf(“%d”,&item);

q=(struct node *)malloc(sizeof(struct node);


qdata=item;
qnext=null;
head=q;
p=head;

for(i=1;i<n;i++)
{
Printf(“enter the value of next node”);
Scanf(“%d”,&item);

q=(struct node *)malloc(sizeof(struct node);


qdata=item;
qnext=null;
pnext=q; //link node
p=pnext; //jump to current node
}
While(p!=null)
{
Printf(“%d”,pdata);
P=pnext;
}
Return 0;
}

Date :10 aug 2024


Operation on Linked List
1) Create
2) Insert a node
3) Delete a node
4) Traverse/Display a listed list
5) Reverse a
6) Concatenation
7) Length
8) Search
9) Sort
10) Merge

3) Delete a node from the linked


list
a) Deletion at the front of
the list (first position)
b) Deletion at the end of
the list(last position)
c) Deletion at any position
in the list (middle of
list)

3.a Deletion at the front of the list


3.b Deletion at the end of the
list(last position)
3.c Deletion at any position in the
list (middle of list)

4) Traverse/Display a listed list


Void display(struct node
*head)
{
Struct node *Ptr=head;
While(ptr!=null)
{
Printf(“%”,ptrdata);
Ptr=ptrnext;
}

5) Concatenation of linked list


Struct node *concatenate(struct
node *x,Struct node *Y)
Struct node concatenate(struct
node *X,Struct node *Y)
{
While (Xlink!=NULL)
{
X=Xlink;
}
Xlink=Y;
Return(x);
}

6) Length of linked list

Int length(struct node *head)


{
Int cnt=0;
While(head!=null)
{
Cnt++;
Head=headlinK;
}
Return cnt;
}

// to implement single linked


list
Create();
Display();
Insert();
Delete();

Int main()
{
Struct node *head=null;
Int n,ch,pos,
}

Date: 14 aug 2024


Doubly linked list
Operation
1) create
2) insert
3) Delete
4) Traversing
1) Create :
a) Create a new node
b) Add newly created node at
the end
i) If empty list
ii) If doubly linked list is
not empty .

Struct node
{
Struct node * prev;
Int data;
Struct node *next;
}
Int main()
{
Struct
node*head=malloc(siz
eof(struct node));
Headprev=null;
Headdata=10;
Headnext=null;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy