0% found this document useful (0 votes)
107 views35 pages

Data Structure

The document discusses different data structures and their uses. It defines data structures as organized ways of storing data in a computer so it can be used efficiently. Common data structures include stacks, queues, linked lists, trees, and graphs. Stacks follow LIFO order and have push and pop operations. Queues follow FIFO order and have enqueue and dequeue operations. Linked lists use nodes connected by pointers to dynamically store data.

Uploaded by

sushmitha patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views35 pages

Data Structure

The document discusses different data structures and their uses. It defines data structures as organized ways of storing data in a computer so it can be used efficiently. Common data structures include stacks, queues, linked lists, trees, and graphs. Stacks follow LIFO order and have push and pop operations. Queues follow FIFO order and have enqueue and dequeue operations. Linked lists use nodes connected by pointers to dynamically store data.

Uploaded by

sushmitha patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

DATA STRUCTURE

 Data – a collection of facts (numbers,


words, measurements, observations, etc)
that has been translated into a form that
computers can process
 A data structure is a particular way of
organizing data in a computer so that it
can be used effectively.
 The idea is to reduce the space and time
complexities of different tasks.
 eg: listing the names in alphabetical
order would give their position meaning
Why do we need data structure ?
 Each Data Structure allows data to be stored in specific manner.
 Data Structure allows efficient data search and retrieval.
 Specific Data structures are decided to work for specific problems.
 Data structures can be used to organize the storage and retrieval of
information stored in both main memory and secondary memory.
 To solve real life problems efficiently
1. Insertion
2. Deletion
3. Sorting
4. Searching
5. Traversing
Classification of data structure
STACK
WHAT IS STACk ?
Stack is a linear data structure which follows a particular
order in which the opeartions are performed.The order may
be LIFO (Last In First Out) or FILO (First In Last Out).
OPERATIONs ON STACK
There are two operations in the
stack:
1.PUSH
2.POP

Two conditions :
1.Stack Overflow
2.Stack Underflow
PUSH: push() function is used to insert an element at the top of
the stack.The element is added to the stack container and the
size of the stack is increased by 1.

void push(element item)

if(top>=MAX_STACK_SIZE-1)

stackFull();

else

stack[++top]=item;

}
POP: pop() function is used to remove an element from the top of
the stack(newest element in the stack).The element removed from
the stack container and the size of the stack is decreased by 1.

void pop ()

if(top== -1)

return stackEmpty();

else

return stack [top--];

}
APPLICATIONS

 Infix to Postfix /Prefix conversion.


 Used in many algorithms like:
Tower of Hanoi.
Tree Traversals.
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.
 The process to add an element into queue is called Enqueue and the process
of removal of an element from queue is called Dequeue.
Queue can be implemented using an Array, Stack or Linked List. The
easiest way of implementing a queue is by using an Array.
Initially the head(FRONT) and the tail(REAR) of the queue points at the
first index of the array (starting the index of array from 0)
As we add elements to the queue, the tail keeps on moving ahead,
always pointing to the position where the next element will be inserted,
while the head remains at the first index.
Algorithm for ENQUEUE operation
 Check if the queue is full or not.
 If the queue is full, then print overflow error and exit the
program.
 If the queue is not full, then increment the tail(rear) and add
the element.
Algorithm for DEQUEUE operation
 Check if the queue is empty or not.
 If the queue is empty, then print underflow error and exit
the program.
 If the queue is not empty, then print the element at the
head(front) and increment the head.
Complexity Analysis of Queue Operations
 we know exactly, on which position new element will be added and from
where an element will be removed, hence both these operations requires
a single step.
1. Enqueue: O(1)
2. Dequeue: O(1)
3. Size: O(1)
What is a Circular Queue?
Before we start to learn about Circular queue, we should first understand, why we
need a circular queue, when we already have linear queue data structure.
In a Linear queue, once the queue is completely full, it's not possible to insert more
elements. Even if we dequeue the queue to remove some of the elements, until the
queue is reset, no new elements can be inserted. You must be wondering why?

When we dequeue any element to remove it from the queue, we are actually moving
the front of the queue forward, thereby reducing the overall size of the queue. And we
cannot insert new elements, because the rear pointer is still at the end of the queue.
 Circular Queue is also a linear data structure, which follows the principle
