# DATA STRUCTURESx - Vidyavahini.org

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

1

|
P a g e

Data Structures

Chapter 1

DEFINITION

“The logical or mathematical model of a particular organization of data is called
Data Structure.

[OR]

“The collection of data elements whose organization is characterized by
accessing functions that are used to store and retrieve individual data elements is
known as
Data Structure.

[O
R]

“Arranging the data elements in some particular order is known as
Data
Structure.

GOALS OF DATA STRUCTURE

1.

Correctness

2.

Efficiency

3.

Robustness

4.

Adoptability

5.

Reusability

CORRECTNESS
:

A Data Structure is designed to work correctly for all possible
inputs.

EFFICIENCY
:
Use of efficient memory space

ROBUSTNESS

A program produces the correct output for all inputs.

ADOPTABILITY

Use the modern software projects to increase in CPU speed.

REUSABILITY

The same code be a component of different systems in various

operation situations.

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

2

|
P a g e

CLASSIFICATION OF DATA STRUCTURES

Data Structure can be classified into two categories:

1.

Primitive Data Structure

2.

Non
-
Primitive Data Structure

PRIMITIVE DATA STRUCTURE

“Data Structure that
is
directly
operates

on by machine level instructions.”

Ex: int, float, char, double and pointer.

NON
-
PRIMITIVE DATA STRUCTURE

Data Structure that
is

not

directly
operates

on

by the machine level instructions.”

Ex: arrays, stacks, queues, linked list, trees and graphs.

Non
-
P
rimitive Data Structure is further classified into
two types:

1.

Linear Data Structure

2.

Non
-
Linear Data Structure

LINEAR DATA STRUCTURE

The adjacency relationship between the elements is known as Linear Data
Structure.[The sequential arrangement of data is
known as Linear Data Structure]

Ex: arrays, strings, stacks, queues, linked list.

NON
-
LINEAR DATA STRUCTURE

There is no adjacency relationship between the data elements is known as Non
-
Linear Data Structure.

[Other than adjacency relationship between the
elements or non
-
sequential
arrangement of data is known as Non
-
Linear Data Structure]

DATA STRUCTURE

PRIMITIVE DATA STRUCTURE

EX: Int, float, char,
double,pointer

NON
-
PRIMITIVE DATA STRUCTURE

LINEAR DATA STRUCTURE

EX: Arrays,
Strings,Stacks,Queues,Linked list

NON
-
LINEAR DATA
STRUCTURE

EX: Trees and Graphs

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

3

|
P a g e

OPERATIONS ON PRIMITIVE DATA STRUCTURE

1.

Creation

2.

Selection

3.

Update

4.

Destroy

CREATION

Used to create a storage representation for a particular Data
Structure

Ex: int a=100;

a

variable name

Value

1000

address

SELECTION

U獥s⁴漠occ敳e⁤慴a⁷i瑨t渠a⁳瑲畣瑵t攠

䕸㨠

(“%d”,&a);

UPDATE

Used to modify the data in a s
tructure
.

Ex: a=200;

Destroy

U獥s⁴漠d敳瑲潹⁰慲瑩c畬ar⁤慴a⁦ 潭⁩瑳⁳瑯tag攠牥er敳e湴n瑩潮o

䕸E

-
P剉R䥔䥖䔠䑁I䄠協剕䍔C剅

Traversing

2.

Sorting

3.

Merging

4.

Searching

5.

Insertion

6.

Deletion

TRAVERSING

-
arra湧n湧n潦⁤ata⁥ 敭敮瑳e

M䕒䝉NG

䍯浢ini湧n潦⁴睯o潲⁭潲攠 ata⁥ 敭敮瑳e

Fi湤i湧n瑨攠灡牴tc畬ar⁤慴a⁥ 敭敮琮

䥎卅ST䥏N

䥮獥I琠a⁮敷⁥ e浥湴mi湴漠nh攠獴e畣瑵t攮

䑅D䕔E低

100

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

4

|
P a g e

POINTERS AND DYNAMIC MEMORY ALLOCATION

CHAPTER 2

POINTER

A pointer is a variable which holds the address of another variable of same data
type.

Syntax:

data type * variable name;

Ex: int *ptr;

There are two pointer operators

1.

*

Asterisk

operator used to store the address of another variable.

2.

&

Address operator gives the address of a variable.

Note :
All types of pointer variables occupies only two bytes of memory.

POINTER
-
TO
-
POINTER
:

A pointer variable holds the address of another
pointer variable.

Ex: int a=10;

int

*ptr;

p
tr=&a;

An example

program for
p
ointer
-
to
-
pointer.

#include<stdio.h>

#include<conio.h>

void

main( )

{

i
nt a, *ptr;

f
loat b, *fptr;

c
har c, *cptr;

c
lrscr( );

a=10;

b=20.22;

c=’A’;

iptr
=&a;

fptr
=&b;

cptr
=&c;

printf(“
\
n address of a=%u”,iptr);

1000

10

a

1001

1002

1003

1004

1000

ptr

1005

1006

1007

1008

1009

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

5

|
P a g e

printf(“
\
n address of b=%u”,fptr);

printf(“
\
n address of c=%u”,cptr);

printf(“
\
n value of a=%u”,*iptr);

printf(“
\
n value of b=%u”,*fptr);

printf(“
\
n value of c=%u”,*cptr);

getch( );

}

1.

Write a program to swap two variable using call by address

#include<stdio.h>

#include<conio.h>

void

swap (int *,int *);

void

main ()

{

i
nt

a,b;

clrscr();

printf

(“
\
n enter two numbers”);

scanf
(“%d%d”,&a,&b);

S
wap
(&a,&b)

printf
(“swapping of two number= 5d%d”,a,b);

printf
(“value of a= %d”, a);

printf
(“value of b= %d”, b);

getch();

}

void

swap ( int *m,int *n)

{

int

temp= *m;

*m=*n;

*n=temp;

}

Output
:

enter two
numbers:
100200

Value of
a=200
Value of
b=100

STATIC MEMORY ALLOCATION:

