Vivekananda
College of Engineering & Technology
Nehru nagar post, Puttur, D.K. 5742013
Module-2 :
QUEUE AND LINKED LIST
Contents
2.1 QUEUES
Definition:
“A queue is an ordered list in which insertion is done at rear end and deletion is done at 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,
queue is also known as First-In-First-Out (FIFO) lists.
Applications of Queues:
• Vehicles waiting at a toll gate
• Customer care
• Serving requests on a single shared resource like a printer
• CPU task scheduling
2.2 QUEUE REPRESENTATION USING ARRAY
• Queues may be represented by one-way lists or linear arrays.
• Queues will be maintained by a linear array QUEUE and two pointer variables:
– FRONT -containing the location of the front element of the queue
– REAR -containing the location of the rear element of the queue.
• The condition
FRONT = NULL
or
FRONT=-1 || FRONT >REAR will indicate that the queue is empty.
• Initialization : FRONT =-1, REAR= -1
• On inserting 1st Value to Queue , both REAR & FRONT are incremented by 1. Now
both FRONT & REAR pointer has value 0. ie, Queue has only one element when both
FRONT & REAR has same pointer value.
• 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
Page 2
• 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
Implementation of Queue:
#include <stdio.h> #include<stdlib.h> int
queue[5];
int front=-1; int rear=-1; int size,i;
void enqueue()
{
int num; if(rear==size-1)
{
printf("overflow\n");
}
else
{
if(front==-1)
{
front=0;
}
printf("enter the item to be inserted\n"); scanf("%d",&num);
rear++; queue[rear]=num;
}
}
void dequeue()
{
if(front==-1||front>rear)
{
printf("Underflow\n");
}
else if(front==rear)
{
printf("deleted item =%d\n",queue[front]); front=rear=-1;
}
else
{
printf("deleted item is %d\n",queue[front]);
Page 3
front++;
}
}
void display()
{ if(front==-1||front>rear)
{
printf("queue is empty\n");
}
else
{
printf("The queue elements are\n");
for(i=front;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
}
}
int main()
{
int choice;
printf("enter the size of queue\n");
scanf("%d",&size);
printf("\n\n--------MENU----------- \n");
printf("1.DISPLAY\n");
printf("2.INSERT\n");
printf("3.DELETE\n");
printf("4.EXIT\n");
printf(" ----------------------- ");
while(1)
{
printf("\nENTER YOUR CHOICE:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: display();
break;
case 2:
enqueue();
break;
case 3: dequeue();
break;
case 4: exit(0);
default: printf("\nInvalid choice:\n");
}
}
}
Page 4
2.3 Circular queue
• In case of queue represented as an array, once the value of the rear reaches the
maximum size of the queue, no more elements can be inserted.
• However, there may be the possibility that space on the left of the front index is
vacant. Hence, in spite of space on the left of front being empty, the queue is
considered to be full.
• This wastage of space in the array implementation of a queue can be avoided by
shifting the elements to the beginning of array if the space is available.
• In order to do this, the values of Rear and Front indices have to be changed
accordingly.
• 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.
Page 5
Implementation of Circular Queue
#include <stdio.h>
#include<stdlib.h>
int queue[5];
int front=-1;
int rear=-1;
int size,i;
void enqueue()
{
int num;
if((rear+1)%size==front)
{
printf("overflow\n");
}
else
{
if(front==-1)
{
front=0;
}
printf("enter the item to be inserted\n");
scanf("%d",&num);
rear=(rear+1)%size;
queue[rear]=num;
}
}
void dequeue()
{
if(front==-1)
{
printf("Underflow\n");
}
else if(front==rear)
{
printf("deleted item =%d\n",queue[front]);
front=rear=-1;
}
else
{
printf("deleted item is %d\n",queue[front]);
front=(front+1)%size;
}
}
void display()
{
int i;
if(front==-1)
{
printf("\n Queue is empty..");
Page 6
}
else
{
printf("\nElements in a Queue are :");
for(i=front;i!=rear;i=(i+1)%size)
{
printf("%d,", queue[i]);
}
printf("%d,", queue[rear]);
}
}
int main()
{
int choice;
printf("enter the size of queue\n"); scanf("%d",&size);
printf("\n\n--------MENU ----------- \n");
printf("1.DISPLAY\n"); printf("2.INSERT\n");
printf("3.DELETE\n"); printf("4.EXIT\n");
printf(" ---------------------- ");
while(1)
{
printf("\nENTER YOUR CHOICE:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: display(); break;
case 2:
enqueue(); break;
case 3: dequeue(); break;
case 4: exit(0);
default: printf("\nInvalid choice:\n");
}
}
}
Page 7
2.4 CIRCULAR QUEUES USING DYNAMIC ARRAYS
• A dynamically allocated array is used to hold the queue elements.
• Let capacity be the number of positions in the array queue.
• To add an element to a full queue, first increase the size of this array using a function
realloc.
• As with dynamically allocated stacks, array doubling is used.
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 obtain the configuration as shown in figure (e), follow the steps
1) Create a new array newQueue of twice the capacity.
2) Copy the second segment (i.e., the elements queue [front +1] through queue [capacity-
1]) to positions in newQueue beginning at 0.
3) Copy the first segment (i.e., the elements queue [0] through queue [rear]) to positions
in newQueue beginning at capacity – front – 1.
Page 8
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.
Page 9
2.5 MULTIPLE STACKS AND QUEUES
Page 10
Page 11
2.6 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.
Page 12
Declaration of Linked List
struct node
{
int data;
struct node *next;
};
Summary:
• It is a linear Data structure, collection of data elements which are stored in known
consecutive locations.
• Some extra space would be required to store pointers.
• Insertion and deletion is easy.
• Random Access takes time
• Binary search is not possible
• It is of dynamic size
2.7 Types of linked list
1. Singly linked list
2. Doubly linked list
3. Circular singly linked list
4. Circular doubly linked list
1. Singly linked list:
• Has only single link to next node
• Only forward traversal is possible
Page 13
2. Doubly linked list
• Has only double link to next and previous node
• Only forward and backward traversal is possible
3. Circular Singly linked list
• It is a linked list in which each node is having link to its next node and last node is
connected back to its first node.
4. Doubly circular linked list
• It is a linked list in which each node is having link to its next and previous node node
and last node is connected to its first node and first node is connected to last node.
Page 14
2.8 Representation of Linked Lists in Memory
LIST will be maintained in memory as follows.
1. LIST requires two linear arrays such as INFO and LINK- such that INFO[K] and
LINK[K] contains the information part and the next-pointer field of a node of LIST.
2. LIST also requires a variable name such as START which contains the location of
the beginning of the list, and a nextpointer sentinel denoted by NULL-which
indicates the end of the list.
3. The subscripts of the arrays INFO and LINK will be positive, so choose NULL = 0,
unless otherwise stated.
2.9 Representing Linked List in C
Singly Linked List Implementation: Creating List code
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void create()
{
int choice;
//create linked list
struct node *head=NULL, *newnode,*temp;
while(choice)
{
newnode=(struct node*) malloc(sizeof(struct node));
Page 15
printf("Enter data\n");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
{
temp=head=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
printf("Do you want to add one more node(0/1)\n");
scanf("%d",&choice);
}
Traversing the singly Linked list
void display()
{
temp=head;
printf("The elements of linked list are\n ");
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}
The following code prints the number of elements in linked list:
void count()
{
temp=head;
int count=0;
while(temp!=NULL)
{
temp=temp->next;
count++;
}
printf("\nThe total elements of linked list are %d\n",count);
}
2.10 Insertion Algorithms
Algorithms which insert nodes into linked lists come up in various situations.
1. Inserts a node at the beginning of the list
Page 16
2. Inserts a node at the end of the list
3. Inserts a node after the node with a given location
4. Inserts a node into a sorted list
1. Inserts a node at the beginning of the list
struct node
{
int data;
struct node *next;
};
void insertbeg()
{
struct node *newnode;
newnode=(struct node*) malloc(sizeof(struct node));
printf("Enter data\n");
scanf("%d",&newnode->data);
newnode->next=head;
head=newnode;
}
2. Inserts a node at the end of the list
struct node
{
int data;
struct node *next;
};
void insertend()
{
struct node *newnode;
newnode=(struct node*) malloc(sizeof(struct node));
printf("Enter data\n");
scanf("%d",&newnode->data);
newnode->next=NULL;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
Page 17
3. Inserts a node after the node with a given location.
void insertatpos()
{
int pos,i=1;
printf(“enter the position\n”);
scanf(“%d”,&pos); if(pos>count)
{
printf(“Invalid”);
}
else
{
temp=head;
while(i<pos)
{
temp=temp->next;
i++;}
struct node *newnode;
newnode=(struct node*) malloc(sizeof(struct node));
printf("Enter data\n");
scanf("%d",&newnode->data);
newnode->next=temp->next;
temp->next=newnode;
}
4. Inserting into a Sorted Linked List
void inserttosorted()
{
//struct node *newnode,*head,*temp;
newnode=(struct node*) malloc(sizeof(struct node));
printf("Enter data\n");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head->next==NULL && head->data>newnode->data)
{
newnode->next=head;
head=newnode;
}
else{
temp = head;
while (temp!= NULL&& newnode->data>temp->data) {
prevnode=temp;
temp = temp->next;
Page 18
}
prevnode->next=newnode;
newnode->next = temp;
2.11 Deletion into a Linked list
Deletion Algorithms:
Deletion of nodes from linked lists come up in various situations.
1. Delete a node from the beginning
2. Delete a node from the end of list
3. Deletes the node following a given node/ at the specified position.
2. Deletes the node with a given ITEM of information.
All deletion algorithms will return the memory space of the deleted node N to the beginning
of the list.
1. Delete a node from the beginning
void deleteatbeg()
{
if(head==NULL)
{
printf("List is empty\n");
return;
}
temp=head;
printf("deleted node is %d\n",temp->data);
head=head->next;
free(temp);
2. Delete a node from the end of list
void deleteatend()
{
if(head==NULL)
{
printf("List is empty\n");
return;
}
if(head->next==NULL) //Single node
{
Page 19
printf("deleted node is %d\n",head->data);
head=NULL;
free(head);
return;
}
temp=head;
while(temp->next!=NULL)
{
prevnode=temp;
temp=temp-
>next;
}
prevnode->next=NULL;
printf("deleted node is %d\n",temp->data);
free(temp);
}
3. Deletes the node at the specified position
void deleteatpos()
{
int pos,i=1;
if(head==NULL)
{
printf("List is empty\n");
return;
}
printf("enter the position\n");
scanf("%d",&pos);
if(head->next==NULL&&pos==1) //Single node
{
printf("deleted node is %d\n",head->data);
head=NULL;
free(head);
return;
}
else{
temp=head;
if(pos==1)
{
head=temp->next;
printf("deleted node is %d\n",temp->data);
free(temp);
}
else
{
while(i<pos)
Page 20
{
prevnode=te
mp;
temp=temp-
>next; i++;
}
prevnode->next=temp->next;
printf("deleted node is %d\n",temp-
>data); free(temp);
}
}
}
4. Deleting the Node Following a Given Node
void deleteafternode()
{
int data;
if(head==NULL)
{
printf("List is empty\n");
return;
}
printf("enter the data of a node after which the node has be deleted\n");
scanf("%d",&data);
prevnode=head;
while(prevnode!=NULL)
{
if(prevnode->data==data)
{
temp=prevnode->next;
printf("deleted node is %d\n",temp->data);
prevnode->next=temp->next;
free(temp);
break;
}
else{
prevnode=prevnode->next;
}
}
}
5. deleting the Node with given item of information
Page 21
void deletewithdata()
{
int data;
if(head==NULL)
{
printf("List is empty\n");
return;
}
printf("enter the data to be deleted\n");
scanf("%d",&data);
if(head->next==NULL&&head->data==data) //Single node
{
printf("deleted node is %d\n",head->data);
head=NULL;
free(head);
return;
}
temp=head;
if(temp->data==data)
{
printf("deleted node is %d\n",temp->data);
head=temp->next;
free(temp);
return;
}
else{
while(temp!=NULL)
{
if(temp->data==data)
{
printf("deleted node is %d\n",temp->data);
break;
}
else{
prevnode=temp;
temp=temp->next;
}
}
prevnode->next=temp->next;
free(temp);
}
}
2.12 Searching a Linked list
Page 22
1. Search in an unsorted list
void searchinunsortedlist()
{
int data;
printf("Enter data to be searched\n");
scanf("%d",&data);
temp = head;
while (temp != NULL)
{
if(temp->data==data)
{
printf("search is successful\n");
exit(0);
}
else
{
temp = temp->next;
}
}
printf("search is UNsuccessful\n");
}
2. Search in a sorted list
void searchinsorted()
{
int data;
printf("Enter data to be searched\n");
scanf("%d",&data);
temp = head;
while (temp!= NULL||data>temp->data)
{
if(temp->data==data)
{
printf("search is successful\n");
exit(0);
}
else
{
temp=temp->next;
}
Page 23
}
printf("search is UNsuccessful\n");
2.13 Concatenation of two SLL
struct node{
int data;
struct node *link;
};
void dataconca()
{
struct node *head1,*head2,*temp1,*temp2,*newnode1,*newnode2,*j;
int n,i;
printf ("number of elements in a:");
scanf("%d",&n);
head1=NULL;
for(i=0;i<n;i++)
{
newnode1=malloc(sizeof(struct node));
printf("enter the data\n");
scanf("%d",&newnode1->data);
newnode1->link=0;
if(head1==0)
{
head1=temp1=newnode1;
}
else
{
temp1->link=newnode1;
temp1=newnode1;
}
}
printf ("number of elements in b:");
scanf("%d",&n);
head2=NULL;
for(i=0;i<n;i++)
{
newnode2=malloc(sizeof(struct node));
printf("enter the data\n");
scanf("%d",&newnode2->data);
newnode2->link=0;
if(head2==0)
{
head2=temp2=newnode2;
}
Page 24
else
{
temp2->link=newnode2; temp2=newnode2;
}
}
j = head1;
while(j->link != NULL){ j = j->link;
}
j->link = head2; head2 = NULL;
printf("Concatenated list is\n"); j=head1;
while(j!=NULL)
{
printf("%d \t",j->data); j=j->link;
}
}
2.14 Reverse a given list
void datarev()
{
prevnode=0; currentnode=nextnode=head;
while(nextnode!=NULL)
{
nextnode=nextnode->next; currentnode-
>next=prevnode; prevnode=currentnode;
currentnode=nextnode;
}
head=prevnode;
}
Page 25
2.15 LINKED STACK AND QUEUES
i. Linked Stack
struct node *top=NULL;
void push(int x)
{
struct node * newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->link=top;
top=newnode;
}
void pop()
{
struct node *temp;
temp=top;
if(temp==NULL)
{
printf("Underflow\n");
}
else
{
printf("deleted item is=%d\n",top->data);
top=top->link;
free(temp);
}
}
void display()
{
struct node *temp;
temp=top;
if(temp==NULL)
{
printf("Stack is empty\n");
}
else
{
while(temp!=NULL)
{
printf("%d \t",temp->data);
temp=temp->link;
}
}
}
ii. Linked Queue
Page 26
struct node
{
int data;
struct node *next;
};
struct node *front=NULL,*rear=NULL;
void enque(int n)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=n;
newnode->next=NULL;
if(front==NULL && rear==NULL)
{
front=rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}
void deque()
{
struct node *temp;
temp=front;
if(front==NULL)
{
printf("Underflow\n");
}
else
{
printf("deleted node is:\n");
printf(“%d\n",temp->data);
printf(" \n");
front=front->next;
free(temp);
}
void display()
{
struct node *temp;
if(front==NULL && rear==NULL)
{
printf("Queue is empty\n");
}
else
{
temp=front;
while(temp!=NULL)
Page 27
{
printf(“%d\n",temp->data);
temp=temp->next;
}
}
}
2.16 Polynomials
Page 28
Page 29
Page 30
Page 31
Page 32
Page 33
Page 34
Page 35
Page 36
Page 37
Page 38
QUESTIONS:
1) Give the node structure to create singly linked list of integers and write functions to
perform the following :
(i) Create a list.
(ii) Assume the list contains 3 nodes with data 10,20,30. Insert a node with data
40 at the end of the list.
(iii) Insert a node with data 50 between the nodes having data values 10 and 20.
(iv) Display the singly linked list.(Appeared in Dec.2016/Jan.2017)
2) Give the node structure to create a linked list of integers and write C functions to perform
the following
i) Create a three nodes list with data 10, 20 and 30.
ii) Insert a node with the data value 15 in between the nodes
having the data values 10 and 20.
iii) Delete the node whose data is 20
iv) Display the resulting singly linked list.( Appeared in June/July.2017)- 8 marks
1) Write the following functions for singly linked list:
2) (1) Reverse the list (ii) Concatenate two lists ( Appeared in Dec.2017/Jan.2018)-
8 marks
3) Write C functions to perform the following:
i) Reversing a singly linked list.
ii) Concatenating singly linked list.
iii) Finding the length of the list. ( Appeared in June/July 2017)- 6 marks
Page 39
3) Given 2 singly linked lists. LIST-l and LIST-2. Write an algorithm to form a new list
LIST-3 Using concatenation of the lists LIST-I and LIST-2.( Appeared in June/ July.2018)-
8 marks
Page 40
Module1: KMP Algorithm
Page 41