of FIFO(First In First Out), but instead of ending the queue at the last position, it
again starts from the first position after the last, hence making the queue behave
like a circular data structure.

Implementation of Circular Queue
 Insert Operation
 Step 1: If REAR = SIZE-1 then
REAR = 0
Else
REAR=REAR + 1
Step 2: If FRONT = REAR then
Write ("Circular Queue Overflow")
Step 3: CQ[REAR]=X
Step 4: If FRONT = -1 then
FRONT=0
 Delete Operation
 Step 1: If FRONT = -1 then
Write ("Circular Queue Underflow")
Step 2: Return (CQ [FRONT])
Step 3: If FRONT = REAR then
FRONT=REAR=-1
Step 4: If FRONT = SIZE-1 then
FRONT=0
Else
FRONT=FRONT+1
Double Ended Queue Datastructure
 Double Ended Queue is also a Queue data structure in which the insertion and
deletion operations are performed at both the ends (front and rear). That means,
we can insert at both front and rear positions and can delete from both front and
rear positions.
INPUT RESTRICTED QUEUE
An input-restricted Deque is one where deletion can be made
from both ends, but insertion can be made at one end only.

Output Restricted Deque


An output-restricted Deque is one where insertion can be made at both ends,
but deletion can be made from one end only.
Priority Queue
Priority Queue is an extension of queue with following
properties.
Every item has a priority associated with it.
An element with high priority is dequeued before an element
with low priority.
If two elements have the same priority, they are served
according to their order in the queue.
Linked List Representation

 Linked list can be visualized as a chain of nodes, where every node


points to the next node.
 For example :

Linked list example


 consider a school taking the children to a movie theatre as
excursion. But there, every child gets to sit separately. So each
one is given a sheet of paper that has the seat number of the
next child. Like that, by finding the child in the first row, we can
find all the children with the link.
SINGLE

DOUBLE

CIRCULAR
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.
Adding a new node in linked list is a more than one step activity. We
shall learn this with diagrams here. First, create a node using the same
structure and find the location where it has to be inserted.
 Imagine that we are inserting a node B (NewNode),
between A (LeftNode) and C (RightNode). Then point B.next to C

 NewNode.next −> RightNode;
 Now, the next node at the left should point to the new node.
 LeftNode.next −> NewNode;
Doubly Linked List
 Doubly Linked List is a variation of Linked list in which navigation is
possible in both ways, either forward and backward easily as
compared to Single Linked List.
 Link − Each link of a linked list can store a data called an element.

 Next − Each link of a linked list contains a link to the next link called
Next.

 Prev − Each link of a linked list contains a link to the previous link
called Prev.
 Doubly Linked List contains a link element called first and last.
 Each link carries a data field(s) and two link fields called next and prev.
 Each link is linked with its next link using its next link.
 Each link is linked with its previous link using its previous link.
 The last link carries a link as null to mark the end of the list.
 Deletion is also a more than one step process. We shall learn with
pictorial representation. First, locate the target node to be removed, by
using searching algorithms.
 The left (previous) node of the target node now should point to the
next node of the target node −

 LeftNode.next −> TargetNode.next;


Circular Linked List
 Circular Linked List is a variation of Linked list in which the first
element points to the last element and the last element points to
the first element. Both Singly Linked List and Doubly Linked List can
be made into a circular linked list.

21 30 45
Singly Linked List as Circular
 In singly linked list, the next pointer of the last node points to the first
node.

Doubly Linked List as Circular


 In doubly linked list, the next pointer of the last node points to the first
node and the previous pointer of the first node points to the last node
making the circular in both directions.
Advantages of linked list over array

 Both Arrays and Linked List can be used to store linear data of similar
types, but they both have some advantages and disadvantages
over each other.
 The size of the arrays is fixed: So we must know the upper limit on
the number of elements in advance. Also, generally, the allocated
memory is equal to the upper limit irrespective of the usage, and in
practical uses, the upper limit is rarely reached.
 Inserting a new element in an array of elements is expensive
because a room has to be created for the new elements and to
create room existing elements have to be shifted.

You might also like