Вы находитесь на странице: 1из 37

PROGRAM.

NO:-1
Title:- Sorting list using array
Theory:-
Here sorting of list, we are using array. First we are
taking choise of user about ascending or decending order
of list. The elements of array are fixed by programme.
Then by using for loop & condition we are sorting list
as per users choise. Arry is a type of data structure
which contains a group of similar data types that share a
common name and has fixed length. The elements of an
array start at zero and at n-1 i.e in continuous pattern.
In sorting, we make usr of a temp.
Variablewhich tempararely stores the elements of a
arrary for carring out sorting operation.

Algorithm:
1) Consider an arrary a such that
a ={n1,n2…….nm}
2) N = a. length
3) For decending list
For {int i=0; i<n; i++}
{
For ( int j = i+1 ; j ++)
If a[i] < a [j]
{
Int temp = a [i];
a [i] = a [j];
a [j]= temp;
}
}
}
4) For ascending order
For (int I = 0; i<n; i++
{
For (int j = 0i +1 ; j < n; j++)
{
If (a[i] = a [j]) --
1--
{
Int temp = a [j];
a [j] = a [i];
a [j] =temp;
}
}
}
5) Display.

Conclusion:-
Thus elements I a given array sorted.

--2—
ROGRAM:-2
Title:- The Tower Of Hanoi
Theory:-
The tower at Honai problems consist of three
peg. A,B & C five disks of differing diameters are placed
on peg A so that a arger disk is always below a smaller
disk. The abject is to move the five disks to peg C using
peg Bas auxillary. Only the top disks an any peg may be
moved to another peg and a larger disk may be never
rest on smaller one.
Basically the tower of Honai problems works on
the principle of recursive function.
Let us consider the general casse of tri disks.
Suppose we had a solution for cn-11 disks we could state
the solution for n disks in terms of solutions for (n-1)
disks. This can be done by merely contiuously
substracting 1form n to eventually produce 1 or merely
moving a single disk from peg A to C.
Therefore we wil have developed a recursive
soln it we can state a soln for n disks in terms of n-1.
Suppose we could move four disks from peg A
to C. Then we could move the largest disks from A to C
and again apply the soln for four disks from B to C using
the now empty peg A as an aux
Thus we may state the recursive solution to
tower of Hanoi problem.

ALGORITHM:-
1)If n = 1 then move the single disk form A to C and
exit.

2) Move the top n-1 disk from A to B using C as


auxiliary.

3) Move the remaining disk from A to C

4) Move the n-1 disks from B to C using A as


auxiliary.
FUNCTIONS:-
Void towers (int n,char from peg, char. To peg, char
aux peg)

RESULT:-
Thus we have solved the towers of Hanoi by
using recursive function.
--3--
PROGRAM:-3
Title:- COPY BYTES FROM ONE FILES TO
ANOTHER.

Theory:-
As we copy characters using file reader or
file writer class, similary we are copying bytefrom one
file to another using file input stream & file output
stram.
First we bad created two objects of inbuild
class file, assign a particular file with .dat extention.
Then by using .read () fn we can read modify it. Then by
using .exit () we can exit from loop. After all these
things we have to close it that’s why we are using .close
() to close file.
Java provides two kinds of type stream

CLASS:-
1) INPUT STREAM CLASS:-
Input stream classes that are used to read 8
bit byte includea super classes for supporting
various I/P related functions.
2) OUTPUT STREAM CLASS:-
It si an adstract class.

INPUT
DATA
INFILE INPUT
PROGRAM
DATA
INFILE
Write ()

ALGORITHM:-

1) Create 2 objects of class file input stream this and


for files Input .txt and output .txt.
--4--
2) Pass infile and outfile as an arrangement if is and
f os resp.