C
reation of storage space in memory for a variable at compile time
is
called static memory
allocations.

[
we can’t

change the memory blocks at run time
]

E
x:
char name;

1000 1001 1002
-

-

-

-

-

-

-

-

-

--

--

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-
1009

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

6

|
P a g e

The variable name allocates 10bytes of memory and it can not increase or decrease memory
space at run time.

DYNAMIC MEMORY ALLOCATION

Creation of space for a variable during run time is known as dynamic memory allocations.

[W
ecan increase or decrease
,

create or delete memory space at run time)

MEMORY ALLOCATION FUNCTION

Creation of memory block at run time there are 3 standard library f
unctions under alloc.h
headerfile will be
available

and so de
-
all
o
cate(delete) the memory space we can use one
standard libraryfunction under alloc.h header file.

There are 3 memory allocation function
s:

1.

Malloc( )

The malloc function reserve a block of
memory of specified size and returns
a pointer to the first byte of allocated space.

Syntax:
malloc()ptr=(data type*)malloc(size of(data type));

Ex:
ptr=(int *)malloc(size of(int));

2.

Calloc( )

Copy allocation is another memory creation function that is norm
ally used to
create multiple storage space.

Syntax:
ptr=(data type *)calloc(n,(size of(
data type
)));

Ex:
ptr=(student *)calloc(10,(size of(
data type
)));

struct student

{

int

regno;

Char name;

};

Student s;

Block 1

Block2

Block3

Block12

←12
bytes→

12 bytes

12 bytes

…………………………………

12bytes

3.

Realloc( )

Memory allocated used malloc or calloc might be in efficient of or
exceeds

we can change the memory size with help of realloc()Function.

Syntax:
ptr=re
a
lloc(ptr,new
size);

Ex:
ptr=(int *)calloc(10,size of (int ));

There is memory de
-
allocation function called
free()

While

creating a memory block at run time, it over respond to release or delete the memory
space when it not required.

Syntax:
free(pointer variable);

Ex:
Free(ptr);

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

7

|
P a g e

RECURSION

CHAPTER 3

Defn.:
“A function call itself again and again up to certain condition is called recursion”.

Syntax:

f
unction()

{

if
(condition)

return;

.

.

function();

}

1.

Write a program to find the factorial of a given
number without recursion

#include<stdio.h>

#include<conio.h>

void

main()

{

int

i,n,fact=1;

clrscr();

printf(“enter the number”);

scanf(“%d”,&n);

for(i=n;i>=1:i++)

{

fact=fact*I;

printf(“factorial= %d”,fact);

}

getch();

}

2.

Write a program to find
factorial of given number using recursion

#include<stdio.h>

#include<conio.h>

int

fact(int);

void

main()

{

int n,f;

clrscr();

printf(“enter the number”);

f=fact(n);

printf(“factorial = %d “,f);

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

8

|
P a g e

}

int

fact(int n)

{

if
(n==0)

return(1);

else

return(n*fact(n
-
1));

}

3.

Write a program to generate n
Fibonacci

series using recursion

#include<stdio.h>

#include<conio.h>

int

fib(int);

void

main()

{

int

n,I;

clrscr();

printf
(“enter the number’);

scanf
(“%d”,&n);

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

printf
(“%d”,fib(1));

getch();

}

int

fib(
int
k)

{

if
(k==1)

return
(0);

else

if(k==2)

return(1);

else

return(fib(k
-
1)+fib(k
-
2));

}

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

9

|
P a g e

TOWER OF H
A
NOI PROBLEM

It is introduced by
Hanoi
, begins

with the 3
poles

and n disks where all disks place
d on
left

most pole (stand A). The

3 disks are

of

different sizes and stored in increasing order from
top to bottom in stand

A
.

The
problem is to find the number move all the day from l
eft stand to right stand (stand
c) we
have to follows 3 rules.

1.

O
nly
one
diskcan

be moved at a time (count increase by 1)

2.
O
nly top disk on any stand
can

be moved to any other stand.

3.

The

larger disks can’t be placed on a smaller one.

In g
eneral the solution requires

2
n

moves for ‘n’ disks.

for

1

disk the minimum number

of
moves=2
1
-
1=1move.

for

2

disks the minimum
number

of moves=2
2
-
1=3moves.

for

3

disks the minimum
number

of moves=2
3
-
1=7moves.

for

4

disks the minimum
number

of moves=2
4
-
1=15moves.

for

5

disks the minimum
number

of moves=2
5
-
1=31moves.

Ex:

for 3 disks

A to C

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

10

|
P a g e

A to B

C to B

A to C

B to A

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

11

|
P a g e

B to C

A to C

Number
of steps = Step 1: A

C

Step 2: A

B

Step 3: C

B

Step 4: A

C

Step 5: B

A

Step 6: B

C

Step 7: A

C

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

12

|
P a g e

LAB PROGRAM 1:

Write a program to working of Hanoi

problem for n disks print the total number moves taken
by programs

#include<stdio.h>

#include<conio.h>

void

tower(int,char,char,char);

int

count=0;

void

main()

{

int

n;

clrscr();

printf
(“enter
number

of

disks”);

scanf
(“%d”,&n);

t
ower(n,’A’,’B’,’C’);

printf
(“
\
n n
umber

of moves

=%d”,count);

getch();

}

void

tower(int n,charSA,charSB,charSC)

{

if
(n<=0)

printf
(“
\
n no
-
disks on the stand”);

else

if
(n==1)

printf
(“
\
n move disk from %c to %c”,SA,SC);

Count++
;

else

{

t
ower(n
-
1,SA,SC,SB);

t
ower(1,SA,SB,SC);

t
ower(n
-
1,SB,SA,SC);

}

}

A program

to find the greatest common divisor of given 2 no using recursive function

#include<stdio.h>

#include<conio.h>

void

main()

{

i
nt a,b;

c
lrscr();

p
rintf(“enter two
number
s”);

scanf
(“%d%d”,&a,&b);

p
rintf(“gcd=%d”,gcd(a,b));

g
etch();

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

13

|
P a g e

}

