Dsa Module 2 Notes
Dsa Module 2 Notes
MODULE-2
QUEUE AND LINKED LIST
{
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
default: printf("Incorrect choice \n");
}
}
}
void insert()
{
int item;
if (rear = = SIZE - 1)
printf("Overflow \n");
else
{
printf("Element to be inserted in the Queue\n : ");
scanf("%d", &item);
rear = rear + 1;
queue[rear] = item;
}
}
void delete()
{
if (front > rear)
{
printf("Underflow \n");
return ;
}
else
{
printf("Element deleted from the Queue: %d\n", queue[front]);
front = front + 1;
}
}
void display()
{
if (front>rear)
printf("Empty Queue \n");
else
{
printf("Queue: \n");
for (int i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}
---------------------------------------------------------------------------------------------------------------------
3. Write a c Function in C to insert (), delete() and display into the ordinary/Linear Queue
Answer:
// Function to insert into the Queue
void insert()
{
int item;
if (rear = = SIZE - 1)
printf("Overflow \n");
else
{
printf("Element to be inserted in the Queue\n : ");
scanf("%d", &item);
rear = rear + 1;
queue[rear] = item;
}
}
// Function to delete from the Queue
void delete()
{
if (front > rear)
{
printf("Underflow \n");
return ;
}
else
{
if (front>rear)
printf("Empty Queue \n");
else
{
printf("Queue: \n");
for (int i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}
---------------------------------------------------------------------------------------------------------------------
4. Write C routines to implement the operations of the stack?
Answer:
// global declaration of variables
#include <stdio.h>
# define size 10
void push(int item);
void pop();
void display();
int stack[size];
int top = -1, item, item_popped;
---------------------------------------------------------------------------------------------------------------------
5. Write a menu driven program in C to implement the operations of stack
Answer:
#include <stdio.h>
# define size 10
void push(int item);
void pop();
void display();
int stack[size];
int top =-1,item,item_popped;
void main()
{
int ch;
while (1)
{
printf("1.Insert Element into stack\n");
printf("2.Delete Element from stack\n");
printf("3.Display the COntent of stack\n");
printf("4.Exit\n");
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch)
{
case 1:printf("Enter the item to be pushed:");
scanf("%d",&item);
push(item);
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
default: printf("Incorrect choice \n");
}
}
}
void pop()
{
if (top == -1)
{
printf ("Stack is under flow \n");
return ;
}
else
{
item_popped = stack[top--];
printf ("item deleted is =%d \n", item_popped);
}
}
void display()
{
int i;
if (top == -1)
{
printf ("Stack is under flow \n");
return ;
}
for( i=top; i>=0;i--)
printf ("item =%d\n", stack[i]);
}
---------------------------------------------------------------------------------------------------------------------
6. What are the disadvantages of ordinary Queue? Discuss the implementation of circular
Queue?
Answer: Disadvantages of Ordinary Queue
1. Fixed Size: An ordinary queue, typically implemented using arrays, has a fixed size.
Once it reaches its capacity, it cannot accept more elements until some are
dequeued/deleted, leading to potential underutilization of space.
2. Wasted Space: When elements are dequeued/deleted from the front, the space occupied
by those elements remains unused unless the queue is entirely cleared or a resizing
mechanism is implemented.
3. Inefficient Use of Space: In an array-based queue, after several enqueue/insert and
dequeue/delete operations, the front of the queue may shift, leading to a situation where
there is unused space at the start of the array, even though there might be space at the
end.
4. Complexity in Implementation: Managing the indices for front and rear pointers in a
basic array-based queue can lead to complexity, especially when wrapping around the
end of the array.
A circular queue overcomes the limitations of a standard queue by reusing the empty spaces
created by dequeue operations. Here’s how it works and a simple implementation:
Key Concepts
Front and Rear Pointers: In a circular queue, we maintain two pointers: front
(indicating the position of the first element) and rear (indicating the position of the last
element).
Wrap Around: When the rear pointer reaches the end of the array, it wraps around to the
beginning if there is space.
---------------------------------------------------------------------------------------------------------------------
7. Define Circular Queue? Mention the operations that can be performed on circular
queue?
Answer: A circular queue is a data structure that implements a queue in a circular manner, in
which the first element is appended to the last element.
The operations that can be performed on circular Queue are:
insertion, deletion, display, search etc.
---------------------------------------------------------------------------------------------------------------------
8. Write a C program to perform insertion, deletion and display circular queue using
arrays?
Answer:
#include <stdio.h>
# define SIZE 5
void insert();
void delete();
void display();
int queue[SIZE];
int rear = SIZE-1;
int front = 0,count=0, item;
void main( )
{
int ch;
while (1)
{
printf("1.Insert Operation\n");
printf("2.Delete Operation\n");
void delete()
{
if (count== 0)
{
printf("Circular Queue is Underflow \n");
return ;
}
else
{
printf("Element deleted from the Queue: %d\n", queue[front]);
front = (front + 1) % SIZE;
count--;
}
}
void display()
{
int f,i;
if (count== 0)
{
printf("Circular Queue is Underflow \n");
return ;
}
else
{
printf("Contents of Circular Queue are: \n");
for (f=front,i=1;i<=count;i++)
printf("%d ", queue[front]);
front = (front + 1) % SIZE;
printf("\n");
}
}
---------------------------------------------------------------------------------------------------------------------
9. Define Linked list and explain with neat diagram different types of linked list?
Answer: A linked list is a data structure which is collection of zero or more nodes where each
node is connected to one or more nodes. If each node in the list has only one link, it is called
singly linked list.
The different types of linked list are:
Singly Linked List
Doubly Linked List
Circular Singly Linked List
Circular Doubly Linked List
Singly Linked List: A singly linked list is a collection of zero or more nodes where each node
has two or more fields but only one link field which contains address of the next node. Each
node in the list can be accessed using the link field which contains address of the next node
Example: Consider the Singly Linked list consisting of items 50, 20,45,10,80
Circular Singly Linked List: A circular singly linked list is a singly linked list where the link
field of last node of the list contains address of the first node. The pictorial representation of a
circular list is shown below:
Doubly-linked list: A doubly-linked list is a linear collection of nodes where each node is
divided into three parts:
info – This is a field where the information has to be stored
llink – This is a pointer field which contains address of the left node or previous node in
the list
rlink – This is a pointer field which contains address of the right node or next node in the
list
The pictorial representation of a doubly linked list is shown in figure below:
Circular Doubly linked list: A circular doubly linked list is a variation of doubly linked list in
which every node in the list has three fields:
info – This is a field where the information has to be stored
llink – This is a pointer field which contains address of the left node or previous node in
the list
rlink – This is a pointer field which contains address of the right node or next node in the
list and the
llink of the first node contains address of the last node whereas
rlink of the last node contains address of the first node.
The pictorial representation of the circular doubly linked list is shown below
10. Write a C routine to implement the following operation on Singly Linked List
i. insert_front( ) ii. delete_front( )
Answer:
Function to insert node at front end of Singly Linked List
NODE insert_front ( int item, NODE first)
{
NODE temp;
temp= getnode()
temp-> info = item;
if( first == NULL)
first = temp;
else
temp –> link = first;
return temp;
}
Answer:
Function to insert node at rear end of Singly Linked List
NODE insert_rear ( int item, NODE first)
{
NODE cur,temp;
temp= getnode()
temp-> info = item;
temp->link= NULL;
if ( first == NULL )
return temp;
cur = first;
while ( cur->link != NULL)
{
cur = cur->link;
}
cur->link = temp;
return first;
}
prev->link = NULL;
printf(" The item deleted =%d ", cur->info);
free(cur);
return first;
}
---------------------------------------------------------------------------------------------------------------------
13. Write the function to add and delete node at front end of Circular linked list ?
Answer:
// Function to add node at front end of the circular linked list
NODE insert_front(int item, NODE last)
{
NODE temp;
temp = getnode(); /* Create a new node to be inserted */
temp->info = item;
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
struct node
{
int info;
break;
case 3: display(first);
break;
default: exit(0);
}
}
}
---------------------------------------------------------------------------------------------------------------------
15. Write the C program to implement Queue using linked list?
Answer:
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
struct node
{
int info;
struct node *link;
};
typedef struct node* NODE;
// function to create a new node
NODE getnode()
{
NODE x;
x = ( NODE ) malloc(sizeof(struct node));
if ( x == NULL )
{
printf("Out of memory\n");
exit(0);
}
return x;
}
return temp;
cur = first;
while ( cur->link != NULL)
{
cur = cur->link;
}
cur->link = temp;
return first;
}
printf("%d ",cur->info);
cur = cur->link;
}
printf("\n");
}
linked representation of the below two polynomials and their addition using a circular singly
linked list is:
----------------------------------------------GOOD LUCK---------------------------------------------------