Download PRACTICAL: 4(B)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lattice model (finance) wikipedia , lookup

Red–black tree wikipedia , lookup

Quadtree wikipedia , lookup

Linked list wikipedia , lookup

Interval tree wikipedia , lookup

B-tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL: 1
AIM:
A. 1. Write a program to access a value of a variable using pointer.
2. Write a program to use of pointer in an array.
3. Write a C Program to swap two numbers using pointers and function.
B. 1. Write a program to display marks of three subjects of student using structure.
2. Write a program to access structure member using pointer variable for above
prog.
C. Write a program to add n numbers using DMA.
SOFTWARE REQUIRED: TC
KNOWLEGDE REQUIRED: Pointer and Dynamic Memory allocation.
THEORY/LOGIC:
Pointer: It is a variable which is used to store the address of the variable which is of same type.
Dynamic Memory Allocation: It is a method to allocate the memory at run time.
ALGORITHM:
PROGRAM:
QUESTIONS:
1) What are the differences between pointer variable and normal variable?
2) Differentiate: malloc( ) and calloc( ) function.
3) Why do we need pointer variable?
1
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL: 2
AIM: Searching : (Input size of array from user and allocate memory using DMA)
1.1 : Input an array of N elements and search a number using Linear Search.
1.2 : Input an array of N elements in ascending order and search number using iterative Binary
Search.
1.3 : Use recursive Binary Search for second option.SOFTWARE REQUIRED: TC
KNOWLEGDE REQUIRED: pointer, searching
THEORY/LOGIC:
Searching : To search means to find particular element in given data. If present then successful
search otherwise unsuccessful search.
ALGORITHM:
1 LINEAR search method.
2 Iterative BINARY search method.
3 Recursive BINARY search method
PROGRAM:
CONCLUSION:
QUESTIONS:
1) Differences between linear search and binary search.
2) Explain complexity of linear and binary search.
2
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL: 3
AIM: Sorting: (Input size of array from user and allocate memory using DMA)
2.1: BUBBLE SORT that arranges data in ascending order
2.2: SELECTION SORT that arranges data in descending order
SOFTWARE REQUIRED: TC
KNOWLEGDE REQUIRED: Pointers, Sorting
THEORY/LOGIC:
Bubble sort : Sorting algorithm which sorts by comparing each adjacent pair of values in a list in
turn, swapping the values if necessary, and repeating the pass through the list until there are no
more swaps.
Selection sort : Sorting algorithm that repeatedly looks through the remaining values to search
for the lowest value in order to move it to its final location.
ALGORITHM:
Bubble_Sort(K,N)
1) LAST  N
2) Repeat thru step 5
for PASS  1, 2, 3, . . . . . . N-1
3) EXCHS  0
4) Repeat for i 1, 2, . . . . . LAST-1
if K[ i ] >= K[ i+1]
then
K[ i ] > K[ i+1]
EXCHS  EXCHS + 1
5) if EXCHS = 0
then
Return
else
LAST  LAST - 1
6) Return
3
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
Selection_Sort(K,N)
1)Repeat thru step 4
for PASS  1, 2, . . . . . . N-1
2)
MIN_I  PASS
3)
Repeat for I  PASS + 1, PASS + 2, . . . . . . N
if K[ i ] < K[ MIN_I]
then
MIN_I  I
4)
if MIN_I != PASS
then
K[PASS] > K[ MIN_I ]
5) Return
PROGRAM:
QUESTIONS:
1) What is Complexity? Explain types of Complexity.
2) Compare Bubble sort and Selection sort.
4
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL: 4
AIM: Write a program that enters Infix expression, verify validity and convert it into Postfix
expression (if valid) using Stack.
SOFTWARE REQUIRED: TC
KNOWLEGDE REQUIRED: Stack and Infix, Postfix and Prefix expressions.
THEORY/LOGIC:
Stack: It works in a LIFO manner. Operations are performed from the one end only.
ALGORITHM: Infix to Postfix expression without parenthesis and with parenthesis.
1 ) Infix to Postfix expression without parenthesis
Symbol
Precedence Rank
f
r
+, –
*, /
1
–1
2
–1
a,b,c…
#
3
0
1
-
1. [Initialize the stack]
TOP ← 1
S[TOP] ← '#'
2. [Initialize output string and rank
count]
POLISH ← ' '
RANK ← 0
3. [Get first input symbol]
NEXT ← NEXTCHAR(INFIX)
4. [Translate the infix expression]
Repeat thru step 6 while NEXT ≠ '#'
5. [Remove symbols with greater or equal precedence from stack]
5
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
Repeat while f(NEXT) ≤ f(S[TOP])
TEMP ← POP(S,TOP)
POLISH ← POLISH ○ TEMP
RANK ← RANK + r(TEMP)
If RANK < 1
then Write ('INVALID')
Exit
6. [Push current symbol onto stack & obtain next input symbol]
Call PUSH (S,TOP,NEXT)
NEXT ← NEXTCHAR(INFIX)
7. [Remove remaining elements from stack ]
Repeat while S[TOP] ≠ '#'
TEMP ← POP(S,TOP)
POLISH ← POLISH ○ TEMP
RANK ← RANK + r(TEMP)
If RANK < 1
then Write ('INVALID')
Exit
8. [Is the expression valid?]
If RANK = 1
then Write('VALID')
else Write('INVALID')
Exit
2 ) Infix to Postfix expression with parenthesis
Symbol
I/P
Stack
Precedence f Precedence g
1
+, –
3
*, /
6
^ or ⬆ or
$
7
Variables
9
(
0
)
1. [Initialize stack]
TOP ← 1
S[TOP] ← '('
Rank
r
2
4
5
–1
–1
–1
8
0
-
1
-
6
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
2. [Initialize output string & ra nk count]
POLISH ← ' '
RANK ← 0
3. [Get first input symbol]
NEXT ← NEXTCHAR(INFIX)
4. [Translate the infix expression]
Repeat thru step 7 while NEXT ≠ ' '
5. [Remove symbols with greater precedence from stack]
If TOP < 1
then Write ('INVALID')
Exit
Repeat while f(NEXT) < g(S[TOP])
TEMP ← POP(S,TOP)
POLISH ← POLISH ○ TEMP
RANK ← RANK + r(TEMP)
If RANK < 1
then Write ('INVALID')
Exit
6. [Are there matching parentheses?]
If f (NEXT) ≠ g (S[TOP])
then Call PUSH (S,TOP,NEXT)
else POP(S,TOP)
7. [Get next input symbol]
NEXT ← NEXTCHAR(INFIX)
8. [Is the expression valid?]
If TOP ≠ 0 OR RANK ≠ 1
then Write('INVALID')
else Write('VALID')
Exit
PROGRAM:
QUESTIONS:
Convert given infix expression into postfix expression:
a+b*(c+d)/f+d*e
7
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL: 5
AIM:
4.1 Write a program to check whether the given string is palindrome or not using Stack.
4.2 Write a program that enters valid Postfix expression and evaluate the expression using Stack.
SOFTWARE REQUIRED: Turbo C/Code block/Netbeans
KNOWLEDGE REQUIRED: All Stack Operations
THEORY/LOGIC:
4.1 Palindrome String
4.2 Postfix Evaluation
1. Find the leftmost operator in the expression.
2. Select two operands immediately to the left of the operator found.
3. Perform the indicated operation.
4. Replace the operator and operands with the result.
ALGORITHM:
PROGRAM:
QUESTIONS:
1. List out applications of stack.
2. Compare Stack and Queue Data Structure.
8
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL 6
AIM: Write a program to implement Simple Queue with all operations. Check the queue
contents and conditions with different combinations of insert and delete.
SOFTWARE REQUIRED: TC
KNOWLEDGE REQUIRED: Queue
THEORY/LOGIC:
ALGORITHM:
Procedure QINSERT (Q, F, R, N, Y). Given F and R, pointers to the front and rear elements of
a queue, a queue Q consisting of N elements, and an element Y, this procedure inserts Y at the
rear of the queue. Prior to the first invocation of the procedure, F and R have been set to zero.
1. [Overflow?]
If R >= N
then Write (“OVERFLOW”)
Return
2. [Increment rear pointer]
R←R+1
3. [Insert element]
Q [R] ← Y
4. [Is front pointer properly set?]
If F=0
then F ←1
Return
□
-------------------------------------------------------------------------------------------------------------------------9
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
Function QDELETE (Q, F, R). Given F and R, the pointers to the front and rear elements of a
queue, respectively, and the queue Q to which they correspond, this function deletes and
returns the last element of the queue. Y is a temporary variable.
1. [Underflow?]
If F = 0
then Write (“UNDERFLOW”)
Return(0)
(0 denotes an empty queue)
2. [Delete element]
Y ← Q [F]
3. [Queue empty? ]
If F = R
then F ← R ← 0
else F ← F + 1 (Increment Front pointer)
4. [Return element]
Return (Y)
□
PROGRAM:
QUESTIONS:
1. List out applications of Queue.
10
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL 7
AIM: Write a program to implement Circular Queue with all operations. Check the queue
contents and conditions with different combinations of insert and delete.
SOFTWARE REQUIRED: TC
KNOWLEDGE REQUIRED: Circular Queue
THEORY/LOGIC:
ALGORITHM:
Procedure CQINSERT (F, R, Q, N, Y). Given pointers to the front and rear elements of a
circular queue, F and R, a vector Q consisting of N elements, and an element Y, this procedure
inserts Y at the rear of the queue. Initially, F and R are set to zero.
1. [Reset rear pointer?]
If R = N
then R ← 1
else R ← R + 1
2. [Overflow?]
If F = R
then Write (“OVERFLOW”)
Return
3. [Insert element]
Q [R] ← Y
4. [Is front pointer properly set?]
If F = 0
then F ←1
Return
□
--------------------------------------------------------------------------------------------------------------------------
11
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
Function CQDELETE ( F, R, Q, N). Given F and R, pointers to the front and rear elements of a
circular queue, respectively, and a vector Q consisting of N elements, this function deletes and
returns the last element of the queue. Y is a temporary variable.
1. [Underflow?]
If F = 0
then Write (“UNDERFLOW”)
Return(0)
2. [Delete element]
Y ← Q [F]
3. [Queue empty? ]
If F = R
then F ← R ← 0
Return (Y)
4. [Increment front pointer]
If F = N
then F ← 1
else F ← F + 1
Return (Y)
PROGRAM:
QUESTIONS:
1. Show the content of circular queue with front and rear pointer after each operation.
Initially queue is empty. Size of queue is 5. The sequence of operation given below:
 Insert 10, 50, 40, 80
 Delete
 Insert 200, 70, 150
 Delete
 Delete
 Delete
