1
1
B.C.A./B.Sc.IT-SEMESTER-2
SUB :- DS
1
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
2
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
o function calloc(n,size)
o function free(block)
Dangling pointer problem.
Structures.
Enumerated constants , Unions
2 Sorting and Bubble sorting
Searching Insertion sorting
Quick sorting
Bucket sorting
Merge sorting
Selection sorting
Shell sorting
Basic searching technique
Sequential searching
Binary searching
Graph Introduction
Adjacency matrix and adjacency lists
Graph traversal
o Depth first search (dfs)
o Implementation
o Breadth first search (bfs)
o Implementation
Shortest path problem
Minimal spanning tree
3 Introduction Introduction
To data Primitive and simple structures
Structure Linear andnonlinear structures file
organization.
Elementary Introduction
Data Structure Stack
o Definition
o Operations on stack
o Implementation of stacks using
arrays
3
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
4
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
5
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
7
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
8
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
3. Asymptotic notation
When we study algorithms, we are interested in characterizing them
according to their efficiency.
We are usually interesting in the order of growth of the running time
of an algorithm, not in the exact running time. This is also referred
to as the asymptotic notation.
We need to develop a way to talk about rate of growth of functions
so that we can compare algorithms.
Asymptotic notation gives us a method for classifying functions
according to their rate of growth
4. Classes of algorithm
By implementation way
i. It is further classified into following sub categories.
Recursion or Iteration
A recursive algorithm means that it invokes
itself repeatedly until a certain condition
matches.
An iterative algorithms use repetitive constructs
like loops and sometimes additional data
structures like stacks to solve the given
problems.
Logical
The logic component expresses the axioms
(maximum) that may be used in the computation
and the control component determines the way
in which deduction is applied to the axioms.
Serial or Parallel or Distributed
A computer which can execute on instruction of
an algorithm at a time is known as serial
computers. An algorithm designed for such an
environment is called a serial algorithm.
Deterministic of Non-Deterministic
Deterministic algorithms solve the problem with
exact decision at every step of the algorithm.
9
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Exact or Approximate
While many algorithms reach an exact solution,
approximation algorithms try to find an
approximation that is close to the true solution.
Quantum Algorithm
This runs on a realistic model of quantum
computation.
These algorithms use some essential feature of
quantum computation such as quantum
superposition or quantum embarrassing
situation.
By design paradigm
i. There is a number of paradigms which is different from each
other. Also, it will include many different types of algorithm.
Brute-force or exhaustive search
This is the natural method of trying every
possible solution to see which is best.
Divide and conquer
A divide and conquer algorithm repeatedly
reduces into smaller problems until the problems
are not enough to solve easily.
Dynamic programming
When a problem shows optimal substructure,
meaning the optimal solution to a problem can
be constructed form optimal solutions to sub
problems, and overlapping sub problems,
meaning the same sub problems are used to
solve many different problem instances, a
quicker approach called dynamic programming
avoids recomputing solutions that have already
been computed
The greedy method
A greedy algorithm is similar to a dynamic
programming algorithm, but the difference is
that solutions to the sub problems do not have to
be known at each stage, instead a “greedy”
10
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
11
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
12
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
5. Big-Oh Notation
“Big-O” notation was introduced in P.Bachmann’s 1892 book
Analytische Zahlentheorie.
The notation works well to compare algorithm efficiencies because
we want to say that the growth of effort of a given algorithm
approximates the shape of standard function.
Big –O is a characteristic scheme that measure properties of
algorithm complexity performance and/or memory requirements.
The algorithm complexity can be determined by elimination
constant factors in the analysis of algorithm.
Based on the complexity representation of the Big-O notation, the
algorithm can be categorized as:
i. Constant time O(1)
ii. Logarithmic time O(log n) and o(n log n)
iii. Linear time O(n)
iv. Polynomial time O(nc)
v. Exponential timeO(cn)/O(2n)
vi. Cubic timeo(n3)
vii. Quadratic timeO(n2)
13
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
6. Big-Omega Notation
Just as the big-OH notation provides an asymptotic way of saying
that a function is “less than or equal to” another function, the
following notations provide an asymptotic way of saying that a
function grows at a rate that is “greater than or equal to” that of
another.
Let f(n) and g(n) be functions mapping non-negative integers to real
numbers.
We say that f(n) is Ω (g(n)) (pronounce “f(n) is big-Omega of
g(n)” i.e. f(n)=Ω (g(n)) if g(n) is O(f(n)), if there is real constant
c>0 and an integer constant n0>1 such that
|f(n)|≥c|g(n)|,for n≥n0
If f(n) is non-negative, we can simplify the last condition to f(n)=(Ωg(n)) {there
exists positive constants such that
15
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
SUMMARY
16
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Introduction
Data types
Arrays
Handling arrays
o Initializing the arrays
Multidimensional arrays
o Initialization of two
dimensional array
Pointers
o Advantages and
disadvantages of
pointers
o Declaring and
initializing pointers
o Pointer arithmetic
Array of pointers
Passing parameters to the
functions
Relation between pointers and
arrays
Scope rules and storage classes
o Automatic variables
o Static variables
o External variables
o Register variable
Dynamic allocation and de-
allocation of memory
o function malloc(size)
o function calloc(n,size)
o function free(block)
Dangling pointer problem.
Structures.
Enumerated constants
Unions
17
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Data Types
• To store data in computer variable is require. Now, which type of value
we want to store in variable that is known by data type
• C language has three basic data types:
1. Fundamental/Primary data type
2. Derived data type
3. User define data type
Primary
data type
Floating
Integral
point
Long
Integer Character Float Double
Double
Short Long
Integer
Integer Integer
• Derived Data Type
• Derived data type are those which are provided by ‘C’ language to
us.
• In general we can say that we are using Array and Pointer which
are derived data type of C language.
• User Define Data Type
• C language provides facility to create our own new data type using
typedef
• We can create user define data type using following.
• Syntax: typedef <language data type><new data type>
• Example typedef int number;
• Here now we can create a variable of integer type by
using numeric keywords.
• Example: number a,b,sum;
18
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
SR NO QUESTION ANSWER
1 DATA TYPES ARE USED TO DECIDE TYPE OF DATA OR
______ VALUE
2 C LANGUAGE SUPPORT _________ , __________ & PRIMARY
___________ CATEGORIES OF DATA TYPE. USER
DEFI
NED
DERIVED
3 TYPEDEF MEANS ____________ TYPE
DEFI
NITI
ON
4 LIST OUR USER DEFINED DATA TYPES IN C. STRUCTU
RE
UNION
ENUM
5 INTEGER DATATYPE OCCUPIES _____ BYTES OF 2
MEMORY
6 FLOAT DATATYPE OCCUPIES _____ BYPTES OF 4
MEMORY
7 CHARACTER DATATYPE OCCUPIES ___ BYTE 1
OF MEMORY
19
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Arrays
20
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Syntax:
datatype arrayname[size];
• Here, while declaring array we require following three things.
• A data type is the types of elements that will contained in the array
like int, char, float, double etc.
• An arrayname is name of valid array variable name and it must
follow all the identifiers rules.
• An size represents the maximum number of elements that can be
stored inside the array
• Example : inta avg[10];
•
Compile time initialization of array elements
21
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Syntax:
datatype
arrayname[size]={val1,val2,val3,……};
• First way
int number[5]={1,4,6,7,3};
• Second way
int number[5]={1,2,3} //rest of elements will be set to zero
• Third way
int number[ ]={1,2,3,4,5}
• Fourth way
char nm[ ]={‘h’,’a’,’r’,’d’,’I’,’k’,’\0’};
Run time initialization of array elements
int a[5];
for(i=0;i<5;i++)
{
printf(“\n enter array elements:”);
scanf(“%d”,&a[i]);
}
Syntax:
datatype
arrayname[row_size][column_size];
• Here, while declaring array we require following three things.
• Initialization of array is done at compile time as well as run time.
• Int a[2][3]={0,1,2,0,1,2};
• Int a[2][3]={{0,1,2},{0,1,2}};
22
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
a[0][0] a[0][1]
0 a[0][2]
a[1][0] a[1][1]
1
RO a[1][2]
W a[2][0] a[2][1]
2 a[2][2]
column 0 column 1
column 2
MULTI-DIMENSIONAL ARRAY
• Multi dimensional array is also defined same like a one-dimensional
array, with a separate pair of square brackets for each subscript.
• It means that we know that two dimensional arrays will require tw0 pairs
of square brackets, so if we assume three dimensional arrays then it will
require three pairs of square brackets and so on.
Syntax:
datatype
arrayname[exp1][exp2]…….[expn];
• Example
• float table[5][5][5]; char page[2][2][2];
23
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Pointers
DEFINITION OF POINTER
• The variable that holds(stores) address of other variable are called
pointers.
• In other words pointer is simply variable that contains address, which is a
location of another simple or ordinary variable or array elements in
memory.
• A pointer is a variable that “points to” another variable by storing its
address.
24
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Array of Pointers
• As we know that pointer contains address, so an array of pointers would
be an collection of addresses.
• Syntax: datatype *array[subscript1];
datatype *array[subscript1][subscript2]….;
• Example: int *a[5];
int *arr[2][2];
Passing Parameters to the function
CALL BY VALUE CALL BY REFERENCE
This is the usual method to call a function In this method, the address of the
in which only the value of the variable is variable is passed as an argument
passed as an argument
Any alternation in the value of the Any alternation in the value of the
argument passed is local to the function argument passed is accepted in the
25
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
3 WHICH IS AN INDIRECTION *
OPERATOR?
4 WHICH DOES NOT INITIALIZE PTR TO INT *PTR = &A;
NULL (ASSUMING VARIABLE
DECLARATION OF A AS INT A=0;)?
26
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
27
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Example:
#include <stdio.h>
#include <conio.h>
28
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
printf("\n\t %d",i);
}
printf("\n\n\t %d",i);
getch();
}
Keyword : register
Storage Location : CPU Register
Initial Value : Garbage
Life : Local to the block in which variable is declared.
Scope : Local to the block.
Example: Output:
#include <stdio.h>
123456789
#include <conio.h>
10
void main()
{
register int i;
for(i=1;i<10;i++)
printf(“%d”,i);
getch();
}
29
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
back to the same function again, the static variables have the same values
they had last time around. By default, global variables are static variables.
Keyword : static
Storage Location : Main memory
Initial Value: Zero and can be initialize only once.
Life : depends on function calls and the whole application or
program.
Scope : Local to the block.
Example:
#include<stdio.h>
#include<conio.h> Output:
void print();
void main() value of static variable is 0
{ value of static variable is 1
clrscr();
print();
print();
getch();
}
void print()
{
static int k=0;
printf(“\n the value of static variable is %d”,k);
k++;
}
Keyword : extern
30
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
31
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
32
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Example:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *array;
int n,i;
clrscr();
for(i=0;i<n;i++)
{
printf(“\n enter array elements”);
scanf(“%d”,&array[i]);
}
for(i=0;i<n;i++)
{
printf(“\n array elements=%d”,array[i]);
}
getch();
}
2) calloc():
Description:
calloc () is also used to allocate the memory dynamically (run
time) but calloc() and malloc() have the following
differences:
33
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
malloc() calloc()
1) malloc() has only one 1) calloc() has two
argument. arguments.
2) malloc() allocates single 2) calloc() allocates multiple
block of storage space blocks of storage space.
3) malloc() gives the garbage 3) calloc() initializes the
value of memory memory to zero.
Syntax:
p=(int *)calloc(n,sizeof(int));
Header File: <stdlib.h>, <alloc.h>
Example showing the difference between malloc() and
calloc() :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *p1;
int *p2;
int n=5;
clrscr();
p1=(int *)malloc(n*sizeof(int));
p2=(int *)calloc(n,sizeof(int));
printf(“\n p1=%d”,*p1);
printf(“\n p2=%d”,*p2);
getch();
}
Output:
34
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
3) realloc():
Description:
realloc() is used to change the size of previously
allocated memory(malloc() or calloc()).
The new size of the memory can be smaller or
larger. If the size of memory is larger then it is
appended at the end of the data that is data will not
be changed but if the size of the memory is smaller
then the data will be changed. This is known as
reallocation of memory.
Syntax:
p=(int *)realloc(p,50);
void main()
{
char s[50];
char *msg;
clrscr();
printf(“\n enter the string”);
35
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
gets(s);
msg=(char *)malloc(strlen(s)+1);
strcpy(msg,s);
printf(“\n MESSAGE=%s”,msg);
printf(“\n”);
printf(“\n enter new string”);
gets(s);
msg=(char *)realloc(msg,(strlen(msg)+strlen(s)+1));
strcat(msg,s);
printf(“\n FINAL MESSAGE=%s”,msg);
getch();
}
Output:
4) free():
Description:
When memory is allocated using malloc() and calloc()
then that memory must be freed when the program
finishes using free() because the memory area is finite.
Syntax:
free(p1);
p1=NULL;
36
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
37
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
If any pointer is pointing the memory address of any variable but after some
variable has deleted from that memory location while pointer is still pointing
such memory location. Such pointer is known as dangling pointer and this
problem is known as dangling pointer problem.
Initially:
Later:
38
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Structure
Enumerated Constants
enum suit{
club=0;
diamonds=10;
hearts=20;
spades=3;
};
39
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Above code defines the type of the data but, no any variable is
created. Variable of type enum can be created as:
enum boolean{
false;
true;
};
enum boolean check;
#include<stdio.h>
enum week{ sunday, monday, tuesday, wednesday, thursday, friday,
saturday};
int main(){
enum week today;
today=wednesday;
printf("%d day",today+1);
return0;
}
Output
4 day
Unions
Unions are quite similar to the structures in C. Union is also a derived
type as structure. Union can be defined in same manner as structures just
the keyword used in defining union in union where keyword used in
defining structure was struct.
union car{
char name[50];
int price;
};
Union variables can be created in similar manner as structure variable.
union car{
40
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
char name[50];
int price;
}c1, c2, *c3;
OR;
union car{
char name[50];
int price;
};
-------Inside Function-----------
union car c1, c2, *c3;
In both cases, union variables c1, c2 and union pointer variable c3 of type union
car is created.
41
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
42
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
43
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
44
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Bubble sorting
Insertion sorting
Quick sorting
Bucket sorting
Merge sorting
Selection sorting
Shell sorting
Basic searching
technique
Sequential searching
Binary searching
Sorting
45
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Bubble sort
46
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
47
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
{
printf("\nEnter array elements a[%d]:",i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
changes=0;
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
changes++;
}
}
if(changes==0)
break;
printf("\nAfter pass %d elements are:",i+1);
for(k=0;k<n;k++)
printf("\t%d",a[k]);
printf("\n");
}
printf("\nAfter sorting array elements are:");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
48
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
49
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Insertion sort
50
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
51
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
printf("\t%d",a[k]);
printf("\n");
}
printf("\n\nSorted array");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
getch();
}
Quick sort
53
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
54
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
55
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
56
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Bucket sort
57
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
58
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
{
bucktcnt[j]=0;
j++;
}
j=0;
while(j<n)
{
r=(a[j]/divsr)%10;
buckt[r][bucktcnt[r]]=a[j];
bucktcnt[r]++;
j++;
}
/*collect all elements in order*/
j=0;
p=0;
while(j<10)
{
k=0;
while(k<bucktcnt[j])
{
a[p]=buckt[j][k];
p++;
k++;
}
j++;
}
i++;
divsr=divsr*10;
}
printf("\nSorted Elements:");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
getch();
}
59
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
60
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Selection sort
61
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
62
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
min=j;
}
if(min!=i)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
printf("\n\nSorted array:");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
getch();
}
63
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Merge sort
64
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
65
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
66
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
m_sort(low,mid);
m_sort(mid+1,high);
merge_sort(low,mid,high);
}
}
void merge_sort(int low,int mid,int high)
{
int i,j,k,temp[100];
i=low;
j=mid+1;
k=low;
do
{
if(a[i]>=a[j])
temp[k++]=a[j++];
else
temp[k++]=a[i++];
}while((i<=mid)&&(j<=high));
while(i<=mid)
temp[k++]=a[i++];
while(j<=high)
temp[k++]=a[j++];
for(i=low;i<=high;i++)
{
a[i]=temp[i];
}
}
67
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
68
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Shell sort
69
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
clrscr();
printf("\nEnter array size:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter array elements a[%d]:",i);
scanf("%d",&a[i]);
}
printf("\nEnter maximum number (odd value):");
scanf("%d",&num);
while(num>=1)
{
for(j=num;j<n;j++)
{
k=a[j];
for(i=j-num;i>=0 && k<a[i];i=i-num)
a[i+num]=a[i];
a[i+num]=k;
}
printf("\nIncrement=%d\n",num);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n");
num=num-2;
}
printf("\nSorted array");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
70
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
71
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
SEARCHING METHOD
1. Sequential searching
72
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
73
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Binary searching
int a[100],n,i,ele,flag=0;
clrscr();
printf("\nEnter array size:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter array element a[%d]:",i);
scanf("%d",&a[i]);
if(i>0)
{
if(a[i-1]>a[i])
{
printf("\nnumber should be greater than
previous value");
getch();
break;
} }}
printf("\nEnter element to be searched");
scanf("%d",&ele);
for(i=0;i<n;i++)
{
if(a[i]==ele)
{
printf("\nYour element %d is at position %d",ele,i);
flag==1;
}}
if(flag==0)
printf("\nElement not found");
getch();
}
75
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
SUMMARY
The process of arranging the data or information in some logical
order is known as sorting
The logical order may be ascending or descending based on our
requirement.
There are mainly two types of sorting available are.
o Internal sorting
o External sorting
There are mainly five sorting techniques
76
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
o Bubble sort
o Insertion sort
o Quick sort
o Bucket sort
o Merge sort
o Selection sort
o Shell sort
The process of “looking up” specific records from the database is
known as searching
There are mainly two types:
o Binary
o Sequential
77
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
78
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
79
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
3. Graph Traversal
A graph can be traversed using two methods as below:
a. Depth First Search(DFS)-Implementation
When a graph is traversed by visiting the nodes in the
forward direction as long as possible the traversal is called
depth-first traversal.
The algorithm repeatedly searches deeper by visiting
unvisited vertices and whenever an unvisited vertex is not
found, it backtracks to previous vertex to find out whether
there are still unvisited vertices.
80
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
81
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
82
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
A spanning tree of a graph is just a sub graph that contains all the
vertices and is a tree(with no cycle).
A graph may have many spanning trees.
A minimum spanning tree(MST) for graph is a subgraph of graph
that contains all the vertices of G(graph).
If a graph G is not a connected graph, then it cannot have any
spanning tree.
In this case, it will have a spanning forest. Suppose a graph(G)
with n vertices then the MST will have (n-1) edges, assuming that
the graph is connected.
To obtain minimum spanning tree of connected weighted and
undirected graph, different algorithms are use which are listed as
under:
i. Krukal’s Algorithm
ii. Prim’s Algorithm
iii. Sollin’s Algorithm
83
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
SUMMARY
In mathematics and computer science, graph theory is the study of
graphs, which are mathematical structures used to model pairwise
relations between objects.
A "graph" in this context is made up of "vertices" or "nodes" and
lines called edges that connect them.
A graph may be undirected, meaning that there is no distinction
between the two vertices associated with each edge, or its edges
may be directed from one vertex to another; see graph
(mathematics) for more detailed definitions and for other variations
in the types of graph that are commonly considered. Graphs are
one of the prime objects of study in discrete mathematics.
84
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Introduction
Primitive and simple structures
Linear and nonlinear structures
file organization.
3. DATA-STRUCTURE
Data structure is the possible ways of organizing data items that
defines how the data items are stored in memory and relationship with
each other. Data structure is the possible ways that defines the
relationship between data items.
DATA STRUCTURE = ORGANIZED DATA + ALLOWEDOPE
4. CELLS
Cells is the memory area that used to store elementary data items, it
can be refered as a single bit, byte, group of bytes.
5. FIELDS
Field is a smallest piece of information that can be reference by a
programming language.
85
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Data Structure
Primitive Non-primitive
Integer
Numeric Character
Non-Numeric Array
Linear Non-Linear
Tree
Real Linked List
Graph
Double Stack
Long Queue
Logical File and structure
86
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
87
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
ii) Graph:
It is also non-linear data structure which contains two main
pair:- Vertices and edges.
Depending on nature of representation, there are different
types of graph.
88
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
SUMMARY
A collection of organized data is known as DATA STRUCTURE
A data structure mainly divides into two parts
o Primitive data structure
o Non primitive data structure
Linear data structure
Non-linear data structure
There are mainly three types of array:
o 1 D Array
o 2 D Array
o Multi dimensional array
89
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
An array and structure are related with each other in two way:
o Array as member of structure.
o Array of structure means structure variable as an array.
Any data structure whose size is fixed at compile time is known as
static data structure(allocation)
Any data structure whose size is not fixed means during the
program executing, but it expands at run time is known as dynamic
data structure(allocation)
When we using dynamic memory allocation, it allocates memory at
run time that’s why it saves memory space.
There are mainly four types of dynamic memory allocation
functions are as below:
o malloc: it allocates memory for the requested byte of size
p=(structure *) malloc (size * size of(structure))
o calloc: it allocates memory for elements of an array. It
initializes value to zero
p=(structure *)calloc(n,element size)
o free: it removes previously allocated memory
free(p);
o realloc: it modifies the size of previously allocated memory
p=realloc(p,newsize)
90
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
91
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
o Function to insert
an element into the
queue
o Function for
deletion from
circular queue
o Circular queue
with array
implementation
Deques
Priority Queues
What is stack?
Stack is a linear data structure in which insertions and deletions of
an element are done at one end which is known as TOS (Top Of
Stack).
Stack supports LIFO( Last In First Out)
92
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Application of Stacks
o Stack is used in the mobile. Message sent by one user and
another message sent by same user after some time, in that
case the message that was sent last by the user arrives in the
inbox first
o Consider a stack of plates placed on counter in a restaurant,
During dinner time, plates are taken from the top of
place(stack) and waiter puts the washed plates on the top of
position which is known as TOS
o The most important application of stack is recursion.
o It is also used in memory management and in operating
systems
Types of stack/implementation of stack
o Static Stack/Stack(array)
o Dynamic Stack(linked list)
Static stack:
a. Push operation
To insert an element on the stack we are using push
operation
This operation inserts the element only on the TOS
Algorithm (we have to define size first)
Step-1:First we have to check for stack overflow
If(tos>=size)
Stack is full
Step-2:Tos=tos+1(increment the pointer value by 1)
Step-3:Stack[tos]=ele; where ele is an element that is to be
inserted
Step-4: End
b. Pop operation
To delete an element from the stack we are using pop
operation
93
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
c. Peep operation
If we want to access some information stored at some
location in stack then peep operation is required.
The index value is subtracted from tos
Algorithm
Step-1: If tos<0---stack is empty
Step-2:Input the position of the element that you want to
read
Step-3:If(element<0 || element>tos+1)
Out of range
Step-4: Else Print peeped element
Stack[tos-pos+1]
Step-5: End
d. Display operation
Step-1: If(tos<0)—stack is empty
Step-2: else
for(i=tos;i>=0;i--)
print element that is stack[i]
Step-3: End
e. Update operation
Step-1: If tos<0—stack is empty
94
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
95
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
printf("\nStack is full");
else
tos++;
stack[tos]=ele;
}
void display()
{
int i;
if(tos<0)
printf("\nstack is empty");
else
{
for(i=tos;i>=0;i--)
printf("\nElement at position %d is %d",i,stack[i]);
}
}
void peep()
{
int pos;
if(tos<0)
printf("\nstack is empty");
else
printf("\nEnter value of pos:");
scanf("%d",&pos);
if(pos<0 || pos>tos+1)
printf("\nout of range");
else
printf("\nPeeped element is %d",stack[tos-pos+1]);
}
void pop()
{
if(tos<0)
printf("\nStack is empty");
else
printf("\nelement deleted");
tos--;
96
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
}
void update()
{
int pos;
if(tos<0)
printf("\nstack is empty");
else
{
printf("\nEnter postion:");
scanf("%d",&pos);
if(pos<=0 || pos>tos+1)
printf("\nout of range");
else
{
printf("\nenter new value:");
scanf("%d",&stack[tos-pos+1]);
//printf("\nelement=%d",stack[tos-pos+1]);
}
}
}
Dynamic stack:
a. Push operation
Step-1:Allocate the memory to the node (node *t)
t=(node *)malloc(sizeof(node));
Step-2:Assign data part and next part of node
t->info=ele;
t->next=tos;
top=t;
Step-3: End
b. Pop operation
Step-1:Check whether stack is empty or not.
If(tos==NULL)
Stack is empty
Step-2: If the stack is not empty then
t=tos
97
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
tos=t->next
free(t)
Step-3: End
c. Peep operation
Step-1: Enter the position from user
Step-2:Initialize i=0,j=1;
Step-3:node *temp
for(temp=tos; temp!=NULL;temp=temp->next)
i++;
Step-4:if(pos>i)—stack is full
Step-5:else
temp=tos;
for(j=1;j<pos;j++)
temp=temp->next;
printf(“%d”,temp->info)
Step-5: End
d. Display operation
Step-1: while(temp!=NULL)
printf(“%d”,temp->info)
temp=temp->next;
Step-2: End
e. Update operation
Step-1: Enter the position from user
Step-2:Initialize i=0,j=1;
Step-3:node *temp
for(temp=tos; temp!=NULL;temp=temp->next)
i++;
Step-4:if(pos>i)—stack is full
Step-5:else
temp=tos;
98
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
new1=(node *)malloc(sizeof(node)
scanf(“%d”,new1->info)
for(j=1;j<pos;j++)
temp=temp->next;
temp->info=new1->info
Step-5: End
Program
#include<stdio.h>
#include<conio.h>
struct list
{
int info;
struct list *next;
};
typedef struct list node;
struct list *new1;
node *tos;
void push(int);
void pop();
void peep();
void display();
void update();
void main()
{
tos=NULL;
clrscr();
push(10);
push(11);
push(12);
push(13);
push(14);
display();
pop();
display();
peep();
update();
99
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
display();
getch();
}
void push(int ele)
{
node *t;
t=(node *)malloc(sizeof(node));
t->info=ele;
t->next=tos;
tos=t;
}
void display()
{
node *temp;
temp=tos;
while(temp!=NULL)
{
printf("\nStack Element is:%d",temp->info);
temp=temp->next;
}
}
void pop()
{
node *temp;
if(tos==NULL)
printf("\nStack is empty");
else
temp=tos;
tos=temp->next;
free(temp);
}
void peep()
{
int pos,i,j;
node *temp;
printf("\nEnter position:");
100
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
scanf("%d",&pos);
for(temp=pos;temp!=NULL;temp=temp->next)
{
i++;
}
if(pos>i)
printf("\nStack is empty");
else
{
temp=tos;
for(j=1;j<pos;j++)
{
temp=temp->next;
}
printf("\nValue at %d is %d",pos,temp->info);
}
}
void update()
{
int pos,i=0,j;
node *temp;
printf("\nEnter position:");
scanf("%d",&pos);
for(temp=pos;temp!=NULL;temp=temp->next)
{
i++;
}
if(pos>i)
printf("\nStack is full");
else
{
new1=(node *)malloc(sizeof(node));
printf("\nenter new value");
scanf("%d",&new1->info);
temp=tos;
for(j=1;j<pos;j++)
101
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
{
temp=temp->next;
}
temp->info=new1->info;
}
}
Operator
b
c c
*
After the insertion of b in the stack, operator + comes so now the
precedence should be checked. As the precedence of + is less than * so
now elements should be popped.
102
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Operator
+
Then a is pushed in the stack.
Operator
+
So athe element a is popped. Now we get
+a *bc which is our prefix expression.
Step 2: Suppose we want to convert infix to postfix then we have to start the
infix statement from front. When the operator comes then they should not be
pushed in stack.
Infix: a+b*
Operator
+ b
a a
After the insertion of b in the stack, operator * comes so now the
precedence should be checked. As the precedence of * is more than + so it
remains as it is.
Operator
+,*
b
a
Then last element c is pushed.
Operator
+,*
c
b
So now
a as the expression is complete, the elements are popped.
First + have low precedence so it is placed at last then * operator. After
that elements are popped and are written from back that is as c is popped
first then c*+ then b is popped so bc*+ and then a is popped so abc*+.
Finally we get
103
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
1
2 2
3 3 6
4 4 4 24
104
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
105
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
What is queue?
106
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
STACK QUEUE
In stack both the insertion and In queue insertion takes place at
deletion takes place at only one rear and deletion takes place at
end know as TOS (top of stack) front
In stack we have to keep track of In queue we have to keep track of
only TOS both the ends that is front and rear
Types of queue/implementation of queue
o Static Queue/Queue(array)
107
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Step-2:rear = rear +1
Step-3:q[rear]=element
Step-4: End
b. Delete operation
Algorithm
Step-1:If front <0
Output “queue underflow”
Step-2: front ++;
Step-3: End
c. Display operation
Algorithm
Step-1: If fornt<0---queue underflow
Step-2:for(i=front;i<=rear;i++)
Printf(“%d”,queue[i]);
Step-3:End
Program:
#include<stdio.h>
#include<conio.h>
#define size 100
int front=-1,rear=-1;
int queue[size];
void insert(int);
void deleted();
void display();
void main()
{
clrscr();
insert(10);
insert(11);
insert(12);
insert(13);
insert(14);
108
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
display();
printf("\n\n");
deleted();
display();
getch();
}
void insert(int ele)
{
if(rear>size)
printf("\nQueue is full");
else
{
rear++;
queue[rear]=ele;
if(rear==0)
front=0;
}
}
void deleted()
{
if(front<0)
printf("\nQueue is empty");
else
{
if(front==rear)
{
front=-1;
rear=-1;
}
else
front++;
}
}
void display()
{
int i;
109
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
if(front<0)
printf("\nQueue empty");
else
{
for(i=front;i<=rear;i++)
{
printf("\nElement at position %d is %d",i,queue[i]);
}
}
}
Dynamic queue:
a. Insert operation
Step-1:Allocate the memory to the node (node *q)
q=(node *)malloc(sizeof(node));
Step-2:Assign data part and next part of node
q->info=ele;
q->next=NULL;
Step-3:If queue is empty then:
if(front==NULL)
front=rear=q
Step-4:If queue is not empty
rear-next=q;
rear=q;
Step-5: End
b. Delete operation
Step-1:Check whether queue is empty or not.
Step-2: If the queue is empty then
If(front==NULL)---queue empty
Step-3: If queue is not empty then
node *q
q=front
front=q->next
110
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Step-4:free(q)
Step-5:End
c. Display operation
Step-1: node *q
q=front
Step-2:Check if queue is empty or not
If(q==NULL)---queue empty
Step-3: If queue is not empty then
While(q!=NULL)
Printf(“%d”,q->info);
q=q->next
Step-4:END
Program
#include<stdio.h>
#include<conio.h>
struct list
{
int info;
struct list *next;
};
typedef struct list node;
node *front,*rear;
void insert(int);
void deleted();
void display();
void main()
{
front=rear=NULL;
clrscr();
insert(10);
insert(11);
insert(12);
display();
111
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
deleted();
display();
getch();
}
void insert(int ele)
{
node *t;
t=(node *)malloc(sizeof(node));
t->info=ele;
t->next=NULL;
if(front==NULL)
{
front=rear=t;
}
else
{
rear->next=t;
rear=t;
}
}
void deleted()
{
node *temp;
temp=front;
if(temp==NULL)
printf("\nQueue is empty");
else
front=temp->next;
free(temp);
}
void display()
{
node *temp;
int i=0;
temp=front;
if(temp==NULL)
112
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
printf("\nQueue is empty");
else
{
while(temp!=NULL)
{
printf("\nElement is %d",temp->info);
temp=temp->next;
}
}
}
Circular queue:
The problem with simple queue is that once we insert the elements
and when we are removing elements using front pointer these
elements becomes blank. As the elements become blank we cannot
insert any elements in that blank space.
So to overcome this problem there is technique available known as
circular queue. In this technique when rear reaches the queue’s size
the first element will become the queue’s new rear
113
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
a. Insert operation
Step-1:If front==1 and rear==size (output queue overflow)
Step-2:else if (front==0)
Front=rear=1
q[rear]=element
Step-3:else if (rear==size)
rear=1
q[rear]=element
Step-4:else
rear=rear+1
q[rear]=element
Step-5: End
b. Delete operation
Step-1:If front==0 (output queue is empty)
Step-2: If front==rear
front=rear=0
Step-3: else if (front==size)
front=1
Step-4:else
114
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
front=front+1
Step-5:End
c. Display operation
Step-1: If front==0 (output queue is empty)
Step-2:else if (front >rear)
for(i=1;i<=rear;i++)
printf(“%d”,queue[i])
for(i=front;i<=size;i++)
printf(“%d”,queue[i])
Step-3: else
for(i=front;i<=rear;i++)
printf(“%d”,queue[i])
Step-4:END
Program
#include<stdio.h>
#include<conio.h>
#define size 5
int queue[size];
int front=0,rear=0;
void insert(int);
void deleted();
void display();
void main()
{
clrscr();
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
display();
printf("\n\n");
115
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
deleted();
display();
printf("\n\n");
insert(60);
display();
getch();
}
void insert(int ele)
{
if(front==1 && rear==size)
printf("\nQueue is full");
else if(front==0)
{
front=rear=1;
queue[rear]=ele;
}
else if(rear==size)
{
rear=1;
queue[rear]=ele;
}
else
{
rear++;
queue[rear]=ele;
}
}
void deleted()
{
if(front==0)
printf("\nQueue is empty");
else if(front==rear)
front=rear=0;
else if(front==size)
front=1;
else
116
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
front++;
}
void display()
{
int i;
if(front==0)
printf("\nQueue is empty");
else if(front>rear)
{
for(i=1;i<=rear;i++)
{
printf("\nElement at %d is %d",i,queue[i]);
}
for(i=front;i<=size;i++)
{
printf("\nElement at %d is %d",i,queue[i]);
}
}
else
{
for(i=front;i<=rear;i++)
{
printf("\nElement at %d is %d",i,queue[i]);
}
}
}
117
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
118
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
SUMMARY
Stack is a linear data structure.
It has LIFO structure means that last inputted element is the first out
from the stack.
The place from insertion of elements and deletions of elements are take
place is known as TOS(Top of the stack)
It supports various operations for the manipulation of elements. If we
want to insert elements then push operations is used.
If we want to deleted elements then pop operation is used.
If we want to know the top of the stack element then peep operation is
used.
If we want to know the size of stack elements then we have to create
size() function through which we find out the size of it.
If we want to know the stack is full or empty and based on that
appropriated message is display then we have to create isFull() and
isEmpty()function for that.
Stack is very important part and very useful in large applications
For example,it is used in memory management and in operating system.
Another example is the tennis balls in their container(we cannot remove
2 balls at the same time)
There are two ways through stack is implemented are static stack and
dynamic stack.
Static stack uses array to perform all the operations on stack and
dynamic stack uses structure and pointer to perform all the operations
Queue is a linear data structure which works FCFS (first come first
serve) basis.
A queue insert element at one end is known as rear pointer variable
whereas it delete element
From one end is known as front pointer variable. It means that insertion
of elements and
Deletion of it occurs at different end of a queue.
To insert elements at rear end a queue performs enqueue operations. To
delete elements from the front end a queue performs dequeue operations
and we can retrieve elements from the both end also.
119
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
121
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
122
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
123
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
124
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
125
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
a. Singly linked list is the most basic of all the linked data structure
b. A singly linked list is a collection of nodes and each node contains
the pointer to next element
c. In singly link list we can move from left to right that is only in one
dimension but we cannot return back.
d. Each node represents structure, containing variables for
information and a structure pointer to itself.
struct list
{
int info;
struct node *next;
};
typedef struct list node;
node *p;
e. Create operation
Algorithm
Step-1Check whether any node exists in the list or not
Step-2:If list is Empty then create node
126
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
if(q==NULL)
{
q=(node *)malloc(sizeof(node));
q->data=info;
q->next=NULL;
}
Step-3:If the list is not Empty then create the node after the
last node
while(q->next!=NULL)
{
q=q->next;
}
q->next=(node *)malloc(sizeof(node));
q->next->data=info;
q->next->next=NULL;
Step-4: End
f. Display operation
Algorithm
Step-1: while(q!=NULL)
{
printf(“\nElement is %d”,q->info);
q=q->next;
}
Step-2: End
127
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
i. Count operation
Algorithm
Step-1: Initialize one counter variable for counting total no
of nodes (c=0)
Step-2:while(q!=NULL)
{
q=q->next
}
return c;
Step-3:End
Program:
#include<stdio.h>
#include<conio.h>
struct list
{
int info;
struct list *next;
128
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
};
typedef struct list node;
node *p;
void create(int,node *);
void display(node *);
void addbeg(int,node *);
void addafter(int,int,node *);
void deleted(int,node *);
int count(node *);
void sort(node *);
void search(int,node *);
void main()
{
p=NULL;
clrscr();
create(10,p);
create(20,p);
create(5,p);
display(p);
addbeg(0,p);
printf("\n\n");
addafter(2,500,p);
deleted(20,p);
display(p);
sort(p);
printf("\n\n");
display(p);
search(15,p);
getch();
}
void create(int ele,node *q)
{
if(q==NULL)
{
p=(node *)malloc(sizeof(node));
p->info=ele;
129
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
p->next=NULL;
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
q->next=(node *)malloc(sizeof(node));
q->next->info=ele;
q->next->next=NULL;
}
}
void display(node *q)
{
while(q!=NULL)
{
printf("\nElement is %d",q->info);
q=q->next;
}
}
int count(node *q)
{
int c=0;
while(q!=NULL)
{
q=q->next;
c++;
}
return (c);
}
void addbeg(int ele,node *q)
{
p=(node *)malloc(sizeof(node));
p->info=ele;
p->next=q;
130
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
}
void addafter(int c,int ele,node *q)
{
node *temp;
int i;
for(i=1;i<c;i++)
{
q=q->next;
if(q==NULL)
{
printf("\nposition is out of range");
return;
}
}
temp=(node *)malloc(sizeof(node));
temp->info=ele;
temp->next=q->next;
q->next=temp;
}
void deleted(int ele,node *q)
{
node *temp;
if(q->info==ele)
{
p=q->next;
free(q);
return;
}
while(q->next->next!=NULL)
{
if(q->next->info==ele)
{
temp=q->next;
q->next=q->next->next;
free(temp);
return;
131
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
}
q=q->next;
}
}
void sort(node *q)
{
int t;
node *temp;
while(q!=NULL)
{
temp=q->next;
while(temp!=NULL)
{
if(q->info > temp->info)
{
t=q->info;
q->info=temp->info;
temp->info=t;
}
temp=temp->next;
}
q=q->next;
}
}
void search(int num,node *q)
{
while(q!=NULL)
{
if(q->info==num)
{
printf("\nSearch success");
printf("\nSearch element %d ",num);
return;
}
q=q->next;
}
132
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
printf("\nSearch unsuccessful");
getch();
}
1 WORD QUESTION ANSWER
133
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Program
#include<stdio.h>
#include<conio.h>
struct list
{
int info;
struct list *next,*prev;
};
typedef struct list node;
node *p;
void create(int,node *);
void display(node *);
void addbeg(int,node *);
void addafter(int,int,node *);
void deleted(int,node *);
int count(node *);
void sort(node *);
void search(int,node *);
void main()
{
p=NULL;
clrscr();
create(10,p);
create(20,p);
create(30,p);
display(p);
addbeg(0,p);
printf("\n\n");
addafter(2,500,p);
deleted(20,p);
display(p);
sort(p);
134
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
printf("\n\n");
display(p);
search(15,p);
getch();
}
void create(int ele,node *q)
{
node *temp;
if(q==NULL)
{
p=(node *)malloc(sizeof(node));
p->prev=NULL;
p->info=ele;
p->next=NULL;
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
temp=(node *)malloc(sizeof(node));
temp->next=NULL;
temp->info=ele;
temp->prev=q;
q->next=temp;
}
}
void display(node *q)
{
while(q!=NULL)
{
printf("\nElement is %d",q->info);
q=q->next;
}
135
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
}
int count(node *q)
{
int c=0;
while(q!=NULL)
{
q=q->next;
c++;
}
return (c);
}
void addbeg(int ele,node *q)
{
p=(node *)malloc(sizeof(node));
p->prev=NULL;
p->info=ele;
p->next=q;
q->prev=p;
}
void addafter(int c,int ele,node *q)
{
node *temp;
int i;
for(i=1;i<c;i++)
{
q=q->next;
if(q==NULL)
{
printf("\nposition is out of range");
return;
}
}
temp=(node *)malloc(sizeof(node));
temp->prev=q;
temp->next=q->next;
temp->info=ele;
136
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
temp->next->prev=temp;
q->next=temp;
return;
}
void deleted(int ele,node *q)
{
node *temp;
if(q->info==ele)
{
p=q->next;
q->next->prev=NULL;
free(q);
return;
}
while(q->next->next!=NULL)
{
if(q->next->info==ele)
{
temp=q->next;
q->next=q->next->next;
q->next->prev=temp->prev;
free(temp);
return;
}
q=q->next;
}
}
void sort(node *q)
{
int t;
node *temp;
while(q!=NULL)
{
temp=q->next;
while(temp!=NULL)
{
137
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
if(q->info>temp->info)
{
t=q->info;
q->info=temp->info;
temp->info=t;
}
temp=temp->next;
}
q=q->next;
}
}
void search(int num,node *q)
{
while(q!=NULL)
{
if(q->info==num)
{
printf("\nSearch success");
printf("\nSearch element %d ",num);
return;
}
q=q->next;
}
printf("\nSearch unsuccessful");
getch();
}
138
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
139
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Program
#include<stdio.h>
#include<conio.h>
struct list
{
int info;
struct list *next;
};
typedef struct list node;
node *p;
void create(int,node *);
void display(node *);
void addbeg(int,node *);
void addafter(int,int,node *);
void deleted(int,node *);
int count(node *);
void sort(node *);
void search(int,node *);
void main()
{
p=NULL;
clrscr();
create(10,p);
create(11,p);
create(12,p);
display(p);
printf("\n\n");
addbeg(0,p);
addafter(3,12,p);
deleted(11,p);
sort(p);
search(5,p);
display(p);
getch();
140
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
141
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
}
void addbeg(int ele,node *q)
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->info=ele;
temp->next=q;
while(q->next!=p)
{
q=q->next;
}
q->next=temp;
p=temp;
}
void addafter(int c,int ele,node *q)//pos is same as c var.
{
node *temp;
int i;
for(i=1;i<c;i++)
{
q=q->next;
if(q==p)
{
printf("\nposition is out of range");
return;
}
}
temp=(node *)malloc(sizeof(node));
temp->info=ele;
temp->next=q->next;
q->next=temp;
}
void deleted(int ele,node *q)
{
node *temp;
if(q->info==ele)
142
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
{
do
{
q=q->next;
}while(q->next!=p);
q->next=p;
p=p->next;
return;
}
while(q->next->next!=p)
{
if(q->next->info==ele)
{
temp=q->next;
q->next=temp->next;
free(temp);
return;
}
q=q->next;
}
}
void sort(node *q)
{
int t;
node *temp;
do
{
temp=q->next;
while(temp!=p)
{
if(q->info>temp->info)
{
t=q->info;
q->info=temp->info;
temp->info=t;
}
143
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
temp=temp->next;
}
q=q->next;
}while(q!=p);
}
void search(int num,node *q)
{
do
{
if(q->info==num)
{
printf("\nSearch success");
printf("\nSearch element %d",num);
return;
}
q=q->next;
}while(q!=p);
printf("\nSearch unsuccessful");
getch();
}
Program
#include<stdio.h>
#include<conio.h>
struct list
{
int info;
struct list *next,*prev;
};
typedef struct list node;
node *p;
void create(int,node *);
void display(node *);
void addbeg(int,node *);
144
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
145
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
temp->next=p;
temp->info=ele;
temp->prev=q;
q->next=temp;
}
}
void display(node *q)
{
do
{
printf("\nElement is %d",q->info);
q=q->next;
}while(q!=p);
}
int count(node *q)
{
int c=0;
do
{
q=q->next;
c++;
}while(q!=p);
return (c);
}
void addbeg(int ele,node *q)
{
node *temp;
while(q->next!=p)
{
q=q->next;
}
temp=(node *)malloc(sizeof(node));
temp->prev=q;
temp->info=ele;
temp->next=p;
p->prev=temp;
146
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
q->next=temp;
p=temp;
}
void addafter(int c,int ele,node *q)
{
node *temp;
int i;
for(i=1;i<c;i++)
{
q=q->next;
if(q==p)
{
printf("\nposition is out of range");
return;
}
}
temp=(node *)malloc(sizeof(node));
temp->prev=q;
temp->next=q->next;
temp->info=ele;
temp->next->prev=temp;
q->next=temp;
return;
}
void deleted(int ele,node *q)
{
node *temp;
if(q->info==ele)
{
temp=q;
p=temp->next;
while(q->next!=p)
{
q=q->next;
}
147
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
p->prev=q;
return;
}
do
{
if(q->next->info==ele)
{
temp=q->next;
q->next=temp->next;
temp->next->prev=q;
// free(temp);
return;
}
q=q->next;
}while(q!=p);
}
void sort(node *q)
{
int t;
node *temp;
do
{
temp=q->next;
while(temp!=p)
{
if(q->info>temp->info)
{
t=q->info;
q->info=temp->info;
temp->info=t;
}
temp=temp->next;
}
q=q->next;
}while(q!=p);
148
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
}
void search(int num,node *q)
{
do
{
if(q->info==num)
{
printf("\nSearch success");
printf("\nSearch element %d",num);
return;
}
q=q->next;
}while(q!=p);
printf("\nSearch unsuccessful");
getch();
NOTE: All the algorithms will be same only conditions will be changed.
149
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
SUMMARY
Linked list is a non linear data structure which defines as a collection of
nodes.
A main limitation of an array (used in stack and queue ) is that elements
must be stored in ho-mogeneous manner and fixed amount of data is
stored. Where as in linked list representation this limitation is overcome
with the use of node.
There are mainly five types of linked list which listed below:
o Singly linked list contains information part and address of next
node. In this last node contains NULL value in address part.
o Doubly linked list contains information part and address of next
node as well address of previous node. In this last node contains
NULL value, in the next node address part.
o Circular doubly linked list Is same like as doubly linked list but in
the last node next address contains the address of first node. And
first node previous part contains the address of last node.
o Circular doubly linked list is same like as doubly linked list but in
the last node next address contains the address of first node. And
first node previous part contains the address of last node.
o Header linked list contains a special type of information that how
many number of nodes are there in linked list. There are two types
of header linked lists:
Grounded header linked list
Circular header linked list
Operations performed in a linked list are as below:
150
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
UNIT-5 - TREE
Introduction
o Objectives
o Basic terminology
o Properties of a tree
Binary trees
o Properties of binary
trees
o Implementation
o Traversals of a binary
tree
In order traversal
Post order
traversal
Preorder
traversal
Binary search trees (bst)
o Insertion in bst
o Deletion of a node
o Search for a key in bst
o Height balanced tree
o b-tree
Insertion & Deletion
1) What is tree? What is root of the node?
Ans:
Tree is a non-linear data structure which is a set of one or more nodes
such that:
1) There is specially designated node known as root of the tree.
2) The remaining nodes are divided into n disjoint se of nodes T1,
T2…..Tn, each of which is a tree.
151
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
The above figure shows tree which is divided into two disjoint sets.
{B,D,E} and {C,F}
2) What are leaf and non-leaf nodes?
Ans:
Leaf nodes are the nodes that do not have any children.
Non-leaf nodes are the nodes that have children.
Example:
In the above example, nodes D, E and F are leaf nodes and nodes A,B and
c are non-leaf nodes.
152
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
D E H I
B C
D E F
153
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
B C
D E F
In the above example, all the nodes except A have in-degree 1.
Note: Root is the only tree which have in-degree 0.
154
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
155
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
B C
D E F G
2) Complete binary tree:-
If all the leaves of strictly binary tree are at same depth / level
‘d’ is known as complete binary tree.
Example:
156
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Advantage:
The main advantage of complete binary tree is that we can easily
find out parent or root node of any node and left and right sub tree
of any node.
Example:
Suppose we want to find the parent or root node of node E then we
have to write
floor(N/2)
=floor (5/2)
=floor(2.5)
=2
=B
So, Parent node of node E is node B.
Now, suppose that we want to find left and right sub tree of node B,
so we have to write
Left Sub tree Right Sub tree
2N 2N+1
=2*2 =2*2+1
=4 =5
=D =E
So, left and right sub tree of node B is D and E
157
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
1) Sequential Representation:-
In sequential representation, nodes are stored in one dimension array.
Also assign level number to every node in tree.
We can assign node numbers in such a way that root is assigned the
number 1. Then, left sub tree must be assigned 2p+1 and right sub tree
must be assigned 2p+2.
Example:
158
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
C
2B
3
4 5 6 7
D E
A F G
C
B
F G
NULL NULL NULL NULL NULL NULL NULL
NULL E
D
struct node
{
int data;
struct node *lefttree;
struct node *righttree;
};
159
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Example:
Create the binary tree for following example
20 5 15 17 45 10
4
5
5
1
5
1 1
0 7
160
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
161
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
10) What is tree traversal? Explain tree traversal techniques with example.
Ans:
Tree traversal is the method or technique to display data in binary
tree.
In traversing method, tree is processed in such a way that each node
is visited only once.
There are 3 main tree traversal techniques:
Preorder traversal
Inorder traversal
Postorder traversal
162
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
1) Preorder traversal:-
It follows three rules:
If the tree is not empty then:-
1) Visit the root node
2) Traverse left sub tree-recursively
3) Traverse right sub tree-recursively
In this, (D) is used for root node, (L) is used for left sub tree
and ( R ) is used for right sub tree.
D-Root 1
D-L-R
L R
2 3
163
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Example:
1 A
2 5
B C
3 4 6 7
A – B -C- D – E – F – G
D D E
L FR G
2) Inorder traversal:-
It follows three rules:
If the tree is not empty then:-
1) Traverse left sub tree-recursively
2) Visit root node
4) Traverse right sub tree-recursively
In this, (D) is used for root node, (L) is used for left sub tree
and ( R ) is used for right sub tree.
D-Root 2
L-D-R
L R
1 3
Example:
Left sub Right
tree sub tree
4 A
2 6
B C
1 3 5 7
C -B- D –A- F – E – G
D E L F D RG
164
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
3) Postorder traversal:-
It follows three rules:
If the tree is not empty then:-
1) Traverse left sub tree-recursively
2) Traverse right sub tree-recursively
3) Visit root node.
In this, (D) is used for root node, (L) is used for left sub tree
and ( R ) is used for right sub tree.
D-Root 2
L-R-D
L R
1 3
Left sub Right
tree sub tree
165
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Example:
7
A
3 6
B C
1 2 4 5
C -B- D - F – E – G-A
D E F G
L R D
1 WORD QUESTION ANSWER
166
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
167
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
SUMMARY
A tree is hierarchical non-liner data structure that is used to store
information.
The tree data structure has the mainly three components: root (for a base),
branches (for growth) and leaves (for existence).
The root node of a tree is the node with no parents. There is at most one
root node in a rooted tree.
A leaf node has no children.
A directed edge refers to the link from the parent to the child (the arrows
in the picture of the tree).
The binary tree creation follows a very simple principle – for the new
element to be added, compare it with the current element in the tree.
If its value is less than the current element in the tree.
If its value is less than the current element in the tree then move towards
the left side of that element.
If its value is greater than the current element in the tree then move
towards the right side of that element.
If there is no sub tree on the left, make our new element as the left child
of that current element or else compare it with the existing left child and
follow the same rule.
Exactly the same hs to be done for the case when our new element is
greater than the current element in the tree but this time with the right
child.
The depth of a node n is the length of the path from the root to the node.
The set of all nodes at a given depth is sometimes called a level of the
tree. The root node is at depth zero. A tree with only a root node has a
height of zero.
We can also find out the level of a node in a binary tree. For example,
root is at level ‘0’ and level of any other node is one or more than the
level of its ancestor.
Siblings are nodes that share the same parent node.
168
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
169
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
These traversal methods are limited to binary trees only and it’s not
implemented for any other tree.
Here, given below short summary of 3 methods:
Pre Display Go to left Go to
order info right
In Go to left Display Go to
order info right
170