BS351 - 1 Data Structure Notes For qp24
BS351 - 1 Data Structure Notes For qp24
Data Structure is a particular way of storing, processing and organizing data in the memory of the
computer so that these data can easily be retrieved and efficiently used in the future as per the
requirement.
Some examples of Data Structures are Arrays, Linked Lists, Stack, Queue, Trees, etc. Data Structures
are widely used in almost every aspect of Computer Science, i.e., Compiler Design, Operating
Systems, Graphics, Artificial Intelligence, and many more.
A stack is a linear data structure in which the insertion of a new element and removal of an existing
element takes place at the same end represented as the top of the stack.
Applications of Stacks:
Function calls
Reverse a data
Recursion
Expression evaluation
Syntax parsing
Memory management
Q 4) Recurrence in data structure
A recurrence is a well-defined mathematical function, where the function being defined is applied
within its own definition.
The problems that can be described using recurrence are easily expressed as recursive functions in
programming. Recursion is useful in situations where one or more smaller version of the same
problem can solve that problem itself.
Example are Fibonacci series, factorial..etc
Q 5) Circular Queue.
A Circular Queue is an extended version of a normal queue where the last element of the queue is
connected to the first element of the queue forming a circle.
n a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we
can not insert the next element even if there is a space in front of queue.
Operations on Circular Queue:
Front
Rear
Enqueuer()
dequeuer()
Q 6) linked stack
Linked stack can be defined as stack implementation using linked list. To overcome the limitation of
fixed size allocation, when implementing stacks using arrays , single linked list are used .
Each element in the stack will be represented as a node of the list. Addition and deleteion of the node
will be done only at end.
The first node is considered to be at the top of the stack and pointed by the pointer-top. The second
node is connected through the address field of the first node. The last node is the bottom of the stack
and its link field is set to NULL.
A linear search is the simplest and sequential search approach in data structure used to search for an
element in a data set. It examines each element by traversing the list until it finds a match, starting at
the beginning of the data set, until the end. The search is finished and terminated once the target
element is located.
The steps used in the implementation of Linear Search are listed as follows -
Q 9) symbol table
A symbol table is a data structure used by a language translator such as a compiler or interpreter,
where each identifier, symbol, constant, procedure and function in a program's source code is
associated with information relating to its declaration or appearance in the source.
A symbol table is a set of pairs ( name, value), where the value represents the collection of attributes
associated with the name.
The operations performed on symbol tables are :
Inserting the <key, information> pairs into the collection.
Removing the <key, informations> pairs by specifying the key
Searching for a particular key
Retrieving the information associated with a key
1) Primitive and non-primitive: Primitive data structures define a set of primitive element that don’t
involve any other elements as its sub parts( for example- data structures defined for integers and
characters. ) these are generally primary or built-in data types in programming language.
Non primitive data structures will define a set of derived elements such as arrays, class. That is they
can store that data of many data types. Examples of non-primitive data structure are Array, Linked list,
stack.
2) Linear and non- linear: A linear data structure is a structure in which the elements are stored
sequentially, and the elements are connected to the previous and the next element. As the elements are
stored sequentially, so they can be traversed or accessed. Examples are-are Array, Queue, Stack,
Linked List.
A non-linear data structure is also another type of data structure in which the data elements are not
arranged in a contiguous manner. They are used to represent the data in hierarchical or network
relationship among the elements.. In the case of linear data structure, element is connected to two
elements (previous and the next element), whereas, in the non-linear data structure, an element can be
connected to more than two elements. Trees and Graphs are the types of non-linear data structure.
3) static and dynamic: Static data structures have a fixed size and are allocated in memory during
compile-time, while dynamic data structures can grow and shrink in size during runtime.an Array is
example of static and linked list can be considered as dynamic data structure.
4) persistent and Ephemeral : a data structure that supports operations on the most recent version as
well as the previous version is termed as persistent data structure.
An Ephemeral data structure supports operations on the most recent version of the data only.
5) sequential access and direct access: if any element in the data structure can be accessed without
accessing its previous element, its called as direct access. Array is an example of direct access.
If any data structure allows accessing of one element after accessing its previous element, known as
sequential access. Linked list is an example.
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
float scanNum(char ch)
{ int value;
value = ch;
return float(value-'0'); //return float from character
}
int isOperator(char ch) {
if(ch == '+'|| ch == '-'|| ch == '*'|| ch == '/' || ch ==
'^') return 1; //character is an operator
return -1; //not an operator
}
int isOperand(char ch)
{ if(ch >= '0' && ch <=
'9')
return 1; //character is an operand
return -1; //not an operand
}
float operation(int a, int b, char op) {
//Perform operation
if(op == '+')
return b+a;
else if(op == '-')
return b-a;
else if(op ==
'*')
return b*a;
else if(op == '/')
return b/a;
else if(op ==
'^')
return pow(b,a); //find b^a
else
return INT_MIN; //return negative infinity
}
float postfixEval(string postfix)
{ int a, b;
stack<float> stk;
string::iterator it;
for(it=postfix.begin(); it!=postfix.end(); it++) {
//read elements and perform postfix evaluation
if(isOperator(*it) != -1) {
a = stk.top();
stk.pop();
b = stk.top();
stk.pop();
stk.push(operation(a, b, *it));
}else if(isOperand(*it) > 0) {
stk.push(scanNum(*it));
}
}
return stk.top();
}
main() {
string post = "53+62/*35*+";
cout << "The result is: "<<postfixEval(post);
}
Q 14 –a) compare iteration and recursion. Explain use of stack in recurrence with implementation.
The following table highlights all the important differences between recursion and
iteration −
Recursion Iteration
Recursion uses the selection structure. Iteration uses the repetition structure.
The system crashes when infinite Iteration uses the CPU cycles again
recursion is encountered. and again when an infinite loop occurs.
Recursion terminates when the base case Iteration terminates when the
is met. condition in the loop fails.
Recursion reduces the size of the code. Iteration increases the size of the code.
struct Node
{ int data;
Node* next;
Node* prev;
Node(int x)
{ data = x;
next = nullptr;
prev = nullptr;
}
};
Node(int value)
{ data = value;
next = nullptr;
}
}
Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree from weighted
undirected graph. Prim's algorithm finds the subset of edges that includes every vertex of the graph
such that the sum of the weights of the edges can be minimized.
Prim's algorithm starts with the single node and explores all the adjacent nodes with all the connecting
edges at every step. The edges with the minimal weights causing no cycles in the graph got selected.
1. Initialize a tree with a single vertex, chosen arbitrarily from the graph.
2. Grow the tree by one edge: Of the edges that connect the tree to vertices not yet in the tree,
find the minimum-weight edge, and transfer it to the tree.
3. Repeat step 2 (until all vertices are in the tree).
Step 1 - First, we have to choose a vertex from the above graph. Let's choose B.
Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are two edges from
vertex B that are B to C with weight 10 and edge B to D with weight 4. Among the edges, the edge
BD has the minimum weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the other edges. In this
case, the edges DE and CD are such edges. Add them to MST and explore the adjacent of C, i.e., E
and A. So, select the edge DE and add it to the MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would create a cycle to the
graph. So, choose the edge CA and add it to the MST.
So, the graph produced in step 5 is the minimum spanning tree of the given graph. The cost of the MST
is given below -
Algorithm
1. Step 1: Select a starting vertex
2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5. [END OF LOOP]
6. Step 5: EXIT
Q 16-b) implementation of heap with an example. Applications of heaps.
A heap is a tree where each parent node is greater than or equal to its child nodes in a max-heap or less
than or equal to its child nodes in a min-heap. This means the largest or smallest value is always at the
top (root) of the tree
Applications of Heaps: