0% found this document useful (0 votes)
72 views

Module 5 - PCD - Data Structure

This document provides an introduction to data structures. It defines data structures as organized ways to store and access data efficiently. The document discusses primitive data types like integers and strings, as well as non-primitive data structures like linear (stacks, queues, linked lists) and non-linear (trees, graphs) structures. It then focuses on stacks and queues, defining them as linear data structures that follow LIFO and FIFO principles respectively, and describing their common operations like push, pop, enqueue and dequeue.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Module 5 - PCD - Data Structure

This document provides an introduction to data structures. It defines data structures as organized ways to store and access data efficiently. The document discusses primitive data types like integers and strings, as well as non-primitive data structures like linear (stacks, queues, linked lists) and non-linear (trees, graphs) structures. It then focuses on stacks and queues, defining them as linear data structures that follow LIFO and FIFO principles respectively, and describing their common operations like push, pop, enqueue and dequeue.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Unit - 5

Pointers
and
Introduction to
Data Structures
• Introduction to Data Structures:
Primitive and non primitive datatypes,
Definition and applications of Stacks,
Queues, LinkedLists and Trees
C Data Structures
• Data is recorded facts that can be manipulated in
order to produce useful information/results.
• Data Structure can be defined as the group of data
elements which provides an efficient way of storing
and organizing data in the computer so that it can be
used efficiently. 
• Data Structures are the main part of many computer
science algorithms as they enable the programmers
to handle the data in an efficient way. It plays a vital
role in enhancing the performance of a software or a
program as the main function of the software is to
store and retrieve the user's data as fast as possible.
Need of Data Structures
• As applications are getting complexed and amount of data is
increasing day by day, there may arise the following problems:
– Processor speed: To handle very large amout of data, high speed
processing is required, but as the data is growing day by day to the billions
of files per entity, processor may fail to deal with that much amount of
data.
– Data Search: Consider an inventory size of 106 items in a store, If our
application needs to search for a particular item, it needs to traverse 106
items every time, results in slowing down the search process.
– Multiple requests: If thousands of users are searching the data
simultaneously on a web server, then there are the chances that a very
large server can be failed during that process
• in order to solve the above problems, data structures are used.
Data is organized to form a data structure in such a way that all
items are not required to be searched and required data can be
searched instantly.
Advantages of Data Structures
• Efficiency: 
– Efficiency of a program depends upon the choice of
data structures. For example: suppose, we have
some data and we need to perform the search for a
perticular record. In that case, if we organize our
data in an array, we will have to search sequentially
element by element. hence, using array may not be
very efficient here. There are better data structures
which can make the search process efficient like
ordered array, binary search tree or hash tables.
Advantages of Data Structures
• Reusability: Data structures are reusable,
i.e. once we have implemented a particular
data structure, we can use it at any other
place. Implementation of data structures
can be compiled into libraries which can be
used by different clients.
Data Structure Classification
Primitive Data structure:
• The primitive data structures are the basic data types
that are available in most of the programming
languages and can be manipulated directly by machine
instructions.
• The primitive data types are used to represent single
values.
• They are
– Integer,
– Float and Double ,
– Character ,
– String
– Boolean
Primitive Data structure:
• Integer: This is used to represent a number without
decimal point.
• Eg: 12, 90
• Float and Double: This is used to represent a number
with decimal point.
• Eg: 45.1, 67.3
• Character : This is used to represent single character
• Eg: ‘C’, ‘a’
• String: This is used to represent group of characters.
• Eg: "New Horizon College of Engineering"
• Boolean: This is used represent logical values either
true or false.
Non-Primitive Data structure
• The data structures that are derived from
primary data types are known as non-Primitive
data types. These data types are used to store
group of values.

• These once again can be classified as