3) While ( cb = L is.read (j) ! = -1

{ for s.write (b); // for reading & Writing bytes.


4) Close L is and L os resp.& display the O/P

CONCLUSION:-

Thus data is copied from one file to another using


bytes stream.
--5--
PROGRAM:-4

Title:- CONCATENATION AND BUFFERING OF FILES.


Theory:-
It is possible to combine two or moreinput stresm
into a single input stream. This process is known as
concatenation of files and is achieved using the sequence
Input stream class. One of the constructers of this class
takes two input stream object as arguments and
combines them to constructs a single input stream.
Java also supports creation of buffers to store
temporarity data that is read from or written to a
stream. The process is known as buffered I 10 operation.
A buffer sists between the program and the source and
functions like a filter. Buffer can be created using the
buffered input stream and buffered output stream class.
ALGORITHM:-
1) Close two objects of class file input stream f1
& f2 file “text’.txt” and “text2.text” res.
2) Pass f1 and f2 as arg to the sequence input
constructor.

3) F3 = new sequence input stream (F1,F2)

4) Creare an tip buffered named in buffer and


cannot if to file F3.

5) Similarly create op buffered named out buffer


and connect it system.out

6) Then use while loop.


While(cch = buffer.read))! = -1)
{
Outbuffer.write ((char) ch);
}
For reading and writing bytes to f3
7) Close files f1 and f2 buffer and out buffer.

CONCLUSION:-
This we have concatenated and buffer the
contents of two files.
--6--
PROGRAM:-5

Title:- PROGRAM FOR STACK


Theory:-
A stack ison ordered list in which we can insert and
delete the elements are made at one called top.

ADT:-
It is mathematical and logigal description of
datatype is knownas as ADT. There are two types of
stack.
1) Instance
2) Operation.

INSTANCE:-
Stack is a data structure which stores the data in
such a way that we canadd a element or delete an
element from only one side i.e from top of stack i.e stack
follows before scenario i.e the LIFO. & the first element
that we are inserted an the stack will deleted first.

OPERATION:-
There are four types of operation we can
perform on stack i.e push,pop,size can perform on stack.
In which push & pop operation are very important for
implementation of stack.

PUSH(l):-
By using push operation we can insert an
element an to the stack from only one side i.e from only
one side ie from top of stack we cant insert an element
from bottom of stack

POP():-
If we want to delete an element from the stack
then we are using POP() operation in which we can
delete an element from only one end i.e only of stack but
we have to check that whether stack is empty or not.

--7--
ALGORITHM OF STACK:-
Algorithm isempty ()
Return (TOP<0)
Algorithm size ()
Return (size top)
Algorithm push (l)
If (size ==n)
Throw full stock exception
Top = top+1
}
[top] = u
Procedure push (l)
If (size ==n)
Throws full stack exception
Top  top + 1
Procedure pop ()
If (is empty() )
Throws Empty stack Exception
Temp  S[top]
S[top]  null
Top  top -1

EXAMPLE:-
1) Delete an element from bottom
2) Insert an element A from bottom.
3) Insert element B,C,D TO stack.
4) Delete an element from top.
5) Insert an element
E,F,G to stack.
The size of stack
is 6

STEP-I:-
1) Delete an element
from bottom.
As there is no
element present in
stack. Stack
undergoes
A
underflow.

--8--
STEP-II:- 2) Insert an element A

0 top = bottom
=0

STEP-III :- 3) Insert an element


D B, C,D

C
B

A
5

4

3  TOP

1
 BOTTOM
0

--9--
STEP-IV :- 4) Delete an element from top.

C
B

A

3

2  TOP

1
 BOTTOM
0

STEP- V :- 5) Insert an element E,F,G

G 5  TOP

F 4

C
B

A

3

1
 BOTTOM

CONCLUSION:-
Here we are studied stack method.

--10--
PROGRAM:-6
Title:- PROGRAM FOR QUEUE
Theory:-
The queue can be formally defined as ordered
collection of element has two ends name front and rare.
QUEUE AS AN ADT:-
A is mathematical and logigal description of array is
called as ADT. It has two parts Instance and Operations.
INSTANCE:-
The queue is collection of elements in which
element can be inserted by one end called rare and
elements get detected from one end called front.