2. Compare Simple queue and Circular queue.
12
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL 8
AIM: Write a program to implement following operations of the Singly Linked List.
a) Insert a node at the front of the linked list.
b) Insert a node at the end of the linked list.
c) Delete a node before specified value.
SOFTWARE REQUIRED: TC
KNOWLEDGE REQUIRED: Concepts Regarding Linked List.
THEORY/LOGIC: A list has been defined which consists of ordered set which may vary in
number. Simple way to represent linear list is to expand each node to contain the link or pointer
to the next node. This representation is called one way chain or singly linked linear list.
ALGORITHM:
Function INSERT (X, FIRST). Given X, a new element, and FIRST, a pointer to the first element
of a linked linear list whose typical node contains INFO and LINK fields, this function inserts X.
AVAIL is a pointer to the top element of the availability stack; NEW is a temporary pointer
variable. It is required that X precedes the node whose address is given by FIRST.
1. [Underflow?]
If AVAIL = NULL
then Write (“AVAILABILITY STACK UNDERFLOW”)
Return(FIRST)
2. [Obtain address of next free node]
NEW ← AVAIL
3. [Remove free node from availability stack]
AVAIL ← LINK(AVAIL)
4. [Initialize fields of new node and its link to the list]
INFO(NEW) ← X
LINK(NEW) ← FIRST
5. [Return address of new node]
13
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
Return(NEW)
□
Function INSEND (X, FIRST). Given X, a new element, and FIRST, a pointer to the first element
of a linked linear list whose typical node contains INFO and LINK fields, this function inserts X.
AVAIL is a pointer to the top element of the availability stack; NEW and SAVE are temporary
pointer variables. It is required that X be inserted at the end of the list.
1. [Underflow?]
If AVAIL = NULL
then Write (“AVAILABILITY STACK UNDERFLOW”)
Return(FIRST)
2. [Obtain address of next free node]
NEW ← AVAIL
3. [Remove free node from availability stack]
AVAIL ← LINK(AVAIL)
4. [Initialize fields of new node]
INFO(NEW) ← X
LINK(NEW) ← NULL
5. [Is the list empty?]
If FIRST = NULL
then Return(NEW)
6. [Initiate search for the last node]
SAVE ← FIRST
7. [Search for end of list]
Repeat while LINK(SAVE) ≠ NULL
SAVE ← LINK(SAVE)
14
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
8. [Initialize fields of new node]
LINK(SAVE) ← NEW
9. [Return address of first node]
Return(FIRST)
□
PROGRAM:
QUESTIONS:
1) What is circularly linked list?
2) Write the steps to insert P after N to the given singly linked list.
15
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL 9
AIM: Write a program to implement Doubly Linked List with insert & delete operations.
SOFTWARE REQUIRED: TC
KNOWLEDGE REQUIRED:
THEORY/LOGIC:
ALGORITHM:
PROGRAM:
Procedure DOUBINS (L, R, M, X). Given a doubly linked linear list whose left-most and rightmost node addresses are given by the pointer variables L and R, respectively, it is required to
insert a node whose address is given by the pointer variable NEW. The left and right links of a
node are denoted by LPTR and RPTR, respectively. The information field of a node is denoted by
the variable INFO. The name of an element of the list is NODE. The insertion is to be performed
to the left of a specified node with its address given by the pointer variable M. The information
to be entered in the node is contained in X.
1. [Obtain new no0de from availability stack]
NEW ⇐ NODE
2. [Copy information field]
INFO(NEW) ← X
3. [Insert into an empty list?]
If R = NULL
then LPTR(NEW) ← RPTR(NEW) ← NULL
L ← R ← NEW
Return
4. [Left most insertion?]
If M = L
then LPTR(NEW) ← NULL
16
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
RPTR(NEW) ← M
LPTR(M) ← NEW
L ← NEW
Return
5. [Insert in middle]
LPTR(NEW) ← LPTR(M)
RPTR(NEW) ← M
LPTR(M) ← NEW
RPTR (LPTR(NEW)) ← NEW
□
Return
Procedure DOUBDEL (L, R, OLD). Given a doubly linked linear list with the addresses of the
left-most and right-most nodes given by the pointer variables L and R, respectively, it is required
to delete the node whose address is contained in the variable OLD. Nodes contain left and right
links with names LPTR and RPTR, respectively.
1. [Underflow?]
If R = NULL
then Write(“UNDERFLOW”)
Return
2. [Delete node]
If L = R
then L ← R ← NULL
else If OLD = L
(Left-most node being deleted)
then L ← RPTR(L)
LPTR(L) ← NULL
else If OLD = R
(Right-most node being deleted)
then R ← LPTR(R)
17
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
RPTR(R) ← NULL
else RPTR (LPTR(OLD)) ← RPTR (OLD)
LPTR (RPTR(OLD)) ← LPTR (OLD)
3. [Return deleted node]
Restore(OLD)
□
Return
QUESTIONS:
1) Define following Terms:
Binary Tree, Binary Search Tree, Threaded Binary Tree, Heap Tree, AVL Tree,
Complete Binary Tree, Full Binary Tree.
2) List out Advantages and Disadvantages of Doubly linked list over Singly Linked List
18
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL 10
AIM: Implement a program that creates Binary Search Tree and display it.
SOFTWARE REQUIRED: TC
KNOWLEDGE REQUIRED: Binary Tree
THEORY/LOGIC:
Definition: A binary search tree is a binary tree. It may be empty. If it is not empty then it
satisfies the following properties:
(1) The root element has a key.
(2) The keys (if any) in the left subtree are smaller than the key in the root.
(3) The keys (if any) in the right subtree are larger than the key in the root.
(4) The left and right subtrees are also binary search trees.
ALGORITHM:
CREATE_TREE (INFO, NODE)
NODE: structure type variable to point both left and right child
INFO: Information or data stored at node.
1. [Check whether the tree is empty]
If NODE = NULL
INFO (NODE) ← INFO
LEFT_CHILD (NODE) ← NULL
RIGHT_CHILD (NODE) ← NULL
2. [Test for the left child]
If INFO (NODE) ≥INFO
LEFT_CHILD (NODE) ← call CREATE_TREE(INFO, LEFT_CHILD(NODE))
RIGHT_CHILE (NODE) ← call CREATE_TREE(INFO, RIGHT_CHILD(NODE))
3. [Finish]
Return (NODE)
PROGRAM:
QUESTIONS:
1) Construct Binary Search Tree for following Data:
Judy , Mary, Bill, Tom, Fred, Jane, Joe, Dave, Alice
19
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL: 11
AIM: Write a program to implement various Tree traversal techniques.
SOFTWARE REQUIRED: TC
KNOWLEDGE REQUIRED: Binary Tree
THEORY/LOGIC:
ALGORITHM:
Preorder(T)
1) if(T= = NULL)
then
write("Empty Tree")
return
2) write(INFO(T))
3) Preorder(LPTR(T))
4) Preorder(RPTR(T))
Inorder(T)
1) if(T= = NULL)
then
write("Empty Tree")
return
2) Inorder(LPTR(T))
3) write(INFO(T))
4) Inorder(RPTR(T))
Postorder(T)
1) if(T= = NULL)
then
write("Empty Tree")
return
2) Preorder(LPTR(T))
3) Preorder(RPTR(T))
4) write(INFO(T))
PROGRAM:
QUESTIONS:
1. Write Preorder, Postorder and Inorder Traversal for above tree.
2. Draw BST for given Preorder and Inorder traversal
Preorder: 7,10,4,3,1,2,8,11
Inorder: 4,10,3,1,7,11,8,2
20
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL 12
AIM: Write a program to implement BFS and DFS Graph traversal techniques
SOFTWARE REQUIRED: TC
KNOWLEDGE REQUIRED: Graph Concepts
THEORY/LOGIC:
ALGORITHM:
Graph Traversal :
Search for a certain node or traverse all nodes in the graph
Depth First Search
Once a possible path is found, continue the search until the end of the path
Breadth First Search
Start several paths at a time, and advance in each one step at a time
BFS :
Queue is used in the implementation of BFS. BFS visits the nodes level by level, so it will start
with root node than it moves to next level.
A
B
E
C
D
F
BFS : A B C D E F
ALGORITHM:
BFS:
1. Push the root/starting node in the Queue.
2: Repeat up to step 4 until the queue is empty.
3: Remove the node from the Queue.
4: If the removed node has unvisited child nodes, mark them as visited and insert the unvisited
children in the queue. Go to step 2.
21
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
Depth First Search
Once a possible path is found, continue the search until the end of the path.
1. Push the root/ starting node in the Stack.
2: Repeat until stack is empty.
3: Peek the node of the stack.
4: If the node has unvisited child nodes, get the unvisited child node, mark it as traversed and
push it on stack.
5: If the node does not have any unvisited child nodes, pop the node from the stack. Go to step 2.
PROGRAM:
QUESTIONS:
1) Compare BFS and DFS
22
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
PRACTICAL 13
AIM: In an array of 20 elements, arrange 15 different values, which are generated randomly
between 1,00,000 to 9,99,999. Use hash function to generate key and linear probing to avoid
collision. H(x) = (x mod 18) + 2. Write a program to input and display the final values of array.
SOFTWARE REQUIRED: TC
KNOWLEDGE REQUIRED: Hashing concepts
THEORY/LOGIC:
ALGORITHM:
PROGRAM:
/* Generate data randomly & store using hashing with Linear probing*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
unsigned long int arr[20]={0},temp;
int i,count=0,hk;
clrscr();
printf("Data Generated Randomly:\n");
for(i=1;;i++)
{
if(count<20)
{
temp=(rand()%10000000)*7;
/*Generate random data*/
/*To select data in the given range*/
if(temp>=100000L && temp<=999999L)
{
hk=temp%18+2;
/*Hashing Function*/
printf("\nHashKEY=%d ->\t%lu",hk,temp);
if(arr[hk]!=0)
{
while(arr[hk]!=0) /*Linear Probing*/
{
23
MARKS OBTAINED
SIGN OF FACULTY
CSPIT (IT)
IT214 Data Structures & Files
hk++;
if(hk>20)
hk=0;
}
arr[hk]=temp;
/*Store Data in the array*/
}
else
arr[hk]=temp;
count++;
}
}
else break;
}
printf("\n\n ARRAY after Linear Probing: ");
for(i=0;i<=19;i++)
printf("\narr[%d] -> %lu",i,arr[i]);
getch();
}
QUESTIONS:
1) Given the following set of keys to insert into a hash table that holds exactly 11 values: 113
,117 , 97 , 100 , 114 , 108 , 116 , 105 , 99 Which of the following best demonstrates the contents
of the has table after all the keys have been inserted using linear probing?
(a) 100, __, __, 113, 114, 105, 116, 117, 97, 108, 99
(b) 99, 100, __, 113, 114, __, 116, 117, 105, 97, 108
(c) 100, 113, 117, 97, 14, 108, 116, 105, 99, __, __
(d) 117, 114, 108, 116, 105, 99, __, __, 97, 100, 113
24
MARKS OBTAINED
SIGN OF FACULTY