– Linear data structures
– Non linear data structures
Non-Primitive Data structure
• Linear data structure
– A linear data structure traverses the data elements
sequentially, in which only one data element can
directly be reached. These include stack, queue and
linked list.
• Non Linear data structure
– Every data item is attached to several other data
items in a way that is specific for reflecting
relationships. The data items are not arranged in a
sequential structure. Ex: Trees, Graphs
Linear data structure
• Linear data structure
– A data structure is called linear if all of its elements
are arranged in the linear order. In linear data
structures, the elements are stored in non-
hierarchical way where each element has the
successors and predecessors except the first and last
element.
• Types of Linear Data Structures are given below:
– Arrays
– Linked List
– Stack
– Queue
Non Linear data structure
• This data structure does not form a sequence i.e.
each item or element is connected with two or
more other items in a non-linear arrangement.
The data elements are not arranged in
sequential structure.
• Types of Non Linear Data Structures are given
below:
– Trees
– Graphs
Arrays
• An array is a collection of similar type of data items
and each data item is called an element of the array.
• The data type of the element may be any valid data
type like char, int, float or double.
• The elements of array share the same variable
name but each one carries a different index number
known as subscript. The array can be one
dimensional, two dimensional or multidimensional.
• The individual elements of the array age are:
– age[0], age[1], age[2], age[3],......... age[98], age[99].
Linear data structure
• A linear data structure traverses the data
elements sequentially, in which only one data
element can directly be reached.
• These include
– stack,
– queue and
– linked list.
Stack
• A Stack is a linear data structure that follows
the LIFO (Last-In-First-Out) principle.
• Stack has one end, whereas the Queue has two
ends (front and rear).
• It contains only one pointer top pointer pointing to
the topmost element of the stack.
• Whenever an element is added in the stack, it is
added on the top of the stack, and the element can
be deleted only from the stack.
• In other words, a stack can be defined as a
container in which insertion and deletion can be
done from the one end known as the top of the
stack.
Standard Stack Operations
• The following are some common operations implemented on the
stack:
– push(): When we insert an element in a stack then the operation is
known as a push. If the stack is full then the overflow condition
occurs.
– pop(): When we delete an element from the stack, the operation is
known as a pop. If the stack is empty means that no element exists
in the stack, this state is known as an underflow state.
– isEmpty(): It determines whether the stack is empty or not.
– isFull(): It determines whether the stack is full or not.'
– peek(): It returns the element at the given position.
– count(): It returns the total number of elements available in a stack.
– change(): It changes the element at the given position.
– display(): It prints all the elements available in the stack.
PUSH operation
• The steps involved in the PUSH operation is
given below:
– Before inserting an element in a stack, we check
whether the stack is full.
– If we try to insert the element in a stack, and the
stack is full, then the overflow condition occurs.
– When the new element is pushed in a stack, first, the
value of the top gets incremented,
i.e., top=top+1, and the element will be placed at the
new position of the top.
– The elements will be inserted until we reach
the max size of the stack.
PUSH operation
POP operation
• The steps involved in the POP operation is given
below:
– Before deleting the element from the stack, we
check whether the stack is empty.
– If we try to delete the element from the empty stack,
then the underflow condition occurs.
– If the stack is not empty, we first access the element
which is pointed by the top
– Once the pop operation is performed, the top is
decremented by 1, i.e., top=top-1.
POP operation
Applications of stack:
• The various applications in which stacks are used are
• Conversion of expressions:
– The mathematical expressions that we write in a program are
called infix expressions. In infix expressions, an operator is in
between 2 operands. Evaluating these expressions is difficult
and hence they are converted into either postfix or prefix
expression.
• Evaluation of expressions:
– An arithmetic expression represented in the form of either
postfix or prefix can be easily evaluated.
– To implement recursion
– To reverse a string
– To check whether a string is palindrome or not
– To store history data
Queues
• 1. A queue can be defined as an ordered list
which enables insert operations to be performed
at one end called REAR and delete operations to
be performed at another end called FRONT.
• 2. Queue is referred to be as First In First Out list.
Queues
The operations possible on queue are

• 1. Insert operation (adding element/ Enqueue)


