0% found this document useful (0 votes)
7 views8 pages

DSUJ

The document provides an overview of data structures and abstract data types (ADTs), explaining their definitions, implementations, and usefulness in programming. It discusses the differences between static and dynamic data structures, basic operations performed on data structures, and algorithm complexity. Additionally, it covers stacks and queues, their operations, and advanced types like deques and priority queues, along with examples and applications.

Uploaded by

princesorout6342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

DSUJ

The document provides an overview of data structures and abstract data types (ADTs), explaining their definitions, implementations, and usefulness in programming. It discusses the differences between static and dynamic data structures, basic operations performed on data structures, and algorithm complexity. Additionally, it covers stacks and queues, their operations, and advanced types like deques and priority queues, along with examples and applications.

Uploaded by

princesorout6342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

DSUJ

Unit - 1
1. What are Data Structures and Abstract Data Types (ADTs)? How are they useful
in programming?

Ans -

Data structures -
A data structure is a technique of organizing the data so that the data can be utilized efficiently.
There are two ways of viewing the data structure:

Mathematical/ Logical/ Abstract models/ Views: The data structure is the way of
organizing the data that requires some protocols or rules. These rules need to be modeled that
come under the logical/abstract model.

Implementation: The second part is the implementation part. The rules must be
implemented using some programming language.

Abstract data types -


An abstract data type is an abstraction of a data structure that provides only the interface to
which the data structure must adhere. The interface does not give any specific details about
something should be implemented or in what programming language.

In other words, we can say that abstract data types are the entities that are definitions of data
and operations but do not have implementation details. In this case, we know the data that we
are storing and the operations that can be performed on the data, but we don't know about the
implementation details. The reason for not having implementation details is that every
programming language has a different implementation strategy for example; a C data structure
is implemented using structures while a C++ data structure is implemented using objects and
classes.

2. Explain the difference between Static and Dynamic Implementations of Data


Structures with examples.
Ans -

Static Data structure -


Static data structures have a fixed size determined at compile time. Arrays are a common
example of static data structures, where memory is allocated once and cannot be resized
dynamically. For instance, in int arr[10];, the array has a predefined size of 10 elements,
meaning it cannot store more or fewer elements than declared. The advantage of static
structures is their predictable memory usage and fast access time due to direct indexing.
However, they can lead to inefficient memory utilization if the allocated space is not fully
utilized or if additional space is needed beyond the allocated limit. On the other hand.

Dynamic Data Structure -


Dynamic Data Structure is that kind of data structure that changes its size during runtime. The
values store in the data structure can be changed easily either it be static or dynamic data
structure. But the dynamic data are designed in such a way that both the data and the size of
the data structure can be easily changed at the runtime.

The main use case for which the Dynamic Data Structures are defined is to easily facilitate the
change in the size of the data structure at the runtime without hindering the other operations
that are associated with that data structure before increasing or decreasing the size of the data
structure.

In comparison to the static data structures where we can only alter the data present in the data
structure at runtime, in the dynamic data structures, both the data present in the data structure
and the size of the data structure can be easily changed accordingly. In static data structures, we
need to pre-define or calculate the size of the data structure.

3. What are the basic operations performed on data structures? Explain with
examples.

Ans -

Types of Data Structures -

1. Primitive Data structure -


The primitive data structures are primitive data types. The int, char, float, double, and pointer
are the primitive data structures that can hold a single value.
2. Non-Primitive Data structure -

1. Linear Data Structure -


The arrangement of data in a sequential manner is known as a linear data structure. The data
structures used for this purpose are Arrays, Linked list, Stacks, and Queues. In these data
structures, one element is connected to only one another element in a linear form.

2.Non-linear Data Structure -


When one element is connected to the 'n' number of elements known as a non-linear data
structure. The best example is trees and graphs. In this case, the elements are arranged in a
random manner.

We will discuss the above data structures in brief in the coming topics. Now, we will see the
common operations that we can perform on these data structures.

Data Structure Operations -

1. Searching: We can search for any element in a data structure.

2. Sorting: We can sort the elements of a data structure either in an ascending or descending
order.

3. Insertion: We can also insert the new element in a data structure.

4. Updation: We can also update the element, i.e., we can replace the element with another
element.

