Module-2-DSA_VTU
Module-2-DSA_VTU
MODULE 2
QUEUES: Queues, Circular Queues, Using Dynamic Arrays, Multiple Stacks and queues.
LINKED LISTS: Singly Linked, Lists and Chains, Representing Chains in C, Linked Stacks
and Queues, Polynomials.
DEFINITION
• “A queue is an ordered list in which insertions (additions, pushes) and deletions (removals and
pops) take place at different ends.”
• The end at which new elements are added is called the rear, and that from which old elements
are deleted is called the front.
• Given a queue Q = (a0, a1,……… an-1) , a0, is the front element an-1 is the rear element, ai+1 is
behind ai 0< =i < n-1.
If the elements are inserted A, B, C, D and E in this order, then A is the first element deleted
from the queue. Since the first element inserted into a queue is the first element removed, queues
are also known as First-In-First-Out (FIFO) lists.
• Figure indicates the way elements will be deleted from the queue and the way new elements
will be added to the queue.
• Whenever an element is deleted from the queue, the value of FRONT is increased by 1; this
can be implemented by the assignment FRONT := FRONT + 1
• When an element is added to the queue, the value of REAR is increased by 1; this can be
implemented by the assignment REAR := REAR + 1
VTU Mysore 1
DATA STRUCTURES-BCS304 MODULE 2
VTU Mysore 2
DATA STRUCTURES-BCS304 MODULE 2
In the queue, two variables are used which are front and rear. The queue increments rear in
addq( ) and front in delete( ). The function calls would be addq (item); and item =delete( );
4. addq(item)
void addq(int *rear, element item)
{
// add an item to the queue
if (rear == MAX_QUEUE_SIZE-1)
{ queue_Full();
return;
}
queue [++rear] = item;
}
VTU Mysore 3
DATA STRUCTURES-BCS304 MODULE 2
5. deleteq( )
element deleteq(int *front, int *rear)
{ /* remove element at the front of the queue */
if (front == rear)
return queue_Empty( ); /* return an error key
return queue[++front];
}
Program: Delete from a queue
6. queueFull( )
The queueFull function which prints an error message and terminates execution
void queueFull()
{
fprintf(stderr, "Queue is full, cannot add element");
exit(EXIT_FAILURE);
}
VTU Mysore 4
DATA STRUCTURES-BCS304 MODULE 2
Drawback of Queue
When item enters and deleted from the queue, the queue gradually shifts to the right as
shown in figure.
In this above situation, when we try to insert another item, which shows that the queue is full
. This means that the rear index equals to MAX_QUEUE_SIZE -1. But even if the space is
available at the front end, rear insertion cannot be done.
VTU Mysore 5
DATA STRUCTURES-BCS304 MODULE 2
Method 2:
Circular Queue
• It is “The queue which wrap around the end of the array.” The array positions are
arranged in a circle.
• In this convention the variable front is changed. front variable points one position
counterclockwise from the location of the front element in the queue. The convention
for rear is unchanged.
CIRCULAR QUEUES
• It is “The queue which wrap around the end of the array.” The array positions are
arranged in a circle as shown in figure.
• In this convention the variable front is changed. front variable points one position
counterclockwise from the location of the front element in the queue. The convention
for rear is unchanged.
VTU Mysore 6
DATA STRUCTURES-BCS304 MODULE 2
• When the array is viewed as a circle, each array position has a next and a previous
position. The position next to MAX-QUEUE-SIZE -1 is 0, and the position that
precedes 0 is MAX-QUEUE-SIZE -1.
• When the queue rear is at MAX_QUEUE_SIZE-1, the next element is inserted at
position 0.
• In circular queue, the variables front and rear are moved from their current position
to the next position in clockwise direction. This may be done using code
if (rear = = MAX_QUEUE_SIZE-1)
rear = 0;
else rear++;
VTU Mysore 7
DATA STRUCTURES-BCS304 MODULE 2
Element deleteq()
/* remove front element from the queue */
element item;
if (front == rear)
return queueEmpty( ); /* return an error key */
front = (front+1)% MAX_QUEUE_SIZE;
return queue[front];
}
Program: Delete from a circular queue
VTU Mysore 8
DATA STRUCTURES-BCS304 MODULE 2
Consider the full queue of figure (a). This figure shows a queue with seven elements in
an array whose capacity is 8. A circular queue is flatten out the array as in Figure (b).
To get a proper circular queue configuration, slide the elements in the right segment (i.e.,
elements A and B) to the right end of the array as in figure (d)
VTU Mysore 9
DATA STRUCTURES-BCS304 MODULE 2
Below program gives the code to add to a circular queue using a dynamically allocated array.
Below program obtains the configuration of figure (e) and gives the code for queueFull.
The function copy (a,b,c) copies elements from locations a through b-1 to locations
beginning at c.
void queueFull( )
{ /* allocate an array with twice the capacity */
element *newQueue;
MALLOC ( newQueue, 2 * capacity * sizeof(* queue));
/* copy from queue to newQueue */
int start = ( front + ) % capacity;
if ( start < 2) /* no wrap around */
copy( queue+start, queue+start+capacity-1,newQueue);
else
{ /* queue wrap around */
VTU Mysore 10
DATA STRUCTURES-BCS304 MODULE 2
VTU Mysore 11
DATA STRUCTURES-BCS304 MODULE 2
VTU Mysore 12
DATA STRUCTURES-BCS304 MODULE 2
The top[i] == boundary[i+1] condition in push implies only that a particular stack ran out of
memory, not that the entire memory is full. But still there may be a lot of unused space between
other stacks in array memory as shown in Figure.
Therefore, create an error recovery function called stackFull , which determines if there is any
free space in memory. If there is space available, it should shift the stacks so that space is
allocated to the full stack.
VTU Mysore 13
DATA STRUCTURES-BCS304 MODULE 2
LINKED LIST
DEFINITION
A linked list, or one-way list, is a linear collection of data elements, called nodes, where the
linear order is given by means of pointers. That is, each node is divided into two parts:
• The first part contains the information of the element, and
• The second part, called the link field or nextpointer field, contains the address of the
next node in the list.
A linked list is a dynamic data structure where each element (called a node) is made up of two
items - the data and a reference (or pointer) which points to the next node. A linked list is a
collection of nodes where each node is connected to the next node through a pointer.
VTU Mysore 14
DATA STRUCTURES-BCS304 MODULE 2
DATA LINK
VTU Mysore 15
DATA STRUCTURES-BCS304 MODULE 2
REPRESENTING CHAIN IN C
The following capabilities are needed to make linked representation
1. A mechanism for defining a node’s structure, that is, the field it contains. So self-
referential structures can be used
2. A way to create new nodes, so MALLOC functions can do this operation
3. A way to remove nodes that no longer needed. The FREE function handles this
operation.
VTU Mysore 16
DATA STRUCTURES-BCS304 MODULE 2
VTU Mysore 17
DATA STRUCTURES-BCS304 MODULE 2
Printing list
VTU Mysore 18
DATA STRUCTURES-BCS304 MODULE 2
Linked Stack
The representation of n ≤ MAX_STACKS
VTU Mysore 19
DATA STRUCTURES-BCS304 MODULE 2
Functions push and pop add and delete items to/from a stack.
void push(int i, element item)
{/* add item to the ith stack */
stackPointer temp;
MALLOC(temp, sizeof(*temp));
temp→data = item;
temp→link = top[i];
top[i] = temp;
}
Add to a linked stack
Function push creates a new node, temp, and places item in the data field and top in the link field. The
variable top is then changed to point to temp. A typical function call to add an element to the ith stack would
be push (i,item).
element pop(int i)
/* remove top element from the ith stack */
stackPointer temp = top[i];
element item;
if (! temp)
return stackEmpty();
item = temp→data;
top[i] = temp→link;
free (temp) ;
return item;
VTU Mysore 20
DATA STRUCTURES-BCS304 MODULE 2
Linked Queue
The representation of m ≤ MAX_QUEUES queues,
#define MAX-QUEUES 10 /* maximum number of queues */
Typedef struct queue *queuePointer;
typedef struct {
element data;
queuePointer link;
} queue;
queuePointer front[MAX_QUEUES], rear[MAX_QUEUES];
Functions addq and deleteq implement the add and delete operations for multiple
queues.
void addq(i, item)
VTU Mysore 21
DATA STRUCTURES-BCS304 MODULE 2
Function deleteq is similar to pop since nodes are removing that is currently at the start
of the list. Typical function calls would be addq (i, item); and item = deleteq (i);
APPLICATIONS OF LINKED LISTS – POLYNOMIALS
1. Representation of the polynomial:
where the ai are nonzero coefficients and the ei are nonnegative integer exponents such that
em-l > em-2 > ... > e1 > e0 ≥ 0.
Present each term as a node containing coefficient and exponent fields, as well as a pointer
tothe next term.
Assuming that the coefficients are integers, the type declarations are:
typedef struct polyNode
*polyPointer; typedef struct {
int coef; int expon;
polyPointer link;
} polyNode;
polyPointer a,b;
VTU Mysore 22
DATA STRUCTURES-BCS304 MODULE 2
2. Adding Polynomials
To add two polynomials, examine their terms starting at the nodes pointed to by a and b.
• If the exponents of the two terms are equal, then add the two coefficients and create a new term
for the result, and also move the pointers to the next nodes in a and b.
• If the exponent of the current term in a is less than the exponent of the current term inb,
then create a duplicate term of b, attach this term to the result, called c, and advance the pointer
to the next term in b.
• If the exponent of the current term in b is less than the exponent of the current term ina,
then create a duplicate term of a, attach this term to the result, called c, and advance the pointer
to the next term in a
Below figure illustrates this process for the polynomials addition.
VTU Mysore 23
DATA STRUCTURES-BCS304 MODULE 2
VTU Mysore 24
DATA STRUCTURES-BCS304 MODULE 2
Analysis of padd:
To determine the computing time of padd, first determine which operations contribute to the
cost. For this algorithm, there are three cost measures:
(l) Coefficient additions
(2) Exponent comparisons
(3) Creation of new nodes for c
3. Erasing a Polynomial
Void erase(polyPointer *ptr)
{
polyPointer temp;
while(*ptr)
{
Temp=*ptr;
*ptr=(*ptr)->link;
Free(temp)
}
}
Program:erasing polynomial
VTU Mysore 25
DATA STRUCTURES-BCS304 MODULE 2
• We can free the nodes that are no longer used and can reuse the nodes later by maintain
a list called freed. When new node is needed, we examine this list. If the list is not
empty then we may use one of the nodes. Only when list is empty, we need to create
anode using malloc.
• Let avail be a variable of type polyPointer that points to first node in our list of freed
nodes. We call this list as available space list or avail list.
• Initially set avail to NULL. Instead of using malloc or free we use getNode and
retNode.
• Erase circular list in a fixed amount of time independent of number of nodes in list
using cerase
polyPointer getNode(void)
{
polyPointer node;
if(avail)
{
node=avail;
avail=avail->link;
}
else
malloc(node, sizeof(*node));
return node
}
Program:getNode function
VTU Mysore 26
DATA STRUCTURES-BCS304 MODULE 2
VTU Mysore 27