OPERATIONS:-
Que – full ( ):- Cheks where queue is full or not.
Que – empty ( ) :- Cheks the queue is empty or
not.
Que – insert ( ) :- Insert the element in queuee
from rare end.
Que – delete ( ):- Delete the elements in queue
from fornt end.

QUEUE IMPLEMENTATION:-

1) Linear array
2) Circular array.

1
20 30 40 50
0

Front rear

Queue represent FIFO properties.

--11--
ALGORITHM:-

Procedure isempty( ) –
if (f = = -1)
the queue is empty
Procedure insert (l) –
if (size = = N)
return full Queue Exception.
r = (r + 1 ) % N
A [ r] = L
Procedure delete ( ) :
Is ( isempty (j))

Return empty queue exception


Temp A[ F]
A[ F ] null
F=[f+1]%N

EXAMPLE:-
Create a circular queue of size 6 and perform following
operations.
i) Delete an element from right.
ii) Add A,B,to left.
iii) Add C to left.
iv) Add D,E to right.
v) Delete an element from right.
vi) Add F,G,H to right
vii) Add I to left.
viii) Delete an element from left.
ix) Delete an element from right.

1 2 3 4 5 6
Left
Right

l=r = 0

--12--
1) Delete an element from right.
As there Is no element present in queue, queue underflow occurs.
2) Add A,B to left.
1 2 3 4 5 6
Left
Right A B

l=6, r=1
3) Add C to left

1 2 3 4 5 6
Left
Right A C B

l=5, r=1
4) Add D, E to right

1 2 3 4 5 6
Left
Right A D E C B

l=5, r=3

5) Delete an element from left

1 2 3 4 5 6
Left
Right A D E B

l=6, r=3
--13--
6) Delete an element from right

1 2 3 4 5 6
Left
Right A D B

l=6, r=2

7) Add F,G,H to right

1 2 3 4 5 6
Left
Right A D F G H B

l=6, r=5
8)Add I to left
As queue is full then it undergoes overflow.

9) Delete an element from left


1 2 3 4 5 6
Left
Right A D F G H

l=1, r=5
10) Delete an element from right

1 2 3 4 5 6
Left
Right A D F G

l=1, r=4
CONCLUSION:-
Hence we are studied queue method.

--14--
PROGRAM:7
Title:- QUICK SORT

AIM:- TO IMPLEMENT THE QUICK SORT.


Theory:-
The quick sort is a arrary algorithm that uses the
divide and conquer strategy. In this method division is
dynamically carried out. The three steps of quick sort are
follows.

DIVIDE:-
Split the array into two sub arrays that each
element in the left sub array is less than or equal the
middle element and each element in the right sub array
is greater than the middle element. The splitting of the
arrry into sub array is based on pivot element. All the
elements that are less than pivot should be in the left.
Sub array and all the elements that are move than pivot
should be in right sub array.

CONQUER:-
Recursively sort the two sub array.

COMBINE :-
Combine all the sorted elements in a
group to form a list of sorted elements.
In merge sort the division of array
based on the position of array elements but in
quick sort this division is based on actual value
of elements.

ALGORITHM:-
Public vaid quick ( int low, int high)
{
If m, I;
If (low,high)
{
m = Partition (low, m-1);
quick (low,m-1);
quick (m+1 ,high);
--15--
}
}

Public int partion (int low, int high)


{
Int pivot = A [low], I = low, j = high;
{
Whie ( A [i]< = pivot)
I++;
While ( A[j] > pivot)
J -- ;
If (I < j )
Swap (ijj);
}
Swap (low, j );
Return j;
}
Public void swap ( int ; int j)
{
Int temp;
Temp = A [i];
A[i] = A [j];
A[j] = temp;
}
CONCLUSION:-
Thus we implement quick sort.

EXAMPLE:-

List- 30 70 90 10 50
Step I- 30 70 90 10 50
I j
(low pivot) (high)
Step II- 30 70 90 10 50 (stop incrementing I, and
start pivot i j
decrementing j)
Step III- 30 70 90 10 50 (stop decrementing j)
pivot i j --
16--

Step Iv - 30 10 90 70 50 (Swaping i&j)


pivot i j