• 2. Delete operation (removing element/ dequeue)
• 3. Display operation (display elements in queue)
The operations possible on queue are
Types of Queue
• Circular queue,
• Double ended queue,
• Priority queue.
Types of Queue
• Circular queue:
– Circular queue is a linear data structure. It follows
FIFO principle. In circular queue the last node is
connected back to the first node to make a circle.
Circular linked list follow the First In First Out
principle. Elements are added at the rear end and
the elements are deleted at front end of the queue
Types of Queue
• Double ended queue:
• It is a homogeneous list of elements in which
insertion and deletion operations are performed
from both the ends. That is, we can insert
elements from the rear end or from the front
ends. Hence it is called double ended queue. It is
commonly referred as deque.
Types of Queue
• Priority queue:
• Here a priority is assigned to each element of
the queue. These priorities are considered at the
time of processing the elements. The elements
having higher priority are processed before that
of lower priority.
Applications of Queue:
• 1. Used in job scheduling algorithm.
• 2. Used in printers to store the requests made
for printing when the printer is busy.
• 3. It can be used to store data in first in first out
format.
• 4. For CPU scheduling.
Disadvantages of arrays:
• 1. The size of the array is fixed and the memory
allocation is done before the start of execution in static
arrays. The memory required for an application may be
more, less or equal. If the memory is more then it
results in wastage of space or if the space is less it is not
possible to allocate extra memory for arrays during
execution.
• 2. Array items are stored in continuous memory – which
may not be available always. However there might still
be enough space available and cannot be used as they
are not continuous.
• 3. Insertion and deletion operations involving array is
tedious.
Linked list
• All the disadvantages of the arrays can be overcome
using linked lists
• A Linked list is a data structure which is collection of
zero or more nodes with each node consisting of two
field’s data/info and link. The data field holds the actual
information that has to be processed and link contains
the address of the next node.
The linked lists are classified as:
• Singly linked list
• Doubly linked list
• Circular singly linked list
• Circular doubly linked list
Singly linked list
• A linked list is a linear collection of data elements,
called nodes, each pointing to the next node by means
of a pointer. It is a data structure consisting of a group
of nodes which together represent a sequence. Under
the simplest form, each node is composed of data and a
reference (in other words, a link) to the next node in
the sequence. This structure allows for efficient
insertion or removal of elements from any position in
the sequence during iteration
Doubly linked list
• A doubly linked list is a linked data structure that
consists of a set of sequentially linked records called
nodes. Each node contains two fields, called links, that
are references to the previous and to the next node in
the sequence of nodes. The beginning and ending
nodes' previous and next links, respectively, point to
some kind of terminator, typically a sentinel node or
null, to facilitate traversal of the list.
Circular singly linked list
• In the last node of a list, the link field often
contains a null reference, a special value used to
indicate the lack of further nodes. A less
common convention is to make it point to the
first node of the list; in that case the list is said to
be 'circular' or 'circularly linked'; otherwise it is
said to be 'open' or 'linear'
Circular doubly linked list
• In the case of a circular doubly linked list, the
only change that occurs is that the end, or "tail",
of the said list is linked back to the front, or
"head", of the list and vice versa.
Applications of linked lists
• Linked lists are used in the following applications
– 1. Polynomial manipulation.
– 2. Linked dictionary or symbol table of a
compiler.
– 3. Multiple precision arithmetic operations.
Trees
• A tree is a non linear data structure made up of
nodes/vertices and edges without having any cycle
that represents parents- child relationship between
the data items stored in it. The tree with no nodes is
called the null or empty tree. A tree that is not empty
consists of a root node and potentially many levels of
additional nodes that form a hierarchy.
The pictorial representation of a tree
The pictorial representation of a tree
• Some of the terminologies used to describe tree
data structure are as follows:
– Root: the top node in a tree
– Parent: node which have children.
– Siblings: nodes with same parent.
– Leaf: a node with no children
– Degree: number of sub trees of a node
– Edge: connection between one node to another.
Binary tree
• A binary tree is a tree in which each node can
have at most two children. One of them is
designated as the left child and the other one
has right child.
Applications of trees
• 1. For decision making assignment problems like
decision trees
• 2. To implement game playing applications like
chess, checkers etc.
• 3. To implement searching problem.
• 4. To implement sorting problem.
• 5. To represent infix, prefix and postfix
expressions.
Abstract data type (ADT):
• ADT is a collection of related data items together
with basic relations between them and
operations to be performed on them.
– Stack ADT
– Queue ADT
– Linked List ADT
Abstract data type (ADT):
• Stack ADT: A list of data items that can only be
accessed at one end called the top of the stack
using the operations push and pop.
• Queue ADT: A list of data items in which an item
can be deleted from the front end of the queue
and an element can be inserted at the rear end
of the queue using the operations Enqueue and
Dequeue.
• Linked List ADT: A list of connected nodes,
where in each node is made up of two parts 1.
Information part and 2. Address part. Using
operations insert, delete, traverse and display.

You might also like