Wa0000.
Wa0000.
Page 1
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 2
DATA STRUCTURES AND APPLICATIONS (BCS304)
Example:
Elements 10, 20, 30, 40 and 50 i entered into queue as in Fig. 2.4.
Page 3
DATA STRUCTURES AND APPLICATIONS (BCS304)
Example:
Initially, rear points to -1. So when queue is empty, rear should point to -1 and front
should point to 0 (Fig 2.6(a)). Elements are deleted from the front end. For example,
delete 10, 20, 30, 40 and 50 from the queue in the fig. 2.4 will result in the queue in the
fig.2.6(b). When front>rear it results empty queue. Hence we need to make the initial
condition as front=0 and rear = -1 when front>rear.
Page 4
DATA STRUCTURES AND APPLICATIONS (BCS304)
}
item = q[front];
printf("deleted element is:%d ", item);
front++;
if (front > rear) {
front = 0;
rear = -1;
}
}
iv) Queue Display
If queue is having some items, then it should be displayed one after the other. If there is
no item, then it should display error message.
Case – 1: if the queue is empty then
if(front>rear)
{ printf(“Queue is empty”);
return;
}
Case – 2: if the queue is having some
items, then
for(i=front;i<rear;i++)
printf(“%d\t”,q[i]); Fig.2.7. Queue with items
Page 5
DATA STRUCTURES AND APPLICATIONS (BCS304)
Questions
1.Define Queue.
2.List Queue Operations.
3.What is the Overflow condition of Queue.
4. What is the Underflow condition of Queue.
Page 6
DATA STRUCTURES AND APPLICATIONS (BCS304)
• If front =-1 and rear =-1 we cannot distinguish between queue empty and
queuefull. To avoid this confusion we set front=0 and rear =0 in circular queue.
Page 7
DATA STRUCTURES AND APPLICATIONS (BCS304)
Insert Function
void insert() {
int item;
if (front == (rear + 1) % MAX) {
printf(“\n Circular Queue overflow”);
return;
} else {
rear = (rear + 1) % MAX;
printf(“\ n Enter the element to be inserted”);
scanf(“% d”, &item);
q[rear] = item;
}
}
3. Delete and Underflow in Circular Queue
We continue deletion of element from the last CQ got in Fig. 2.10. In a circular queue, the
element is always deleted at front position. So delete 10 first since element of q[front] is
Page 8
DATA STRUCTURES AND APPLICATIONS (BCS304)
10.Now front should point to 1, since next element to be deleted is 20. Fig.2.11 shows the
step by step procedure to delete from CQ. When front=0 and we try to delete more items,
then queue underflow occurs.
Delete Function
void delete() {
if (front == rear) {
printf(“ Circular Queue underflow”);
return;
} else {
front = (front + 1) % MAX;
printf(“\n deleted element is % d”, q[front]);
}
}
4. Display in Circular Queue
Page 9
DATA STRUCTURES AND APPLICATIONS (BCS304)
void display() {
int i;
if (front == rear) {
printf(“circular queue is empty\n”);
} else {
printf(“\n contents of circular queue”);
for (i = (front + 1) % MAX; i != rear; i = (i + 1) % MAX)
{
printf(“% d\t”, q[i]);
}
printf(“% d\t”, q[i]);
}
}
Questions:
1.What is circular Queue.
2.Why Circular Queue is better than the linear Queue.
3.What is the initial Condition for Circular Queue.
4.What is the condition for the circular queue to be empty.
5. What is the condition for the circular queue to be overflow.
Page 10
DATA STRUCTURES AND APPLICATIONS (BCS304)
• To get a proper circular queue configuration, slide the elements in the right segment
(i.e., elements A and B) to the right end of the array as in figure (d).
• 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.
• Below program gives the code to add to a circular queue using a dynamically
allocated array.
void addq( element item)
{
/* add an item to the queue */
Page 11
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 12
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 13
DATA STRUCTURES AND APPLICATIONS (BCS304)
Example:
Page 14
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 15
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 16
DATA STRUCTURES AND APPLICATIONS (BCS304)
equal amount of space bounded by indices b[i] and e[i]. This is shown in Fig.
2.14(b)
Page 17
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 18
DATA STRUCTURES AND APPLICATIONS (BCS304)
Questions
1.What is Multiple Stack.
2.What is multiple Queue.
3.How Multiple Stack grow
4.What is the condition for the multiple stack
5. How Multiple Queue grow
6.What is the condition for the multiple Queue
Page 19
DATA STRUCTURES AND APPLICATIONS (BCS304)
So, there must be a data structure that removes the restrictions on the maximum number
of elements and the storage condition to write efficient programs. A linked list does not
store its elements in consecutive memory locations and the user can add any number of
elements to it.
Elements in a linked list can be accessed only in a
sequential manner But like an array, insertions and
deletions can be done at any point in the list in a
constant time.There are 2 fields in linked list
1. Data
2. Link to next node
So we can write the structure of linked list as in
Fig.2.15.
Fig. 2.15: Linked List Structure
Page 20
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 21
DATA STRUCTURES AND APPLICATIONS (BCS304)
Questions
1.Define Linked List.
2.How Linked list is represented.
3.What are the two fields in linked list
4.How Linked List is represented in memory
void create() {
Page 22
DATA STRUCTURES AND APPLICATIONS (BCS304)
3.Insert to End
Page 23
DATA STRUCTURES AND APPLICATIONS (BCS304)
Suppose we have empty list, then first is equal to NULL. Then directly create new node
and make it as first. But suppose we have list as in Fig.1. Now if we want to perform
insert end, then new node to be attached to right side of the last node (right of 10 here).
Create a new node and name it as temp and read new data(30) to temp as in Fig.3. Check
if lastlink=NULL and if it is not equal to NULL make last=lastlink and
lastlink=NULL as in Fig.3 and then connect new node ‘temp’ to ‘lastlink’ as in
Fig.3.
Page 24
DATA STRUCTURES AND APPLICATIONS (BCS304)
last = last->link;
}
last->link = temp;
}
}
}
}
Page 26
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 27
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 28
DATA STRUCTURES AND APPLICATIONS (BCS304)
• The push operation is used to insert an element into the stack. The new element is
added at the topmost position of the stack. Consider the linked stack shown in Fig.
3.9.2(a). To insert an element with value 9, we first check if TOP=NULL. If this
is the case, then we allocate memory for a new node, store the value in its DATA
part and NULL in its NEXT part. The new node will then be called TOP.
However, if TOP! =NULL, then we insert the new node at the beginning of the
linked stack and name this new node as TOP. Thus, the updated stack becomes as
shown in Fig. 3.9.2(b).
Page 29
DATA STRUCTURES AND APPLICATIONS (BCS304)
• Figure 3.9.3 shows the algorithm to push an element into a linked stack. In Step 1,
memory is allocated for the new node. In Step 2, the DATA part of the new node
is initialized with the value to be stored in the node. In Step 3, we check if the new
node is the first node of the linked list. This is done by checking if TOP = NULL.
In case the IF statement valuates to true, then NULL is stored in the NEXT part of
the node and the new node is called TOP. However, if the newnode is not the first
node in the list, then it is added before the first node of the list (that is, the TOP
node) and termed as TOP.
Page 30
DATA STRUCTURES AND APPLICATIONS (BCS304)
• Figure 3.9.5 shows the algorithm to delete an element from a stack. In Step 1, we
first check for the UNDERFLOW condition. In Step 2, we use a pointer
• PTR that points to TOP. In Step 3, TOP is made to point to the next node in
sequence. In Step 4, the memory occupied by PTR is given back to the free pool.
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void pop() {
if (top == NULL)
printf("\nStack is overflow!!!\n");
else {
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
Page 32
DATA STRUCTURES AND APPLICATIONS (BCS304)
top = temp->next;
free(temp);
}
}
void display() {
if (top == NULL)
printf("\nStack is Empty!!!\n");
else {
struct Node *temp = top;
while (temp->next != NULL) {
printf("%d--->", temp->data);
temp = temp->next;
}
printf("%d--->NULL", temp->data);
}
}
2.8 Linked Queue
• We have seen how a queue is created using an array. Although this technique of
creating a queue is easy, its drawback is that the array must be declared to have
some fixed size.
• If we allocate space for 50 elements in the queue and it hardly uses 20–25
locations, then half of the space will be wasted. And in case we allocate less
memory locations for a queue that might end up growing large and large, then a lot
of re-allocations will have to be done, thereby creating a lot of overhead and
consuming a lot of time.
• In case the queue is a very small one or its maximum size is known in advance,
then the array implementation of the queue gives an efficient implementation.
But if the array size cannot be determined in advance, the other alternative ,i.e.
the linked representation is used.
• In a linked queue, every element has two parts, one that stores the data and
another which holds the address of the next element. The START pointer of the
linked list is used as FRONT. Here we will also use another pointer called REAR,
which will store the address of the last element in the queue. All insertions will be
Page 33
DATA STRUCTURES AND APPLICATIONS (BCS304)
done at the rear end and all the deletions will be done at front end. If
FRONT=REAR=NULL, then it indicates that the queue is empty. The linked
representation of queue is shown in Fig. 3.10.1.
• The insert operation is used to insert an element into the queue. The new element
is added as the last element of the queue. Consider the linked queue shown in Fig.
3.10.2 (a). To insert an element with value 9, we first check if FRONT=NULL. If
this is the case, then we allocate memory for a new node, store the value in its
DATA part and NULL in its NEXT part. The new node will then be called
FRONT and REAR. However, if FRONT! =NULL, then we insert the new node
at the rear end of the linked queue and name this new node as REAR. Thus, the
updated stack becomes as shown in Fig. 3.10.2(b).
• Figure 3.10.3 shows the algorithm to insert an element into a linked queue. In Step
1, memory is allocated for the new node.
• In Step 2, the DATA part of the new node is initialized with the value to be stored
in the node.
• In Step 3, we check if the new node is the first node of the linked queue.
This is done by checking if FRONT = NULL. In case the new node is tagged as
FRONT and REAR. Also NULL is stored in the NEXT part of the node.
However, if the new node is not the first node in the list, then it is added at the
REAR end of the linked queue.
Page 34
DATA STRUCTURES AND APPLICATIONS (BCS304)
Delete Operation
• The delete operation is used to delete the element that is first inserted
• The delete operation is used to delete the element that is first inserted into the
queue. i.e. the element whose address is stored at FRONT.
• However, before deleting the value, we must first check if FRONT=NULL,
because if this is the case, then it means that the queue is empty and no more
deletions can be done. If an attempt is made to delete a value from a stack that is
already empty, an UNDERFLOW message is printed.
• Consider the stack shown in Fig. 3.10.4(a). To delete an element, we first check if
FRONT=NULL. If it is false, then we delete the 1st node pointed by the FRONT.
The FRONT will now point to the 2nd element of the linked queue. Thus the
updated queue becomes as shown in Fig. 3.10.4(b).
Page 35
DATA STRUCTURES AND APPLICATIONS (BCS304)
PTR that points to FRONT. In Step 3, FRONT is made to point to the next node in
sequence. In Step 4, the memory occupied by PTR is given back to the free pool.
Page 36
DATA STRUCTURES AND APPLICATIONS (BCS304)
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong selection!!! Please try
again!!!\n");
}
}
}
void insert() {
NODE newNode;
newNode = (NODE)malloc(sizeof(struct Node));
printf("Enter USN: ");
scanf("%s", newNode->usn);
printf("Enter NAME: ");
scanf("%s", newNode->name);
printf("Enter Branch: ");
scanf("%s", newNode->branch);
printf("Enter phone Number: ");
scanf("%llu", &newNode->phno);
printf("Enter Semester: ");
scanf("%d", &newNode->sem);
newNode->next = NULL;
if (front == NULL) {
front = rear = newNode;
front->next = NULL;
Page 37
DATA STRUCTURES AND APPLICATIONS (BCS304)
rear->next = NULL;
} else {
rear->next = newNode;
rear = newNode;
rear->next = NULL;
}
printf("\nInsertion is Success!!!\n");
}
void delete() {
if (front == NULL)
printf("\nQueue is Underflow!!!\n");
else {
temp = front;
front = front->next;
printf("\nDeleted node is with usn: %s", temp->usn);
free(temp);
}
}
void display() {
if (front == NULL)
printf("\nQueue is Empty!!!\n");
else {
temp = front;
while (temp->next != NULL) {
printf("The Student information in the node is\n");
printf("\nUSN:%s\nNAME:%s\nBRANCH:%s\nPHONE
NO.:%llu\nSEM:%d\n",
temp->usn, temp->name, temp->branch, temp-
>phno, temp->sem);
temp = temp->next;
Page 38
DATA STRUCTURES AND APPLICATIONS (BCS304)
}
printf("\nUSN:%s\nNAME:%s\nBRANCH:%s\nPHONE
NO.:%llu\nSEM:%d\n", temp->usn,
temp->name, temp->branch, temp->phno, temp->sem);
}
}
Question Bank
1.Define Linked Stack
2.Deifne Linked Queue
3.What are the operations of Linked Stack
4. What are the operations of Linked Queue
Page 39
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 40
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 41
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 42
DATA STRUCTURES AND APPLICATIONS (BCS304)
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
struct node // polynomial node
{
int coef;
int x, y, z;
struct node *link;
};
typedef struct node *NODE;
Page 43
DATA STRUCTURES AND APPLICATIONS (BCS304)
NODE x;
x = (NODE)malloc(sizeof(struct node));
return x;
} // end of getnode
NODE readpoly() {
NODE temp, head, cur;
char ch;
head = getnode(); // create a head node and set all values to -1
it is
// similar to FIRST in SLL program
head->coef = -1;
head->x = -1;
head->y = -1;
head->z = -1;
head->link = head; // self reference
do {
temp = getnode(); // create a polynomial node
printf("\nEnter the coefficient and exponent in decreasing
order\n");
scanf("%d%d%d%d", &temp->coef, &temp->x, &temp->y, &temp->z);
cur = head;
while (cur->link != head) // find the last node
cur = cur->link;
cur->link = temp; // connect new node to the last node
temp->link = head; // point back to head
printf("\nDo you want to enter more coefficients(y/n)");
fflush(stdin); // to clear the stdin buffer
scanf("%c", &ch);
} while (ch == 'y' || ch == 'Y');
return head; // return the polynomial list
} // end of readpoly
Page 44
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 45
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 46
DATA STRUCTURES AND APPLICATIONS (BCS304)
a = a->link;
break;
}
} while (!done); // repeate until not done
lastc->link = c; // point back to head of C
return c; // return answer
}
Page 47
DATA STRUCTURES AND APPLICATIONS (BCS304)
{
ex = cur->x; // exponent of x
ey = cur->y; // exponent of y
ez = cur->z; // exponent of z
cof = cur->coef; // coefficient
res += cof * pow(x, ex) * pow(y, ey) *
pow(z, ez); // compute result for each polynomial
cur = cur->link; // move to next node
}
printf("\nresult: %d", res);
} // end of evaluate
void main(void) {
int i, ch;
NODE a = NULL, b, c;
while (1) {
printf("\n1: Represent first polynomial A");
printf("\n2: Represent Second polynomial B");
printf("\n3: Display the polynomial A");
printf("\n4: Display the polynomial B");
printf("\n5: Add A & B polynomials"); // C=A+B
printf("\n6: Evaluate polynomial C");
printf("\n7: Exit");
printf("\n Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter the elements of the polynomial A");
a = readpoly();
break;
case 2:
printf("\nEnter the elements of the polynomial B");
b = readpoly();
Page 48
DATA STRUCTURES AND APPLICATIONS (BCS304)
break;
case 3:
print(a); // display polynomial A
break;
case 4:
print(b); // display polynomial A
break;
case 5:
c = addpoly(a, b); // C=A+B
printf("\nThe sum of two polynomials is: ");
print(c); // display polynomial C
printf("\n");
break;
case 6:
evaluate(c); // Evaluate polynomial C
break;
case 7:
return;
default:
printf("\nInvalid choice!\n");
} // end of switch
} // end of while
} // end of main
Questions
1.What is polynomial
2.How to perform Polynomial addition
3.What are the three conditions to be followed to add Polynomial
Page 49
DATA STRUCTURES AND APPLICATIONS (BCS304)
Question Bank
1. Give the disadvantage of ordinary queue and how it can be solved using circular queue. Explain
with suitable example, how would you implement circular queue using dynamically allocated
array
2. Write insert and delete functions of circular queue.
3. Explain multiple stacks and queues with program.
4. Define Queue. Implement the operations of queue using arrays.
5. What is circular queue? Explain how it is differ from linear queue. Write a C program for
primitive operations of circular queue
6. For the given circular queue shown in Fig. write the values of front and rear in the table after
each specified operation is performed. Queue full empty conditions must be considered. 0 - 7
indicate the array indices.
7. What is linked list? Explain the different types of linked list with examples.
8.Give a node structure to create a linked list of integers and write a C function to perform the
following.
a. Create a three-node list with data 10, 20 and 30
b. Inert a node with data value 15 in between the nodes having data values 10 and 20
c. Delete the node which is followed by a node whose data value is 20
d. Display the resulting singly linked list.
9. With node structure show how would you store the polynomials in linked lists? Write C
function for adding two polynomials represented as circular lists.
10. Write a C function to add two-polynomials represented as circular list with header node.
11. Write a C function to perform the following i. Reversing a singly linked list ii. Concatenating
singly linked list. iii. Finding the length of the circular linked list. iv. To search an element in
the singly linked list
12. Write a node structure of linked stack. Write a function to perform push and pop operations on
linked stack.
13.Write a function for singly linked lists with integer data, to search an element in the list that is
Page 50
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 51