lab manual comp
TRANSCRIPT
-
8/7/2019 Lab Manual COMP
1/37
PROGRAM. NO:-1
Title:- Sorting list using arrayTheory:-
Here sorting of list, we are using array. First we are
taking choise of user about ascending or decending orderof list. The elements of array are fixed by programme.
Then by using for loop & condition we are sorting listas per users choise. Arry is a type of data structurewhich contains a group of similar data types that share acommon name and has fixed length. The elements of anarray 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. length3) For decending list
For {int i=0; i
-
8/7/2019 Lab Manual COMP
2/37
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
-
8/7/2019 Lab Manual COMP
3/37
ROGRAM:-2
Title:- The Tower Of HanoiTheory:-
The tower at Honai problems consist of three
peg. A,B & C five disks of differing diameters are placedon peg A so that a arger disk is always below a smallerdisk. The abject is to move the five disks to peg C usingpeg Bas auxillary. Only the top disks an any peg may bemoved to another peg and a larger disk may be neverrest on smaller one.
Basically the tower of Honai problems works onthe principle of recursive function.
Let us consider the general casse of tri disks.
Suppose we had a solution for cn-11 disks we could statethe solution for n disks in terms of solutions for (n-1)disks. This can be done by merely contiuouslysubstracting 1form n to eventually produce 1 or merelymoving a single disk from peg A to C.
Therefore we wil have developed a recursivesoln it we can state a soln for n disks in terms of n-1.
Suppose we could move four disks from peg Ato C. Then we could move the largest disks from A to Cand again apply the soln for four disks from B to C using
the now empty peg A as an auxThus 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 andexit.
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 asauxiliary.FUNCTIONS:-
-
8/7/2019 Lab Manual COMP
4/37
Void towers (int n,char from peg, char. To peg, charaux peg)
RESULT:-Thus we have solved the towers of Hanoi by
using recursive function.--3--
PROGRAM:-3Title:- COPY BYTES FROM ONE FILES TOANOTHER.
Theory:-As we copy characters using file reader or
file writer class, similary we are copying bytefrom onefile to another using file input stream & file outputstram.
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 byusing .exit () we can exit from loop. After all thesethings we have to close it thats 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 8bit byte includea super classes for supportingvarious I/P related functions.
2)OUTPUT STREAM CLASS:-It si an adstract class.
INPUTDATA
INFILE PROGRAMINPUTDATA
INFILE
-
8/7/2019 Lab Manual COMP
5/37
Write ()
ALGORITHM:-
1) Create 2 objects of class file input stream this andfor 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 usingbytes stream.
-
8/7/2019 Lab Manual COMP
6/37
--5--
PROGRAM:-4
Title:- CONCATENATION AND BUFFERING OF FILES.Theory:-
It is possible to combine two or moreinput stresminto a single input stream. This process is known as
concatenation of files and is achieved using the sequenceInput stream class. One of the constructers of this classtakes two input stream object as arguments andcombines them to constructs a single input stream.
Java also supports creation of buffers to storetemporarity data that is read from or written to astream. The process is known as buffered I 10 operation.A buffer sists between the program and the source andfunctions 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 inputconstructor.
3)F3 = new sequence input stream (F1,F2)
4)Creare an tip buffered named in buffer andcannot if to file F3.
5)Similarly create op buffered named out bufferand connect it system.out
6)Then use while loop.
-
8/7/2019 Lab Manual COMP
7/37
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 ofstack.
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 anelement from only one side i.e from top of stack i.e stackfollows before scenario i.e the LIFO. & the first elementthat 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 forimplementation of stack.
PUSH(l):-
-
8/7/2019 Lab Manual COMP
8/37
By using push operation we can insert anelement an to the stack from only one side i.e from onlyone side ie from top of stack we cant insert an elementfrom bottom of stack
POP():-If we want to delete an element from the stack
then we are using POP() operation in which we candelete an element from only one end i.e only of stack butwe have to check that whether stack is empty or not.
--7--
ALGORITHM OF STACK:-Algorithm isempty ()
Return (TOP
-
8/7/2019 Lab Manual COMP
9/37
3)Insert element B,C,D TO stack.4)Delete an element from top.
5) Insert an elementE,F,G to stack.
The size of stackis 6
STEP-I:-1)Delete an element
from bottom.As there is noelement present instack. Stackundergoes
underflow.
--8--STEP-II:- 2) Insert an element A
5
4
3
2
1
0 top = bottom
=0
STEP-III :- 3) Insert an elementB, C,D
A
D
C
B
A
-
8/7/2019 Lab Manual COMP
10/37
5
4
3
TOP
2
1
BOTTOM0
--9--STEP-IV :- 4) Delete an element from top.
5
4
3
2 TOP1
BOTTOM
C
B
A
-
8/7/2019 Lab Manual COMP
11/37
0
STEP- V :- 5) Insert an element E,F,G
5 TOP4
3
2
1
BOTTOM
CONCLUSION:-Here we are studied stack method.
--10--PROGRAM:-6
Title:- PROGRAM FOR QUEUETheory:-
The queue can be formally defined as orderedcollection of element has two ends name front and rare.
G
F
E
C
B
A
-
8/7/2019 Lab Manual COMP
12/37
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 whichelement can be inserted by one end called rare andelements 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 queuefrom fornt end.
QUEUE IMPLEMENTATION:-
1)Linear array2)Circular array.
1
0
20 30 40 50
Front rear
Queue represent FIFO properties.
--11--
ALGORITHM:-
Procedure isempty( )
if (f = = -1)
-
8/7/2019 Lab Manual COMP
13/37
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.
-
8/7/2019 Lab Manual COMP
14/37
1 2 3 4 5 6
Left
Right
l=6, r = 1
3) Add C to left
1 2 3 4 5 6
Left
Right
l=5, r = 1
4) Add D, E to right
1 2 3 4 5 6
Left
Right
l=5, r = 3
5) Delete an element from left
1 2 3 4 5 6
Left
Right
l=6, r = 3
A B
A C B
A D E C B
A D E B
-
8/7/2019 Lab Manual COMP
15/37
--13--
6) Delete an element from right
1 2 3 4 5 6
Left
Right
l=6, r = 2
7) Add F,G,H to right
1 2 3 4 5 6
LeftRight
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
l=1, r = 5
10) Delete an element from right
1 2 3 4 5 6Left
Right
l=1, r = 4
A D B
A D F G H B
A D F G H
A D F G
-
8/7/2019 Lab Manual COMP
16/37
CONCLUSION:-Hence we are studied queue method.
--14--
PROGRAM:7Title:- QUICK SORT
AIM:- TO IMPLEMENT THE QUICK SORT.Theory:-
The quick sort is a arrary algorithm that uses thedivide and conquer strategy. In this method division isdynamically carried out. The three steps of quick sort arefollows.
DIVIDE:-Split the array into two sub arrays that each
element in the left sub array is less than or equal themiddle element and each element in the right sub arrayis greater than the middle element. The splitting of thearrry into sub array is based on pivot element. All theelements 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 inquick sort this division is based on actual valueof elements.
ALGORITHM:-Public vaid quick ( int low, int high)
-
8/7/2019 Lab Manual COMP
17/37
{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 50Step I- 30 70 90 10 50
I j
-
8/7/2019 Lab Manual COMP
18/37
(low pivot) (high)Step II- 30 70 90 10 50 (stop incrementing I, andstart 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 50pivot 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 10i j
(pivot low) (high)Checking condition for I & j Swaping I & ji.e l.sub list = 1030
R.sub list:-
-
8/7/2019 Lab Manual COMP
19/37
70 90i j
(pivot low) (high)Checking condition for I & j no operation can be
done.i.e r.sub list = 70 90started list = 10 30 50 70 90 --
17--
PROGRAM : 8Title:- 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 divideand conquer strateqy.In this method division is carried out.
Merge sort on an input array withn elements consists of three steps.DIVIDE:-
Partition array into two sub lists S1 abd S2 with n/2elements each.
CONQUER:- Then sort sub list S1 and sublist S2.COMBINE:- Merge S1 and S2 into a unique sortedgroup.ALGORITHM:-
Public void merge (int low, int high){
Int mid;If (low
-
8/7/2019 Lab Manual COMP
20/37
{Int I,j,k;Int [ ] temp;Temp = new int [10];K= low;J= mid + 1;
While ( i< = mid && j< = high){If (A [ i]
-
8/7/2019 Lab Manual COMP
21/37
--19--
EXAMPLE:-
70
20
30
70 20 30 40 10 50
40
10
50
70
20 30
40
10
50
70
20
30
40 10
5020 70 30
10 40 5020 30 70 10
40
5010
20
30
40
50
70
-
8/7/2019 Lab Manual COMP
22/37
--20--PROGRAM:9
TITLE:- INSERTION SORT
AIM:- TO STUDEY THE IMPLEMENTATION OFINSERTION SORT.
Theory:-
When an array of elements is almost sorted then it isbest case complexity. The best case time complexity ofinsertion sort is O (n).
In an arrary is randomly distributed then it results inaveragecase time complexity which is O(n2).
-
8/7/2019 Lab Manual COMP
23/37
In the list of elements is arranged in desendingorder if we want to sort the elements in ascending orderthen 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 excellentperformance 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 ofequl elements.
5. It is called in place sorting algorithm. The in place
sorting algorithm is an algorithm in which the inputis overitten by output and execute the strongmethod it does not require any more additionalspace.
--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 ) )
{
-
8/7/2019 Lab Manual COMP
24/37
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
Started Unstarted
20
50
10
90
40
Step IIStarted Unstarted
--22--
Step III10
20
50 90
40
Started Unstarted
Step IV10
20 50
90
40
Started Unstarted
50
10
90
40
-
8/7/2019 Lab Manual COMP
25/37
Step V10
20
40
50
90
Started list
--23--PORGRAM:-10
TITLE:- PROGRAM TO SHOW IMPLEMENTATION OF LINKEDLIST.
THEORY:-Linked list is a very common data structure usedto
store sililar data in memory. The elements of linked arenot stored in continues memory location. They arescattered all over the memory. But these elements arestill boubded to each other. This order and boundingbetween thr elements is maintained byexplicit linksbetween them as shown in fig.
Linear linked list.
-
8/7/2019 Lab Manual COMP
26/37
Into Next Into Next Into Next
LeftNode-1 node-2 node
Each item in the list is called a node and containstwo fields an information field and next address field.
Some of the function of linear linked list such ascreate 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)
4 2 9 null
-
8/7/2019 Lab Manual COMP
27/37
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: 11TITLE:- PROGRAM FOR THE APPLICATION OF TREE.THEOTY:-
A tree is a finite set of elements that is eitherempty or is partitioned in to disjoint subsets. The firstsubset contains is single element called theroot of thetree.The other subsets are themselves tree. They arecalled subtree of root. These subtree can be emptyeachelements of a tree is called a need of the tree.
In following programme we have theimplementation of three types of tree which are inorder,postorder, preorder,FUNCTION INORDER
Public void inorder (node temp)
-
8/7/2019 Lab Manual COMP
28/37
{If (temp; = null){Inorder (temp.left);System.out print l.n ( + temp.data);Inorder ( temp.right);
}}
INORDER
In order H D B I K I E A F CA
--26--POSTORDER:-
Public posr order (node.temp){If (temp: = null){Post order (temp.left)
{System.out.print ln(+ temp.data);}Postorder(temp.right);}}
EXAMPLE:-
A
B
D
C
E
D
FE
D
GE
D
H I
J
K
A
C
A
B
A
D
E
F
E
C
A
F
C
A
G
H I
J
K
-
8/7/2019 Lab Manual COMP
29/37
INORDER H D B I K I E A F CG
CONCLUSION:-Thus we have implemented tree.
--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 graph2)Directed graph
-
8/7/2019 Lab Manual COMP
30/37
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
-
8/7/2019 Lab Manual COMP
31/37
It has two types of searching algo1)Depth first search using Adjacency matrix2)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%
-
8/7/2019 Lab Manual COMP
32/37
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 EXPERIMENTSSr.No. Name of the Experiment
01. Sorting list using array
02 The Tower at Hanoi
03 Copy Bytes from one files toanother
04 Concatenation and buffering offiles.
-
8/7/2019 Lab Manual COMP
33/37
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 ofSort.
10 Program to show implementation oflinked list.
11 Program for the application of tree.
12 Program for implementation ofgraph
Detailed Syllabus Lectures
/Week
1 Introduction to data Structures
Defination
The abstract data Type (ADT)
Arrays
Strings
Recursion
05
2 File Handling
File organization
Types of files
File operations
04
-
8/7/2019 Lab Manual COMP
34/37
3 Sorting and Searching
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
07
4 Stack
The Stack as an ADT
Representation Stack operations
Applications
03
5 Queue
The Queue as an ADT
Representation
Queue operations
Circular and Priority Queues
Applications
03
6 Linked List 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
10
7 Trees
Basic tree concepts
Binary tree operations and applications
Binary tree representations
12
-
8/7/2019 Lab Manual COMP
35/37
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
Graph as an ADT
Graph representation
Graph Traversal (Depth First Search , Breadth First
Search)
04
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 ofrespective 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 :
-
8/7/2019 Lab Manual COMP
36/37
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++ . 3 rd
EDI-Pearson Education
Sanjay Pahuja, A Practical To data Structure & Algorothm, 1 st Edi, New
Age International Edi
Alan L. Tharp File Organization and Processing ,Amazon publication
LABORATORY MANUAL
Department
Of
Computer Engineering
-
8/7/2019 Lab Manual COMP
37/37
Semester :- III
DATA STRUCTURES & FILES