5. Deletion: We can also perform the delete operation to remove the element from the data
structure.

4. What is Algorithm Complexity?

Ans -
Algorithmic Complexity is a measure of how long an algorithm would take to complete given an
input of size n. If an algorithm needs to scale, it should calculate the output within a finite and
practical time bound, even for large values of n. For this reason, complexity is determined
asymptotically as n approaches infinity. While complexity is generally in terms of time,
sometimes complexity is also evaluated in terms of space, which interprets the algorithm's
memory requirements.

Analysis of the complexity of an algorithm is practical when comparing algorithms or seeking


improvements. Algorithmic complexity lies within a branch of theoretical computer science
called computational complexity theory. It is significant to state that we're concerned about the
order of the complexity of an algorithm, not the actual execution time in terms of milliseconds.

Algorithmic Complexity is also known as Complexity or Running Time.

5. How are Single and Multi-Dimensional Arrays represented and accessed in


memory?

Ans -
An array is a collection of elements stored in contiguous memory locations. A single-
dimensional array (1D array) consists of elements that can be accessed using a single index. For
example, int arr[5] = {10, 20, 30, 40, 50}; stores five integers in a linear sequence, where arr[0]
represents the first element, arr[1] represents the second, and so on. The memory address of
an element can be calculated using the formula: Address = Base Address + (Index × Size of Data
Type). In contrast, a multi-dimensional array consists of multiple rows and columns. A 2D array
is represented as int matrix[3][3];, where elements are stored in a row-major or column-major
order. In row-major order, elements of each row are stored sequentially in memory, while in
column-major order, elements of each column are stored consecutively. Accessing elements in a
2D array requires two indices, such as matrix[i][j]. Multi-dimensional arrays are widely used in
real-world applications such as image processing, game development, and scientific
computations where data is naturally structured in multiple dimensions.

Unit - 3
1. What is a Stack and what are the primitive operations performed on a Stack?
Explain with examples.

Ans -
They follow the Last-In-First-Out (LIFO) principle, meaning the last item added is the first one to
be removed. Stacks have a top pointer that points to the topmost element. All insertions and
deletions happen at this top position. The main operations are push (to add an element), pop
(to remove the top element), and peek (to check the top element without removing it). Stacks
are like a pile of plates. When you add a new plate, it goes on top of the pile. And when you
need a plate, you take the one from the top. In computing, stacks are used for memory
management, function calls, expression evaluation, backtracking algorithms, and more. Stacks
can be implemented using arrays or linked lists. Array implementation is simple but has a fixed
size, while linked lists can grow dynamically. Common applications include undo/redo
operations in text editors, browser history navigation, parentheses matching, and recursion
implementation. Stacks are efficient for insertion and deletion operations at the top, with a
time complexity of O(1).

Operations on Stack
Certain actions are made available to us so that we can manipulate a stack.

1. push() to insert an element into the stack

2. pop() to remove an element from the stack

3. top() Returns the top element of the stack.

4. isEmpty() returns true if stack is empty else false.

5. size() returns the size of stack.

2. How are stacks used in managing function calls through stack frames?

Ans -
Stacks are critical to the internal workings of most programming languages, particularly in the
management of function calls via stack frames. When a function is called during the execution
of a program, a new stack frame is created and pushed onto the call stack. This frame contains
information such as the return address (to know where to resume execution after the function
completes), parameters passed to the function, local variables, and sometimes intermediate
results. Each time a function is called, a new frame is pushed onto the stack, and when the
function returns, its stack frame is popped off. This structure enables recursive function calls as
each recursive call creates a new frame, preserving the state of the previous calls. For example,
in the case of computing factorial recursively, each call to the function stores its own n value
and waits for the result of factorial(n-1), which is managed efficiently through the stack. When
the base case is reached, the stack starts to unwind, returning results back through each frame.
This behavior of stacks makes them ideal for tracking function execution in an organized and
structured manner.

3. Explain the process of converting an infix expression to postfix and prefix using
a stack.

