Data Structure &
Algorithms
Dr.U.Sridevi/AP/MCA/Excel Engineering
College
DATA STRUCTURE
Data: Collection of raw facts.
Data structure is representation of the logical
relationship existing between individual
elements of data.
Data structure is a specialized format for
organizing and storing data in memory that
considers not only the elements stored but also
their relationship to each other.
CLASSIFICATION OF DATA STRUCTURE
PRIMITIVE DATA STRUCTURE
There are basic structures and directly operated
upon by the machine instructions.
Data structures that are directly operated
upon the machinelevel instructions are
known as primitive data structures.
Integer, Floatingpoint number, Character
constants, string constants, pointers etc, fall in
this category.
The most commonly used operation on data
structure are broadly categorized into
following types:
• Create
• Selection
• Updating
• Destroy or Delete
NONPRIMITIVE DATA STRUCTURE
The Data structures that are derived from the
primitive data structures are called Nonprimitive
data structure.
The nonprimitive data structures emphasize on
structuring a group of homogeneous (same type) or
heterogeneous (different type) data items.
Linea Data
structures:
NonLinear
Data
structures:
ABSTRACT DATA TYPE (ADT)
ADT is a collection of data and a set of operations
that can be performed on the data.
It enables us to think
abstractly about the data
We can separate concepts
from implementation.
Typically, we choose a data structure and
algorithms that provide an implementation of
an ADT.
LINEAR LIST
□ Linear list is a data object whose instances are
of the form (e1 ,e2 ,..., en )
□ ei is an element of the list.
□ e1 is the first element, and en is the last
element.
□ n is the length of the list.
□ When n = 0, it is called an empty list.
□ e1 comes before e2 , e2 comes before e3 , and so
on.
IMPLEMENTATIONS OF
LINEAR LIST
Arraybased (Formulabased)
Uses a mathematical formula to determine where (i.e.,
the memory address) to store each element of a list
Linked list (Pointerbased)
The elements of a list may be stored in any arbitrary set
of locations Each element has an explicit pointer (or
link) to the next element Indirect addressing
The elements of a list may be stored in any arbitrary set
of locations Maintain a table such that the ith table
entry tells us where the ith
element is stored
Simulated pointer
Similar to linked representation but integers replace the
C++ pointers
FORMULABASED REPRESENTATION
A formulabased representation uses an array
to represent the instances of an object. Each
position of the array, called a cell or a node,
holds one element that makes up an instance
of that object. Individual elements of an instance
are located in the array, based on a mathematical
formula, e.g., a simple and often used formula is
Location(i) = i − 1,
which says the ith
element of the list is in
position i − 1. We also need two more variables,
length and MaxSize, to completely characterize
the list type.
LINKED LISTS
One way to overcome the inefficiency problem of the
previous approach is to assign space on a needonly
base. No space will be assigned if there is no need; and
whenever there is a need, another piece of space will
be assigned to an element. Since, we can’t guarantee
all the pieces of spaces assigned at different times
will be physically adjacent, besides the space assigned
for the elements, we also have to keep track of the
location information of previously assigned pieces.
Hence, in a linked representation, each element of an
instance is presented in a cell or node, which also
contains a pointer that keeps information about the
location of another node.
CONT…
CIRCULAR LIST
□ Some application might be simpler, or run
faster, by representing a list as a circular list,
and/or adding a Head node, at the front.
DOUBLY LINKED LIST
 Doubly linked list is a type of linked list in which each
node apart from storing its data has two links. The first
link points to the previous node in the list and the
second link points to the next node in the list. The first
node of the list has its previous link pointing to NULL
similarly the last
 node of the list has its next node pointing to NULL.
 The two links help us to traverse the list in both
backward and forward direction. But storing an extra
link requires some extra space.
CONT…
INDIRECT ADDRESSING
This approach combines the formulabased
approach and that of the linked representation.
As a result, we can not only get access to
elements in Θ(1) times, but also have the
storage flexibility, elements will not be
physically moved during insertion and/or
deletion.
In indirect addressing, we use a table of pointers to
get access to a list of elements, as shown in the
following figure.
STACKS
A stack is a container of objects that are inserted
and removed according to the lastin firstout
(LIFO) principle. In the pushdown stacks only
two operations are allowed: push the item into
the stack, and pop the item out of the stack. A
stack is a limited access data structure elements
can be added and removed from the stack only at
the top. push adds an item to the top of the
stack, pop removes the item from the top. A
helpful analogy is to think of a stack of books;
you can remove only the top book, also you can
add a new book on the top.
CONT…
 Applications
 The simplest application of a stack is to reverse a
word. You push a given word to stack letter by letter
and then pop letters from the stack.
 Another application is an "undo" mechanism in text
