unit 2 data structure
unit 2 data structure
QUEUE:
Queue is an ordered (linear) collection of items where an item is inserted at one end called the
rear and an existing item is removed at the other end called the front. Queue follows FIFO (First
in First out).
A queue is an ordered collection of items where the addition of new items and the removal of
existing items always take place at different ends.
Operations on queue:
Queue(): create a new queue that is empty. It needs no parameters and returns an empty
queue
Enqueuer(item): adds a new item to the rear of the queue. It needs the item and returns
nothing. This operation is generally called as push
Dequeu(): removes the front item from the queue. It needs no parameters and returns the
item. The queue is modified. This operation is generally called as pop
Isempty(): test to see whether the queue is empty. It needs no parameters and returns a
Boolean balue.
Size(): returns the number of items in the queue. It needs no parameters and returns an
integer.
Disadvantages
static Data Structure, fixed size.
If the queue has a large number of enqueue and dequeue operations, at some point
(in case of linear increment of front and rear indexes) we may not be able to insert
elements in the queue even if the queue is empty (this problem is avoided by
using circular queue).
Maximum size of a queue must be defined prior
2. Linked list: queues are also represented using linked lists. In queues an item should be
inserted from rear end and an item should be removed from the front end. The pointer
front contains location of the first node of the linked list and another pointer rear contains
location of the last node.
Algorithm for insertion: let q is the array name consisting n elements. Front is the pointer that
contains the location of the element to be deleted and rear contains the location of the inserted
element. Item is the element to be inserted
Step1: if rear=n-1
Step2: display “ queue is full or overflow”
Step3: exit
Step4: end if
Step5: if front=-1 then
Step6: front=0
Step7: rear=0
Step8: else
Step9: rear=rear+1
Step10: q[rear]=item
Step11: return
Algorithm for deletion: let q is the array name consisting n elements. Front is the pointer that
contains the location of the element to be deleted and rear contains the location of the inserted
element. Item is the element to be inserted. This algorithm deletes the element at front position
Step1: if front=-1
Step2: display “ queue is empty or underflow”
Step3: exit
Step4: end if
Step5: item=q[front]
Step6: if front=rear then
Step7: front=0
Step8: rear=0
Step9: else
Step10: front=front+1
Step11: return
Types of queue:
Linear or simple queue: in simple queue insertion occurs at the rear end of the list and deletion
occurs at the front end of the list.
Circular queue: a circular queue is a queue in which all nodes are treated as circular such that the
last node follows the first node.
Front=0
Rear=6
Dequeue( double ended queue): it is a queue in which insertion and deletion takes place at both
the ends
LINKED LIST
A linked list is a collection of nodes. Each node is divided into two parts. The first part
contains Data part store the data item Link part store the address of next node
Linked list start with a special pointer called start pointer and terminated with NULL
pointer. An arrow is used to indicate the location of the next node. The NULL tells about
end of the list
Singly linked list: it is linear collection of data item called node, where each node has divided
into two parts i.e. data part and link part, data part store the data item and link part store the
address of next node. Limitation of the linked list is we can traverse only in one direction i.e
forward direction.
A singly linked list is a list that consists of a collection of nodes, and each node has two parts;
one part is the data part, and another part is the address. The singly linked can also be called a
chain as each node refers to another node through its address part. We can perform various
operations on a singly linked list like insertion, deletion, and traversing.
Doubly linked list:
A doubly linked list is another type of linked list. It is called a doubly linked list because it
contains two addresses while a singly linked list contains a single address. It is a list that has total
three parts, one is a data part, and others two are the pointers, i.e., previous and next. The
previous pointer holds the address of the previous node, and the next pointer holds the address of
the next node. Therefore, we can say that list has two references, i.e., forward and backward
reference to traverse in either direction.
We can also perform various operations on a doubly-linked list like insertion, deletion, and
traversing.
Data part store the data item , backward direction store address of previous node and forward
direction store address of next node.
Advantage of doubly linked list is we can traverse in any direction forward or backward.
Drawback of doubly linked list is it requires more memory compared to singly linked list
because we need an extra pointer to point previous node.
Inserting node at the beginning
Node *newnode
newnode->data=value
newnode->link=start
start=newnode
A singly linked list is a list that consists of a collection of nodes, and each node has two parts;
one part is the data part, and another part is the address. The singly linked can also be called a
chain as each node refers to another node through its address part. We can perform various
operations on a singly linked list like insertion, deletion, and traversing.
Memory representation of singly linked list
A singly linked list is a list that consists of a collection of nodes, and each node has two parts;
one part is the data part, and another part is the address. The singly linked can also be called a
chain as each node refers to another node through its address part. We can perform various
operations on a singly linked list like insertion, deletion, and traversing.
Memory representation of Doubly linked list: A doubly linked list is another type of linked list.
It is called a doubly linked list because it contains two addresses while a singly linked list
contains a single address. It is a list that has total three parts, one is a data part, and others two
are the pointers, i.e., previous and next. The previous pointer holds the address of the previous
node, and the next pointer holds the address of the next node. Therefore, we can say that list has
two references, i.e., forward and backward reference to traverse in either direction.
We can also perform various operations on a doubly-linked list like insertion, deletion, and
traversing.