Ans -
Expression conversion is one of the most important applications of stacks. An infix expression is
the standard arithmetic expression where operators are placed between operands (e.g., A + B).
However, for ease of computation, especially by computers, infix expressions are often
converted to postfix (Reverse Polish Notation) or prefix (Polish Notation). In postfix, the
operator follows the operands (e.g., AB+), and in prefix, the operator precedes the operands
(e.g., +AB). To convert an infix expression to postfix using a stack, the algorithm reads the
expression from left to right and uses a stack to temporarily hold operators and parentheses.
Operands are added directly to the result string. When an operator is encountered, it is pushed
onto the stack, but before doing so, the algorithm checks the precedence and associativity of
the operators already in the stack to decide whether to pop them to the result or not.
Parentheses are handled with high priority — when a closing parenthesis is encountered, the
stack is popped until the matching opening parenthesis is found. For converting to prefix, the
expression is typically reversed, the parentheses are swapped, and the same logic as postfix
conversion is applied, followed by reversing the result. This use of stack ensures that operator
precedence and associativity are maintained correctly during conversion.

4. How is a postfix expression evaluated using a stack? Explain with an example.

Ans -
Evaluating a postfix expression using a stack is straightforward and highly efficient. Unlike infix
expressions, postfix expressions do not require parentheses or operator precedence rules, as
the position of the operator clearly defines the order of operations. To evaluate a postfix
expression, a stack is used to hold operands. The algorithm scans the expression from left to
right. When an operand is encountered, it is pushed onto the stack. When an operator is found,
the required number of operands (typically two for binary operators) are popped from the
stack, the operation is performed, and the result is pushed back onto the stack. This process
continues until the end of the expression, and the final result is the only value left in the stack.
For example, for the expression 6 2 3 + - 3 8 2 / + *, the operations would be executed in the
correct order by pushing operands and applying operators accordingly. Step-by-step: 2 + 3 = 5,
then 6 - 5 = 1, then 8 / 2 = 4, then 3 + 4 = 7, and finally 1 * 7 = 7. This approach is used by many
compilers and interpreters to evaluate arithmetic expressions efficiently.

5. What is a Queue and how is it implemented using arrays and linked lists?

Ans -
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle, meaning the
first element added is the first one to be removed. This is analogous to a line at a ticket counter
where people are served in the order they arrive. Queues are widely used in scheduling,
resource management, and handling asynchronous data (like printer queues or keyboard
buffers). The primitive operations on a queue include enqueue (insertion at the rear), dequeue
(removal from the front), peek/front (retrieve front without removing), and isEmpty (check if
queue is empty). There are two common implementations: array-based and linked list-based. In
the array implementation, a fixed-size array is used with two pointers (front and rear) to track
the beginning and end of the queue. However, once the rear reaches the end of the array,
insertion cannot continue even if there are empty spaces at the front due to dequeued
elements. This problem is addressed using a circular queue, where the rear wraps around to the
beginning of the array when space is available. In the linked list implementation, nodes are
dynamically created for each element, and pointers keep track of the front and rear. This
implementation is more flexible as it allows dynamic memory allocation and avoids the problem
of fixed size. Queues are essential in scenarios where data needs to be processed in the order of
arrival.

6. What are Dequeues and Priority Queues? How do they differ from regular
queues?

Ans -
Dequeues (Double-Ended Queues) and Priority Queues are advanced types of queues that
extend the functionality of a standard FIFO queue. A deque allows insertion and deletion at
both the front and rear ends. It can be used as both a stack and a queue, depending on how it is
manipulated. There are two types of deques: input-restricted, where deletion can occur from
both ends but insertion is limited to one end, and output-restricted, where insertion can occur
at both ends but deletion is restricted to one end. Deques are useful in implementing
algorithms that require quick access to both ends of a sequence, such as sliding window
problems or palindrome checking. A priority queue, on the other hand, stores elements along
with their priorities. In a priority queue, the element with the highest (or lowest, depending on
implementation) priority is dequeued first, regardless of the order of insertion. This is different
from a standard queue where the order of arrival strictly determines the order of removal.
Priority queues are typically implemented using heaps or balanced trees and are crucial in
algorithms like Dijkstra's shortest path, CPU scheduling, and event-driven simulations. Both
deques and priority queues enhance the flexibility and capability of linear data structures by
supporting more complex processing requirements.

You might also like