0% found this document useful (0 votes)
28 views

Dsa Module 2 Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Dsa Module 2 Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

BCS304-DSA MODULE-2 NOTES

YENEPOYA INSTITUTE OF TECHNOLOGY


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

BCS304 –DATA STRUCTURE NOTES

MODULE-2
QUEUE AND LINKED LIST

1. Define ordinary Queue or Linear Queue?


Answer: Ordinary Queue/ Linear Queue is a linear data structure in which the elements are
inserted at rare end and deleted from front end. Queue follows the pattern last in last out.
Therefore queue is aslo called as LILO or FIFO.
Consider a queue with five elements, Queue size is 5
Queue 10 20 30 50 100
---------------------------------------------------------------------------------------------------------------------
2. Write a program in c to perform insertion, deletion and display operation on ordinary
Queue/ Linear Queue?
Answer:
#include <stdio.h>
# define SIZE 10
void insert();
void delete();
void display();
int queue[SIZE];
int rear = - 1;
int front = 0;
main( )
{
int ch;
while (1)
{
printf("1.Insert Operation\n");
printf("2.Delete Operation\n");
printf("3.Display the Queue\n");
printf("4.Exit\n");
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch)
Dept. of CSE Page 1
BCS304-DSA MODULE-2 NOTES

{
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;
}
}

Dept. of CSE Page 2


BCS304-DSA MODULE-2 NOTES

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
{

Dept. of CSE Page 3


BCS304-DSA MODULE-2 NOTES

printf("Element deleted from the Queue: %d\n", queue[front]);


front = front + 1;
}
}
// Function to display items of the Queue
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");
}
}
---------------------------------------------------------------------------------------------------------------------
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;

// Function to perform push operation


void push(int item)
{
if (top == size-1)
{
printf ("Stack is over flow \n");
return ;
}
else
stack[++top] = item;

Dept. of CSE Page 4


BCS304-DSA MODULE-2 NOTES

//Function to perform pop operation


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);
}
}

//Function to display content of stack


void display()
{
int i;
for( i=top; i>=0;i--)
printf ("item =%d \n", stack[i]);
}

---------------------------------------------------------------------------------------------------------------------
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)

Dept. of CSE Page 5


BCS304-DSA MODULE-2 NOTES

{
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 push(int item)


{
if (top == size-1)
{
printf ("Stack is over flow \n");
return ;
}
else
stack[++top] = item;
}

void pop()
{
if (top == -1)
{
printf ("Stack is under flow \n");
return ;

Dept. of CSE Page 6


BCS304-DSA MODULE-2 NOTES

}
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.

Dept. of CSE Page 7


BCS304-DSA MODULE-2 NOTES

Implementation of Circular Queue

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");

Dept. of CSE Page 8


BCS304-DSA MODULE-2 NOTES

printf("3.Display the Queue\n");


printf("4.Exit\n");
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("\nElement to be inserted in the Queue : ");
scanf("%d", &item);
insert(item);
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
default: printf("Incorrect choice \n");
}
}
}

void insert(int item)


{
if (count == SIZE)
printf("Circular Queue is Overflow \n");
else
{
rear = (rear + 1)% SIZE;
queue[rear] = item;
count++;
}
}

void delete()
{
if (count== 0)
{
printf("Circular Queue is Underflow \n");
return ;
}
else

Dept. of CSE Page 9


BCS304-DSA MODULE-2 NOTES

{
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

Dept. of CSE Page 10


BCS304-DSA MODULE-2 NOTES

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

Dept. of CSE Page 11


BCS304-DSA MODULE-2 NOTES

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;
}

Function to delete node at front end of Singly Linked List


NODE delete_front (NODE first)
{
NODE temp;
if( first == NULL)
{
printf(" Singly Linked List is Empty ");
return;
}
if( first->link == NULL)
{
printf(" The item deleted =%d ", first->info);
free(first);
return NULL;
}
temp=first;
temp=temp->link;
printf(" The item deleted = %d ", first->info);
free(first);
return temp;
}
---------------------------------------------------------------------------------------------------------------------
11. Write a C routine to implement the following operation on Singly Linked List
ii. insert_rear( ) ii. delete_rear( ) iii. display()

Dept. of CSE Page 12


BCS304-DSA MODULE-2 NOTES

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;
}

