Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Data Structures

Overview of Data Structures

 Data structure is a representation of the logical relationship existing between


individual elements of data.
 Data Structure is a way of organizing all data items that considers not only the elements
stored but also their relationship to each other.
 We can also define data structure as a mathematical or logical model of a particular
organization of data items.
 The representation of particular data structure in the main memory of a computer is
called as storage structure.
 The storage structure representation in auxiliary memory is called as file structure.
 It is defined as the way of storing and manipulating data in organized form so that it can
be used efficiently.
 Data Structure mainly specifies the following four things
o Organization of Data
o Accessing methods
o Degree of associativity
o Processing alternatives for information
 Algorithm + Data Structure = Program
 Data structure study covers the following points
o Amount of memory require to store.
o Amount of time require to process.
o Representation of data in memory.
o Operations performed on that data.

Classification of Data Structure


Data Structures are normally classified into two broad categories

1. Primitive Data Structure


2. Non-primitive data Structure

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.

Non primitive Data Type

 These are more sophisticated data structures.


 These are derived from primitive data structures.
 The non-primitive data structures emphasize on structuring of a group of homogeneous
or heterogeneous data items.
 Examples of Non-primitive data type are Array, List, and File etc.
 A Non-primitive data type is further divided into Linear and Non-Linear data structure
o Array: An array is a fixed-size sequenced collection of elements of the same
data type.
o List: An ordered set containing variable number of elements is called as Lists.
o File: A file is a collection of logically related information. It can be viewed as a
large list of records consisting of various fields.

Linear data structures

 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

 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

Difference between Linear and Non Linear Data Structure


Linear Data Structure Non-Linear Data Structure
Every item is related to its previous and next Every item is attached with many other
time. items.
Data is arranged in linear sequence. Data is not arranged in sequence.
Data items can be traversed in a single run. Data cannot be traversed in a single run.
Eg. Array, Stacks, linked list, queue. Eg. tree, graph.
Implementation is easy. Implementation is difficult.

Operation on Data Structures


Design of efficient data structure must take operations to be performed on the data structures
into account. The most commonly used operations on data structure are broadly categorized
into following types

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.

Stacks in Data Structure

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.

Basic features of Stack

1. Stack is an ordered list of similar data type.


2. Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
3. push() function is used to insert new elements into the Stack and pop() function is used
to remove an element from the stack. Both insertion and removal are allowed at only
one end of Stack called Top.
4. Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty.

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.

Algorithm for PUSH Operation

A simple algorithm for Push operation can be derived as follows −


5. begin procedure push: stack, data
6.
7. if stack is full
8. return null
9. endif
10.
11. top ← top + 1
12. stack[top] ← data
13.
14. end procedure

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

A simple algorithm for Pop operation can be derived as follows −


begin procedure pop: stack

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.

Basic Operations on 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.

Algorithm for enqueue operation

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.

Difference between Stack and Queue


Stack Queue
A Linear List Which allows insertion or
A Linear List Which allows insertion atone end
deletion of an element at one end only is called
and deletion at another end is called as Queue
as Stack
Since insertion and deletion of an element are Since insertion and deletion of an element are
performed at one end of the stack, the elements performed at opposite end of the queue, the
can only be removed in the opposite order of elements can only be removed in the same order
insertion. of insertion.
Stack is called as Last In First Out (LIFO) List. Queue is called as First In First Out (FIFO) List.
The most and least accessible elements are Insertion of element is performed at FRONT end
called as TOP and BOTTOM of the stack and deletion is performed from REAR end
Example of stack is arranging plates in one
Example is ordinary queue in provisional store.
above one.
Insertion operation is referred as PUSH and Insertion operation is referred as ENQUEUE
deletion operation is referred as POP and deletion operation is referred as DQUEUE
Task Scheduling by Operating System uses
Function calling in any languages uses Stack
queue

Introduction to Linked Lists


Linked List is a very commonly used linear data structure which consists of group of nodes in
a sequence.
Each node holds its own data and the address of the next node hence forming a chain like
structure.
Linked Lists are used to create trees and graphs.

Advantages of Linked Lists

 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.

Disadvantages of Linked Lists


 The memory is wasted as pointers require extra memory for storage.
 No element can be accessed randomly; it has to access each node sequentially.
 Reverse Traversing is difficult in linked list.

Applications of Linked Lists

 Linked lists are used to implement stacks, queues, graphs, etc.


 Linked lists let you insert elements at the beginning and end of the list.
 In Linked Lists we don't need to know the size in advance.

Types of Linked List

Following are the various types of linked list.


 Simple Linked List − Item navigation is forward only.
 Doubly Linked List − Items can be navigated forward and backward.
 Circular Linked List − Last item contains link of the first element as next and the first
element has a link to the last element as previous.

Basic Operations

Following are the basic operations supported by a list.


 Insertion − Adds an element at the beginning of the list.
 Deletion − Deletes an element at the beginning of the list.
 Display − Displays the complete list.
 Search − Searches an element using the given key.
 Delete − Deletes an element using the given key.

Singly Linked List


 Singly linked lists contain nodes which have a data part as well as an address
part i.e. next, which points to the next node in the sequence of nodes.
 The operations we can perform on singly linked lists
are insertion, deletion and traversal.
Doubly Linked List
 In a doubly linked list, each node contains a data part and two addresses, one for
the previous node and one for the next node.

Circular Linked List


 In circular linked list the last node of the list holds the address of the first node hence
forming a circular chain.

You might also like