Step v - 30 10 90 70 50 (Start increatming i)


pivot i j

Step VI - 30 10 90 70 50 (stop incrementing i,


and start pivot i
decrementing j)

Step VII - 30 10 90 70 50
pivot i,j

Step VIII - 30 10 90 70 50
pivot I j

Step IX - 30 10 50 70 90 (swaping)
pivot middle

Step X - 30 10 50 70 90 (Taking l.sub list -


r.sub list)
l.sub list r.sub list

l.sub list:-
30 10
i j
(pivot low) (high)
Checking condition for I & j Swaping I & j
i.e l.sub list = 1030

R.sub list:-
70 90
i j
(pivot low) (high)
Checking condition for I & j no operation can be
done.
i.e r.sub list = 70 90
started list = 10 30 50 70 90 --
17--
PROGRAM : 8
Title:- MERGE SORT

AIM:- TO IMPLEMENT THE MERGE SORT.


Theory:-
The merge sort is a sorting algorithm that uses
the aivide and conquer algorithm that uses the divide
and conquer strateqy.
In this method division is carried out.
Merge sort on an input array with
n elements consists of three steps.
DIVIDE:-
Partition array into two sub lists S1 abd S2 with n/2
elements each.

CONQUER:- Then sort sub list S1 and sublist S2.


