You are on page 1of 19

CS 2208 DATA STRUCTURES LAB 0 0 3 2

AIM:
To develop programming skills in design and implementation of data
structures and
their applications.

1. Implement singly and doubly linked lists.


2. Represent a polynomial as a linked list and write functions for
polynomial
addition.
3. Implement stack and use it to convert infix to postfix expression
4. Implement a double-ended queue (dequeue) where insertion and
deletion operations are possible at both the ends.
5. Implement an expression tree. Produce its pre-order, in-order, and
postorder traversals.
6. Implement binary search tree.
7. Implement insertion in AVL trees.
8. Implement priority queue using binary heaps
9. Implement hashing with open addressing.
10. Implement Prim's algorithm using priority queues to find MST of an
undirected graph.

Ex.No:1 Singly Linked List


Aim:

Write a C program to implement singly linked list operation.

Algorithm:

Step1: Initiate the list

Step2: Declare the functions additem, list items, delnode, insert.

Step3: Get the option from the user for list process.

Step4: In addtem process give the number of nodes and nodes value.

Step5: In listitems process display the items untill the current node point’s null
value.

Step6: In delnode process check the list status, if not null get the value of
deleted

node and delete the appropriate node in the list.

Step7: In insert process give the value of the new node and give the new node
position

in the existing list.

Step8: After the entire process give the option to exit from the program.
Ex.No:2 Doubly Linked List

Date:

Aim:

Write a C program to implement doubly linked list operation.

Algorithm:

Step 1: Initialize pointers start & last as NULL.

Step 2: create a doubly linked list by inserting each node at front.

Step 3: Print the Menu.

Step 4: If the choice is 1, then enter the element to be inserted to the left of the

already existing node in a doubly linked list.

Step 5: If the choice is 2, then delete all the occurrences of the element to be

deleted in the doubly linked list.

Step 6: If the choice is 3, then display all the elements in doubly linked list.

Step 7: If the choice is 4, then exit.

Step 8: Repeat steps from 3 to 7 until the choice is 7.

Step 9: Stop.
Ex.No:3 Polynomial Addition

Date:

Aim:

Write a C program to Represent a polynomial as a linked list and write


functions for polynomial addition

Algorithm:

Step1: Start the program

Step2: Initiate the pointers to each polynomial expressions

Step3: Get the value of co – efficient and exponent value of two polynomial
expressions

Step4: Compare the exponent of two polynomial expressions.

Step5: If the exponent values are same add the polynomial co – efficient.

Step6: If the exponent values are different add the biggest exponent’s co –
efficient value in to the result polynomial.

Step7: Print the result polynomial expression.

Step8: Stop the program.


Ex.No:4 Infix to Postfix Conversion

Date:

Aim:

Write a C program to Implement stack and use it to convert infix to postfix


expression

Algorithm:

Step1: Start the program.

Step2: Initialize the stack.

Step3: Read the given infix expression into string called infix.

Step4: If the character is an operand, place it on the output.

Step5: If the character is an operator, push it on to the stack. if the stack


operator has a higher or equal priority than input operator then pop that
operator from the stack and place it onto the output.

Step6: If the character is a left parenthesis, push it onto the stack.

Step7: If the character is a right parenthesis, pop all operators from the stack till
it encounters left parenthesis, discard both the parenthesis in the output.

Step8: Display the postfix expression for the given infix expression.

Step9: Stop the program.

Ex.No:5 Double Ended Queue


Date:

Aim:

Write a C program to Implement a double-ended queue (dequeue) where


insertion and deletion operations are possible at both the ends.

Algorithm:

Step1: Start the program.

Step2: Initialize the queue.

Step3: Declare the following function Rinsert(), RDelete(), LInsert() and LDelete()
to insert and delete an element into and from the queue.

Step4: Declare the function Display to show the elements of queue.

Step5: Procedure Linsert()

 [Create new node]


 [Insert item into the newnode temp]
 [Next pointer of newnode points to start node]
 [Now newnode temp is start node]

Step6: Procedure Ldelete()


 [Check for Underflow condition]
 [Consider temp as start node]
 [Move the start pointer and assign the info of temp to item]
 [Delete front node. Also return the item to be deleted]
Step7: Procedure Rinsert()
• [Create a newnode and insert data into it]
• [Consider temp as start node]
• [Link newnode by checking whether the list contains nodes or
not]
Step7: After the entire process give the option to exit from the program.
Step8: Stop the program.

Ex.No:6 Expression Tree


Date:

Aim:

Write a C program to implement an expression tree. Produce its pre-order,


in-order, and post order traversals.

Algorithm:

Step1: Start the program

Step2: Declare the function inorder(), postorder(), preorder() to show the in –


order,

pre – order and post – order traversals

Step3: Read the input in the form of postfix expression

Step4: Give the option the execute the above function

Step5: Option [i] to execute the infix() function

Tree traversal of this function is Left Child RootRight Child

Option [r] to execute the prefix() function

Tree traversal of this function is Root Left ChildRight Child

