Professional Documents
Culture Documents
2
Why data structures?
Ultimate goal -- to write efficient programs. In order to do that, one
needs to organize the data in such a way that it can be accessed and manipulated
efficiently.
Any data structure is designed to organize data to suit a specific purpose
so that it can be accessed and worked in appropriate ways both effectively and
efficiently.
3
Data type
A data type, in programming, is a classification that specifies
numbers.
Examples of data types
Data types
Integer Whole numbers 7, 12, 999
The data type defines which operations can safely be performed to create,
transform and use the variable in another computation. When a program
language requires a variable to only be used in ways that respect its data
type, that language is said to be strongly typed. This prevents errors, because
while it is logical to ask the computer to multiply a float by an integer (1.5 x
5), it is illogical to ask the computer to multiply a float by a string (1.5 x
Alice). When a programming language allows a variable of one data type to be
used as if it were a value of another data type, the language is said to be
weakly typed.
Algorithm definition
Insert
Insert amount to be withdraw amount
again
Series of no
process
including Deduct amount from balance
loop and
selection
exit
Algorithm
– Three types of algorithm basic control structure
• Sequential
• Selection
• Repeatition (Looping)
Algorithm
• Basic algorithm characteristics
– Finite solution
– Clear instructions
– Has input to start the execution
– Has output as the result of the execution
– Operate effectively
• Algorithm creation techniques
– Flowchart, pseudo code, language etc
• Factors for measuring good algorithm
– Running time
– Total memory usage
Abstraction
Abstraction is one of the key concepts of object-oriented programming (OOP)
from the user. That enables the user to implement more complex logic on top
You need to know how to use your coffee machine to make coffee. You need to
provide water and coffee beans, switch it on and select the kind of coffee you
want to get.
The thing you don’t need to know is how the coffee machine is working internally
to brew a fresh cup of delicious coffee. You don’t need to know the ideal
temperature of the water or the amount of ground coffee you need to use.
Someone else worried about that and created a coffee machine that now acts as
an abstraction and hides all these details. You just interact with a simple interface
that doesn’t require any knowledge about the internal implementation.
Abstraction example 2
Abstraction in OOP
just need to know which methods of the object are available to call and
which input parameters are needed to trigger a specific operation. But you
don’t need to understand how this method is implemented and which kinds of
data types and we can perform basic operations with them such as addition,
when we need operations for our user-defined data type which have to be
defined. These operations can be defined only as and when we require them.
So, in order to simplify the process of solving problems, we can create data
structures along with their operations, and such data structures that are not
defined by a set of values and a set of operations. The definition of ADT only
mentions what operations are to be performed but not how these operations
memory and what algorithms will be used for implementing the operations. It
The process of providing only the essentials and hiding the details is known as
abstraction.
ADTs cont…
abstraction.
ADTs Cont…
The user of data type does not need to know how that data type is
implemented, for example, we have been using Primitive values like int,
float, char data types only with the knowledge that these data type can
operate and be performed on without any idea of how they are
implemented.
So a user only needs to know what a data type can do, but not how it will be
implemented. Think of ADT as a black box which hides the inner structure and
design of the data type. Now we’ll define three ADTs namely List ADT, Stack
ADT, Queue ADT.
ADTs and their functions
List ADT
List ADT
•The List ADT Functions is given below:
•get() – Return an element from the list at any given position.
•insert() – Insert an element at any position of the list.
•remove() – Remove the first occurrence of any element from a non-empty list.
•removeAt() – Remove the element at a specified location from a non-empty list.
•replace() – Replace an element at any position by another element.
•size() – Return the number of elements in the list.
•isEmpty() – Return true if the list is empty, otherwise return false.
•isFull() – Return true if the list is full, otherwise return false.
Linked list consists of:
•
– a sequence of nodes,
– data fields
– one or two links or references pointing to the next
and/or previous nodes
3 12 11
Stack ADTs
•In Stack ADT Implementation instead of data being stored in each node,
the pointer to data is stored.
•The program allocates memory for the data and address is passed to the stack ADT.
•The head node and the data nodes are encapsulated in the ADT.
The calling function can only see the pointer to the stack.
•The stack head structure also contains a pointer to top and count of number
of entries currently in stack.
Stack ADTs
Top
Stack Application
Queue ADTs
•The queue abstract data type (ADT) follows the basic design of the
stack abstract data type.
•Each node contains a void pointer to the data and the link pointer to the next
element in the queue. The program’s responsibility is to allocate memory for storing the
data.
•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.
•isEmpty() – Return true if the queue is empty, otherwise return false.
•isFull() – Return true if the queue is full, otherwise return false.
•
– First-In-First-Out (FIFO) data structure
– the first element added to the queue will be the
first one to be removed (post office, bank etc)
Out
In
Back Front
Features of ADT
Abstraction: The user does not need to know the implementation of the data
structure.
Better Conceptualization: ADT gives us a better conceptualization of the real
world.
Robust: The program is robust and has the ability to catch errors.
Basic terminologies
As the name indicates, Data Structure is used for organizing the data in
memory. There are various ways of organizing the data in the memory, for eg.
array, list, stack, queue, and many more. Check out these free data structure
courses today!
The data structure isn’t a programming language like C, C++, Java, etc. It is a
set of algorithms that can be used in any programming language to organize
the data in the memory.
Need of Data Structure
increasing day by day, which may cause problems with processing speed,
searching data, handling multiple requests etc. Data structure provides a way
of organizing, managing, and storing data efficiently. With the help of data
structure, the data items can be traversed easily. Data structure provides
Data structure and algorithms are two of the most important aspects of
computer science. Data structures allow us to organize and store data, while
structure and algorithms will help you become a better programmer. You will
be able to write code that is more efficient and more reliable. You will also
Primitive Data Structures directly operate according to the machine instructions. These are
the primitive data types. Data types like int, char, float, double, and pointer are primitive
data structures that can hold a single value.
Non-primitive data structures are complex data structures that are derived from primitive
data structures. Non – Primitive data types are further divided into two categories.
manner where every element is connected to its previous and next elements.
the simplest data structure where each data element can be accessed directly by only using
2] Linked List –
A linked list is a linear data structure that is used to maintain a list-like structure in the
computer memory. It is a group of nodes that are not stored at contiguous locations. Each
node of the list is linked to its adjacent node with the help of pointers.
3] Stack –
Stack is a linear data structure that follows a specific order during which the operations are
performed. The order could be FILO (First In Last Out) or LIFO (Last In First Out).
Non-Linear Data Structure –
Non-linear Data Structures do not have any set sequence of connecting all its
elements and every element can have multiple paths to attach to other
implement but are more efficient in utilizing memory. Some examples of non-
Root
of node 20
7
Types of Non-Linear Data Structure
2] Graph
Static Data Structures are data structures where the size is allocated at the
compile time. Hence, the maximum size is fixed and cannot be changed.
Dynamic Data Structures are data structures where the size is allocated at the
run time. Hence, the maximum size is flexible and can be changed as per
requirement.
Data Structure Operations –
Searching – We can easily search for any data element in a data structure.
Deletion – We can delete the data elements from the data structure.
Updation – We can update or replace the existing elements from the data
structure.
Advantages of Data Structure –
Data structures allow storing the information on hard disks.
An appropriate choice of ADT (Abstract Data Type) makes the program more
efficient.
Using appropriate data structures can help programmers save a good amount
of time while performing operations such as storage, retrieval, or processing
of data.