OOPS_Data structure
OOPS_Data structure
Base Class: A base class is a class in Object-Oriented Programming language, from which other
classes are derived. The class which inherits the base class has all members of a base class as well as
can also have some additional properties. The Base class members and member functions are
inherited to Object of the derived class. A base class is also called parent class or superclass.
Derived Class: A class that is created from an existing class. The derived class inherits all members
and member functions of a base class. The derived class can have more functionality with respect to
the Base class and can easily access the Base class. A Derived class is also called a child
class or subclass.
Encapsulation in C++ is defined as the wrapping up of data and information in a single unit. In Object
Oriented Programming, Encapsulation is defined as binding together the data and the functions that
manipulate them.
Consider a real-life example of encapsulation, in a company, there are different sections like the
accounts section, finance section, sales section, etc. Now,
The finance section handles all the financial transactions and keeps records of all the data
related to finance.
Similarly, the sales section handles all the sales-related activities and keeps records of all the
sales.
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
4. What is the fundamental idea of OOPs? Specirfy and four OOPs language
There are many object-oriented programming languages, including JavaScript, C++, Java, and
Python.
A program module is a self-contained independent program segment only it does not provide
security to data whereas an object is a collection of data members and member functions that
operate on data and data is provided with security.
Modular programming (also called "top-down design" and "stepwise refinement") is a software
design technique that emphasizes separating the functionality of a program into independent,
interchangeable modules, such that each contains everything necessary to execute only one aspect
of the desired functionality.
The length of the programmes developed using OOP language is much larger than the
procedural approach. Since the programme becomes larger in size, it requires more time to
be executed that leads to slower execution of the programme.
Programmers need to have brilliant designing skill and programming skill along with proper
planning because using OOP is little bit tricky.
OOPs take time to get used to it. The thought process involved in object-oriented
programming may not be natural for some people.
Everything is treated as object in OOP so before applying it we need to have excellent
thinking in terms of objects.
Limitations
It requires more time and effort to learn and master the OOP concepts and principles.
It demands careful planning, designing, and documentation of the classes and objects
involved in the project.
It can increase the complexity and difficulty of understanding and maintaining large-scale
projects.
DATA STRUCTURES
In linear data structure, single level is involved. Therefore all the elements in single run only.
Linear data structures are easy to implement because computer memory is arranged in a
linear way. Its examples are array, stack, queue, linked list, etc.
Data structures where data elements are not arranged sequentially or linearly are
called non-linear data structures. In a non-linear data structure, single level is not involved.
Therefore, all the elements in single run only. Non-linear data structures are not easy to
implement in comparison to linear data structure. It utilizes computer memory efficiently in
comparison to a linear data structure. Its examples are trees and graphs.
3. Name the data structure whose relationship between data elements by means of links
A linked list is a linear data structure that consists of a series of nodes connected by pointers.
Each node contains data and a pointer/reference to the next node in the list.
Types of linked lists-
Singly Linked List
Doubly Linked List
Circular Linked List
Circular Doubly Linked List
Header Linked List
5. Define Array
Array is a linear data structure where all elements are arranged sequentially. It is a collection
of elements of same data type stored at contiguous memory locations.
A data structure is a way of organizing and storing data in a computer so that it can be
accessed and manipulated efficiently. It provides a framework for managing data elements and their
relationships. They are essential for tasks like searching, sorting, and retrieving information.
o Examples:
Arrays: Fixed-size collection of elements of the same data type. Elements are
accessed using an index.
Linked Lists: Dynamic collection of nodes, where each node contains data
and a reference to the next node.
Examples:
Trees: Hierarchical structure with a root node and child nodes. Used for representing hierarchical
relationships (e.g., file systems, organization charts).
Graphs: Consists of nodes connected by edges. Used for modeling complex relationships (e.g., social
networks, transportation networks).
Linear search is a type of sequential searching algorithm. In this method, every element within the
input array is traversed and compared with the key element to be found. If a match is found in the
array the search is said to be successful; if there is no match found the search is said to be
unsuccessful and gives the worst-case time complexity.
The algorithm for linear search is relatively simple. The procedure starts at the very first index of the
input array to be searched.
Step 1 − Start from the 0th index of the input array, compare the key value with the value present in
the 0th index.
Step 2 − If the value matches with the key, return the position at which the value was found.
Step 3 − If the value does not match with the key, compare the next element in the array.
Step 4 − Repeat Step 3 until there is a match found. Return the position at which the match was
found.
Step 5 − If it is an unsuccessful search, print that the element is not present in the array and exit the
program.
13. 5 marks, Write an algorithm to search element in an array using binary search.
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the
search interval in half. The idea of binary search is to use the information that the array is sorted and
reduce the time complexity to O(log N).
14. Write an algorithm to perform PUSH and POP operation on stack
Stacks are a type of container adaptors that follow LIFO(Last In First Out) property, where a new
element is added at one end and an element(at the top) is removed from that end only. Basically,
the insertion and deletion happen on the top of the stack itself.
tack::push()
push() function is used to insert or ‘push’ an element at the top of the stack. This is an inbuilt
function from C++ Standard Template Library(STL). This function belongs to the <stack> header
file. The element is added to the stack container and the size of the stack is increased by 1.
Syntax: stackname.push(value)
Parameters: The value of the element to be inserted is passed as the parameter.
Result: Adds an element of value the same as that of the parameter passed at the top of the
stack.
Examples: Input : mystack
mystack.push(6);
Output : 6
Input : mystack
mystack.push(0);
mystack.push(1);
Output : 0, 1
Errors and Exceptions:
Shows an error if the value passed doesn’t match the stack type.
Shows no exception throw guarantee if the parameter doesn’t throw any exception.
Output - 2 1 0
stack::pop()
The pop() function is used to remove or ‘pop’ an element from the top of the stack(newest or
the topmost element in the stack). This is an inbuilt function from C++ Standard Template
Library(STL). This function belongs to the <stack> header file. The element is removed from the
stack container and the size of the stack is decreased by 1.
Syntax: stackname.pop()
Parameters: No parameters are passed.
Result: Removes the newest element in the stack or basically the top element.
Output - 2 1
Initial:
Current element is 23
The first element in the array is assumed to be sorted.
The sorted part until 0th index is : [23]
First Pass:
Compare 1 with 23 (current element with the sorted part).
Since 1 is smaller, insert 1 before 23 .
The sorted part until 1st index is: [1, 23]
Second Pass:
Compare 10 with 1 and 23 (current element with the sorted part).
Since 10 is greater than 1 and smaller than 23 , insert 10 between 1 and 23 .
The sorted part until 2nd index is: [1, 10, 23]
Third Pass:
Compare 5 with 1 , 10 , and 23 (current element with the sorted part).
Since 5 is greater than 1 and smaller than 10 , insert 5 between 1 and 10
The sorted part until 3rd index is : [1, 5, 10, 23]
Fourth Pass:
Compare 2 with 1, 5, 10 , and 23 (current element with the sorted part).
Since 2 is greater than 1 and smaller than 5 insert 2 between 1 and 5 .
The sorted part until 4th index is: [1, 2, 5, 10, 23]
Final Array:
The sorted array is: [1, 2, 5, 10, 23]
16. Apply binary search 10,20,30, 35,40,45,50,55, 60 and search for the item 35.
To understand the working of binary search, consider the following illustration:Consider
an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23.
The Binary Search Algorithm can be implemented in the following two ways
Iterative Binary Search Algorithm
Recursive Binary Search Algorithm
Output Result
Searching: it is Finding the location of data within the data structure that satisfies the searching
condition or the criteria.
Inserting: Adding new data in the data structure is referred to as insertion.
Deleting: in this operation Removing data from the data structure is referred to as deletion.
Sorting: Arranging the data in some logical order, for example, is numerical increasing order or
alphabetically.
Merging: Combining the data of two different sorted files into a single sorted file.
Must follow the below-given steps while Designing data structures:
First In a particular Program, Determine the logical picture of the data.
Second, Select the representation of data.
Third, Develop those operations that will be applied to
Different Data Structures are as follows: