Professional Documents
Culture Documents
53003190073
PRACTICAL 1
1.1. Write a program to store the elements in 1-D array and perform the operations like searching, sorting and
reversing the elements. [Menu Driven]
Linear Search or Sequential Search: This is the simplest method for searching. In this
technique of searching, the element to be found in searching the elements to be found is
searched sequentially in the list. This method can be performed on a sorted or an unsorted list
(usually arrays). In case of a sorted list searching starts from 0 th element and continues until
the element is found from the list or the element whose value is greater than (assuming the list
is sorted in ascending order), the value being searched is reached.
1
Data Structure Practical
53003190073
while(j<n)
{
if(A[j]==item)
{
break;
}
j=j+1;
}
printf("Found element %d at position %d \n",item,j+1);
}
Output:
Conclusion: In the above practical, element 5 was found at position 3 in the array A=[1,3,5,7,8]
using search operation.
2
Data Structure Practical
53003190073
Sorting is the operation performed to arrange the records of a table or list in some order
according to some specific ordering criterion. Sorting is performed according to some key value
of each record.
The records are either sorted either numerically or alphanumerically. The records are then
arranged in ascending or descending order depending on the numerical value of the key. Here is
an example, where the sorting of a lists of marks obtained by a student in any particular subject
of a class.
Algorithm: Consider LA is a linear array with N elements and K is a positive integer such that
K<=N. Following is the algorithm to update an element available at the Kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. Stop
#include<stdio.h>
#include<conio.h>
int temp=*xp;
*xp=*yp;
*yp=temp;
int i,j,min_idx;
for(i=0;i<n-1;i++)
min_idx=i;
3
Data Structure Practical
53003190073
for(j=i+1;j<n;j++)
if(arr[j]<arr[min_idx])
min_idx=j;
swap(&arr[min_idx],&arr[i]);
int i;
for(i=0;i<size;i++)
printf("%d",arr[i]);
printf("\n");
//Driver code
int main()
int arr[]={0,23,14,12,9};
int n=sizeof(arr)/sizeof(arr[0]);
clrscr();
printArray(arr,n);
selectionSort(arr,n);
printArray(arr,n);
getch();
return 0;
4
Data Structure Practical
53003190073
Output:
Conclusion: In the above experiment, the array arr[]={0,23,14,12,9} is sorted and the output is
arr[]={0,9,12,14,23}.
Algorithm:
STEP 1: START
STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}
STEP 3: length= sizeof(arr)/sizeof(arr[0])
STEP 4: PRINT "Original Array:"
STEP 5: REPEAT STEP 6 and STEP 7 UNTIL i<length
STEP 6: PRINT arr[i]
STEP 7: i=i+1
STEP 8: PRINT new line.
STEP 9: PRINT "Array in reverse order"
STEP 10: SET i=length-1. REPEAT STEP 11 and STEP 12 UNTIL i>=0
STEP 11: PRINT a[i]
STEP 12: i=i-1
STEP 13: RETURN 0.
STEP 14: END
5
Data Structure Practical
53003190073
#include<stdio.h>
#include<conio.h>
void reverseArray(int arr[],int start,int end)
{
int temp;
while(start<end)
{
temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
}
}
void printArray(int arr[],int size)
{
int i;
for(i=0;i<size;i++)
printf("%d",arr[i]);
printf("\n");
}
int main()
{
int arr[]={1,2,3,4,5,6};
int n=sizeof(arr)/sizeof(arr[0]);
clrscr();
printArray(arr,n);
reverseArray(arr,0,n-1);
printf("Reverse array is: \n");
printArray(arr,n);
getch();
return 0;
}
6
Data Structure Practical
53003190073
Output:
1.2. Read the two arrays from the user and merge them and display the elements in sorted order.
Theory: Merge sort is a sorting technique based on divide and conquer technique. With worst-
case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
It divides input array in two halves, calls itself for the two halves and then merges the two sorted
halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key
process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-
arrays into one.
Algorithm: 1. create two arrays of some fixed size and define their elements in sorted fashion.
2. Take two variables i and j, which will be at the 0th position of these two arrays.
3. Elements will be compared one by one using i and j in for loop, and whichever element is
smaller than the other, that element will get inserted to final array and the position (either i or j)
will move by one, whereas the other array’s track position will remain in that same place.
4. Above work will be done till we reach the end of either array. After that, one of the array
whose elements are still to be added, its elements will get straightaway added to the final array.
7
Data Structure Practical
53003190073
int main()
{
int arr1[] = {1, 3, 5, 7};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int arr3[n1+n2];
mergeArrays(arr1, arr2, n1, n2, arr3);
8
Data Structure Practical
53003190073
return 0;
}
Output:
Conclusion: In the above practical, element 8 was found at position 1 in the array after merging
1,2,3,4,5,6,7,8 using search operation.
1.3 Write a program to perform the Matrix addition, Multiplication and Transpose Operation.
1.3.1. Matrix Multiplication:
Theory: In article C Programming Matrix Multiplication a matrix is a grid that is used to store
data in a structured format. It is often used with a table, where the data is represented in
horizontal rows and vertical columns. Matrices are often used in programming languages and are
used to represent the data in a graphical structure. In programming if the user wants to multiply,
add, subtract and divide two matrices, then the order of the matrix should be declared first. Once
the order of the matrix is declared for the first and second matrix, then the elements (input) for
the matrices are needed to be entered by the user. If the order of the matrix is not proportionate to
each other, then the error message will be displayed which is implanted by a programmer in the
condition statement. If a matrix contains only one row then it is called a row vector, and if it
contains only one column then it is called a column vector.
9
Data Structure Practical
53003190073
A matrix that contains the same number of rows and columns then it is called a square matrix.
Matrix is used to store a group of related data. Some of the programming languages are used to
support matrices as a data type that offers more flexibility than a static array. Instead of storing
the values in a matrix, it can be stored as an individual variable, a program can access and
perform operations on the data more efficiently. In C programming matrix multiplications are
done by using arrays, functions, pointers. Therefore, we are going to discuss an algorithm for
Matrix multiplication along with the flowchart, which can be used to write programming code
for 3×3 matrix multiplication in a high-level language. This detailed explanation will help you to
analyze the working mechanism of matrix multiplication and will help to understand how to
write code.
#include<conio.h>
#define N 4
int i,j,k;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
res[i][j]=0;
for(k=0;k<N;k++)
res[i][j]+=mat1[i][k]*mat2[k][j];
10
Data Structure Practical
53003190073
int main()
int i,j;
int res[N][N];
int mat1[N][N]={{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4}};
int mat2[N][N]={{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4}};
clrscr();
multiply(mat1,mat2,res);
for(i=0;i<N;i++)
for(j=0;j<N;j++)
printf("%d",res[i][j]);
printf("\n");
getch();
return 0;
Output:
11
Data Structure Practical
53003190073
Conclusion: In the above practical, element 16 was found at position 4 in the matrix form
10 10 10 10
20 20 20 20
30 30 30 30
40 40 40 40 using search operation.
1.3.2. Matrix Addition
Theory:
• Addition of matrices is commutative which means A+B = B+A
• Addition of matrices is associative which means A+(B+C) = (A+B)+C
• The order of matrices A, B and A+B is always same
• If order of A and B is different, A+B can’t be computed
• The complexity of addition operation is O(m*n) where m*n is order of matrices
Algorithm/Pseudo code:
Matrix addition:
1. Input the order of the matrix.
2. Input the matrix 1 elements.
3. Input the matrix 2 elements.
12
Data Structure Practical
53003190073
4. Repeat from i = 0 to m
5. Repeat from j = 0 to n
6. mat3[i][j] = mat1[i][j] + mat2[i][j]
7. Print mat3.
int main()
{
int A[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
return 0;
}
Output:
13
Data Structure Practical
53003190073
Conclusion: In the above practical, element 16 was found at position 4 in the matrix form
2222
4444
6666
8 8 8 8 using search operation.
1.3.3. Transpose Operation
Theory: Multiplication of matrices is non-commutative which means A*B ≠ B*A.
Multiplication of matrices is associative which means A*(B*C) = (A*B)*C. For computing
A*B, the number of columns in A must be equal to number of rows in B. Existence of A*B does
not imply existence of B*A. The complexity of multiplication operation (A*B) is O(m*n*p)
where m*n and n*p are order of A and B respectively. The order of matrix C computed as A*B
is m*p where m*n and n*p are order of A and B respectively.
Algorithm:
1. Input the order of the matrix1 (m * n).
2. Input the order of matrix2 (p * q).
3. Input the matrix 1 elements.
4. Input the matrix 2 elements.
5. Repeat from i = 0 to m
14
Data Structure Practical
53003190073
6. Repeat from j = 0 to q
7. repeat from k = 0 to p
8. sum=sum+ mat1[c][k] * mat2[k][d];
9. mat3[c][d]=sum
10. print mat3.
#include<conio.h>
#define N 4
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
B[i][j]=A[j][i];
int main()
int A[N][N]={{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4}};
int B[N][N],i,j;
clrscr();
transpose(A,B);
for(i=0;i<N;i++)
for(j=0;j<N;j++)
printf("%d",B[i][j]);
printf("\n");
getch();
return 0;
15
Data Structure Practical
53003190073
Output:
Conclusion: In the above practical, element 16 was found at position 4 in the matrix form
1234
1234
1234
1 2 3 4 using search operation.
16
Data Structure Practical
53003190073
PRACTICAL 2
2.1 Program to demonstrate all insertion methods on Linked List
Theory:
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not
stored at a contiguous location; the elements are linked using pointers.
A linked list is represented by a pointer to the first node of the linked list. The first node is called
the head. If the linked list is empty, then the value of the head is NULL.
Each node in a list consists of at least two parts:
1) Data
2) Pointer (Or Reference) to the next node
In C, we can represent a node using structures.
Algorithm/Pseudo code:
Add a node at the front:
1. allocate node
2. put in the data
3. Make next of new node as head
4. move the head to point to the new node
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
{
printf("the given previous node cannot be NULL");
return;
}
// clrscr();
printf("Insert 6. So linked list becomes 6->NULL\n");
append(&first, 6);
printf("Created Linked list is: ");
printList(first);
getch();
return 0;
}
21
Data Structure Practical
53003190073
Output:
Conclusion:
Why Linked List?
Arrays can be used to store linear data of similar types, but arrays have the following limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the
usage.
2) Inserting a new element in an array of elements is expensive because the room has to be
created for the new elements and to create room existing elements have to be shifted.
Advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion
Drawbacks:
1) Random access is not allowed. We have to access elements sequentially starting from the first
node. So we cannot do binary search with linked lists efficiently with its default implementation.
Read about it here.
2) Extra memory space for a pointer is required with each element of the list.
3) Not cache friendly. Since array elements are contiguous locations, there is locality of
reference which is not there in case of linked lists.
1.2. Given a reference (pointer to pointer) to the head of a list and an int, inserts a new
node on the front of the list.
Theory:
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not
stored at a contiguous location; the elements are linked using pointers.
22
Data Structure Practical
53003190073
A linked list is represented by a pointer to the first node of the linked list. The first node is called
the head. If the linked list is empty, then the value of the head is NULL.
Each node in a list consists of at least two parts:
1) Data
2) Pointer (Or Reference) to the next node
In C, we can represent a node using structures
Algorithm/Pseudo code:
Code:
#include<stdio.h>
#include<conio.h>
// A linked list node
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
23
Data Structure Practical
53003190073
/* Given a node as prev_node, insert a new node after the given node */
void insertAfter(struct Node* prev_node, int new_data)
{
/* 1. allocate new node */
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
/*2. check if the given prev_node is NULL */
if (prev_node == NULL) {
printf("the given previous node cannot be NULL");
return;
}
/* 3. put in the data */
new_node->data = new_data;
/* 4. Make next of new node as next of prev_node */
new_node->next = prev_node->next;
/* 5. Make the next of prev_node as new_node */
prev_node->next = new_node;
/* 6. Make prev_node as previous of new_node */
new_node->prev = prev_node;
/* 7. Change previous of new_node's next node */
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = new_node;
/* 7. Make last node as previous of new node */
new_node->prev = last;
return;
}
// This function prints contents of linked list starting from the given
node
void printList(struct Node* node)
{
struct Node* last;
printf("\nTraversal in forward direction \n");
while (node != NULL) {
printf(" %d ", node->data);
last = node;
node = node->next;
}
Output:
25
Data Structure Practical
53003190073
Conclusion:
The above program shows different types for insertion of nodes in linked list.
PRACTICAL 3
3. Implement the following for Stack:
3.1. Write a program to implement the concept of Stack with Push, Pop, Display
and Exit operations.
Theory:
Stack is a LIFO (last in first out) structure. It is an ordered list of the same type of elements. A
stack is a linear list where all insertions and deletions are permitted only at one end of the list.
When elements are added to stack it grow at one end. Similarly, when elements are deleted from
a stack, it shrinks at the same end.
isfull()
Algorithm of isfull() function −
26
Data Structure Practical
53003190073
end procedure
isempty()
Algorithm of isempty() function −
end procedure
Push Operation
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps −
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Pop Operation
A Pop operation may involve the following steps −
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
27
Data Structure Practical
53003190073
Code:
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int top;
unsigned capacity;
int* array;
};
int main()
{
struct Stack* stack = createStack(100);
push(stack, 10);
push(stack, 20);
push(stack, 30);
push(stack, 40);
push(stack, 50);
push(stack, 60);
push(stack, 130);
push(stack, 230);
printf("%d popped from stack\n", pop(stack));
printf("Top element is %d\n", peek(stack));
return 0;
}
Output:
Conclusion:
If the stack is full isfull function is called and according to pop and push input from user
respective function is called and peek is used to display the output.
29
Data Structure Practical
53003190073
3.2. Write a program to convert an infix expression to postfix and prefix conversion
Theory:
One of the applications of Stack is in the conversion of arithmetic expressions in high-level
programming languages into machine readable form. As our computer system can only
understand and work on a binary language, it assumes that an arithmetic operation can take place
in two operands only e.g., A+B, C*D,D/A etc. But in our usual form an arithmetic expression
may consist of more than one operator and two operands e.g. (A+B)*C(D/(J+D)).
These complex arithmetic operations can be converted into polish notation using stacks which
then can be executed in two operands and an operator form.
Infix Expression
It follows the scheme of <operand><operator><operand> i.e. an <operator> is preceded and
succeeded by an <operand>. Such an expression is termed infix expression. E.g., A+B
Postfix Expression
It follows the scheme of <operand><operand><operator> i.e. an <operator> is succeeded by both
the <operand>. E.g., AB+
Algorithm/Pseudo code:
Let, X is an arithmetic expression written in infix notation. This algorithm finds the
equivalent postfix expression Y.
Code:
30
Data Structure Practical
53003190073
// Stack type
struct Stack
{
int top;
unsigned capacity;
int* array; //array implemenation
};
// Stack Operations
struct Stack* createStack( unsigned capacity )
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
if (!stack)
return NULL;
return stack;
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1 ;
}
char peek(struct Stack* stack)
{
return stack->array[stack->top]; // to read top element from stack
}
char pop(struct Stack* stack)
{
if (!isEmpty(stack))
return stack->array[stack->top--] ;
return '$';
}
void push(struct Stack* stack, char op)
{
stack->array[++stack->top] = op;
}
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
// If the scanned character is an ‘)’, pop and output from the stack
// until an ‘(‘ is encountered.
else if (exp[i] == ')')
{
while (!isEmpty(stack) && peek(stack) != '(')
exp[++k] = pop(stack);
if (!isEmpty(stack) && peek(stack) != '(')
return -1; // invalid expression
else
pop(stack);
}
else // an operator is encountered
{
while (!isEmpty(stack) && Prec(exp[i]) <= Prec(peek(stack)))
exp[++k] = pop(stack);
push(stack, exp[i]);
}
32
Data Structure Practical
53003190073
exp[++k] = '\0';
printf( "%s", exp );
}
return 0;
}
Output:
Conclusion:
After the running of the program the infix is converted to postfix.
33
Data Structure Practical
53003190073
Theory:
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the
puzzle is to move the entire stack to another rod, obeying the following simple rules:
1) Only one disk can be moved at a time.
2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another
stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
3) No disk may be placed on top of a smaller disk.
Algorithm/Pseudo code:
Take an example for 2 disks:
Let rod 1 = 'A', rod 2 = 'B', rod 3 = 'C'.
Code:
#include <stdio.h>
int i=0;
// C recursive function to solve tower of hanoi puzzle
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
printf("\n Step %d: Move disk 1 from rod %c to rod %c", ++i,from_rod,
to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("\n Step %d: Move disk %d from rod %c to rod %c", ++i,n, from_rod,
to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
int main()
{
int n = 4; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}
Output:
34
Data Structure Practical
53003190073
Conclusion:
In the above program we have successfully moved the entire stack from one rod to another
without breaking the simple rules.
PRACTICAL 4
4. Implement the following for Queue:
4.1. Write a program to implement the concept of Queue with Insert, Delete, Display and
Exit operations
Theory: A Queue is a linear structure which follows a particular order in which the operations
are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of
consumers for a resource where the consumer that came first is served first. The difference
between stacks and queues is in removing. In a stack we remove the item the most recently
added; in a queue, we remove the item the least recently added.
To implement a queue using array, create an array arr of size n and take two variables front and
rear both of which will be initialized to 0 which means the queue is currently empty. Element
rear is the index upto which the elements are stored in the array and front is the index of the first
element of the array.
Algorithm/Pseudo code:
Now, some of the implementation of queue operations are as follows:
35
Data Structure Practical
53003190073
Enqueue: Addition of an element to the queue. Adding an element will be performed after
checking whether the queue is full or not. If rear < n which indicates that the array is not full then
store the element at arr[rear] and increment rear by 1 but if rear == n then it is said to be an
Overflow condition as the array is full.
Dequeue: Removal of an element from the queue. An element can only be deleted when there is
at least an element to delete i.e. rear > 0. Now, element at arr[front] can be deleted but all the
remaining elements have to shifted to the left by one position in order for the dequeue operation
to delete the second element from the left on another dequeue operation.
Front: Get the front element from the queue i.e. arr[front] if queue is not empty.
Display: Print all element of the queue. If the queue is non-empty, traverse and print all the
elements from index front to rear.
int main() {
//deQueue is not possible on empty queue
deQueue();
//enQueue 5 elements
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
display();
return 0;
}
36
Data Structure Practical
53003190073
Output:
37
Data Structure Practical
53003190073
Conclusion:
In the above program if the queue is empty data is inserted and if the queue is full the message is
prompted that queue is full and when delete function is called the first element which came in is
deleted.
Algorithm/Pseudo code:
Circular Queue Operations
The circular queue work as follows:
1. Enqueue Operation
check if the queue is full
for the first element, set value of FRONT to 0
circularly increase the REAR index by 1 (i.e. if the rear reaches the end, next it would be at
the start of the queue)
add the new element in the position pointed to by REAR
2. Dequeue Operation
check if the queue is empty
return the value pointed by FRONT
circularly increase the FRONT index by 1
for the last element, reset the values of FRONT and REAR to -1
However, the check for full queue has a new additional case:
Case 1: FRONT = 0 && REAR == SIZE - 1
Case 2: FRONT = REAR + 1
The second case happens when REAR starts from 0 due to circular increment and when its
value is just 1 less than FRONT, the queue is full.
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;//condition for empty queue
39
Data Structure Practical
53003190073
// Adding an element
void enQueue(int element) {
if (isFull())
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;//condition whn queue was empty... set
new front
rear = (rear + 1) % SIZE; //set the new rear
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}
// Removing an element
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
} else {
element = items[front];
if (front == rear) {//when there is only one element
front = -1;
rear = -1;
}
// Q has only one element, so we reset the
// queue after dequeing it. ?
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}
int main() {
// Fails because front = -1
40
Data Structure Practical
53003190073
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
display();
deQueue();
display();
enQueue(7);
display();
return 0;
}
Output:
41
Data Structure Practical
53003190073
Conclusion:
The circular queue works just as normal queue but there is no overflow condition in a circular
queue until the queue is actually full. Therefore, entering a new element is easy.
Algorithm/Pseudo code:
1. Create an empty array ‘arr’ of size ‘n’
initialize front = -1 , rear = 0
Inserting First element in deque, at either front or rear will lead to the same result.
After insert Front Points = 0 and Rear points = 0
43
Data Structure Practical
53003190073
int deque_arr[MAX];
int front=-1;
int rear=-1;
int main()
{
int choice,item;
while(1)
{
printf("\n\n1.Insert at the front end\n");
printf("2.Insert at the rear end\n");
printf("3.Delete from front end\n");
printf("4.Delete from rear end\n");
printf("5.Display\n");
printf("6.Quit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
44
Data Structure Practical
53003190073
}
if(front==-1) /*if queue is initially empty*/
{
front=0;
rear=0;
}
else if(rear==MAX-1) /*rear is at last position of queue */
rear=0;
else
rear=rear+1;
deque_arr[rear]=item ;
}/*End of insert_rearEnd()*/
int delete_frontEnd()
{
int item;
if( isEmpty() )
{
printf("\nQueue Underflow\n");
exit(1);
}
item=deque_arr[front];
if(front==rear) /*Queue has only one element */
{
front=-1;
rear=-1;
}
else
if(front==MAX-1)
front=0;
else
front=front+1;
return item;
}/*End of delete_frontEnd()*/
int delete_rearEnd()
{
int item;
if( isEmpty() )
{
printf("\nQueue Underflow\n");
exit(1);
}
item=deque_arr[rear];
46
Data Structure Practical
53003190073
int isFull()
{
if ( (front==0 && rear==MAX-1) || (front==rear+1) )
return 1;
else
return 0;
}/*End of isFull()*/
int isEmpty()
{
if( front == -1)
return 1;
else
return 0;
}/*End of isEmpty()*/
void display()
{
int i;
if( isEmpty() )
{
printf("\nQueue is empty\n");
return;
}
printf("\nQueue elements :\n");
i=front;
if( front<=rear )
{
while(i<=rear)
printf("%d ",deque_arr[i++]);
}
else
{
while(i<=MAX-1)
printf("%d ",deque_arr[i++]);
i=0;
while(i<=rear)
printf("%d ",deque_arr[i++]);
}
printf("\n");
}/*End of display() */
Output:
47
Data Structure Practical
53003190073
Conclusion:
In in the above program we can do insertion and deletion for both front and rear end.
48