int

gcd(int a,int b)

{

if
(a==b)

return
(a);

else

if
(a>b)

retur
n
(gcd(a
-
b,b));

else

return
(gcd(a,b
-
a));

}

LAB PROGRAM 2

A program

to find binomial co
-
efficient

nc
r
=(n!)/(n
-
r)!r!

#include<stdio.h>

#include<conio.h>

int fact(int);

void main()

{

int r,n,ans;

clrscr();

printf("
\
n enter n and r values : ");

scanf("%d%d",&n,&r);

ans=fact(n)/(fact(n
-
r)*fact(r));

printf("
\
n ncr=%d",ans);

getch();

}

int fact(int m)

{

if(m==0)

return(1);

else

return(m*fact(m
-
1));

}

PASCAL’S TRIANGLE

1

1 1

1 2 1

1

3 3 1

1 4 6 4 1

1 5 10 10

5

1

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

14

|
P a g e

#include<stdio.h>

#include<conio.h>

int

fact(int);

void

main()

{

int

n,r,ans;

clrscr();

printf
(“
\
n enter nand r values:”);

scanf
(“%d%d”,&n,&r);

if
(n<r)

printf(“Invalid Input”);

else

{

a
ns=(fact(n)/fact(n
-
r)*fact(r));

printf
(“
\
n nc
r
=%d”,ans);

}

getch();

}

int

fact(int m)

{

if
(m==0)

return
(1);

else

return
(m*fact(m
-
1));

}

Advantages of recursion:

1.

U
sed in divide and conquer method of problem solving.

2.

W
e can solve problem in easy way(ex: tower of Hanoi problem in easy, you can
reduce the
size of coding using recursion)

3.

R
ecursive program are simple

Disadvantages of recursion:

1.

R
ecursive solution is always logical and it

is

very difficult to trace.

2.

R
ecursive calling increase
s

the space complexity.

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

15

|
P a g e

SEARCHING

CHAPTER 4

Definition:

The process of finding the location of the element with a given key value in a
particular data structure.

There are two types of searching techniques,

1.Linear search.

2.Binary search.

1.Linear Search[Sequential Search]:

Linear search is the simplest form of searching technique. The data are organized
in an array form 1
st

position to nth position .The key element is the search element
to starts search form first position to nth position one by one.

if

the key element is
found in the array then it will be called as successful search
and prints the position of the element found.

if

the key element is not found in the array then it will be called as unsuccessful
search.

a

0

1

2

3

4 5

a. Key=10

key element found at 5
th

position.

b. if key = 100

key element not found.

Efficiency of linear search:

1.Best case:
(1)

2.Average case
(log2n)
-
1

3. Worst case: 0(n)

Algorithm for linear searching:

L search(A,key,n)

Where
A=Array

Key=key element to search

N=no. Of elements in array

Step1: loc=
-
1

Step2: for i=0 to i<n

if
(key==a[i])

Loc=(i+ 1)

50

40

20

5

15

30

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

16

|
P a g e

End if

End for

Step3: if(loc==
-
1)

Print “element not found”

else

Print “element is found at “,loc;

End if

Exit.

Lab program
3

A program to perform linear search

#include<stdio.h>

#include<cnoio.h>

int

lsearch(int*,int, int);

void

main()

{

int

a,key,
i
,n
,pos
;

clrscr();

printf
(“
\
n enter n value”);

scanf
(“%d”,&n);

printf
(“
\
n enter array elements”);

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

scanf
(“%d”,&a[i]);

printf
(“
\
n enter key elements”);

scanf
(“%d”,&key);

p
os=lsearch(a,key,n);

if
(pos==
-
1)

Prinf(“
\
n key element is not found”);

else

printf(“
\
n key element is found at %d position”,pos);

getch();

}

int

lsearch(int a[],int key, int n)

{

in
t

I;

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

{

if
(key==a[i])

return
(i+1)

}

return
(
-
1);

}

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

17

|
P a g e

Binary search:

It is the efficient searching technique using divide and concord method . in
this search all the elements are first arranged in either ascending order or
descending

order in the array.

The key element is 1
st

composed with the middle element in the array . if it
is found then the search is successful and return the middle position, otherwise
again the key element whether lesser or greater than the middle element i
f key
element is lesser than the process in repeated again for the 1
st

half of the array.
if

the key element is greater than the process is repeated for the 2
nd

half of the array
,if key element is not found then it will be called us unsuccessful search.

This method is useful for large array

a

0

1

2

3

4

5

Low mid

high

Low=0

High=n
-
1

Mid=(low +high)/2

Efficiency of binary search:

1.Best case: Ω(1)

2.Average case:
(log2n)
-
1

3.Worst case:0(log2n)

Algorithm for binary search:

Bsearch (a,key,low,high)

A=array

Key=key element to search

Low=0

High=n
-
1

Step1:while(low<=high)

Do

Mid=(low+high)/2;

Step2:if(key==q[mid])

return
(mid+1);

else

if
(key<a[mid])

High=mid
-
1;

else

Low=mid+1;

End if

10

20

30

40

50

60

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

18

|
P a g e

End while

Step3:return(
-
1);

Step4:exit;

Lab program
4

A program to perform binary search

#include<stdio.h>

#include<conio.h>

int

bsearch(int*,int, int);

void

main()

{

int

a,key,I,n,low,high
,pos
;

clrscr();

printf
(“
\
n enter n value”);

scanf
(“%d”,&n);

printf
(“
\
n enter array elements”);

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

scanf
(“%d”,&a[i]);

printf
(“
\
n enter key elements”);

scanf
(“%d”,&key);

p
os=bsearch(a,key,n);

if
(pos==
-
1)

p
rinf(“
\
n key element is not found”);

else

printf(“
\
n key element is found at %d
position”,pos);

getch();

}

int

bsearch(int a[],int key, int n)

{

int

low,high,mid;

l
ow=0;

h
igh=n
-
1;

w
hile(low<=high)

{

m
=(low+high)/2;

if
(key==a[mid])

return
(mid+1);

else

if
(key<a[mid])

h
igh=mid
-
1;

else

l
ow=mid+1;

}

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

19

|
P a g e

return
(
-
1);

}

Difference between linear(sequential)search & binary search:

Linear(sequential)search

Binary search

1.search starts one by one form 1
st

to
nth element

1.search starts from middle
element

2.no sorting required in array[array
elements will be in any
order]

2.sorting required in
array[elements ordered in
ascending or descending]

3.used in simple array

3.used in large array

4.worst case in 0(n)

4.worst case in 0(log n)

5.linear search does not works
according to divide and concord
method

5.binary
search works according
to divide and concord method

[it works linear method].

Difference between recursion and iteration:

Recursion

Iteration

1.function calls it’s self again and
agai渮
=
1⹲数敡瑥tly= call猠 瑨攠 bl潣欠 潦o

=
O⹩琠

=
O⹩琠 畳攠 l敳e= 浥浯特m c潭oar攠 瑯t
r散畲獩潮s
=
P⹲散畲獩潮s畳畡lly⁳=潷敲o
=
P⹩瑥tati潮⁵o畡lly⁦=s瑥t.
=
Q⹲散畲獩s攠 c潤敳e 桡v攠 湯n l潯oing=

=
Q⹩瑥tati潮o 浵m琠 桡v攠 l潯oi湧=

=
5⹲散畲獩潮s 睯牫s= acc潲di湧n 瑯t
divid攠e湤=
c潮o潲d⁭整桯e.
=
5⹩琠 d潥o= 湯琠 睯牫= acc潲di湧n 瑯t
divid攠e湤⁣潮=潲d⁭整桯e‮=
=
S⹲散畲獩潮spr潶id敳⁳i浰l攠捯e攮
=
S⹩瑥tati潮⁰r潶id攠earg攠捯e攮
=
䕸㨠瑯睥r⁯映䡡湯=
=
=

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

20

|
P a g e

SORTING

CHAPTER 5

Definition:

Arranging the data elements in ascending or
descending or alphabetical
order is known as sorting.

Or

Arranging the data elements in required order.

Types of sorting:

1. Bubble

sort

2. Selection

sort

3. Merge

sort

4. Insertion

sort

5. Quick

sort

1.

Bubble Sort:

Bubble sort is the most popular sorting

technique because it is a very simple
to understand and implement. At each insertion the largest value moves like a
bobble to the top of the array. The bubble sort method is not efficient for larger
array.

if

number of elements n takes n
-
1 passes to sort

the n element.

Ex:

500 400 300 200 100

Pass
-
1: 400 500 300 200 100

400 300 500 200 100

400 300 200 500 100

400 300 200 100 500

Pass
-
2: 300 400 200 100 500

300 200 400 100 500

300 200 100

400 500

Pass
-
3: 200 300 100 400 500

200 100 300 400 500

Pass
-
5:

100 200 300 400 500

Algorithm:

Bubble sort(a[0,1,2,3……………….n
-
1])

Where A = array of n elements

Step1: for(pass=1;pass<n
-
1;pass++)

do

Step2:

for(j=0;j<
n
-
pass;j++)

do

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

21

|
P a g e

Step3:

if(a[j]>a[j+1)

then

Step4:

temp=a[j];

a[j]=a[j+1];

step5:

a[j+1]=temp;

Lab program

5

A program to perform bubble sort:

#include<stdio.h>

#include<conio.h>

void bsort(int*,int);

void main()

{

int a,n,I
;

clrscr();

printf(“
\
n enter n value”);

scanf(“%d”,&n);

printf(“
\
n enter array elements”);

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

scanf(“%d”,&a[i]);

bsort(a,n);

printf(“
\
n after sorted elements”);

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

printf(“
\
n %d”,a[i]);

getch();

}

void bsort(int
a[],intn)

{

int temp,i(pass),j;

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

{

for(j=0;j<n
-
I;j++)

{

if(a[j]>a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

}

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

22

|
P a g e

2.

Selection sort:

The name selection sort is the selection of element & placing in proper

position.

In selection sort 1
st

we will search smallest element in an array and inter
changing with the 1
st

element. Then search the 2
nd

element and inter change with
the 2
nd

element and continue this process until all elements are completed.

This algori
thm is not effect for larger array.

Ex:

38 47 24 42 17

Pass
-
1:
17

47 24 42 38

Pass
-
2: 17
24

47 42 38

Pass
-
3:17 24
38

42 47

Pass
-
4:17 24 38
42
47

Write a program to sort n elements using selection sort:

#include<stdio.h>

#include<conio.h>

void selection(int*,int);

void main()

{

int a,I,n;

clrscr();

printf(“
\
n enter n value”);

scanf(“%d”,&n);

printf(“
\
n enter array elements”);

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

scanf(“%d”,&a[i]);

selection(a,n);

printf(“
\
n
after sorted elements”);

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

printf(“
\
n %d”,a[i]);

getch();

}

void selection(int a[],int n);

{

int i,j,temp,loc,min;

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

{

for(j=i;j<n
-
I;j++)

{

m
in=a[j];

loc=1;

if(min>a[j+1])

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

23

|
P a g e

{

min=a[j+1];

}

}

temp=a[i];

a[i]
=a[loc];

a[loc]=temp;

}

}

3.

Insertion sort:

Insertion sort inserts each element in appropriate position. The 1
st
p
ass

starts
with comparison of 1
st

element and place the elements in appropriate position. In
2
nd

element is compared with 1
st

element and again the 1
st

element is compared
with 0
th

element and place the element in appropriate position and this contains up
to n
-
1 passes.

Ex:

a 50 40 30 20 10

pass
-
1: 40 50 30 20 10

pass
-
2: 40 30 50 20 10

30 40 50 20 10

Pass
-
3: 30 40 20 50 10

30 20 40 50 10

20 30 40 50 10

Pass
-
4: 20 30 40 50 10

20 30 40 10 50

20 30 10 40 50

20 10 30 40 50

10 20 30 40 50

Write a program to sort n elements using insertion sort:

#include<stdio.h>

#include<conio.h>

void
insert(int*,int);

void main()

{

int a,n,I;

clrscr();

printf(“
\
n enter n value”);

scanf(“%d”,&n);

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

24

|
P a g e

printf(“ enter the array elements”);

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

scanf(“%d”,&a[i]);

printf(“
\
n after sorted array”);

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

pritntf(“
\
n %d”,a[i]);

}

voi
d insertion(int a[],int n)

{

int I,j,max;

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

{

max=a[i];

fro(j=i
-
1;j>=0;j
--
)

{

if(max<a[j])

a[j+1]=a[j];

}

a[j+1]=max;

}

}

4.

Merge sort:

This sorting technique fallows the technique of divide and concord. The given
array elements are divided into two sub array’s.

A,A………A[n/2] and

A[n/2+1]………A[n]

Each sub array is recursively divided into smaller sub array until each array
single el
ement, then we sort each sub set and finally we combine all the sorted
subsets to obtain a solution to the entire problem.

Merge sort can be performed the following steps:

Divide the array into equal parts.

Recursively sort the left part elements.

Recursiv
ely sort the right part elements.

Merge the sorted left and right part elements into a single sorted array.

Ex:

From the above ex. n is the number of elements of array a.

Low=1
st

array index value(low=0,n=7)

High=last array index value (high=n
-
1,7
-
1=6)

Mid
=(low+high)/2=(0+6)/2=3

The left sub array elements.

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

25

|
P a g e

Lab program 6

A program to sort n elements using merge sort:

#include<stdio.h>

#include<conio.h>

void mergesort(int *,int,int);

void merge(int *,int,int);

void main()

{

int n,i,a;

clrscr();

printf("
\
nenter the no. of elements : ");

scanf("%d",&n);

printf("
\
nenter array elements
\
n");

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

scanf("%d",&a[i]);

getch();

}

void mergesort(int a[], int low, int high)

{

int mid;

if(low<high)

{

mid=(low+high)/2;

mergesort(a,low,mid);

mergesort(a,mid+1,high);

merge(a,low,high);

}

}

void mergesort(int a[],int low,int mid,int high)

{

int i=low,k=low,j=mid+1,t;

while((i<=mid) && (j<=high))

{

if(a[i]<a[j])

{

t[k]=a[j];

k++;

j++;

}

}

while(i<=mid)

{

t[k]=a[i];

k++;

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

26

|
P a g e

i++;

}

while(j<=high)

{

t[k]=a[j];

k++;

j++;

}

for(i=0;i<=k
-
1;i++)

a[i]=t[k];

}

5.

Quick sort

It is one of the best technique for a large set of data. This technique
works on the method of partitioning based on dividing the list into the sub
-
list until the sorting is completed.

In quick sort we assume that the first element as the ‘
pivot

element’

and the variable ‘i’ is placed next to the
pivot

element and the variable

‘j’ is
placed at the last element in the given array.

Steps to solve the quick sort

1.

Compare that i
th

element is less than
pivot

element.
if

it is true, increment i
by 1. Repeat the step 1 until the condition false.

if
(i<
pivot
)

I++;

2.

Compare that j
th

element is
greater

than
pivot

element.
if

the condition is
true, decrease the j by 1. Repeat the step 2 until the condition false.

if(j>
pivot
)

j
--
;

3.

if

i is less than j, swap a[i] and a[j]. else swap a[j] and
pivot

element.

if
(i<j)

a[i]

a[j];

else

a[j]

pivot
;

Lab program 7

A program to perform quick sort

#include<stdio.h>

#include<conio.h>

void quicksort(int*,int,int);

void main()

{

int a,i,n;

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

27

|
P a g e

clrscr();

printf("
\
nenter the number of elements :");

scanf("%d",&n);

printf("
\
n enter t
he elements
\
n");

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

scanf("%d",&a[i]);

quicksort(a,0,n
-
1);

printf("
\
n sorted elements are
\
n");

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

printf("%d",&a[i]);

getch();

}

void quicksort(int a[],int low,int high)

{

int i,j,temp,piv;

piv=a[low];

i=low+1;

j=high;

if(low<high)

{

while(1)

{

while((i<=high) && (a[i]<piv))

i++;

while(a[j]>piv)

j
--
;

if(i<j)

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

else

{

temp=a[low];

a[low]=a[j];

a[j]=temp;

break;

}

}

quicksort(a,low,j
-
1);

quicksort(a,j+1,high);

}

}

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

28

|
P a g e

STACKS

CHAPTER 6

The collection of homogeneous data elements where the insertion and
deletion operations takes place at one end.

“A stack is an ordered
collection of elements in which elements can be
inserted and deleted at one end called top of the stack and also called as ‘Last In
First Out [LIFO] structure.”

Stack containing

OPERATIONS ON STACKS

1.

Push

To insert an element into the stack.

2.

Pop

To remove an element from the stack.

3.

Display

To display the elements of the stack.

ARRAY IMPLEMENTATION OF STACKS

It is very simple and easy to
implement stacks using arrays. Stack will be
maintained by a linear array format.

We need to consider a variable ‘
Top

which keeps the position of the top most
element in the stack. This variable is set to
-
1 for an empty stack called as
underflow.

The co
ndition top=max
-
1 indicates that the stack is full and also called as
overflow
.

int

max=5;

int

stack;

Insertion

Deletion

Element 3

Element 2

Element 1

2

3

1

Top

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

29

|
P a g e

top=
-
1

stack

if
(top==
-
1)

printf
(“underflow(stack is empty));

stack

if
(top==max
-
1)

printf
(“stack overflow”);

Or

“stack is full”

Algorithm

int

stack[max]

this is an array of size max.

int

max

this is a variable of stack size.

Top

this is a variable which always point on the top most element.

Algorithm for Push

Push(stack, max, top, ele)

1.

[check for overflow]

if
(top==max
-
1)

Print “stack is overflow”

2.

else

{

3.

[increment the top]

Top++;

[Insert an element into stack]

4.

Stack[top]=ele;

}

5.

return

Algorithm for Pop

1.

[check for underflow]

if
(top==
-
1)

2.

Print “stack is underflow”

3.

else

{

4.

[print the deleted element]

Print stack[top];

5.

Top

}

6.

return
;

10

20

30

40

50

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

30

|
P a g e

Algorithm of Display

Display(stack, max, top)

1.

if
(top==
-
1)

Print “underflow”

2.

else

{

3.

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

4.

Print stack[i];

}

5.

return
;

Lab program 8

A program to implement stack operations using arrays and check for underflow
and overflow on stacks.

#include<stdio.h>

#include<conio.h>

# define max 5

int
stack[max];

int top=
-
1;

void push();

void pop();

void display();

void main()

{

int ch;

while(1)

{

clrscr();

printf("
\
n1.push");

printf("
\
n2.pop");

printf("
\
n3.display");

printf("
\
n4.exit");

printf("
\
nenter your choice : ");

scanf("%d",&ch);

switch(ch)

{

case 1:push(); break;

case 2:pop(); break;

case 3: display(); break;

case 4: exit(0);

default : printf("invalid choice");

}

}

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

31

|
P a g e

}

void push()

{

int ele;

if(top==max
-
1)

printf("stack is overflow");

else

{

printf("enter an element : ");

scanf("%d",&ele);

top++;

stack[top]=ele;

}

}

void pop()

{

int ele;

if(top==
-
1)

printf("stack is underflow");

else

{

ele=stack[top];

printf("deleted element is %d", ele);

top
--
;

}

}

void display()

{

int i;

if(top==
-
1)

printf("stack is underflow");

else

{

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

printf("%d",stack[i]);

}

}

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

32

|
P a g e

POLISH NOTATION

The great polish mathematician ‘
Jan Lukesiewich’

introduced a new technique
for the representation of arithmetic expressions.

“The process of placing operators before or after its operands is known as Polish
Notation.

Operand

a + b

o
perand

Operator

Polish nota
tion is classified into 3 types

1.

Infix notation

The operator is placed between the operand is known as infix notation.

Syntax:
<operand><operator><operand>

Ex:

A + B

2.

Prefix notation

The operation is placed before its operands is known as prefix notation.

Syntax:
<operator><operand><operand>

Ex:

+ A
B

3.

Postfix notation

The operator is placed after its operands is known as postfix notation.

Syntax:
<operand><operand><operator>

Ex:

A B

+

Opera
tor precedence

Three levels of precedence for the 5 binary operations

[+,
-
, *, /, ^]

1.

Highest level

^
[exponent]

2.

Middle level

*, /
[multiplication and division]

3.

Low level

+,
-
[addition and subtraction]

The order of equation must scan from left to right.

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

33

|
P a g e

Examples for prefix notation

1.

A+B = +AB

2.

A+B
-
C = (+AB)

C =
-
+ABC

3.

A^B+C*D = (^AB)+C*D

= (^AB)+(*CD)

= +^AB*CD

LAB PROGRAM 9

A program to convert valid infix expression into postfix notation

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define max 29

char stack[max];

int top=
-
1;

int pre(char);

void push(char);

char pop();

void main()

{

char infix[max],postfix[max],ch,ele;

int i,j=0;

clrscr();

printf("
\
n enter the valid infix expression
\
n");

scanf("%s",&infix)
;

push('#');

for(i=0;i<strlen(infix);i++)

{

ch=infix[i];

if(isalnum(ch))

{

postfix[i]=ch;

j++;

}

else

{

if(ch=='(')

push(ch);

else

if(ch==')')

{

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

{

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

34

|
P a g e

postfix[j]=pop();

j++;

}

ele=top;

}

else

{

while(pre(stack[top]>pre(ch)))

{

postfix[j]=pop();

j++;

}

push(ch);

}

}

}

while(stack[top]!='#');

{

postfix[j]=pop();

j++;

}

postfix[j]='
\
0';

printf("
\
npostfix expression : %s",postfix);

getch();

}

int pre(char ele)

{

switch(ele)

{

case '(':

case '#': return(0);

case '+':

case '
-
': return(1);

case '*':

case '/': return(2);

case '^': return(3);

}

}

void push(
char ele)

{

top++;

stack[top]=ele;

}

char pop()

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

35

|
P a g e

{

char ele;

ele=stack[top];

top
--
;

return(ele);

}

Applications of Stacks

1.

Stacks are used in Recursion

2.

Stacks are used in reversal of a string

3.

Stacks are used in polish notation

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

36

|
P a g e

QUEUES

CHAPTER 7

Queues is a linear list of element in which insertion can take place only at
one end called rear end and deletion can takes place at (other) another end is called
front end. It is also called as FIFO (first in first

out).

Or

Q
ueue is an ordered collection of element the element the element may be
inserted at one end called rear end and element deleted at one end called front end”.

Operation on queues

Insertion:

insert the element into the queue.

Delete:

delete the element into the queue.

Display:

display the element into the queue.

C
lassification or type of queues

Q
ueue

O
rdinary queue circular queue priori
ty queue double
E
nd
ed
Queue

Ordinary queue

Q
ueue is also called FIFO or FCFS(first come first serve). Queue is
maintained linear array and 2 variable called front and rear.

Front:

it contains the location of front element are element of the queue.

Rear:

it contains the location of last element of the queue.

Front end

rear end

0

1 2

3 4

dele
tion

insertion

Conditions:

1.

Front =
-
1

Rear=2

“Queue is empty or underflow”

0

1

2

3

4

Front=
-
1

Rear=
-
1

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

37

|
P a g e

2.

rear=max
-
1

“Queue is full or overflow”

10

20

30

40

50

0 1

2

3

4

30

40

50

0 1 2

3

4

Front=2

rear=4

“Hear also the queue full or overflow”

3.

front==rear

“Indicates only one element in the queue”

Queue

30

0

1

2 3

4

Front=2 rear=2

Algorithm:

Max=contains variable

Queue[max]=array of element

Front=
-
1=font end variable

Rear=
-
1=rear end variable

Algorithm insertion(queue,front,rear,ele)

1.

if(rear==ma
-
1)

2.

print ”queue is full or
overflow”

3.

else

{

4.

Rear++;

5.

Queue[rear]=ele;

6.

if
(front==
-
1)

Front=0;

}

Algorithm deletion(queue,front,rear)

1.

if
(rear==
-
1)

2.

Print”queue is empty or underflow”

3.

else

{

4.front++;

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

38

|
P a g e

5.if(front>rear)

{

Front=
-
1;

Rear=
-
1;

}

}

Algorithm display(queue,front,rear)

1.

if
(rear==
-
1)

2.

Print”queue is empty or underflow”

3.

else

{

4.

for
(i=front to rear)

5.

Print queue[i]

}

Lab program 10

Write a
program to implement queue operation using array

#include<stdio.h>

#include<conio.h>

#define max 5

int

queue[max];

int

front=
-
1;

int

rear=
-
1;

void

main()

{

int

ch;

clrscr();

While(1)

{

printf
(“
\
n 1.insert”);

printf
(“
\
n 2.del”);

printf
(“
\
n 3.display”);

printf
(“
\
n 4.exit”);

printf
(“
\
n enter your choice”);

scanf
(“%d”,&ch);

Switch(ch)

{

Case 1: insert();break;

Case 2;dele();break;

Case 3:display();break;

Case 4:exit(0);

Default : printf(“invalid choice”);

}

}

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

39

|
P a g e

}

void

insert()

{

int

ele;

if
(rear==max
-
1)

printf
(“queue is full or overflow”);

else

{

printf
(“enter an element”);

scanf
(“%d”,&ele);

Rear++;

Queue[rear]=ele;

if
(front==
-
1)

Front=0;

}

}

void

del()

{

int

ele;

if
(rear==
-
1)

printf
(“queue is empty or underflow”);

else

{

Ele=queue[front];

Front++;

printf
(“deleted element =%d”,ele);

if
(front>rear)

{

Front=
-
1;

Rear=
-
1;

}

}

}

}

void

display()

{

int

I;

if
(rear==
-
1)

printf
(“queue is empty or underflow”);

else

{

printf
(“front”);

for
(i=front;i<
=rear;i++)

{

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

40

|
P a g e

printf
(“%d”,queue[i]);

}

printf
(“rear”);

}

}

Circular queue
: In ordinary queue once the queue full

Even though they delete few element it gives queue is full or over we are able
to add
anymore elements decrease where reaches max=1.

Queue

30

40

50

0 1 2 3 4

front

rear

“Always it display the queue full”

To solve this problem an alternate techniques is implemented

“Once the
queue
is full rear again point to the first element of the queue and
insertion can be done at this position if the first position is free.

Algorithm insertion(queue[],front,rear
ele)

1.
if(front==0)&&(rear==max
-
1)||(rear==front
-
1)

2.
print”queue is full”

3. else

{

4.

if
(rear==
-
1)

{

Front=0;

Rear=0;

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

41

|
P a g e

}

5

else

6.
if
(rear==max
-
1)

Rear=0;

else

7.Rear++;

8.

Queue[rear]=else;

}

Algorithm deletion (queue[ ],front,rear)

1.

if
(front==
-
1)

Print”queue is empty”

2.

else

{

3.

if(front==rear)

{

Front==
-
1;

Rear==
-
1;

}

4. else

5. if(front==max
-
1)

Front=0;

6:

else

Front++;

7. }

}

Algorithm display(queue[ ],rear,front)

1.

if(front==
-
1)

Print”queue is empty”

2.

.else

{

3.

if(front>rear)

{

4.

for(i=front;i<max
-
1;i++)

Print”queue[i]

5.

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

Print”queue[i]”;

6.

}

else

{

7.

for(i=front;i<
rear;i++)

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

42

|
P a g e

}

Lab program 11

Write a program to perform circular queue operation using arrays

#include<stdio.h>

#include<conio.h>

#define max 5

int

cqueue[max]

int

front=
-
1;

int

rear=
-
1;

void

cqinsert();

void

cqdel();

void

cqdisplay();

void

main()

{

int

ch;

clrscr();

While(1)

{

printf
(“
\
n1. Insert”);

printf
(“
\
n2.del”);

printf
(“
\
n3.display”);

printf
(“
\
n4.exit”);

printf
(“
\
n enter your choice”);

scanf
(“%d”,&ch);

Switch(ch)

{

Case 1:cqinser();break;

Case 2:del();break;

Case 3:display();break;

Case
4:exit();break;

}

}

}

void

cqinsert()

{

int

ele;

if
((rear==max
-
1)&&(front==0)||(rear==front
-
1)

printf
(“
\
n circular queue is full”);

else

{

printf
(“
\
n enter the element”);

scanf
(“%d”,&ele);

if(rear==
-
1)

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

43

|
P a g e

{

f
ront=0;

r
ear=0;

}

else

if
(rear==max
-
1)

r
ear=0;

else

r
ear++;

c
queue[rear]=ele;

}

}

void

cqdel()

{

int

ele;

`
if
(front==
-
1)

printf
(“
\
n circular queue is empty”);

else

{

Ele=cqueue[front];

if
(front==rear)

{

Front=
-
1;

Rear=
-
1;

}

else

if
(front==max
-
1)

Front=0;

else

Front++;

}

printf
(“
\
n
deleted element =%d”,else);

}

void

cqdisplay()

{

int

I;

if
(front==
-
1)

printf
(“
\
n circular queue is empty”);

else

{

if
(front>rear)

{

for
(i=front;i<max
-
1;i++)

printf
(“%d”,cqueue[i]);

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

44

|
P a g e

}

else

for
(i=front;i<
=rear;i++)

printf
(“%d”,cqueue[i]);

}

}

Priority queue

“Priority queue is a collection of element such that each element can be inserted
according to the priority but the deletion takes place from the 1
st

priority 1
st

element to n
th

priority last element”.

Queue1

0

1

2

3

4

F1=
-
1,r1=
-
1 priority1

Queue2

0

1

2

3

4

F2=
-
1,r2=
-
1

priority2

Queue3

0

1

2

3

4

F3=
-
1,r3=
-
1

priority3

Each queue follows FIFO principal, the element insertion takes places according
into the priority but after completely deleting of 1
st

element then only the 2
nd

queue
element or deleting

takes places after completely 1
st

queue and 2
nd

queue then only
the 3
rd

queue element are possible to delete.

Write a program to perform queue operation using priority technique

#include<stdio.h>

#include<conio.h>

# define max 5

void
(int*,int*,int*);

int

qdel(
int
*,int*,int*);

int

display(int*,int*,int*);

void

main()

{

int
q1[max],q2[max],q3[max];

int

f1=
-
1,r=
-
1,f2=
-
1,r2=
-
1,f3=
-
1,r=
-
1;

int

ch,pre,ele,flag;

While(1)

{

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

45

|
P a g e

printf
(“
\
n 1.insert”);

printf
(“
\
n 2.del”);

printf
(“
\
n 3.display”);

printf
(“
\
n
4.exit”);

printf
(“
\
n enter your choice”);

scanf
(“%d”,&ch);

{

Case: printf(“
\
nenter the priority”);

scanf
(“%d”,&pre);

printf
(“
\
n enter the element”);

scanf
(“%d”,&ele);

if
(pre==1)

Qinsert(q1,&f1,&r1,ele);

if
(pre==2)

Qinsert(q2,&f2,&r2,ele);

if
(pre==3)

Qinsert(q3,&f3,&r3,ele);

else

printf
(“
\
n invalid priority”);

break
;

Case 2:falg=qdelet
e(q1,&f1,&r1);

if
(flag==1)

{

printf
(“
\
n element from first queue”);

}

else

{

printf
(“
\
n first queue is empty”);

Flag=qdelete(q2,&f2,&r2);

if
(flag==1)

printf
(“
\
n element from second queue”);

else

{

printf
(“
\
n element from second queue”);

Flag=qdelete(q3,&f3,&r3);

if(flag==1)

printf(“
\
n element from third queue”);

else

printf(“
\
n third queue is empty”);

}

}

break
;

Case 3:qd
isplay(q1,&f1,&r1);

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

46

|
P a g e

qdisplay(q2,&f2,&r2);

qdisplay(q3,&f3,&r3);

break;

case 4: exit(0);

}

}

}

void

qinsert(int q[ ],int *f,int *r,int ele)

{

if
(*r==max
-
1)

printf
(
\
n queue is full”);

else

{

if
(*r==
-
1)

{

*f=0;

*r=0;

}

else

*r=*r+1;

Q[*r]=ele;

}

}

int

qdelete(int q[ ],int *f,int *r)

{

if
(*f==
-
1)

return
(0);

if
(*f==*r)

printf
(“
\
n delete element %d”,q[*f]);

*f=
-
1;

*r=
-
1;

return
(1);

}

else

{

printf
(“delete
element :%d”,q[*f]);

*f=*f+1;

return
(1);

}

}

void qdisplay(
int
[ ],int*f,int *r)

{

int

I;

if
(*f==
-
1)

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

47

|
P a g e

printf
(“queue is empty”);

else

{

for
(i=*f;i<=*r;i++)

printf
(“%d”,q[i]);

}

}

Double ended queue (DE
queue):

“A
DE
queue a linear list in
which element can be inserted or deleted at
both through the end is
DE
queue”(Double
E
nded queue)

There are two types of DE
queue

1
. Input restricted DE
queue

2. O
utput restric
t
ed
DE
queue

Input restric
ted DE
queue
:

In

Input restricted
DE
queue insertion takes place only at
one
end bu
t deletion takes place at both

the ends.

Output restrict
ed
DE
queue:
In

Output restricted
DEqueue the element inserted at
both

the end
s

but deletion takes
place
at only one end.

Application of queue

1. Queues
are used in time sharing system in which program from
a queue while
waiting to be exec
uting.

2. C
ircular queue are used in operating system.

3. Q
ueue
s

are used
in
network communication system.

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

48

|
P a g e

LINKED LIST

CHAPTER 8

Disadvantages of

arrays

1.

We can’t increase the size of an array at run time.

2.

We can’t decrease the size of an array at run time.

[array size is fixed in advance]

3.

Deletion of an element fro the array is difficult.

4.

Insertion of an element into the array is difficult

[while inserting or deleting the element we can move all other elements
but it takes much time]

To overcome the above disadvantages the dynamic memory allocation
technique is introduced to manipulate the elements at run time.

“Linked list is a linear
collection of nodes containing the information part
and the link part.”

NODE

info

link

Information part

䥴 桯hd猠瑨攠sc瑵tl i湦潲浡瑩潮⁯映瑨攠湯摥o

Li湫⁰慲

䥴⁨ ld猠瑨攠sddr敳e⁯映瑨攠湥
x琠

䑥晩湩湧na⁮潤

{

i湴

i湦漻

N佄E

††††
i湦o

Cra瑩湧n瑨攠湯t攠e琠t畮瑩浥

††††† †† †
⡳瑲畣琠湯摥‪t浡ll潣⡳i穥潦⡳瑲畣琠湯摥t⤻

Mall潣⁦畮 瑩潮⁩猠畳ed⁴漠cr敡瑥⁴桥⁭敭hry⁢l潣欠kt⁲ 湴n浥m

䕸㨠

⩮敷渠*猠s⁰潩湴敲
variable⁷桩c栠桯hd猠瑨攠eddr敳e⁯映 湯瑨敲 湯n攮

Data Structures using ‘ C ’

3
rd

Sem BCA

By : Mr. Madhupriya, HOD of Comp. Science, VVFGC, Tumkur.

49

|
P a g e

Newn

(struct node*)malloc(sizeof(struct node))

Here the malloc function creates the memory block of the node using the size of
and return the address to the pointer variable newn.

NODE

info

next

1000

newn

Assigning values to the new node

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

Newn = info =10;

Newn

next = newn2;