editors; this operation is accomplished by keeping
all text changes in a stack.
CONT…
 Backtracking: This is a process when you need to
access the most recent data element in a series of
elements. Think of a labyrinth or maze how do you
find a way from an entrance to an exit?
 Once you reach a dead end, you must backtrack.
But backtrack to where? to the previous choice point.
Therefore, at each choice point you store on a stack
all possible choices.
 Then backtracking simply means popping a next
choice from the stack.
CONT…
Arraybased implementation
In an arraybased implementation we maintain the
following fields: an array A of a default size (≥ 1), the
variable top that refers to the top element in the stack
and the capacity that refers to the array size.
The variable top changes from 1 to capacity 1. We say
that a stack is empty when top = 1, and the stack is
full when top = capacity1. In a fixedsize stack
abstraction, the capacity stays unchanged, therefore
when top reaches capacity, the stack object throws an
exception. See ArrayStack.java for a complete
implementation of the stack class. In a dynamic stack
abstraction when top reaches capacity, we double up
the stack size.
CONT…
Linked Listbased implementation
Linked Listbased implementation provides the best
(from the efficiency point of view) dynamic stack
implementation. See ListStack.java f or a
complete implementation of the stack class.
QUEUES
 A queue is a container of objects (a linear collection)
that are inserted and removed according to the firstin
first out (FIFO) principle.
 An excellent example of a queue is a line of students
in the food court of the UC. New additions to a line
made to the back of the queue, while removal (or
serving) happens in the front.
 In the queue only two operations are allowed enqueue
and dequeue.
CONT…
CONT…
Implementation
In the standard library of classes, the data type queue is an adapter
class, meaning that a queue is built on top of other data structures.
The underlying structure for a queue could be an array, a Vector, an
ArrayList, a LinkedList, or any other collection.
Regardless of the type of the underlying data structure, a queue must
implement the same functionality.
This is achieved by providing a unique interface.
CIRCULAR QUEUE
 Given an array A of a default size (≥ 1) with two
references back and front, originally set to 1 and 0
respectively. Each time we insert (enqueue) a new
item, we increase the back index; when we remove
(dequeue) an item we increase the front index. Here
is a picture that illustrates the model after a few steps:
CONT…
As you see from the picture, the queue logically
moves in the array from left to right. After several
moves back reaches the end, leaving no space for
adding new elements.
However, there is a free space before the front
index. We shall use that space for enqueueing
new items, i.e. the next entry will be stored at
index 0, then 1, until front. Such a model is
called a wrap around queue or a circular queue