Option [o] to execute the postfix() function

Tree traversal of this function is Left Child Right ChildRoot

Step6: Stop the program.


Ex.No:7 Binary Search Tree

Date:

Aim:

Write a C program to implement binary search tree.

Algorithm:

Step1: Start the program

Step2: Declare the functions insert(), find(), findmin().

Step3: Get the number of element to be insert

Step4: Read the element values.

Step5: The find() function gets the value of element to be search and return the
element

Step6: The findmin() function display the minimum value element of the tree.

Step7: Stop the program.


Ex.No:8 Prim’s Algorithm

Date:

Aim:

Write a C program to Implement Prim's algorithm using priority queues to


find MST of an undirected graph.

Algorithm:

Step 1: Declare the lowcost,closest,cost matrix arrays and other temporary


variables.

Step 2: Get no.of edges, no.of vertices and the edges & their cost.

Step 3: Assign lowcost of all vertices from 2 to n to cost of first vertex to the
vertex and assign closest of all vertices to 1.

Step 4: For all the vertices 2 to n Repeat the following steps.

Assign lowcost of 2 to min,

Assign 2 to k,

For all vertices 3 to n Repeat the following.

Check whether lowcost of j value < min, if so assign lowcost of j to


min

and assign j to k.

Print k and closest ot k values.

Assign lowcost of value to infinity value

Step 5: For all values from 2 to n, repeat the following,

Check whether cost of j,k < lowcost of j,and lowcost of j < infinity,

If so assign cost of k,j value to lowcost of j and assign cost of k to


closest of j.
Step 6: Exit from the program.
Ex.No:9 AVL Tree

Date:

Aim:

Write a C program to implement insertion in AVL trees.

Algorithm:

Step 1: Start the program

Step 2: repeat the step3 while there are elements to be inserted

Step 3:

i) if t= NULL , insert the new node as the root node


ii) else if the value of x is less than the element at t
insert the node at left subtree

(a) if the node is not balanced and if x is less than

the element at left of t rotate left

else double rotate left

iii) else if the value of x is greater than the element at t

insert the node at right subtree

(a) if the node is not balanced and if x is greater

than the element at right of t rotate right

else double rotate right

Step 4: Display the element of the tree


Ex.No:10 Hashing

Date:

Aim:

Write a C program to implement hashing.

Algorithm:

Step1: Start the program

Step2: Create the main function and declare the functions create(), linearprob
and display().

Step3: Create function for generating the hash key and return the hash key.

Step4: The linearprob() function handles the collision.

Step5: If the location indicate by the hash key is empty and placed the number in
the hash table.

Step6: The display function displays the hash table.

Step7: Exit of the Program.


Data Structure Viva Questions

1. What is data structure?


A data structure is a way of organizing data that considers not only the items stored,
but also their relationship to each other. Advance knowledge about the relationship
between data items allows designing of efficient algorithms for the manipulation of
data.

2. List out the areas in which data structures are applied extensively?
The names of areas are:
• Compiler Design,
• Operating System,
• Database Management System,
• Statistical analysis package,
• Numerical Analysis,
• Graphics,
• Artificial Intelligence,
• Simulation

1. What are the major data structures used in the following areas: RDBMS,
Network data model & Hierarchical data model?
The major data structures used are as follows:
• RDBMS - Array (i.e. Array of structures)
• Network data model - Graph
• Hierarchical data model - Trees

1. Minimum number of queues needed to implement the priority queue?

Two. One queue is used for actual storing of data and another for

storing priorities.

2. What is the data structures used to perform recursion?


Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so
knows whom to return when the function has to return. Recursion makes use of

system stack for storing the return addresses of the function calls.

Every recursive function has its equivalent iterative (non-recursive) function.


Even when such equivalent iterative procedures are written, explicit stack is to be
used.
3. What are the notations used in Evaluation of Arithmetic Expressions using
prefix and postfix forms?
Polish and Reverse Polish notations.

4. What is a Register?
A register is a small amount of memory within the CPU that is used to temporarily
store instructions and data.

5. An _________ data type is a keyword of a programming language that


specifies the amount of memory needed to store data and the kind of data
that will be stored in that memory location?
Answer: Abstract

6. What are the different Abstract Data Type Groups?


Integer, Floating-Type, Character & Boolean are the four different data type groups
Explanation: You determine the amount of memory to reserve by determining the
appropriate abstract data type group to use and then deciding which abstract data
type within the group is right for the data. The different abstract data type groups
are Integer, Floating-Type, and Character & Boolean.

7. Which of the following abstract data types are NOT used by Integer Abstract
Data type group?
• Short
• Int
• Float
• long
Explanation: The integer abstract data type group consists of four abstract data
types used to reserve memory to store whole numbers: byte, short, int , and long

1. What are all the applications for the tree data structure?
• Manipulation of the arithmetic expressions.
• Symbol table construction.
• Syntax analysis.
1. Which data structure is used to perform recursion?
The answer is Stack. Stack has the LIFO (Last In First Out) property; it
remembers it's ‘caller’. Therefore, it knows to whom it should return when the
function has to return. On the other hand, recursion makes use of the system stack
for storing the return addresses of the function calls.

