Module 2
Module 2
Module 2
Syllabus
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
Textbook: 1. Ellis Horowitz, Sartaj Sahni and Susan Anderson-Freed, Fundamentals of Data
Structures in C, 2nd Ed, Universities Press, 2014
Reference Books:
1. Seymour Lipschutz, Data Structures Schaum's Outlines, Revised 1 st Ed, McGraw Hill, 2014.
2. Gilberg & Forouzan, Data Structures: A Pseudo-code approach with C, 2 nd Ed, Cengage
Learning,2014.
6. Robert Kruse, Data Structures and Program Design in C, 2 nd Ed, PHI, 1996.
Queues
Queue: A Queue is an ordered list in which insertions (push) and deletions (pop) are made
at different ends. The elements are added at one end is called rear end. The elements are
deleted or removed from another end is called front end.
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 known as Fisrt-In-First-Out(FIFO) lists.
QUEUE OPERATIONS:
The following are the advantages of using a linked list over an array:
Dynamic data structure: The size of the linked list is not fixed as it can vary according
to our requirements. Array is fixed in size we cannot increase or decrease the size of the
array.
Insertion and Deletion: Insertion and deletion in linked list are easier than array as the
elements in an array are stored in a consecutive location. In contrast, in the case of a linked
list, the elements are stored in a random location. Insertion and deletion of element in to
linear array requires more shifting of elements. Shifting of elements become expensive.
Data is organized alphabetically in linear array as shown above. Suppose an element GAT
has to be inserted into above mentioned linear array we need to shift position number 4 to
13 elements like insertion deletion of an element LAT in to linear array needs more
number shiftings from 13th location elements to 7th location elements towards 6th location.
In the above figure, we can observe that each node contains the data and the address of the next
node. The last node of the linked list contains the NULL value in the address part.
REPRESENTING CHAIN IN C
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.
3. The FREE function handles this operation.
Defining a node structure
typedef struct listNode *listPointer
typedef struct {
char data[4];
listPointer list;
} listNode;
Create a New Empty list
listPointer first = NULL
Linked Stack
The representation of n ≤ MAX_STACKS stacks, below is the declarations:
#define MAX_STACKS 10
/* maximum number of stacks */
typedef struct {
int key; /* other fields */
}element;
typedef struct stack *stackPointer;
typedef struct {
element data;
stackPointer link;
} stack;
stackPointer top[MAX_STACKS];
The initial condition for the stacks is: top[i] = NULL, 0 ≤ i < MAX_STACKS
The boundary condition is: top [i] = NULL iff the ith stack is empty
Functions push and pop add and delete items to/from a 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;
}
Function pop returns the top element and changes top to point to the address contained in
its link field. The removed node is then returned to system memory. A typical function call
to delete an element from the ith stack would be item = pop (i);
Linked Queue
The representation of m ≤ MAX_QUEUES queues, below is the declarations:
#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];
The initial condition for the queues is: front[i] = NULL, 0 ≤ i < MAX_QUEUES
The boundary condition is: front[i] = NULL iff the ith queue is empty
Functions addq() and delete() implement the add and delete operations for multiple queues.
element deleteq(int i)
{
/* delete an element from queue i */
queuePointer temp = front[i];
element item;
if (! temp)
return queueEmpty();
item = temp→data;
front[i]= temp→link;
free (temp) ;
return item;
}
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);
Polynomials
Polynomial Representation: The structure declaration of Polynomial to represent in Singly
Linked List is as follows:
typedef struct polyNode *polypointer;
typedef struct{
int coef;
int expon;
polyPointer link;
}polyNode;
polyPointer a,b;
a = 3x14 + 2x8 + 1
b = 8x14 - 3x18 + 10x6
Representation of polynomial
a = 3x14 + 2x8 + 1
Representation of polynomial
b = 8x14 - 3x18 + 10x6
In a circular Singly linked list, the last node of the list contains a pointer to the first node of the
list. We can have circular singly linked list as well as circular doubly linked list.
We traverse a circular singly linked list until we reach the same node where we started. The
circular singly liked list has no beginning and no ending. There is no null value present in
the next part of any of the nodes.
The following image shows a circular singly linked list.
char data;
listPointer link;
}listNode;
listPointer temp;
if(!ptr1) return ptr2;
if(!ptr2) return ptr1;
for( temp=ptr1;temp->link;temp=temp->link);
temp->link=ptr2;
}
Write a c function to Finding the length of the circular list
int length(listPointer last){
listPointer temp;
int count=0;
if(last){
temp=last;
do{
count++;
temp=temp->link;
}
while(temp!=last);
return count;
}
}
node ->link=node;
}else{