Module-2_DS_2024
Module-2_DS_2024
Module 2
QUEUES
• Queue is a linear list of elements in which insertion and deletion take place at
different ends. The end at which new element is inserted is called as rear and the
end from which element is deleted is called as front
• Queue has the property - FIFO
• To implement queue front and rear variables are used
A B C D
front rear
#define MAXQ 25
typedef struct {
int front, rear;
char item[MAX];
}QUEUE;
QUEUE q;
q.rear = -1;
q.front = -1;
Primitive operations – Qinsert, Qdelete
Once MAXQ items are added, even if we delete the items from Front, we can not
add items in vacant places. This is because rear has reached MAXQ value and there
is no way to come back to deleted element position.
This problem may be solved by using array compaction on every item deletion or by
viewing queue as circular not as straight line.
In array compaction method, Qdelete function should be changed as given below.
In this case front is always 0.
Char Qdelete( ){ If(q.rear>=0){ X=q.items[0];
For(i=0; i< q.rear; i++)
q.items[i]=q.items[i+1];
q.rear--;
}}
Circular Queues
When an array is used to have a circular queue : Front is one position counterclockwise
from the first element and rear is current end
[3] [4]
C D
[2] [5]
B E
A F
[6]
[1] G
[0] [7]
Front=0, Rear=7
Circular Queue
[3] [4]
C D
[2] [5]
B E
A F
[6]
[1] G
[0] [7]
Front=0, Rear=7
A B c A A A A A
Algorithm for Circular Queue using Dynamically allocated Array
1. Create a new array newQueue of twice the capacity.
2. If front==0
● Copy queue[front+1] through queue[capacity-1] to positions in
newQueue beginning at 0 and go to step 5.
3. Copy the second segment (ie, queue[front+1] through
queue[capacity-1])to positions in newQueue beginning at 0.
4. Copy the first segment (ie, elements queue[0] through queue[rear] to
positions in newQueue beginning at capacity-front-1.
5. Update rear=capacity -2,front=2*capacity-1, capacity=2*capacity
• Void addcq(char item)
{
rear=(rear+1)% capacity;
If (front==rear) qfull( );
Cq[rear]=item;
}
Note: The function Copy(a, b, c) copies elements from locations a through b-1 to
locations beginning at c
Void qfull( ){ // queue is the current circular queue char *new Queue;
new Queue= (char*)malloc(2*capacity*sizeof(*queue));
int start =(front+1)%capacity; If(start<2)
Copy(queue+start, queue+start+capacity-1, new Queue);
Else {
Copy(queue+start, queue+capacity, new Queue);
Copy(queue, queue+start+rear+1, new Queue+capacity-start);
}
Front=2*capacity-1; Rear=capacity-2;
Capacity =2* capacity;
Free(queue); Queue= newQueue;
}
Multiple stacks and queues
Two stacks
bottommost bottommost
stack 1 stack 2
element delete(int i)
{ /* remove top element from the ith stack */
if (top[i] == boundary[i])
return stack_empty(i);
return memory[top[i]--];
}
Find j, stack_no < j < n such that top[j] < boundary[j+1]
or, 0 j < stack_no
b[0] t[0] b[1] t[1] b[i] t[i] t[i+1] t[j] b[j+1] b[n]
b[i+1] b[i+2]
meet
b=boundary, t=top
Linked List
Definition
Linked list is a collection of one or more
nodes, where each node is a collection of one
or more data field and link fields
Representing chains in C
• Structure definition to define a node to hold
integer data is
typedef struct listNode *listPointer;
typedef struct
{
int data;
listPointer link;
}listNode;
Representing chains in C
• Structure definition to define a node to hold
decimal data is
typedef struct listNode *listPointer;
typedef struct
{ int data1;
float data2;
listPointer link;
}listNode;
Representing chains in C
• After defining the node’s structure, we create a
new empty list. This is accomplished by the
statement:
listPointer first=NULL;
Types of Linked list
• Singly Linked List (SLL)
• Doubly Linked List(DLL)
• Circular Linked list(CLL)
– Circular Singly Linked List(CSLL)
– Circular Doubly Linked List(CDLL)
Singly Linked List
• Definition:
Singly linked list is a linear list of data with
exactly one link field and one or more data
field in the node.
Example:
50 100 80 102 NULL
first
Linked list
• List can be generated in two directions
– The Front end
– The Rear end
Linked list : Basic operations
• The different basic operations can be performed on
Linked list are:
}
Algorithm: SLL: C function to
Steps to follow in insert a node at the
insert front
front end
1. Allocate memory to
the node temp listPointer insert_front(int item, listPointer
2. Load the fields with first)
suitable data {
listPointer temp;
3. Check list emptiness, if temp=(listPointer)malloc(sizeof(listPointer)
list is empty make the );
temp node as first temp->info=item;
4. If list is not empty, link temp->link=NULL;
if(first!=NULL)
the temp node to first
temp->link=first;
node of the existing
return temp;
node
}
Algorithm: SLL: C function to display linked list
display(listPointer first)
Steps to follow {
listPointer temp;
1. Check for list if(first==NULL)
emptiness, if list is {
empty print printf(“List is Empty\n”);
suitable message return;
}
2. If list is not empty, else
print the data from {
first node to last temp=first;
node printf(“The Linked List contents
are\n”);
while(temp!=NULL)
{
printf(“%d\t”,temp->data);
temp=temp->link;
}
SLL: C function to insert a node at the rear
Algorithm: end
Steps to follow
listPointer insert_rear(int item,listPointer first)
{
listPointer temp;
1. Allocate memory to
temp=(listPointer)malloc(sizeof(listPointer)
the node temp );
2. Load the fields with temp->data=item;
suitable data of temp temp->link=NULL;
3. Check list emptiness, if(first==NULL)
if list is empty make return temp;
else
the temp node as first
{
4. If list is not empty, listPointer cur=first;
search for the list’s if(cur!=NULL)
last node, once {
found: attach the cur=cur->link;
‘temp’ node to it }
cur->link=temp;
return first;
}
SLL: C function to delete a node at the
front end
Algorithm: listPointer delete_front(listPointer first)
Steps to follow {
listPointer temp;
1. Check for list
emptiness, if list is if(first==NULL)
empty print suitable {
message printf(“List is Empty\n”);
return first;
}
1. If list is not empty, temp=first;
print the deleted data temp=temp->link;
to the user in the printf(“Deleted data is \n %d \n”,first->data);
front node and make
free(first);
the second node as
first node return temp;
}
SLL: C function to delete a node at the rear end
listPointer delete_rear(listPointer first)
{ if(first==NULL)
Algorithm: { printf(“List is empty: Can not delete\n”);
Steps to follow return first;
}
1. List is empty print the
if(first->link==NULL)
error message
{ printf(“Data deleted %d\n”,first->data);
2. If the list contains only free(first);
one delete it and return return NULL;
NULL }
3. If list contains more listPointer prev=NULL,cur=first;
than one node: while(cur->link!=NULL)
{ prev=cur;
– Find the last node
cur=cur->link;
– Assign the ‘link’ field of
the previous node to }
NULL printf(“Date deleted is %d\n”,cur->data);
– Delete the last prev->link=NULL;
free(cur); cur=NULL;
}\*end of function*\
Linked List with Header Node
• What is a Header Node?
Header node in a Linked List is a special node
which contains the address of the first node
with no data.
• Ex: Head NULL
Empty list
first A B C D
Circular Singly Linked List with Header
• Definition:
Node
Circular Singly Linked List is a linear
homogeneous list of zero or more nodes with a special
node called the “head”; where the last node is
followed by the head node and each node consisting
of exactly one link field
• Example:
head 3 B C D
Circular Doubly Linked List with Header
Node
• Definition:
Circular Linked List is a linear homogeneous list
of zero or more nodes with a special node called the
“head”; where the last node is followed by the head
node and each node consisiting of exactly two link
fields
• Example:
head 1 2 3
Basic operations
• Inserting a node at the front end
• Inserting a node at the rear end
● polynomial A,B;
• advantage: easy implementation
• disadvantage: waste space when sparse
Storage: Using Array of Structures
• To overcome the disadvantage of the previous storage,
we can use one global array to store all the polynomials
• Structure definition #define
MAX_DEGREE 101 typedef
struct
{
int degree;
float coef;
} polynomial;
polynomial P [MAX_DEGREE];
Data structure 2: use one global array to store all
polynomials Array representation of two polynomials
finisha startb finishb avail
A(X)=2X1000+1
B(X)=X4+10X3+3X2+1
starta
coef
exp
0 1 2 3 4 5 6
specification representation
poly <start, finish>
A <0,1>
B <2,5>
● End of Module-2