Every recursive function has its equivalent iterative (non-recursive) function.


Even when such equivalent iterative procedures are written explicit, stack is to be
used.

2. Which data structures algorithm used in solving the eight Queens problem?
Backtracking

3. In an AVL tree, at what condition the balancing is to be done?


If the "pivotal value", or the "height factor", is greater than one or less than
minus one.

4. There are 8, 15, 13, and 14 nodes in four different trees. Which one of them
can form a full binary tree?
The answer is the tree with 15 nodes. In general, there are 2^n-1 nodes in a full
binary tree.

By the method of elimination:

Full binary trees contain odd number of nodes, so there cannot be full binary
trees with 8 or 14 nodes. Moreover, with 13 nodes you can form a complete binary
tree but not a full binary tree. Thus, the correct answer is 15.
5. Convert the expression ((A + B) * C - (D - E) ^ (F + G)) to equivalent Prefix
and Postfix notations.
Prefix Notation: ^ - * +ABC - DE + FG
Postfix Notation: AB + C * DE - - FG + ^
6. List out few of the applications that make use of Multilinked Structures?
The applications are listed below:
• Sparse matrix,
• Index generation.
1. What are the methods available in storing sequential files?

The methods available in storing sequential files are:


a. Straight merging,
b. Natural merging,
c. Polyphase sort,
d. Distribution of Initial runs.

2. In tree construction which is the suitable efficient data structure?

Answer: Linked list is the efficient data structure.


3. Elements of an array are stored _______ in memory.
Answer: Sequentially

4. Allocating memory at runtime is also called as?


Dynamic allocation of memory

5. _________ method places a value onto the top of a stack.


Push method

6. Popping removes an item from the stack at which position.

Pop removes top element from stack

7. Is the size of tree the number of nodes on the tree?


No. The size of the tree determined by using the levels of tree.

8. Is a tree can have a duplicate key?


No. A tree can’t have duplicate key, because through key only that a node is
identified.
9. What is hashing?
Hashing is the technique of scrambling bits of a key into a hash number

10.Priority Queue
A priority queue is essentially a list of items in which each item has associated
with it a priority. In general different items may have different priorities and we
speak of one item having a higher priority than another. Given such a list we can
determine which is the highest (or the lowest) priority item in the list. Items are
inserted into a priority queue in any arbitrary order.

11. Data in a queue is accessible in First in First Out concept


12. The double ended queue inserts and deletes the element in both
sides.

13. What is a spanning Tree?


A spanning tree is a tree associated with a network. All the nodes of the graph
appear on the tree once. A minimum spanning tree is a spanning tree organized so
that the total edge weight between nodes is minimized.

14.Is it possible implement tree using array?


Yes, it is possible.
Here is an example of implementing a binary tree:
Consider an array : a[n];
Place the root in first position.
and let the current position to be i = 0;
place left node at the position: 2i+1
place right node at the position: 2i+2.
Repeat these steps.
15. Whether Linked List is linear or Non-linear data structure?
According to Access strategies Linked list is a linear one. According to Storage Linked
List is a Non-linear one.2312
16. What is the bucket size, when the overlapping and collision occur at same
time?
One. If there is only one entry possible in the bucket, when the collision occurs, there
is no way to accommodate the colliding value. This results in the overlapping of
values.
17.What is binary tree?
A binary tree is a tree which has every node has exactly two links i.e left and
right link
18. When can you tell that a memory leak will occur?
A memory leak occurs when a program loses the ability to free a block of dynamically
allocated memory.
19.What is the average number of comparisons in a sequential search?
f(n)= 1.Pn + 2.Pn + 3.Pn +…+ N.Pn
where
Pn = 1/N
f(n)= 1.1/N +2.1/N + 3.1/N+….+N.1/N
= (1+2+3+….+N)1/N
= N(N+1)/2N
= (N+1)/2
20. Is Pointer a variable?
Yes, a pointer is a variable and can be used as an element of a structure and as
an attribute of a class in some programming languages such as C++, but not Java.
However, the contents of a pointer is a memory address of another location of
memory, which is usually the memory address of another variable, element of a
structure, or attribute of a class.
21. Why do we Use a Multidimensional Array?
A multidimensional array can be useful to organize subgroups of data within an
array. In addition to organizing data stored in elements of an array, a multidimensional
array can store memory addresses of data in a pointer array and an array of pointers.

22. How do you assign an address to an element of a pointer array?


We can assign a memory address to an element of a pointer array by using the
address operator, which is the ampersand (&), in an assignment statement such as
ptemployee[0] = &projects[2];

23. Which process places data at the back of the queue?


Enqueue is the process that places data at the back of the queue.

24. Is a node can reference more than one data element


Yes, a node itself can contain as many data elements as it needs and it can also
reference other data elements. Following is an example of a complex node structure
in a linked list

You might also like