Function to delete node at rear end of Singly Linked List


NODE delete_rear ( NODE first)
{
NODE cur,prev;
if( first == NULL)
{
printf(" Singly Linked List is Empty ");
return;
}
if( first->link == NULL)
{
printf(" The item deleted =%d ", first->info);
free(first);
return NULL;
}
cur=first;
while( first->link !=NULL)
{
prev=cur;
cur= cur->link;
}

Dept. of CSE Page 13


BCS304-DSA MODULE-2 NOTES

prev->link = NULL;
printf(" The item deleted =%d ", cur->info);
free(cur);
return first;
}

Function to display nodes of Singly Linked List


void display(NODE first)
{
NODE cur;
if( first = = NULL)
{
printf("The singly linked list is empty ");
return;
}
cur = first;
while(cur!=NULL)
{
printf(" \nitem=%d",cur->info);
cur = cur->link;
}
printf(" \n");
}
---------------------------------------------------------------------------------------------------------------------
12. Write a C routine to insert node and delete node at the end of Circular Singly Linked
List
Answer:
Function to insert node at end of circular Singly Linked List
NODE insert_rear (int item, NODE last)
{
NODE temp;
temp=getnode();
temp->info= item;
if( last== NULL)
last = temp;
else
first = last->link;
last->link=temp;
temp->link= first;
return temp;

Dept. of CSE Page 14


BCS304-DSA MODULE-2 NOTES

Function to delete node at end of circular Singly Linked List

NODE delete_rear (NODE last)


{
NODE first, cur,prev;
if( last== NULL)
{
printf(" Circular singly linked list is empty ");
return NULL;
}
if( last->link== NULL)
{
printf(" The item deleted=%d ", last->info);
free(last);
return NULL;
}
first= last->link; // address of first node
cur= first;
while(cur != NULL)
{
prev=cur;
cur=cur->link;
}
prev->link = last->link;
printf(" The item deleted=%d ", cur->info);
free(cur);
return prev;
}

---------------------------------------------------------------------------------------------------------------------
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;

Dept. of CSE Page 15


BCS304-DSA MODULE-2 NOTES

if ( last == NULL ) /* Make temp as the first node */


last = temp;
else
temp->link = last->link; /* Insert at the front end */
last->link = temp; /* link last node to first node */
return last; /* Return the last node */
}

// Function to delete node at front end of the circular linked list

NODE delete_front(NODE last)


{
NODE first;
if ( last == NULL ) /* Check for empty list */
{
printf("List is empty\n");
return NULL;
}
if ( last->link == last ) /* Delete if only one node */
{
printf("The item deleted is %d\n", last->info);
free(last);
return NULL;
}
first = last->link; /* obtain node to be deleted */
last->link = first->link; /*Store new first node in link of last */
printf("Item deleted is %d\n", first->info);
free(first); /* delete the old first node */
return last; /* return always address of last node */
}
---------------------------------------------------------------------------------------------------------------------
14. Write the C program to implement stack using linked list?
Answer:

#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
struct node
{
int info;

Dept. of CSE Page 16


BCS304-DSA MODULE-2 NOTES

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;
}

// function to insert node at front end


NODE insert_front(int item, NODE first)
{
NODE temp;
temp = getnode();
temp->info = item;
temp->link = first;
return temp;
}

//finction to delete node at front end


