DSC - MODULE 1
DSC - MODULE 1
Definition:
10
20
30
40
50
10 20 30 40 50
Rear front
The Queue ADT follows a design similar to the Stack ADT, but the order of insertion and
deletion changes to FIFO. Elements are inserted at one end (called the rear) and removed
from the other end (called the front).
It should support the following operations:
enqueue (): Insert an element at the end of the queue.
dequeue (): Remove and return the first element of the queue, if the queue is not
empty.
peek (): Return the element of the queue without removing it, if the queue is not
empty.
size (): Return the number of elements in the queue.
is Empty(): Return true if the queue is empty; otherwise, return false.
Features of ADT
Abstract data types (ADTs) are a way of encapsulating data and operations on that data into a
single unit.
Some of the key features of ADTs include:
Abstraction: The user does not need to know the implementation of the data structure only
essentials are provided.
Better Conceptualization: ADT gives us a better conceptualization of the real world.
Robust: The program is robust and has the ability to catch errors.
Encapsulation: ADTs hide the internal details of the data and provide a public interface for
users to interact with the data. This allows for easier maintenance and modification of the
data structure.
Data Abstraction: ADTs provide a level of abstraction from the implementation details of
the data. Users only need to know the operations that can be performed on the data, not how
those operations are implemented.
Data Structure Independence: ADTs can be implemented using different data structures,
such as arrays or linked lists, without affecting the functionality of the ADT.
Information Hiding: ADTs can protect the integrity of the data by allowing access only to
authorized users and operations. This helps prevent errors and misuse of the data.
Modularity: ADTs can be combined with other ADTs to form larger, more complex data
structures. This allows for greater flexibility and modularity in programming.
Advantages of ADT
Encapsulation: ADTs provide a way to encapsulate data and operations into a single unit,
making it easier to manage and modify the data structure.
Abstraction: ADTs allow users to work with data structures without having to know the
implementation details, which can simplify programming and reduce errors.
Data Structure Independence: ADTs can be implemented using different data structures,
which can make it easier to adapt to changing needs and requirements.
Information Hiding: ADTs can protect the integrity of data by controlling access and
preventing unauthorized modifications.
Modularity: ADTs can be combined with other ADTs to form more complex data structures,
which can increase flexibility and modularity in programming.
Disadvantages of ADT
Overhead: Implementing ADTs can add overhead in terms of memory and processing,
which can affect performance.
Complexity: ADTs can be complex to implement, especially for large and complex data
structures.
Learning Curve: Using ADTs requires knowledge of their implementation and usage, which
can take time and effort to learn.
Limited Flexibility: Some ADTs may be limited in their functionality or may not be suitable
for all types of data structures.
Cost: Implementing ADTs may require additional resources and investment, which can
increase the cost of development.
Examples:
Stack data structure can be implemented in two ways. They are as follows...
1. Using Array
2. Using Linked List
99
50
32
25 N
In above example, the last inserted node is 99 and the first inserted node is 25. The order of
elements inserted is 25, 32,50 and 99.
Operations To implement stack using linked list, we need to set the following things before
implementing actual operations.
Step 1: Include all the header files which are used in the program. And declare all the
user defined functions.
Step 2: Define a 'Node' structure with two members data and next.
Step 3: Define a Node pointer 'top' and set it to NULL.
Step 4: Implement the main method by displaying Menu with list of operations and
make suitable function calls in the main method.
Recursion:
Definition:
Recursion in the data structure can be defined as a method through which problems are
broken down into smaller sub-problems to find a solution.
This is done by replicating the function on a smaller scale, making it call itself and then
combining them together to solve problems.
Types of Stack:
Fixed Size Stack : As the name suggests, a fixed size stack has a fixed size and cannot grow
or shrink dynamically. If the stack is full and an attempt is made to add an element to it, an
overflow error occurs. If the stack is empty and an attempt is made to remove an element
from it, an underflow error occurs.
Dynamic Size Stack : A dynamic size stack can grow or shrink dynamically. When the stack
is full, it automatically increases its size to accommodate the new element, and when the
stack is empty, it decreases its size. This type of stack is implemented using a linked list, as it
allows for easy resizing of the stack.
Implementation of Stack
There are two ways to implement a stack –
Implementation of Stack using Array (Refer page No. 7)
Implementation of Stack using Linked List (Refer page No. 7)
Applications of Stacks:
Function calls: Stacks are used to keep track of the return addresses of function calls,
allowing the program to return to the correct location after a function has finished executing.
Recursion: Stacks are used to store the local variables and return addresses of recursive
function calls, allowing the program to keep track of the current state of the recursion.
Expression evaluation: Stacks are used to evaluate expressions in postfix notation (Reverse
Polish Notation).
Syntax parsing: Stacks are used to check the validity of syntax in programming languages
and other formal languages.
Memory management: Stacks are used to allocate and manage memory in some operating
systems and programming languages.
Advantages of Stacks:
Simplicity: Stacks are a simple and easy-to-understand data structure, making them suitable
for a wide range of applications.
Efficiency: Push and pop operations on a stack can be performed in constant time (O(1)),
providing efficient access to data.
Last-in, First-out (LIFO): Stacks follow the LIFO principle, ensuring that the last element
added to the stack is the first one removed. This behavior is useful in many scenarios, such as
function calls and expression evaluation.
Limited memory usage: Stacks only need to store the elements that have been pushed onto
them, making them memory-efficient compared to other data structures.
Disadvantages of Stacks:
Limited access: Elements in a stack can only be accessed from the top, making it difficult to
retrieve or modify elements in the middle of the stack.
Potential for overflow: If more elements are pushed onto a stack than it can hold, an
overflow error will occur, resulting in a loss of data.
Not suitable for random access: Stacks do not allow for random access to elements, making
them unsuitable for applications where elements need to be accessed in a specific order.
Limited capacity: Stacks have a fixed capacity, which can be a limitation if the number of
elements that need to be stored is unknown or highly variable.
Expression Evaluation:
Infix Expressions
Infix expressions are mathematical expressions where the operator is placed between its
operands. This is the most common mathematical notation used by humans. For example, the
expression "2 + 3" is an infix expression, where the operator "+" is placed between the
operands "2" and "3".
Infix notation is easy to read and understand for humans, but it can be difficult for computers
to evaluate efficiently. This is because the order of operations must be taken into account, and
parentheses can be used to override the default order of operations.
Operator Precedence
Parentheses () Highest
Exponents ^ High
Multiplication * Medium
Division / Medium
Addition + Low
Subtraction - Low
Expression Evaluation:
Algorithms
Problems