Data Structures & Algorithms Unit 1.pptx

  • 1.
  • 2.
    DATA STRUCTURE Data: Collectionof raw facts. Data structure is representation of the logical relationship existing between individual elements of data. Data structure is a specialized format for organizing and storing data in memory that considers not only the elements stored but also their relationship to each other.
  • 3.
  • 4.
    PRIMITIVE DATA STRUCTURE Thereare basic structures and directly operated upon by the machine instructions. Data structures that are directly operated upon the machinelevel instructions are known as primitive data structures. Integer, Floatingpoint number, Character constants, string constants, pointers etc, fall in this category. The most commonly used operation on data structure are broadly categorized into following types: • Create • Selection • Updating • Destroy or Delete
  • 5.
    NONPRIMITIVE DATA STRUCTURE TheData structures that are derived from the primitive data structures are called Nonprimitive data structure. The nonprimitive data structures emphasize on structuring a group of homogeneous (same type) or heterogeneous (different type) data items. Linea Data structures: NonLinear Data structures:
  • 6.
    ABSTRACT DATA TYPE(ADT) ADT is a collection of data and a set of operations that can be performed on the data. It enables us to think abstractly about the data We can separate concepts from implementation. Typically, we choose a data structure and algorithms that provide an implementation of an ADT.
  • 7.
    LINEAR LIST □ Linearlist is a data object whose instances are of the form (e1 ,e2 ,..., en ) □ ei is an element of the list. □ e1 is the first element, and en is the last element. □ n is the length of the list. □ When n = 0, it is called an empty list. □ e1 comes before e2 , e2 comes before e3 , and so on.
  • 8.
    IMPLEMENTATIONS OF LINEAR LIST Arraybased(Formulabased) Uses a mathematical formula to determine where (i.e., the memory address) to store each element of a list Linked list (Pointerbased) The elements of a list may be stored in any arbitrary set of locations Each element has an explicit pointer (or link) to the next element Indirect addressing The elements of a list may be stored in any arbitrary set of locations Maintain a table such that the ith table entry tells us where the ith element is stored Simulated pointer Similar to linked representation but integers replace the C++ pointers
  • 9.
    FORMULABASED REPRESENTATION A formulabasedrepresentation uses an array to represent the instances of an object. Each position of the array, called a cell or a node, holds one element that makes up an instance of that object. Individual elements of an instance are located in the array, based on a mathematical formula, e.g., a simple and often used formula is Location(i) = i − 1, which says the ith element of the list is in position i − 1. We also need two more variables, length and MaxSize, to completely characterize the list type.
  • 10.
    LINKED LISTS One wayto overcome the inefficiency problem of the previous approach is to assign space on a needonly base. No space will be assigned if there is no need; and whenever there is a need, another piece of space will be assigned to an element. Since, we can’t guarantee all the pieces of spaces assigned at different times will be physically adjacent, besides the space assigned for the elements, we also have to keep track of the location information of previously assigned pieces. Hence, in a linked representation, each element of an instance is presented in a cell or node, which also contains a pointer that keeps information about the location of another node.
  • 11.
  • 12.
    CIRCULAR LIST □ Someapplication might be simpler, or run faster, by representing a list as a circular list, and/or adding a Head node, at the front.
  • 13.
    DOUBLY LINKED LIST Doubly linked list is a type of linked list in which each node apart from storing its data has two links. The first link points to the previous node in the list and the second link points to the next node in the list. The first node of the list has its previous link pointing to NULL similarly the last  node of the list has its next node pointing to NULL.  The two links help us to traverse the list in both backward and forward direction. But storing an extra link requires some extra space.
  • 14.
  • 15.
    INDIRECT ADDRESSING This approachcombines the formulabased approach and that of the linked representation. As a result, we can not only get access to elements in Θ(1) times, but also have the storage flexibility, elements will not be physically moved during insertion and/or deletion. In indirect addressing, we use a table of pointers to get access to a list of elements, as shown in the following figure.
  • 16.
    STACKS A stack isa container of objects that are inserted and removed according to the lastin firstout (LIFO) principle. In the pushdown stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack. A stack is a limited access data structure elements can be added and removed from the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top. A helpful analogy is to think of a stack of books; you can remove only the top book, also you can add a new book on the top.
  • 17.
    CONT…  Applications  Thesimplest application of a stack is to reverse a word. You push a given word to stack letter by letter and then pop letters from the stack.  Another application is an "undo" mechanism in text editors; this operation is accomplished by keeping all text changes in a stack.
  • 18.
    CONT…  Backtracking: Thisis a process when you need to access the most recent data element in a series of elements. Think of a labyrinth or maze how do you find a way from an entrance to an exit?  Once you reach a dead end, you must backtrack. But backtrack to where? to the previous choice point. Therefore, at each choice point you store on a stack all possible choices.  Then backtracking simply means popping a next choice from the stack.
  • 19.
    CONT… Arraybased implementation In anarraybased implementation we maintain the following fields: an array A of a default size (≥ 1), the variable top that refers to the top element in the stack and the capacity that refers to the array size. The variable top changes from 1 to capacity 1. We say that a stack is empty when top = 1, and the stack is full when top = capacity1. In a fixedsize stack abstraction, the capacity stays unchanged, therefore when top reaches capacity, the stack object throws an exception. See ArrayStack.java for a complete implementation of the stack class. In a dynamic stack abstraction when top reaches capacity, we double up the stack size.
  • 20.
    CONT… Linked Listbased implementation LinkedListbased implementation provides the best (from the efficiency point of view) dynamic stack implementation. See ListStack.java f or a complete implementation of the stack class.
  • 21.
    QUEUES  A queueis a container of objects (a linear collection) that are inserted and removed according to the firstin first out (FIFO) principle.  An excellent example of a queue is a line of students in the food court of the UC. New additions to a line made to the back of the queue, while removal (or serving) happens in the front.  In the queue only two operations are allowed enqueue and dequeue.
  • 22.
  • 23.
    CONT… Implementation In the standardlibrary of classes, the data type queue is an adapter class, meaning that a queue is built on top of other data structures. The underlying structure for a queue could be an array, a Vector, an ArrayList, a LinkedList, or any other collection. Regardless of the type of the underlying data structure, a queue must implement the same functionality. This is achieved by providing a unique interface.
  • 24.
    CIRCULAR QUEUE  Givenan array A of a default size (≥ 1) with two references back and front, originally set to 1 and 0 respectively. Each time we insert (enqueue) a new item, we increase the back index; when we remove (dequeue) an item we increase the front index. Here is a picture that illustrates the model after a few steps:
  • 25.
    CONT… As you seefrom the picture, the queue logically moves in the array from left to right. After several moves back reaches the end, leaving no space for adding new elements. However, there is a free space before the front index. We shall use that space for enqueueing new items, i.e. the next entry will be stored at index 0, then 1, until front. Such a model is called a wrap around queue or a circular queue