NODE delete_front(NODE first)
{
NODE temp;
if ( first == NULL )
{
printf("List is empty cannot delete\n");
return NULL;
}
temp = first;
temp = temp->link;
printf("Item deleted = %d\n",first->info);
free(first);
return temp;

Dept. of CSE Page 17


BCS304-DSA MODULE-2 NOTES

// Function to display nodes


void display(NODE first)
{
NODE cur;
if ( first == NULL )
{
printf("List is empty\n");
return;
}
printf("The contents of singly linked list\n");
cur = first;
while ( cur != NULL )
{
printf("%d ",cur->info);
cur = cur->link;
}
printf("\n");
}

// Main function of the program


void main()
{
NODE first;
int choice, item;
first = NULL;
for (;;)
{
printf("1:Insert_Front 2:Delete_Front\n");
printf("3:Display 4:Exit\n");
printf("Enter the choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter the item to be inserted\n");
scanf("%d", &item);
first = insert_front (item, first);
break;
case 2: first = delete_front(first);

Dept. of CSE Page 18


BCS304-DSA MODULE-2 NOTES

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;
}

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 )

Dept. of CSE Page 19


BCS304-DSA MODULE-2 NOTES

return temp;
cur = first;
while ( cur->link != NULL)
{
cur = cur->link;
}
cur->link = temp;
return first;
}

//finction to delete node at front end


NODE delete_front(NODE first)
{
NODE temp;
if ( first == NULL )
{
printf("List is empty cannot delete\n");
return NULL;
}
temp = first;
temp = temp->link;
printf("Item deleted = %d\n",first->info);
free(first);
return temp;
}

// Function to display nodes


void display(NODE first)
{
NODE cur;
if ( first == NULL )
{
printf("List is empty\n");
return;
}
printf("The contents of singly linked list\n");
cur = first;
while ( cur != NULL )
{

Dept. of CSE Page 20


BCS304-DSA MODULE-2 NOTES

printf("%d ",cur->info);
cur = cur->link;
}
printf("\n");
}

// Main function of the program


void main()
{
NODE first;
int choice, item;
first = NULL;
for (;;)
{
printf("1:Insert_rear 2: Delete_Front\n");
printf("3:Display 4:Exit\n");
printf("Enter the choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter the item to be inserted\n");
scanf("%d", &item);
first = insert_rear (item, first);
break;
case 2: first = delete_front(first);
break;
case 3: display(first);
break;
default: exit(0);
}
}
}
---------------------------------------------------------------------------------------------------------------------
16. Write the C function to add two polynomials. Show the linked representation of the
below two polynomials and their addition using a circular singly linked list
P1: 5x3 + 4x2 +7x + 3
P2: 6x2 + 5
Output: add the above two polynomials and represent them using the linked list
Answer:
// Function to add two polynomials

Dept. of CSE Page 21


BCS304-DSA MODULE-2 NOTES

NODE add_poly (NODE h1, NODE h2, NODE h3)


{
NODE p1, p2; int cf1, px1, sum;
p1 = h1->link;
while (p1 != h1) /* As long as term of polynomial 1 exists */
{
/* coefficent power of x of poly1 */
cf1 = p1->cf, px1 = p1->px;
p2 = search(p1, h2); /* search power of p1 in p2 */
if (p2 != h2) /* powers of poly1 found in poly 2 */
{
sum = cf1 + p2->cf; /*Add coefficients, insert to poly3*/
h3 = insert_rear (sum, px1, h3);
p2->cf = -999; /* Delete the term of poly2 */
}
else /* If not found, insert term of poly 1 to poly 3*/
h3 = insert_rear (cf1, px1, h3);
p1 = p1->link; /* Get the next term of polynomial 1 */
}
h3 = copy_poly(h2, h3); /* Copy remaining terms of poly 2 into poly 3*/
return h3; /* return total terms in poly 3 */
}

linked representation of the below two polynomials and their addition using a circular singly
linked list is:

Dept. of CSE Page 22


BCS304-DSA MODULE-2 NOTES

Output after addition of polynomial is: P3: 5x3 + 8x2 +7x + 8


---------------------------------------------------------------------------------------------------------------------
17. Write a c routine to perform the following function on polynomial terms
I. read polynomial terms
II. search polynomials terms
III. copy polynomials terms
IV. Insert polynomial terms
Answer:
Try to write answer by yourself refer the class ppt

----------------------------------------------GOOD LUCK---------------------------------------------------

Dept. of CSE Page 23

You might also like