COMBINE:- Merge S1 and S2 into a unique sorted
group.
ALGORITHM:-
Public void merge (int low, int high)
{
Int mid;
If (low<high)
{
Mid= (low + high) /2;
Merge (low, mid);
Merge (mid +1,high)
Combine ( low, mid,high);
}
}
Public void combine (int low,int mid,int high)
{
Int I,j,k;
Int [ ] temp;
Temp = new int [10];
K= low;
J= mid + 1;
While ( i< = mid && j< = high)
{
If (A [ i] <= [i] --
18--

{
Temp [k] = A [j];
I++;
K++;
}
Else
{
Temp [K] = A [j];
J++;
K++;
}
While(i< = mid)
{
Temp [k] = A[i];
I++;
K++;
}
While (j<= high)
{
Temp [k] = A [j];
J++;
K++;
}
For(k = low; k<=high;k++)
A[k] = temp [k];
}
CONCLUSION:-
Thus we implement the merge sort.
--19--

EXAMPLE:-

70 20 30 40 10 50

7 2 3
0 0 0

4 1 5
0 0 0

7 20 3 4 1 5
0 0 0 0 0

20 2070
7 230 70
3
1 2 3 4
10 5
1 7
40 40
14 50
50 50
0 0 0
0 0 0 0 0 000
--20--
PROGRAM:9

TITLE:- INSERTION SORT

AIM:- TO STUDEY THE IMPLEMENTATION OF


INSERTION SORT.

Theory:-
When an array of elements is almost sorted then it is
best case complexity. The best case time complexity of
insertion sort is O (n).

In an arrary is randomly distributed then it results in


average
case time complexity which is O(n2).
In the list of elements is arranged in desending
order if we want to sort the elements in ascending order
then it is in worst case. Time complexity which is O(n2)

ADVANTAGES OF INSERTION SORT:-


1. It is simply to implement.
2. This method is efficient when we want to sort small
number of elements. And this method has excellent
performance on almost sorted list of the elements.
3. More efficient than most other simple O(n2)
algorithms such as selection sort or bubble sort.
4. This is stable does not change the relative order of
equl elements.
5. It is called in place sorting algorithm. The in place
sorting algorithm is an algorithm in which the input
is overitten by output and execute the strong
method it does not require any more additional
space.

--21--

ALGORITHM:-
Public void Insert_ sort (int n)
{
Int temp,j:
For (int i = 1; i< = -1; i++)
{
Temp = A[i];
j= i-1;
while ( ( j>= 0 )& & A[j] > temp ) )
{
A [j+1] = A[j];
J= j-1;
}
A[j+1] = temp;
}
}

CONCLUSION:

Thus we implement the insertion.

EXAMPLE:-
LIST – 20, 50 10 90 40

Step – I:-
20 5 1 9 4
0 0 0 0
Started Unstarted

2 5 1 9 40
0 0 0 0
Step – II
Started Unstarted

--22--

Step – III
1 2 50 9 4
0 0 0 0

Started Unstarted

Step – IV
1 20 5 9 4
0 0 0 0

Started Unstarted
Step –V
1 2 4 5 90
0 0 0 0

Started list

--23--
PORGRAM:-10

TITLE:- PROGRAM TO SHOW IMPLEMENTATION OF LINKED


LIST.

THEORY:-
Linked list is a very common data structure usedto
store sililar data in memory. The elements of linked are
not stored in continues memory location. They are
scattered all over the memory. But these elements are
still boubded to each other. This order and bounding
between thr elements is maintained byexplicit links
between them as shown in fig.
Linear linked list.
Into Next Into Next Into Next
4 2 9 nu
ll
Left
Node-1 node-2 node

Each item in the list is called a node and contains


two fields an information field and next address field.
Some of the function of linear linked list such as
create search and delete are being explained next.

Public node creat (int val)


{
Node new=node(j);
Temp = head;
If (new = = null )
{
s.o.p (“Memory not allowed”);
}
New.data = val;
If (head = null);
{
New.next = head;
} —24--
Else
{
While(temp.next=head;
}
Temp.next=new;
New.next=head;
}
Node search (int key)
{
Node temp; temp = head;
While (temp.next = head)
{
If(temp.data = key)
Return temp;
}
Public void delete(int key)
{
Node temp;
Node temp=head;
While (temp.next). data = key)
{
Temp = temp.next;
}
Temp = temp.next;
Temp.next = temp.next;
}

EXAMPLE:-
1. Create node into next

Null Null

RESULT:-

Thus we have implemented linkedlist.


--25--
PROGRAM: 11
TITLE:- PROGRAM FOR THE APPLICATION OF TREE.
THEOTY:-
A tree is a finite set of elements that is either
empty or is partitioned in to disjoint subsets. The first
subset contains is single element called theroot of the
tree.The other subsets are themselves tree. They are
called subtree of root. These subtree can be emptyeach
elements of a tree is called a need of the tree.
In following programme we have the
implementation of three types of tree which are inorder,
postorder, preorder,
FUNCTION INORDER
Public void inorder (node temp)
{
If (temp; = null)
{
Inorder (temp.left);
System.out print l.n (“ “ + temp.data);
Inorder ( temp.right);
}
}
A
INORDER

B C

F G
E
D E E
D
D D

H I

J
K
In order H D B I K I E A F C
A
--26--
POSTORDER:-
Public posr order (node.temp)
{
If (temp: = null)
A
{
Post order
B (temp.left)
C
{ A E A
System.out.print ln(“”+ temp.data);
F F
} G
D E C
Postorder(temp.right);
C A
} A
} H I
EXAMPLE:-
J

K
INORDER H D B I K I E A F C
G

CONCLUSION:-
Thus we have implemented tree.
A

--27--

PROGRAM:-12

AIM:- PROGRAM FOR IMPLEMENTATION OF GRAPH.

THEORY:-
A graph is collection of nodes which are verticles
connected in pairs by line segments called as edge E.
There are two types of graph.
1) Undirected graph
2)Directed graph
1) Undirected graph:-
In undirected graph where one in each edge is an
undergenerated pair of

EXAMPLE:-

A B

C D

= A-B, A-C, B-A, B-D, C-A, C-D,


D-B, D-C

2) Directed graph:-
It is usually represented as diagraph. It is
represented by a specific direction or by directed pair.

--28--

EXAMPLE:-

A B

D C

= A-B, B-C, C-D, D-A


It has two types of searching algo
1) Depth first search using Adjacency matrix
2) Breadth first search.

ALGORITHM FOR DFS:-


1) Select any node in the graph.
2) Find the adjacent node to node on top at stack and
which is not yet visited.
3) Repeat step until no new adjacent node to the top of
stack node can be found.
4) Repeat step 2 and 3 till stack become empty.

