FDT ManualV1.0
FDT ManualV1.0
}
Fundamentals of Data Structure (24BEELY106)
} I/II Semester
(common to all branches)
LAB MANUAL
Compiled by :
Prabodh C P
Asst Professor
Dept of CSE, SNPSU
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
To veiw a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/
Contents
1 String Operations 2
2 Linear Search 5
3 Binary Search 7
4 Selection Sort 9
6 Stack 17
7 Path Matrix 21
9 Array Operations 29
10 Bubble Sort 33
11 Insertion Sort 36
12 Linear Queue 38
13 Circular Queue 42
i
Listings
1.1 A01StringOperations.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.1 A02LinearSearch.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1 A03BinarySearch.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.1 A04SelectionSort.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.2 out4.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.1 A05SinglyLinkedList.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
6.1 A06StackOperations.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
7.1 A07PathMatrix.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
8.1 A08BST.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
8.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
9.1 B01ArrayOperations.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
9.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
10.1 B02BubbleSort.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
10.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
11.1 B03InsertionSort.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
11.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
12.1 B04LinearQueue.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
12.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
13.1 B05CircularQueue.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
13.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
14.1 B06ShortestPathMatrix.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
14.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
15.1 B07GraphAdjMatrix.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
15.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
ii
Preface
Use and spread the word of Free Software. Free Software leads to a Free Society!
Prabodh C P
iii
CODE::BLOCKS
CODE::BLOCKS TUTORIAL
Using Code::Blocks to compile and execute a C Program
To launch Code::Blocks IDE, Click on
Applications → Programming → Code::Blocks IDE
You will get a window as shown below
iv
LISTINGS LISTINGS
Code::Blocks creates what is called a Workspace to keep track of the project you are working on. It is possible for
you to be working on multiple projects within your workspace. A project is a collection of one or more source (as
well as header) files. Source files are the files that contain the source code for your program. If you are developing a
C program, you are writing C source code (.c files).
In the next step give the Project Title and specify the folder where you want to save your project.
(Note : Dont use any special characters or whitespaces for project title and folder names)
Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the
options). You should be using GNU GCC Compiler. Click Finish to create the new project.
The system will then return to the [MyProgram] window and you are ready to write your program.In the Man-
agement area of the screen (Shift-F2 toggles the Management display), you will see the files that are part of the
project in the Projects tab. To see the source files, click on the triangle symbol to expand the Workspace and its
subdirectories.
Under Sources, there is a file called main.c, which is automatically created for you when you build a console
application. Click on main.c.
main.c contains a simple Hello World program which you can edit later to solve a programming problem. Now let
us see how to compile and execute this main.c program. Just to understand the process of debugging we knowingly
introduce an error in the program by removing the semicolon after the printf statement. We will now compile the
program (To compile a file means to take the instructions that you have written and translate it into machine code
The error messages are shown in the Build messages window at the bottom. Let us now try to understand these
error messages. === Build: Debug in MyProgram (compiler: GNU GCC Compiler) ===
main.c In function main:
main.c 7 error: expected ; before return
main.c 8 warning: control reaches end of non-void function [-Wreturn-type]
=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===
The error messages show the errors in syntax (and not the logical errors). It also indicates the line number and
the type of error. Here in this example the error says that before the return statement in line no 7 a semicolon is
missing. The next message is a warning message which has resulted because of the previous error.
Now go to line number 6 and add a semicolon at the end. Now build your project again.
Now the build message window shows the following message. 0 error(s), 0 warning(s) (0 minute(s), 0 second(s)
This means that the errors and warnings have been successfully resolved. Now it is time to run the program. You
can Execute the project from the Build pull-down menu by clicking on Run option[Ctrl+F10].
An output window pops displaying the output of the program. A greeting message ”Hello world!” is printed on
to the output console.
1
Chapter 1
String Operations
Question
Write a menu driven C program to perform the following string operations using pointers
String Length
String Concatenation
String Reverse
C Code
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 char acString1[40], acString2[40],acString3[40];
7 int iLen, i, iChoice;
8 char *strPtr;
9
10 while(1)
11 {
12 printf("\n1.String Length\n2.String Concatenation\n3.String Reverse\n4.Exit\
n");
13 printf("\nEnter your choice\n");
14 scanf("%d",&iChoice);
15
16 switch(iChoice)
17 {
18 case 1:
19 printf("\nEnter the string : ");
20 scanf("%s", acString1);
21 iLen = 0;
22 strPtr = acString1;
23 while(*strPtr != ’\0’)
24 {
25 iLen++;
26 strPtr++;
27 }
28 printf("\nLength of the string is %d\n", iLen);
29
30 break;
31 case 2:
32 printf("\nEnter the first string : ");
33 scanf("%s", acString1);
34 printf("\nEnter the second string : ");
2
CHAPTER 1. STRING OPERATIONS
35 scanf("%s", acString2);
36
37 strPtr = acString1;
38 iLen = 0;
39 while(*strPtr != ’\0’)
40 {
41 iLen++;
42 strPtr++;
43 }
44
45 strPtr = acString2;
46 i = 0;
47 while(*strPtr != ’\0’)
48 {
49 acString1[iLen + i] = *strPtr;
50 strPtr++;
51 i++;
52 }
53 acString1[iLen+i]=’\0’;
54 printf("\nConcatenated String is %s\n", acString1);
55 break;
56 case 3:
57 printf("\nEnter the string to be reversed: ");
58 scanf("%s", acString2);
59 strPtr = acString2;
60 while(*strPtr != ’\0’)
61 {
62 strPtr++;
63 }
64 strPtr--;
65 i=0;
66 while(strPtr >= acString2)
67 {
68 acString3[i] = *strPtr;
69 strPtr--;
70 i++;
71 }
72 acString3[i]=’\0’;
73 printf("\nReversed String is %s\n", acString3);
74 break;
75 case 4:
76 exit(0);
77 }
78 }
79 return 0;
80 }
Listing 1.1: A01StringOperations.c
Output
=================================
1 /***************************************
2
3 1.String Length
4 2.String Concatenation
5 3.String Reverse
6 4.Exit
7
12
15 1.String Length
16 2.String Concatenation
17 3.String Reverse
18 4.Exit
19
29 1.String Length
30 2.String Concatenation
31 3.String Reverse
32 4.Exit
33
41 1.String Length
42 2.String Concatenation
43 3.String Reverse
44 4.Exit
45
Linear Search
Question
Write a program to find the location of a given number using Linear search.
C Code
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<stdbool.h>
4
5 int main(void)
6 {
7 int iaArr[10],iNum,i,j,iKey;
8 bool bFound;
9
20 bFound = false;
21 for(i=0;i<iNum;i++)
22 {
23 if(iaArr[i] == iKey)
24 {
25 bFound = true;
26 break;
27 }
28 }
29 if(bFound)
30 printf("\nKey element %d found at position %d\n",iKey,i+1);
31 else
32 printf("\nKey element %d not found\n", iKey);
33
34 return 0;
35 }
Listing 2.1: A02LinearSearch.c
Output
5
CHAPTER 2. LINEAR SEARCH
1 /***************************************
2 Enter the number of elements
3 5
4
13
14
26 ***************************************/
Listing 2.2: OUTPUT
Binary Search
Question
Write a C program to search for an element in an array using Binary Search.
C Code
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<stdbool.h>
4 int main(void)
5 {
6 int iaArr[10],iNum,i,iMid,iLow,iHigh,iKey;
7 bool bFound;
8
23 bFound = false;
24 iLow = 0;
25 iHigh = iNum-1;
26 while(iLow <= iHigh)
27 {
28 iMid = (iLow+iHigh)/2;
29 if(iKey == iaArr[iMid])
30 {
31 bFound = true;
32 break;
33 }
34 else if(iKey < iaArr[iMid])
35 {
36 iHigh = iMid-1;
37 }
38 else
39 {
40 iLow = iMid+1;
7
CHAPTER 3. BINARY SEARCH
41 }
42 }
43
44 if(bFound)
45 printf("\nKey element %d found at position %d\n",iKey,iMid+1);
46 else
47 printf("\nKey element %d not found\n", iKey);
48
49 return 0;
50 }
Listing 3.1: A03BinarySearch.c
Output 1
1 /***************************************
2 Enter the number of elements
3 5
4
15
Selection Sort
Question
Write a C program to sort a list of N elements using Selection Sort Algorithm.
C Code
Implementation
1 /***************************************************************************
2 *File : A04SelectionSort.c
3 *Description : Program to implement Selection Sort Algorithm
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 24.04
6 *Date : 7 November 2024
7 ***************************************************************************/
8
9 #include<stdio.h>
10 #include<stdlib.h>
11
12 /***************************************************************************
13 *Function : main
14 *Input parameters: none
15 *RETURNS : 0 on success
16 ***************************************************************************/
17
18 int main(void)
19 {
20 int iNum, i, j, k, iaArr[10], iTemp,iMinPos;
21
22
23 printf("\n*************************************************");
24 printf("\n*\tPROGRAM TO IMPLEMENT SELECTION SORT\t*\n");
25 printf("*************************************************");
26
27 printf("\nEnter no of elements\n");
28 scanf("%d",&iNum);
29
34 for(i=0;i<iNum-1;i++)
35 {
36 iMinPos = i;
37 for(j=i+1;j<iNum;j++)
9
CHAPTER 4. SELECTION SORT
38 {
39
47
52
53 }
54
57 for(i=0;i<iNum;i++)
58 printf("%d\t",iaArr[i]);
59
60 printf("\n");
61 return 0;
62 }
Listing 4.1: A04SelectionSort.c
Output
=================================
1 /***************************************
2
3 *************************************************
4 * PROGRAM TO IMPLEMENT SELECTION SORT *
5 *************************************************
6 Enter no of elements
7 5
8
24
25 ***************************************/
Listing 4.2: out4.c
Question
Write a C program to construct a singly linked list and perform insertion at the end, delete
specified element and display the elements.
C Code
1
2 #include<stdio.h>
3 #include<stdlib.h>
4
5 //Node Structure
6 struct node
7 {
8 int iInfo;
9 struct node* link;
10 };
11
20 int main()
21 {
22 NODEPTR listPtr;
23 int iElem, iChoice;
24 //listPtr points to the first node in the list
25 listPtr = NULL; //initially the list is empty
26
27 while(1)
28 {
29 printf("\n1.Insert\n2.Delete\n3.Display\n4.Search\n5.Exit\n");
30 printf("\nEnter your choice\n");
31 scanf("%d",&iChoice);
32
33 switch(iChoice)
34 {
35 case 1:
36 printf("\nEnter the element to be inserted : ");
37 scanf("%d", &iElem);
38 listPtr = fnInsertRear(listPtr, iElem);
39 break;
40 case 2:
11
CHAPTER 5. SINGLY LINKED LIST
46 case 3:
47 fnDisplay(listPtr);
48 break;
49 case 4:
50 printf("\nEnter the element to be searched : ");
51 scanf("%d", &iElem);
52 fnSearch(listPtr, iElem);
53 break;
54 case 5:
55 exit(0);
56 }
57 }
58 return 0;
59 }
60
119 }
120 else //cur points to the node to be deleted
121 {
122 prev->link = cur->link;
123 printf("\nElement deleted is %d\n", cur->iInfo);
124 free(cur);
125 }
126 }
127 return listPtr;
128 }
129
161 {
162 cur = cur->link;
163 iCount++;
164 }
165 if(cur == NULL)
166 {
167 printf("\nKey element %d not found.\n", iKey);
168 }
169 else
170 {
171 printf("\nKey element %d found in position %d\n", iKey, iCount);
172 }
173 }
174 }
175
Output
=================================
1 /***************************************
2
3 1.Insert
4 2.Delete
5 3.Display
6 4.Search
7 5.Exit
8
12 List is empty
13
14
15 1.Insert
16 2.Delete
17 3.Display
18 4.Search
19 5.Exit
20
26 1.Insert
27 2.Delete
28 3.Display
29 4.Search
30 5.Exit
31
37 1.Insert
38 2.Delete
39 3.Display
40 4.Search
41 5.Exit
42
46 List contents :
47 20 30
48
49 1.Insert
50 2.Delete
51 3.Display
52 4.Search
53 5.Exit
54
60 1.Insert
61 2.Delete
62 3.Display
63 4.Search
64 5.Exit
65
69 List contents :
70 20 30 40
71
72 1.Insert
73 2.Delete
74 3.Display
75 4.Search
76 5.Exit
77
85 1.Insert
86 2.Delete
87 3.Display
88 4.Search
89 5.Exit
90
98 1.Insert
99 2.Delete
100 3.Display
101 4.Search
102 5.Exit
103
111 1.Insert
112 2.Delete
113 3.Display
114 4.Search
115 5.Exit
116
123 1.Insert
124 2.Delete
125 3.Display
126 4.Search
127 5.Exit
128
132 ***************************************/
Listing 5.2: OUTPUT
Stack
Question
Write a C program to demonstrate the working of stack using arrays.
C Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define MAX_SIZE 5
4
7 void fnPush();
8 void fnPop();
9 void fnDisplay();
10
11
12 int main()
13 {
14 int iElem, iChoice;
15
16 while(1)
17 {
18 printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
19 printf("\nEnter your choice\n");
20 scanf("%d",&iChoice);
21
22 switch(iChoice)
23 {
24 case 1:
25 fnPush();
26 break;
27 case 2:
28 fnPop();
29 break;
30 case 3:
31 fnDisplay();
32 break;
33 case 4:
34 exit(0);
35 }
36 }
37 return 0;
38 }
39
40 void fnPush()
41 {
17
CHAPTER 6. STACK
42 int iVal;
43 if(iTop == MAX_SIZE - 1)
44 {
45 printf("\nStack Overflow!!\n");
46 }
47 else
48 {
49 printf("\nEnter the element to be pushed on to the stack : ");
50 scanf("%d", &iVal);
51 iTop++;
52 iaStack[iTop] = iVal;
53 }
54 }
55
56 void fnPop()
57 {
58 int iVal;
59 if(iTop == -1)
60 {
61 printf("Stack Underflow!!\n");
62 }
63 else
64 {
65 iVal = iaStack[iTop];
66 iTop--;
67 printf("\nElement popped from the stack is %d\n", iVal);
68 }
69 }
70
71 void fnDisplay()
72 {
73 if(iTop == -1)
74 {
75 printf("\nStack is Empty\n");
76 }
77 else
78 {
79 printf("\nStack Contents: \n");
80 for(int i=iTop;i>=0;i--)
81 {
82 printf("\n%d", iaStack[i]);
83 }
84 }
85 printf("\n");
86 }
Listing 6.1: A06StackOperations.c
Output
=================================
1 /***************************************
2
3 1.Push
4 2.Pop
5 3.Display
6 4.Exit
7
12 1.Push
13 2.Pop
14 3.Display
15 4.Exit
16
20 Stack is Empty
21
22
23 1.Push
24 2.Pop
25 3.Display
26 4.Exit
27
33 1.Push
34 2.Pop
35 3.Display
36 4.Exit
37
43 1.Push
44 2.Pop
45 3.Display
46 4.Exit
47
53 1.Push
54 2.Pop
55 3.Display
56 4.Exit
57
61 Stack Contents:
62
63 30
64 20
65 10
66
67 1.Push
68 2.Pop
69 3.Display
70 4.Exit
71
77 1.Push
78 2.Pop
79 3.Display
80 4.Exit
81
85 ***************************************/
Listing 6.2: OUTPUT
Path Matrix
Question
Write a C program to generate and Display Path matrix of a graph whose adjacency matrix is
given as input.
C Code
=================================
1 #include <stdio.h>
2
23 int main()
24 {
25 int i, j, numVert;
26 int graph[MAX][MAX];
27
28
37 WarshallTransitiveClosure(graph, numVert);
38
21
CHAPTER 7. PATH MATRIX
49 return 0;
50 }
Listing 7.1: A07PathMatrix.c
Output
1 /***************************************
2 Enter the number of vertices : 5
3 Enter the adjacency matrix :-
4 1 1 0 1 0
5 0 0 1 0 0
6 0 0 0 0 1
7 0 0 1 0 0
8 0 0 0 0 0
9
17 ***************************************/
Listing 7.2: OUTPUT
Question
Write a C program to create and traverse a binary search tree using various tree traversals.
C Code
1 /***************************************************************************
2 *File : 10_BST.c
3 *Description: Binary Search Tree Operations
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 28 September 2023
7 ***************************************************************************/
8
9 #include<stdio.h>
10 #include<stdlib.h>
11
12 struct node
13 {
14 int info;
15 struct node *lbranch;
16 struct node *rbranch;
17 };
18 typedef struct node* NODEPTR;
19
20 /* FUNCTION PROTOTYPES */
21 NODEPTR fnGetNode(void);
22 void fnFreeNode(NODEPTR x);
23 NODEPTR fnInsertNode(int, NODEPTR);
24 void fnInOrder(NODEPTR);
25 void fnPreOrder(NODEPTR);
26 void fnPostOrder(NODEPTR);
27
28 int main()
29 {
30 NODEPTR root = NULL;
31 int iChoice, iItem, i, iNum;
32
33 for(;;)
34 {
35 printf("\n1.Insert\n2.Inorder traversal\n3.Preorder traversal");
36 printf("\n4.Postorder traversal\n5.Exit\n");
37 printf("\nEnter your choice : ");
38 scanf("%d",&iChoice);
39
40 switch(iChoice)
23
CHAPTER 8. BINARY SEARCH TREE
41 {
42 case 1: printf("\nEnter the element : ");
43 scanf("%d", &iItem);
44 root = fnInsertNode(iItem,root);
45 break;
46
83 case 5: exit(0);
84
88 }
89
90 }
91 return 0;
92 }
93
94 NODEPTR fnGetNode(void)
95 {
96 NODEPTR x;
97 x = ( NODEPTR ) malloc (sizeof(struct node));
98 if(x == NULL)
99 {
100 printf("\nOut of Memory");
101 exit(0);
102 }
103 return x;
104 }
105
147 }
148
165 fnInOrder(root->rbranch);
166 }
167 }
168
Output
=================================
1 /***************************************
2 1.Insert
3 2.Inorder traversal
4 3.Preorder traversal
5 4.Postorder traversal
6 5.Exit
7
12 1.Insert
13 2.Inorder traversal
14 3.Preorder traversal
15 4.Postorder traversal
16 5.Exit
17
22 1.Insert
23 2.Inorder traversal
24 3.Preorder traversal
25 4.Postorder traversal
26 5.Exit
27
32 1.Insert
33 2.Inorder traversal
34 3.Preorder traversal
35 4.Postorder traversal
36 5.Exit
37
42 1.Insert
43 2.Inorder traversal
44 3.Preorder traversal
45 4.Postorder traversal
46 5.Exit
47
50 Inorder Traversal is :
51 5 10 20 30
52
53 1.Insert
54 2.Inorder traversal
55 3.Preorder traversal
56 4.Postorder traversal
57 5.Exit
58
61 Preorder Traversal is :
62 20 10 5 30
63
64 1.Insert
65 2.Inorder traversal
66 3.Preorder traversal
67 4.Postorder traversal
68 5.Exit
69
72 Postorder Traversal is :
73 5 10 30 20
74
75 1.Insert
76 2.Inorder traversal
77 3.Preorder traversal
78 4.Postorder traversal
79 5.Exit
80
28
Chapter 9
Array Operations
Question
Write a C program to demonstrate the following operations in an array
insertion of an element
deletion of an element
traversal
C Code
1 #include<stdio.h>
2 #include<stdlib.h>
3 #define LENGTH 50
4
5 int main()
6 {
7 int i, iaList[LENGTH], iNum, iIndex, iVal, iChoice;
8
16
17 while(1)
18 {
19 printf("\nArray Operations : \n=====================\n1.Traversal\n2.Insert\
n3.Delete\n4.Exit\n");
20 printf("\nWhich operation do you want to perform?\n");
21 scanf("%d",&iChoice);
22
23 switch(iChoice)
24 {
25 case 1: /* Array Traversal*/
26 printf("\nArray Traversal to display array contents :\n");
27 for(i=0;i<iNum;i++)
28 {
29 printf("\n%d", iaList[i]);
30 }
31 printf("\n");
32
33 break;
34 case 2:
29
CHAPTER 9. ARRAY OPERATIONS
58 case 3:
59 printf("\nArray Operation: Deletion of an element:\n");
60 printf("\nEnter the index where element is to be deleted : ");
61 scanf("%d",&iIndex);
62 if(iIndex >=iNum)
63 {
64 printf("\nInvalid Index\n");
65 break;
66 }
67
68 iVal = iaList[iIndex];
69 /*Shift elements starting from the element after the given index
till the end one position left*/
70 for(i=iIndex; i<iNum; i++)
71 {
72 iaList[i] = iaList[i+1];
73 }
74 iNum = iNum - 1; //update count of elements in array
75 printf("\nThe deleted element is %d at index %d\n", iVal, iIndex);
76 /* Display Contents of Array*/
77 printf("\nThe contents of the array is :\n");
78 for(i=0;i<iNum;i++)
79 {
80 printf("\n%d", iaList[i]);
81 }
82 printf("\n");
83 break;
84 case 4:
85 exit(0);
86 }
87 }
88 return 0;
89 }
Listing 9.1: B01ArrayOperations.c
Output
=================================
1 /***************************************
2 Enter the number of elements
3 5
4
8 Array Operations :
9 =====================
10 1.Traversal
11 2.Insert
12 3.Delete
13 4.Exit
14
20 4
21 2
22 6
23 8
24 1
25
26 Array Operations :
27 =====================
28 1.Traversal
29 2.Insert
30 3.Delete
31 4.Exit
32
44 4
45 2
46 9
47 6
48 8
49 1
50
51 Array Operations :
52 =====================
53 1.Traversal
54 2.Insert
55 3.Delete
56 4.Exit
57
65 Invalid Index
66
67 Array Operations :
68 =====================
69 1.Traversal
70 2.Insert
71 3.Delete
72 4.Exit
73
85 4
86 2
87 9
88 6
89 1
90
91 Array Operations :
92 =====================
93 1.Traversal
94 2.Insert
95 3.Delete
96 4.Exit
97
101 ***************************************/
Listing 9.2: OUTPUT
Bubble Sort
Question
Write a C program to sort a list of elements using Bubble sort algorithm.
C Code
1 /***************************************************************************
2 *File : B02BubbleSort.c
3 *Description : Program to implement Bubble Sort Algorithm
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 24.04
6 *Date : 7 November 2024
7 ***************************************************************************/
8
9 #include<stdio.h>
10 #include<stdlib.h>
11
12 /***************************************************************************
13 *Function : main
14 *Input parameters: none
15 *RETURNS : 0 on success
16 ***************************************************************************/
17 #include<stdio.h>
18 #include<stdlib.h>
19 int main(void)
20 {
21 int iNum, i, j, k, iaArr[10], iTemp;
22
23 printf("\n*************************************************");
24 printf("\n*\tPROGRAM TO IMPLEMENT BUBBLE SORT\t*\n");
25 printf("*************************************************");
26
27 printf("\nEnter no of elements\n");
28 scanf("%d",&iNum);
29
33
CHAPTER 10. BUBBLE SORT
41 for(i=0;i<iNum-1;i++)
42 {
43 for(j=0;j<iNum-i-1;j++)
44 {
45 printf("\n\nIteration i=%d, j=%d\t now comparing %d and %d\n",i,j, iaArr
[j], iaArr[j+1]);
46 if(iaArr[j] > iaArr[j+1])
47 {
48 iTemp = iaArr[j];
49 iaArr[j] = iaArr[j+1];
50 iaArr[j+1] = iTemp;
51 }
52 for(k=0;k<iNum;k++)
53 {
54 printf("%d\t",iaArr[k]);
55 }
56 getchar();
57 }
58 }
59 printf("\nThe Sorted array is \n");
60 for(i=0;i<iNum;i++)
61 printf("%d\t",iaArr[i]);
62
63 printf("\n");
64 return 0;
65 }
Listing 10.1: B02BubbleSort.c
Output
=================================
1 /***************************************
2 *************************************************
3 * PROGRAM TO IMPLEMENT BUBBLE SORT *
4 *************************************************
5 Enter no of elements
6 5
7
15
19
23
27
31
35
39
43
47
Insertion Sort
Question
Write a C program to sort a list of N elements using Insertion Sort Algorithm.
C Code
1 /***************************************************************************
2 *File : B03InsertionSort.c
3 *Description : Program to implement Insertion Sort Algorithm
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 24.04
6 *Date : 7 November 2024
7 ***************************************************************************/
8
9 #include<stdio.h>
10 #include<stdlib.h>
11
12 /***************************************************************************
13 *Function : main
14 *Input parameters: none
15 *RETURNS : 0 on success
16 ***************************************************************************/
17
18 int main(void)
19 {
20 int iNum, i, j, k, iaArr[10], iTemp,iKey;
21
22
23 printf("\n*************************************************");
24 printf("\n*\tPROGRAM TO IMPLEMENT INSERTION SORT\t*\n");
25 printf("*************************************************");
26
27 printf("\nEnter no of elements\n");
28 scanf("%d",&iNum);
29
36
CHAPTER 11. INSERTION SORT
41 j--;
42 }
43 iaArr[j+1] = iKey;
44
52 }
53
56 for(i=0;i<iNum;i++)
57 printf("%d\t",iaArr[i]);
58
59 printf("\n");
60 return 0;
61 }
Listing 11.1: B03InsertionSort.c
Output
=================================
1
2 /***************************************
3 *************************************************
4 * PROGRAM TO IMPLEMENT INSERTION SORT *
5 *************************************************
6 Enter no of elements
7 5
8
Linear Queue
Question
Write a C program to simulate the working of Linear Queue using arrays.
C Code
1 #include <stdio.h>
2 #include <stdlib.h>
3
5 #define QUEUE_SIZE 5
6 int myQueue[QUEUE_SIZE];
7 int iFront = 0, iRear = -1;
8
9 void fnInsertRear();
10 void fnDeleteFront();
11 void fnDisplay();
12
13 int main()
14 {
15 int iElem, iChoice;
16
17 for(;;)
18 {
19 printf("\nQueue Operations\n");
20 printf("=====================");
21 printf("\n1.Qinsert\n2.Qdelete\n3.Qdisplay\n4.Exit\n");
22 printf("Enter your choice\n");
23 scanf("%d",&iChoice);
24 switch(iChoice)
25 {
26 case 1: if(iRear == QUEUE_SIZE-1)
27 {
28 printf("\nQueue is Full\n");
29 }
30 else
31 {
32 fnInsertRear();
33 }
34 break;
35 case 2: if(iRear == iFront-1)
36 {
37 printf("\nQueue is Empty\n");
38 }
39 else
40 {
38
CHAPTER 12. LINEAR QUEUE
41 fnDeleteFront();
42 }
43 break;
44 case 3: if(iRear == iFront-1)
45 {
46 printf("\nQueue is Empty\n");
47 }
48 else
49 {
50 printf("\nContents of the Queue is \n");
51 fnDisplay();
52 }
53 break;
54
55 case 4: exit(0);
56
63 void fnInsertRear()
64 {
65 int iVal;
66 printf("\nEnter the element to be inserted : ");
67 scanf("%d", &iVal);
68 iRear++;
69 myQueue[iRear] = iVal;
70 }
71
72 void fnDeleteFront()
73 {
74 int iElem;
75 iElem = myQueue[iFront];
76
77 if(iFront == iRear)
78 {
79 iFront = 0;
80 iRear = -1;
81 }
82 else
83 {
84 iFront++;
85 }
86 printf("\nElement deleted is %d\n", iElem);
87 }
88
89 void fnDisplay()
90 {
91 int i;
92 for(i=iFront; i<=iRear; i++)
93 {
94 printf("%d\t", myQueue[i]);
95 }
96 printf("\n");
97 }
Listing 12.1: B04LinearQueue.c
Output
=================================
1 Queue Operations
2 =====================
3 1.Qinsert
4 2.Qdelete
5 3.Qdisplay
6 4.Exit
7 Enter your choice
8 2
9
10 Queue is Empty
11
12 Queue Operations
13 =====================
14 1.Qinsert
15 2.Qdelete
16 3.Qdisplay
17 4.Exit
18 Enter your choice
19 3
20
21 Queue is Empty
22
23 Queue Operations
24 =====================
25 1.Qinsert
26 2.Qdelete
27 3.Qdisplay
28 4.Exit
29 Enter your choice
30 1
31
34 Queue Operations
35 =====================
36 1.Qinsert
37 2.Qdelete
38 3.Qdisplay
39 4.Exit
40 Enter your choice
41 1
42
45 Queue Operations
46 /***************************************
47 =====================
48 1.Qinsert
49 2.Qdelete
50 3.Qdisplay
51 4.Exit
52 Enter your choice
53 1
54
57 Queue Operations
58 =====================
59 1.Qinsert
60 2.Qdelete
61 3.Qdisplay
62 4.Exit
69 Queue Operations
70 =====================
71 1.Qinsert
72 2.Qdelete
73 3.Qdisplay
74 4.Exit
75 Enter your choice
76 2
77
78 Element deleted is 1
79
80 Queue Operations
81 =====================
82 1.Qinsert
83 2.Qdelete
84 3.Qdisplay
85 4.Exit
86 Enter your choice
87 3
88
92 Queue Operations
93 =====================
94 1.Qinsert
95 2.Qdelete
96 3.Qdisplay
97 4.Exit
98 Enter your choice
99 1
100
Circular Queue
Question
Write a C program to implement a circular queue of integers.
C Code
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdbool.h>
5
6 #define QUEUE_SIZE 5
7
8 int myQueue[QUEUE_SIZE];
9 int iFront = -1, iRear = -1;
10
11 void fnInsertRear();
12 void fnDeleteFront();
13 void fnDisplay();
14
15 int main()
16 {
17
18 int iChoice;
19
20 for(;;)
21 {
22 printf("\nQueue Operations\n");
23 printf("=====================");
24 printf("\n1.Qinsert\n2.Qdelete\n3.Qdisplay\n4.Exit\n");
25 printf("Enter your choice\n");
26 scanf("%d",&iChoice);
27
28 switch(iChoice)
29 {
30 case 1: if((iRear + 1) % QUEUE_SIZE == iFront)
31 {
32 printf("\nQueue is Full\n");
33 }
34 else
35 {
36 fnInsertRear();
37 }
38
39 break;
40 case 2: if(iFront == -1)
42
CHAPTER 13. CIRCULAR QUEUE
41 {
42 printf("\nQueue is Empty\n");
43 }
44 else
45 {
46 fnDeleteFront();
47 }
48
49 break;
50 case 3: if(iFront == -1)
51 {
52 printf("\nQueue is Empty\n");
53 }
54 else
55 {
56 printf("\nContents of the Queue is \n");
57 fnDisplay();
58 }
59
60 break;
61
62 case 4: exit(0);
63
70
71 void fnInsertRear()
72 {
73 int iVal;
74 printf("\nEnter the element : ");
75 scanf("%d", &iVal);
76 if(iRear == -1)
77 {
78 iFront++;
79 iRear++;
80 }
81 else
82 iRear = (iRear + 1)%QUEUE_SIZE;
83
84 myQueue[iRear] = iVal;
85 }
86
87 void fnDeleteFront()
88 {
89 int iElem;
90 iElem = myQueue[iFront];
91
92 if(iFront == iRear)
93 {
94 iFront = -1;
95 iRear = -1;
96 }
97 else
98 {
99 iFront = (iFront + 1)%QUEUE_SIZE;
100 }
101 printf("\nDeleted element is %d\n", iElem);
102 }
103
Output
=================================
1 /***************************************
2 Queue Operations
3 =====================
4 1.Qinsert
5 2.Qdelete
6 3.Qdisplay
7 4.Exit
8 Enter your choice
9 2
10
11 Queue is Empty
12
13 Queue Operations
14 =====================
15 1.Qinsert
16 2.Qdelete
17 3.Qdisplay
18 4.Exit
19 Enter your choice
20 3
21
22 Queue is Empty
23
24 Queue Operations
25 =====================
26 1.Qinsert
27 2.Qdelete
28 3.Qdisplay
29 4.Exit
30 Enter your choice
31 1
32
35 Queue Operations
36 =====================
37 1.Qinsert
38 2.Qdelete
39 3.Qdisplay
40 4.Exit
41 Enter your choice
42 1
43
46 Queue Operations
47 =====================
48 1.Qinsert
49 2.Qdelete
50 3.Qdisplay
51 4.Exit
52 Enter your choice
53 3
54
58 Queue Operations
59 =====================
60 1.Qinsert
61 2.Qdelete
62 3.Qdisplay
63 4.Exit
64 Enter your choice
65 1
66
69 Queue Operations
70 =====================
71 1.Qinsert
72 2.Qdelete
73 3.Qdisplay
74 4.Exit
75 Enter your choice
76 3
77
81 Queue Operations
82 =====================
83 1.Qinsert
84 2.Qdelete
85 3.Qdisplay
86 4.Exit
87 Enter your choice
88 2
89
90 Deleted element is 2
91
92 Queue Operations
93 =====================
94 1.Qinsert
95 2.Qdelete
96 3.Qdisplay
97 4.Exit
98 Enter your choice
99 3
100
113 ***************************************/
Listing 13.2: OUTPUT
Question
Write a C program to generate and Display Shortest Path matrix of a graph whose cost adjacency
matrix is given as input.
C Code
1
3 #include <stdio.h>
4 #include <stdlib.h>
5 /******************************************************************************
6 *Function : main
7 *Input parameters: no parameters
8 *RETURNS : 0 on success
9 ******************************************************************************/
10 int main(void)
11 {
12 int iN, i, j, k;
13 int iaFloyd[10][10], iaCost[10][10];
14 printf("\n*********************************************************");
15 printf("\n*\tPROGRAM TO IMPLEMENT FLOYD’S ALGORITHM\t*\n");
16 printf("*********************************************************");
17 printf("\nEnter the number of vertices\n");
18 scanf("%d",&iN);
19 printf("\nEnter the Cost adjacency Matrix\n");
20 for(i=0;i<iN;i++)
21 for(j=0;j<iN;j++)
22 scanf("%d",&iaCost[i][j]);
23 printf("\nInput Graph\n");
24 for(i=0;i<iN;i++)
25 {
26 for(j=0;j<iN;j++)
27 {
28 printf("%d\t",iaCost[i][j]);
29 }
30 printf("\n");
31 }
32 printf("\n");
33
34 for(i=0;i<iN;i++)
35 {
36 for(j=0;j<iN;j++)
37 {
38 iaFloyd[i][j] = iaCost[i][j];
39 }
47
CHAPTER 14. SHORTEST PATH MATRIX
40 }
41
42 for(k=0;k<iN;k++)
43 {
44 for(i=0;i<iN;i++)
45 {
46 for(j=0;j<iN;j++)
47 {
48 if(iaFloyd[i][j] > (iaFloyd[i][k] + iaFloyd[k][j]))
49 iaFloyd[i][j] = (iaFloyd[i][k] + iaFloyd[k][j]);
50 }
51 }
52 }
53 printf("\nAll Pair Shortest Path Matrix\n");
54 for(i=0;i<iN;i++)
55 {
56 for(j=0;j<iN;j++)
57 {
58 printf("%d\t",iaFloyd[i][j]);
59 }
60 printf("\n");
61 }
62 printf("\n");
63 return 0;
64 }
Listing 14.1: B06ShortestPathMatrix.c
Output
=================================
1 /***************************************
2
3 *********************************************************
4 * PROGRAM TO IMPLEMENT FLOYD’S ALGORITHM *
5 *********************************************************
6 Enter the number of vertices
7 4
8
15 Input Graph
16 0 9999 3 9999
17 2 0 9999 9999
18 9999 7 0 1
19 6 9999 9999 0
20
21
28
29 ***************************************/
Listing 14.2: OUTPUT
Question
Write a C program to create a directed graph using Adjacency Matrix.
C Code
1 #include <stdio.h>
2
3 int main()
4 {
5 int iaGraph[10][10], iNum, i, j;
6
10 printf("\nEnter the edges of the graph (1 if edge exists and 0 otherwise) : \n")
;
11 for(i=0; i<iNum; i++)
12 {
13 for(j=0; j<iNum; j++)
14 {
15 printf("\nEdge b/w vertex %d and %d : ", i+1, j+1);
16 scanf("%d", &iaGraph[i][j]);
17 }
18 }
19
50
CHAPTER 15. GRAPH USING ADJACENCY MATRIX
Output
=================================
1 /***************************************
2 Enter the number of Vertices : 5
3