Professional Documents
Culture Documents
Data Strucuture Notes
Data Strucuture Notes
Data types
A particular kind of data item, as defined by the values it can take, the programming
language used, or the operations that can be performed on it.
Primitive Data Structure
Primitive data structures are basic structures and are directly operated upon by machine
instructions.
Primitive data structures have different representations on different computers.
Integers, floats, character and pointers are examples of primitive data structures.
These data types are available in most programming languages as built in type.
o Integer: It is a data type which allows all values without fraction part. We can
use it for whole numbers.
o Float: It is a data type which use for storing fractional numbers.
o Character: It is a data type which is used for character values.
Pointer: A variable that holds memory address of another variable are called pointer.
A data structure is said to be Linear, if its elements are connected in linear fashion by
means of logically or in sequence memory locations.
There are two ways to represent a linear data structure in memory,
o Static memory allocation
o Dynamic memory allocation
The possible operations on the linear data structure are: Traversal, Insertion, Deletion,
Searching, Sorting and Merging.
Examples of Linear Data Structure are Stack and Queue.
Stack: Stack is a data structure in which insertion and deletion operations are
performed at one end only.
o The insertion operation is referred to as ‘PUSH’ and deletion operation is
referred to as ‘POP’ operation.
o Stack is also called as Last in First out (LIFO) data structure.
Queue: The data structure which permits the insertion at one end and Deletion at
another end, known as Queue.
o End at which deletion is occurs is known as FRONT end and another end at
which insertion occurs is known as REAR end.
o Queue is also called as First in First out (FIFO) data structure.
Nonlinear data structures are those data structure in which data items are not arranged
in a sequence.
Examples of Non-linear Data Structure are Tree and Graph.
Tree: A tree can be defined as finite set of data items (nodes) in which data items are
arranged in branches and sub branches according to requirement.
o Trees represent the hierarchical relationship between various elements.
o Tree consist of nodes connected by edge, the node represented by circle and
edge lives connecting to circle.
Graph: Graph is a collection of nodes (Information) and connecting edges (Logical
relation) between nodes.
o A tree can be viewed as restricted graph.
o Graphs have many types:
Un-directed Graph
Directed Graph
Mixed Graph
Multi Graph
Simple Graph
Null Graph
Weighted Graph
1. Create:- The create operation results in reserving memory for program elements. This
can be done by declaration statement. Creation of data structure may take place either
during compile-time or run-time. malloc() function of C language is used for creation.
2. Destroy:- Destroy operation destroys memory space allocated for specified data
structure. free() function of C language is used to destroy data structure.
3. Selection:- Selection operation deals with accessing a particular data within a data
structure.
4. Updation:- It updates or modifies the data in the data structure.
5. Searching:- It finds the presence of desired data item in the list of data items, it may
also find the locations of all elements that satisfy certain conditions.
6. Sorting:- Sorting is a process of arranging all data items in a data structure in a
particular order, say for example, either in ascending order or in descending order.
7. Merging:- Merging is a process of combining the data items of two different sorted list
into a single sorted list.
8. Splitting:- Splitting is a process of partitioning single list to multiple list.
9. Traversal:- Traversal is a process of visiting each and every node of a list in systematic
manner.
A stack is a non-primitive linear data structure. It is an ordered list in which addition of new
data item and deletion of already existing data item is done from only one end, known as top of
stack(TOS).
As all the deletion and insertion in a stack is done from top of the stack, the last added element
will be the first to be removed from the stack. That is the reason why stack is also called Last-
in-First-out (LIFO).
A stack is simply a list of elements with insertions and deletions permitted at one end—called
the stack top. That means that it is possible to remove elements from a stack in reverse order
from the insertion of elements into the stack. Thus, a stack data structure exhibits the LIFO
(last in first out) property.
Push and pop are the operations that are provided for insertion of an element into the stack and
the removal of an element from the stack, respectively.
Operations on a Stack
A stack supports two basic operations:
Push Operation
Pop Operation
Push Operation : The push operation is used to insert an element into the stack. The new
element is added at the topmost position of the stack. However, before inserting the value, we
must first check if TOP=MAX–1, because if that is the case, then the stack is full and no more
insertions can be done. If an attempt is made to insert a value in a stack that is already full, an
OVERFLOW message is printed.
Pop Operation : The pop operation is used to delete the topmost element from the stack.
However, before deleting the value, we must first check if TOP=NULL because if that is the
case, then it means the stack is empty and no more deletions can be done. If an attempt is made
to delete a value from a stack that is already empty, an UNDERFLOW message is printed.
Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but are
limited in size and Linked List requires overhead to allocate, link, unlink, and deallocate, but is
not limited in size. Here we will implement Stack using array.
Push Operation
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.
If the linked list is used to implement the stack, then in step 3, we need to allocate space
dynamically.
Pop Operation
Accessing the content while removing it from the stack, is known as a Pop Operation. In an
array implementation of pop() operation, the data element is not actually removed,
instead top is decremented to a lower position in the stack to point to the next value. But in
linked-list implementation, pop() actually removes data element and deallocates memory
space.
A Pop operation may involve the following steps −
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
Algorithm for Pop Operation
if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure
Application of Stacks
Some of the areas where stacks can be easily applied for a simple and efficient solution.
Reversing a list
Parentheses checker
Conversion of an infix expression into a postfix expression
Evaluation of a postfix expression
Conversion of an infix expression into a prefix expression
Evaluation of a prefix expression
Recursion
Tower of Hanoi
Queue:
Queue is also an abstract data type or a linear data structure, just like stack data structure,
in which the first element is inserted from one end called the REAR(also called tail), and
the removal of existing element takes place from the other end called as FRONT(also
called head).
This makes queue as FIFO(First in First Out) data structure, which means that element
inserted first will be removed first.
Which is exactly how queue system works in real world. If you go to a ticket counter to
buy movie tickets, and are first in the queue, then you will be the first one to get the
tickets. Right? Same is the case with Queue data structure. Data inserted first, will leave
the queue first.
The process to add an element into queue is called Enqueue and the process of removal
of an element from queue is called Dequeue.
Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it, and then
completely erasing it from the memory. Here we shall try to understand the basic operations
associated with queues −
enqueue() − add (store) an item to the queue.
dequeue() − remove (access) an item from the queue.
Queue operations may involve initializing or defining the queue, utilizing it, and then
completely erasing it from the memory. The basic operations associated with queues −
enqueue() − add (store) an item to the queue.
dequeue() − remove (access) an item from the queue.
Few more functions are required to make the above-mentioned queue operation efficient.
These are −
isfull() − Checks if the queue is full.
isempty() − Checks if the queue is empty.
Enqueue Operation
Queues maintain two data pointers, front and rear. Therefore, its operations are
comparatively difficult to implement than that of stacks.
The following steps should be taken to enqueue (insert) data into a queue −
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow error and exit.
Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
Step 4 − Add data element to the queue location, where the rear is pointing.
Step 5 − return success.
Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen
situations.
procedure enqueue(data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
Dequeue Operation
Accessing data from the queue is a process of two tasks − access the data where front is
pointing and remove the data after access. The following steps are taken to
perform dequeue operation −
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce underflow error and exit.
Step 3 − If the queue is not empty, access the data where front is pointing.
Step 4 − Increment front pointer to point to the next available data element.
Step 5 − Return success.
Algorithm for dequeue operation
procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
Applications of Queue
Queue, as the name suggests is used whenever we need to manage any group of objects in an
order in which the first one coming in, also gets out first while the others wait for their turn,
like in the following scenarios:
1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life scenario, Call Center phone systems uses Queues to hold people calling
them in an order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the same
order as they arrive i.e First come first served.
They are a dynamic in nature which allocates the memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked List reduces the access time.
Basic Operations