ALGORITHM FOR BFS:-


1) Algorithm with any node and mark it as visited.
2) Find adjacent node to the node marked in step I.
3) Visit the node which is at the front of queue.
4) Repeat 3 still queue is not empty.

CONCLUSION:-
Thus we have implemented program for graph.

--29--

Aims and objectives


1. To improve quality and result 100%
2. Practical facility and well equipment.
3. Internet facility.
4. Well teaching staff.
5. To provide department library facility.
6. To provide department study facility.
7. By giving personal attention towards students.

LIST OF EXPERIMENTS
Sr.No. Name of the Experiment
01. Sorting list using array

02 The Tower at Hanoi

03 Copy Bytes from one files to


another

04 Concatenation and buffering of


files.
05 Program for Stack.

06 Program for Queue

07 To implement the Quick sort.

08 To implement the Merge Sort

09 To study the implementation of


Sort.

10 Program to show implementation of


linked list.

11 Program for the application of tree.

12 Program for implementation of


graph

Detailed Syllabus Lectures


/Week
1 Introduction to data Structures 05
 Defination
 The abstract data Type (ADT)
 Arrays
 Strings
 Recursion
2 File Handling 04
 File organization
 Types of files
 File operations
3 Sorting and Searching 07
A. Sorting
 Insertion Sort
 Selection Sort
 Exchange sort(Bubble, Quick)
 Merge sort
 Heap sort
B. Searching
 Linear search
 Binary search
 Hashing technique and collision handling
4 Stack 03
 The Stack as an ADT
 Representation
 Stack operations
 Applications
5 Queue 03
 The Queue as an ADT
 Representation
 Queue operations
 Circular and Priority Queues
 Applications
6 Linked List 10
 The linked list as an ADT
 Operations on linked list
 Linked stacks and queues
 The linked list as an data structure
 Array implementation of linked list
 Linked list using Dynamic variable
 Comparison of dynamic and Array implementation of
linked list
 Doubly linked list
 Circular Linked list
7 Trees 12
 Basic tree concepts
 Binary tree operations and applications
 Binary tree representations
 Binary tree Traversals
 Threaded Binary tree
 The Huffman algorithm
 Binary search Tree implementations
 Expression trees
 Introductions of multiway tree (B-tree, B+ Trees, AVL
Tree)

8 Graphs 04
 Graph as an ADT
 Graph representation
 Graph Traversal (Depth First Search , Breadth First
Search)

Theory Examination :
1. Question paper will be comprising of total 7 questions, each of 15 marks.
2. Only 5 questions need to be solved.
3. Q.1 will be compulsory and based on entire syllabus.
4. Remaining questions will be mixed in nature. ( e.g. – suppose Q.2 has a
part(a)from, module 3 then
part (b) will be from any module other than module 3.)
5. In question paper weightage of each module will be proportional to number of
respective lecture
hours as mentioned in the syllabus.

Term Work :

Topic for implementations


1. String functions, Recursion and files
2. Implementing of Stack & Queues (Circular and Priority)
3. Implementation of Linked Lists (Singly & Doubly)
4. Implementation of Binary tree
5. Implementations of Graph.

Text Books :
1. Y.Langsam , M.J.Augenstein and A.M.Tanenbaum. , “Data Stures
using JAVA”, Pearson Edition.
2. J.Foley, Van Dam , S.Feiner, J.Highes, “Computer Graphics
Principles and practice”,
3. 2nd edition , Pearson Education , 2003, ISBN 81 -7808-038-9

Reference Books :
• John R. Hubbard and Harry “data Structure with java”- pearson Education
• Mark Allen weiss ”Data Structure and algorithm Analysis in C++ ”. 3rd
EDI-Pearson Education
• Sanjay Pahuja, “A Practical To data Structure & Algorothm”, 1st Edi, New
Age International Edi
• Alan L. Tharp “File Organization and Processing ”,Amazon publication

LABORATORY MANUAL
Department
Of
Computer Engineering
Semester :- III
DATA STRUCTURES & FILES

Вам также может понравиться