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

Fundamentals of C Lab Record New MANUAL2

The document is a lab manual for a course on fundamentals of data structures in C. It outlines 12 experiments involving implementing various data structures and algorithms in C, including arrays, strings, structures, pointers, stacks, queues, trees, sorting, and hashing. The objectives are to understand and implement basic and advanced data structures in C, apply them to problem solving, and learn functions and recursion through data structures.

Uploaded by

Sridharan D
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Fundamentals of C Lab Record New MANUAL2

The document is a lab manual for a course on fundamentals of data structures in C. It outlines 12 experiments involving implementing various data structures and algorithms in C, including arrays, strings, structures, pointers, stacks, queues, trees, sorting, and hashing. The objectives are to understand and implement basic and advanced data structures in C, apply them to problem solving, and learn functions and recursion through data structures.

Uploaded by

Sridharan D
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

DEPARTMENT OF

BIOMEDICAL ENGINEERING

LAB MANUAL

EC8381 - Fundamentals of Data Structures in C


Laboratory
EC8381 Fundamentals of Data Structures in C Laboratory
OBJECTIVES:
 To understand and implement basic data structures using C
 To apply linear and non-linear data structures in problem solving.
 To learn to implement functions and recursive functions by means of data
structures
 To implement searching and sorting algorithms
LIST OF EXERCISES
1. Basic C Programs –looping, data manipulations, arrays
2. Programs using strings –string function implementation
3. Programs using structures and pointers
4. Programs involving dynamic memory allocations
5. Array implementation of stacks and queues
6. Linked list implementation of stacks and queues
7. Application of Stacks and Queues
8. Implementation of Trees, Tree Traversals
9. Implementation of Binary Search trees
10. Implementation of Linear search and binary search
11. Implementation Insertion sort, Bubble sort, Quick sort and Merge Sort
12. Implementation Hash functions, collision resolution technique
TOTAL: 60 PERIODS
OUTCOMES:
Upon completion of the course, the students will be able to:
 Write basic and advanced programs in C
 Implement functions and recursive functions in C
 Implement data structures using C
 Choose appropriate sorting algorithm for an application and implement it in a
modularized way
INDEX
EXP.NO TOPIC
1 BASIC C PROGRAMS – LOOPING (WHILE LOOP)
2 BASIC C PROGRAMS – LOOPING(DO WHILE LOOP)
3 SUM OF DIGITS OF A NUMBER
4 FINDING AVERAGE OF ARRAY ELEMENTS
5 CALCULATE LENGTH OF STRING
6 C STRING FUNCTION – STRLEN
7 C STRING FUNCTION – STRCMP
8 C STRING FUNCTION – STRCAT
9 ONE DIMENSIONAL ARRAY IN C
10 TWO DIMENSIONAL ARRAY IN C
11 DYNAMIC MEMORY ALLOCATIONS
12 STRUCTURES AND POINTERS
13 IMPLEMENTATION OF STACK ADT USING ARRAY IN C
14 IMPLEMENTATION OF QUEUE ADT USING ARRAY IN C
15 IMPLEMENTATION OF STACK USING LINKED LIST
16 IMPLEMENTATION OF QUEUE USING LINKED LIST
17 EVALUATE POSTFIX EXPRESSION USING STACK
18 IMPLEMENT PRIORITY QUEUE TO ADD AND DELETE ELEMENTS
19 BINARY SEARCH TREE
20 IMPLEMENTATION OF TREE
21 TREE TRAVERSALS (INORDER, PREORDER AND POSTORDER)
22 BINARY SEARCH TREE OPERATIONS
23 LINEAR SEARCH
24 BINARY SEARCH
25 INSERTION SORT
26 BUBBLE SORT
27 QUICK SORT
28 MERGE SORT
29 IMPLEMENTATION OF HASHING
30 COLLISION RESOLUTION IN HASH TABLE
Exp 1 BASIC C PROGRAMS – LOOPING (WHILE LOOP)

Aim:
To write a C program using basic while loop.

Program (while loop):

#include <stdio.h>
int main()
{
int i=3;
while(i<10)
{
printf("%d\n",i);
i++;
}
}

Output:
3456789

Result:
Thus the C program for basic while loop has been verified and executed successfully.
Exp 2 BASIC C PROGRAMS – LOOPING(DO WHILE LOOP)

Aim:
To write a C program using basic do while loop.

Program (do while loop):


#include <stdio.h>
int main()
{
int i=1;
do
{
printf("Value of i is %d\n",i);
i++;
}while(i<=4&&i>=2);

Output:
Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4

Result:
Thus the C program for basic do while loop has been verified and executed successfully.
Exp 3
SUM OF DIGITS OF A NUMBER

Aim:
To write a C program for sum of the digits of a number.

Program :
#include<stdio.h>
int main()
{
int num,n;
int sum=0;
printf("\nEnter an integer value:");
scanf("%d",&num);
n=num;
while(n!=0)
{
sum=sum+n%10;n=n/10;
}
printf("\nThe sum of digitsof%dis%d",num,sum);
}

Output:
Enter an integer value:542

The sum of digits of 542 is 11

Result:
Thus the C program for sum of the digits of a number has been verified and executed
successfully.
Exp 4 FINDING AVERAGE OF ARRAY ELEMENTS

Aim:
To write a C program for finding average of array elements.

Program :
#include<stdio.h>
int main()
{
int a[10],sum,i;
float avg;
printf("Entertenintegers:");
for(i=0;i<=9;i++)
scanf(“%d",&a[i]);
sum=0;
for(i=0;i<=9;i++)
sum=sum+a[i];
avg=sum/10;
printf("\nAverage=%f",avg);
}

Output:
Enter ten integers:10
20
30
40
50
60
70
80
90
100

Average=55.000000

Result:
Thus the C program for using basic do while loop has been verified and executed successfully
Exp 5 CALCULATE LENGTH OF STRING

Aim:
To write a C program to calculate length of string.

Program :
#include<stdio.h>
int main()
{
char s[1000],i;
printf("Enter a string: ");
scanf("%s", s);
for(i=0; s[i]!='\0';++i);
printf("Length of string: %d",i);
return 0;
}

Output
Enter a string: Programiz
Length of string: 9

Result:

Thus the C program to calculate length of string has been verified and executed successfully
Exp 6 C STRING FUNCTION – STRLEN

Aim:
To write a C program for string function – strlen.

Program :
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20]="BeginnersBook";
printf("Length of string str1: %d",strlen(str1));
return 0;
}

Output:
Length of string str1:13

Result:
Thus the C program to string function – strlen has been verified and executed successfully
Exp 7 C STRING FUNCTION – STRCMP

Aim:
To write a C program for string function – strcmp

Program :
#include<stdio.h>
#include<string.h>
int main()
{
char s1[20]="BeginnersBook";
char s2[20]="BeginnersBook.COM";
if(strcmp(s1, s2)==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
return 0;
}

Output:
String 1and 2 are different

Result:
Thus the C program for string function – strcmp has been verified and executed
successfully.
Exp 8 C STRING FUNCTION – STRCAT

Aim:
To write a C program for string function – strcat

Program :
#include<stdio.h>
#include<string.h>
int main()
{
char s1[10]="Hello";
char s2[10]="World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
return 0;
}

Output:
Output string after concatenation: HelloWorld

Result:
Thus the C program for string function strcat has been verified and executed successfully.
Exp 9 ONE DIMENSIONAL ARRAY IN C

Aim:
To write a C program for one dimensional array in c

Program:
#include<stdio.h>
int main()
{
int arr[5], i;
for(i = 0; i < 5; i++)
{
printf("Enter a[%d]: ", i);
scanf("%d", &arr[i]);
}
printf("\nPrinting elements of the array: \n\n");
for(i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}

Output:
Enter a[0]: 10
Enter a[1]: 20
Enter a[2]: 30
Enter a[3]: 40
Enter a[4]: 50

Printing elements of the array:

10 20 30 40 50

Result:
Thus the C program for one dimensional array in c has been verified and executed
successfully.
Exp 10 TWO DIMENSIONAL ARRAY IN C

Aim:
To write a C program for two dimensional array in c

Program:
#include<stdio.h>
int main()
{
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}

OUTPUT:
Enter value for disp[0][0]:10
Enter value for disp[0][1]:20
Enter value for disp[0][2]:30
Enter value for disp[1][0]:40
Enter value for disp[1][1]:50
Enter value for disp[1][2]:60
Two Dimensional array elements:
10 20 30
40 50 60

Result:
Thus the C program for two dimensional array in c has been verified and executed
successfully.
Exp 11 DYNAMIC MEMORY ALLOCATIONS

Aim:
To write a C program for dynamic memory allocations

Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) calloc(num, sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}

Output:
Enter number of elements: 5
Enter elements of array: 10
20
30
40
50
Sum = 150

Result:
Thus the C program for dynamic memory allocations has been verified and executed
successfully.
Exp 12 STRUCTURES AND POINTERS

Aim:
To write a C program for structures and pointers

Program:
#include<stdio.h>
#include<stdlib.h>
struct person
{
int age;
float weight;
char name[30];
};
int main()
{
struct person *ptr;
int i, num;
printf("Enter number of persons: ");
scanf("%d", &num);
ptr = (struct person*) malloc(num * sizeof(struct person));
for(i = 0; i < num; ++i)
{
printf("Enter name, age and weight of the person respectively:\n");
scanf("%s%d%f", &(ptr+i)->name, &(ptr+i)->age, &(ptr+i)->weight);
}
printf("Displaying Infromation:\n");
for(i = 0; i < num; ++i)
printf("%s\t%d\t%.2f\n", (ptr+i)->name, (ptr+i)->age, (ptr+i)->weight);
return 0;
}

Output:
Enter number of persons: 2
Enter name, age and weight of the person respectively:
Adam
2
3.2
Enter name, age and weight of the person respectively:
Eve
6
2.3
Displaying Information:
Adam 2 3.20
Eve 6 2.30

Result:

Thus the C program for structures and pointers has been verified and executed successfully.

Exp 13 IMPLEMENTATION OF STACK ADT USING ARRAY IN C

Aim:
To write a C program for implementation of stack adt using array in c

Program:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
OUTPUT:
Enter the size of STACK[MAX=100]:10

STACK OPERATIONS USING ARRAY


--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:10

Enter the Choice:1


Enter a value to be pushed:20

Enter the Choice:1


Enter a value to be pushed:30

Enter the Choice:1


Enter a value to be pushed:40

Enter the Choice:1


Enter a value to be pushed:50

Enter the Choice:3

The elements in STACK

50
40
30
20
10
Press Next Choice
Enter the Choice:2

The popped elements is 50


Enter the Choice:2

The popped elements is 40


Enter the Choice:3

The elements in STACK

30
20
10
Press Next Choice
Enter the Choice:4

EXIT POINT
Result:
Thus the C program for implementation of stack adt using array has been verified and
executed successfully.

Exp 14 IMPLEMENTATION OF QUEUE ADT USING ARRAY IN C

Aim:
To write a C program for implementation of queue adt using array

Program:

#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
}
OUTPUT:
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:10
Enter the Choice:1
Enter no 2:54
Enter the Choice:1
Enter no 3:98
Enter the Choice:1
Enter no 4:234
Enter the Choice:3
Queue Elements are:
10
54
98
234
Enter the Choice:2
Deleted Element is 10
Enter the Choice:3
Queue Elements are:
54
98
234
Enter the Choice:4

Result:
Thus the C program for implementation of queue adt using array has been verified and
executed successfully.

Exp 15 IMPLEMENTATION OF STACK USING LINKED LIST

Aim:
To write a C program for implementation of stack using linked list

Program:
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *link;
}stk;
int main()
{
int ch,item;
stk *top=NULL,*ptr,*temp;
while(1)
{
printf("\n\t\t Stack Operations");
printf("\n\t\t***************");
printf("\n\t\t 1.Push\n\t\t 2.Pop\n\t\t 3.View\n\t\t 4.Exit");
printf("\n\t\t Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\t\t Push");
printf("\n\t\t*****");
printf("\n\t\t Enter the value : ");
scanf("%d",&item);
temp=(stk*)malloc(sizeof(stk));
temp->data=item;
temp->link=top;
top=temp;
top=temp;

printf("\n\t\t Pushed element is %d ",top->data);


break;
case 2:printf("\n\t\t Pop");
printf("\n\t\t****");
if(top == NULL)
{
printf("\n\t\t Stack is empty." );
break;
}
printf("\n\t\t Popped element is %d",top->data);
top=temp->link;
break;
case 3:printf("\n\t\t View");
printf("\n\t\t*****");
if(top==NULL)
{
printf("\n\t\t Stack is empty");
break;
}
ptr=top;
printf("\n\n\t\t ");
while(ptr!=NULL)
{
printf("%d->",ptr->data);
ptr=ptr->link;
}
break;
case 4:printf("\n\t\t End of program.");
exit(0);
default:printf("\n\t\t Enter no between 1 to 4.");
break;
}
}
}

Output:
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 1
Push
*****
Enter the value : 5
Pushed element is 5
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 1
Push
******
Enter the value : 4
Pushed element is 4
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 1
Push
*****
Enter the value : 3
Pushed element is 3
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 3
View
******
3->4->5->
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 2
Pop
*****
Popped element is 3
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 3
View
******
4->5->
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 4
End of program.

Result:
Thus the C program for implementation of stack using linked list has been verified and
executed successfully.

Exp 16 IMPLEMENTATION OF QUEUE USING LINKED LIST

Aim:
To write a C program for implementation of queue using linked list

Program:
#include<stdio.h>
#include<malloc.h>
struct queue
{
int data;
struct queue *link;
};
int main()
{
int i,ch,x;
struct queue *temp,*ptr,*fr,*p,*re=NULL;
printf("\n\t\t Enter the value : ");
scanf("%d",&x);
temp=(struct queue *)malloc(sizeof(struct queue));
temp->data=x;
temp->link=NULL;
re=temp;
ptr=temp;
fr=temp;
while(1)
{
printf("\n\t\t Queue Operations");
printf("\n\t\t***************");
printf("\n\t\t 1.Enqueue\n\t\t 2.Dequeue\n\t\t 3.View\n\t\t 4.Exit");
printf("\n\t\t Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\t\t Enqueue Operations");
printf("\n\t\t****************");
if(re==NULL)
printf("\n\t\t Empty Queue");
else
{
printf("\n\t\t Enter the value : ");
scanf("%d",&x);
temp=(struct queue*)malloc(sizeof(struct queue));
temp->data=x;
re->link=temp;
temp->link=NULL;
re=temp;
}
break;
case 2:printf("\n\t\t Dequeue Operations");
printf("\n\t\t****************");
if(fr==NULL)
printf("\n\t\t Empty Queue");
else
{
p=fr;
printf("\n\t\t Dequeued element is %d",p->data);
fr=fr->link;
free(p);
}
break;
case 3:printf("\n\t\t Content of Queue");
printf("\n\t\t***************\n\t\t");
if(fr==NULL)
printf("\n\t\t Empty Queue");
else
{
ptr=fr;
while(ptr->link!=NULL)
{
printf("%d->",ptr->data);
ptr=ptr->link;
}
printf("%d",ptr->data);
}
break;
case 4:printf("\n\t\t End of program");
exit(0);
default:printf("\n\t\t Valid choice is 1 to 4");
}
}
}
Output:
Enter the value : 5

Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 1

Enqueue Operations
*****************
Enter the value : 4

Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 1

Enqueue Operations
*****************
Enter the value : 3

Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 3

Content of Queue
***************

5->4->3
Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 2

Dequeue Operations
*****************
Dequeued element is 5

Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 3

Content of Queue
***************

4->3

Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 4

End of program

Result:
Thus the C program for implementation of queue using linked list has been verified and
executed successfully.

Exp 17 EVALUATE POSTFIX EXPRESSION USING STACK

Aim:
To write a C program to evaluate postfix expression using stack

Program:
#include<stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}

OUTPUT:
Enter the expression :: 245+*

The result of expression 245+* = 18


Result:

Thus the C program to evaluate postfix expression using stack has been verified and
executed successfully.

Exp 18 IMPLEMENT PRIORITY QUEUE TO ADD AND DELETE


ELEMENTS

Aim:
To write a C program for implement priority queue to add and delete elements

Program:
#include <stdio.h>
#include <stdlib.h>

#define MAX 5

void insert_by_priority(int);
void delete_by_priority(int);
void create();
void check(int);
void display_pqueue();

int pri_que[MAX];
int front, rear;

int main()
{
int n, ch;

printf("\n1 - Insert an element into queue");


printf("\n2 - Delete an element from queue");
printf("\n3 - Display queue elements");
printf("\n4 - Exit");

create();

while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("\nEnter value to be inserted : ");
scanf("%d",&n);
insert_by_priority(n);
break;
case 2:
printf("\nEnter value to delete : ");
scanf("%d",&n);
delete_by_priority(n);
break;
case 3:
display_pqueue();
break;
case 4:
exit(0);
default:
printf("\nChoice is incorrect, Enter a correct choice");
}
}
}
/* Function to create an empty priority queue */
void create()
{
front = rear = -1;
}

/* Function to insert value into priority queue */


void insert_by_priority(int data)
{
if (rear >= MAX - 1)
{
printf("\nQueue overflow no more elements can be inserted");
return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pri_que[rear] = data;
return;
}
else
check(data);
rear++;
}

/* Function to check priority and place element */


void check(int data)
{
int i,j;

for (i = 0; i <= rear; i++)


{
if (data >= pri_que[i])
{
for (j = rear + 1; j > i; j--)
{
pri_que[j] = pri_que[j - 1];
}
pri_que[i] = data;
return;
}
}
pri_que[i] = data;
}
/* Function to delete an element from queue */
void delete_by_priority(int data)
{
int i;

if ((front==-1) && (rear==-1))


{
printf("\nQueue is empty no elements to delete");
return;
}

for (i = 0; i <= rear; i++)


{
if (data == pri_que[i])
{
for (; i < rear; i++)
{
pri_que[i] = pri_que[i + 1];
}

pri_que[i] = -99;
rear--;

if (rear == -1)
front = -1;
return;
}
}
printf("\n%d not found in queue to delete", data);
}

/* Function to display queue elements */


void display_pqueue()
{
if ((front == -1) && (rear == -1))
{
printf("\nQueue is empty");
return;
}

for (; front <= rear; front++)


{
printf(" %d ", pri_que[front]);
}
front = 0;
}

Output:
1 - Insert an element into queue
2 - Delete an element from queue
3 - Display queue elements
4 - Exit
Enter your choice : 1

Enter value to be inserted : 20

Enter your choice : 1


Enter value to be inserted : 45

Enter your choice : 1

Enter value to be inserted : 89

Enter your choice : 3


89 45 20
Enter your choice : 1

Enter value to be inserted : 56

Enter your choice : 3


89 56 45 20
Enter your choice : 2

Enter value to delete : 45

Enter your choice : 3


89 56 20
Enter your choice : 4

Result:
Thus the C program for implement priority queue to add and delete elements has been verified and
executed successfully.

Exp 19 BINARY SEARCH TREE

Aim:
To write a C program for binary search tree

Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left, *right;
};

struct node *newNode(int item)


{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

void inorder(struct node *root)


{
if (root != NULL)
{
inorder(root->left);
printf("%d \n", root->key);
inorder(root->right);
}
}

struct node* insert(struct node* node, int key)


{

if (node == NULL) return newNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);

return node;
}

int main()
{
struct node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);

inorder(root);

return 0;
}

Output:
20
30
40
50
60
70
80

Result:
Thus the C program for binary search tree has been verified and executed successfully.

Exp 20 IMPLEMENTATION OF TREE

Aim:
To write a c program for implementation of tree

Program:
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node* left;
struct node* right;
};

struct node* createNode(value){


struct node* newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

struct node* insertLeft(struct node *root, int value) {


root->left = createNode(value);
return root->left;
}

struct node* insertRight(struct node *root, int value){


root->right = createNode(value);
return root->right;
}

int main(){
struct node *root = createNode(1);
insertLeft(root, 2);
insertRight(root, 3);

printf("The elements of tree are %d %d %d", root->data, root->left->data, root->right->data);


}

Output:
123

Result:

Thus the C program for implementation of tree has been verified and executed
successfully.
Exp 21 TREE TRAVERSALS (INORDER, PREORDER AND POSTORDER)

Aim:
To write a C program tree traversals (inorder, preorder and postorder)
Program:

#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node* left;
struct node* right;
};

struct node* newNode(int data)


{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}

void printPostorder(struct node* node)


{
if (node == NULL)
return;

printPostorder(node->left);

printPostorder(node->right);

printf("%d ", node->data);


}

void printInorder(struct node* node)


{
if (node == NULL)
return;

printInorder(node->left);

printf("%d ", node->data);

printInorder(node->right);
}

void printPreorder(struct node* node)


{
if (node == NULL)
return;

printf("%d ", node->data);

printPreorder(node->left);

printPreorder(node->right);
}

int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);

printf("\nPreorder traversal of binary tree is \n");


printPreorder(root);

printf("\nInorder traversal of binary tree is \n");


printInorder(root);

printf("\nPostorder traversal of binary tree is \n");


printPostorder(root);

getchar();
return 0;
}
OUTPUT:
Preorder traversal of binary tree is
12453
Inorder traversal of binary tree is
42513
Postorder traversal of binary tree is
45231

Result:
Thus the C program for tree traversals (inorder, preorder and postorder) has been verified
and executed successfully.
Exp 22 BINARY SEARCH TREE OPERATIONS

Aim:
To write a C program for binary search tree operations
Program:
#include<stdio.h>
#include<stdlib.h>
typedef struct tree *node;
node ins(int,node t);
node findmin(node t);
node del(int,node t);
void disp(node t);
struct tree
{
int data;
struct tree *left,*right;
}*root;
int main()
{
node t=NULL;
int data,i,n;
char c;
printf("\n\n\t\t Binary Search tree operations ");
printf("\n\t\t***************************");
printf("\n\t\t Enter the no. of node : ");
scanf("%d",&n);
printf("\n\t\t Enter the elements to insert :\n");
for(i=0;i<n;i++)
{
printf("\n\t\t\t ");
scanf("%d",&data);
t=ins(data,t);
}
l1:
printf("\n\n\t\t Elements displayed in inorder format\n\n\t\t ");
disp(t);
printf("\n\n\t\t Enter the element to delete : ");
scanf("%d",&data);
t=del(data,t);
printf("\n\t\t Content after deletion\n\n\t\t ");
disp(t);
printf("\n\n\t\t Do you want to continue?(y/n) : ");
//c=getch();
//putch(c);
if(c=='y'|| c=='Y')
goto l1;
else
exit(0);
}
node ins(int x,node t)
{
struct tree *newnode;
newnode=malloc(sizeof(struct tree));
if(newnode==NULL)
printf("\n\t\t Out of space.");
else
{
if(t==NULL)
{
newnode->data=x;
newnode->left=NULL;
newnode->right=NULL;
t=newnode;
}
else
{
if(x<t->data)
t->left=ins(x,t->left);
else
t->right=ins(x,t->right);
}
}
return t;
}
node del(int x,node t)
{
node tmp;
if(t==NULL)
printf("\n\t\t Element not found.\n");
else if(x<t->data)
t->left=del(x,t->left);
else if(x>t->data)
t->right=del(x,t->right);
else if(t->left && t->right)
{
tmp=findmin(t->right);
t->data=tmp->data;
t->right=del(t->data,t->right);
}
else
{
tmp=t;
if(t->left==NULL)
t=t->right;
else if(t->right==NULL)
t=t->left;
free(tmp);
}
return t;
}
node findmin(node t)
{
if(t!=NULL)
{
if(t->left==NULL)
return t;
else
return findmin(t->left);
}
return(0);
}
void disp(node t)
{
if(t!=NULL)
{
disp(t->left);
printf("%d ",t->data);
disp(t->right);
}
}
Output :
Binary Search tree operations
**************************
Enter the no. of node : 5
Enter the elements to insert :
1
3
2
5
7
Elements displayed in inorder format
1 2 3 5 7
Enter the element to delete : 3
Content after deletion
1 2 5 7
Do you want to continue?(y/n) : y
Elements displayed in inorder format
1 2 5 7
Enter the element to delete : 7
Content after deletion
1 2 5
Do you want to continue?(y/n) : y
Elements displayed in inorder format
1 2 5
Enter the element to delete : 1
Content after deletion
2 5
Do you want to continue?(y/n) : n

Result:
Thus the C program for binary search tree operations has been verified and executed
successfully.

Exp 23 LINEAR SEARCH

Aim:
To write a C program for linear search

Program:
#include <stdio.h>

int main()
{
int array[100], search, c, n;

printf("Enter the number of elements in array\n");


scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter a number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);

return 0;
}

Output:

Enter the number of elements in array


5
Enter 5 integer(s)
2
3
4
5
6
Enter a number to search
4
4 is present at location 3.
Result:
Thus the C program for linear search has been verified and executed successfully.

Exp 24 BINARY SEARCH

Aim:
To write a C program for binary search

Program:
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d",&n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d",&array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
Output:
Enter number of elements
5
Enter 5 integers
10
20
30
40
50
Enter value to find
20
20 found at location 2.
Result:
Thus the C program for binary search has been verified and executed successfully.

Exp 25 INSERTION SORT

Aim:
To write a C program for insertion sort

Program:

#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++) {


scanf("%d", &array[c]);
}

for (c = 1 ; c <= n - 1; c++) {


d = c;

while ( d > 0 && array[d-1] > array[d]) {


t = array[d];
array[d] = array[d-1];
array[d-1] = t;

d--;
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {


printf("%d\n", array[c]);
}

return 0;
}

Output:
Enter number of elements
5
Enter 5 integers
10
60
90
20
30
Sorted list in ascending order:
10
20
30
60
90
Result:

Thus the C program for insertion sort has been verified and executed successfully.

Exp 26 BUBBLE SORT

Aim:
To write a C program for bubble sort

Program:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

return 0;
}

Output:
Enter number of elements
5
Enter 5 integers
20
60
10
90
80
Sorted list in ascending order:
10
20
60
80
90
Result:

Thus the C program for bubble sort has been verified and executed successfully.

Exp 27 QUICK SORT

Aim:
To write a C program for quick sort

Program:
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}

temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);

}
}

int main(){
int i, count, number[25];

printf("How many elements are u going to enter?: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


for(i=0;i<count;i++)
scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}
Output:
How many elements are u going to enter?: 5
Enter 5 elements: 20
60
90
10
50
Order of Sorted elements: 10 20 50 60 90
Result:

Thus the C program for quick sort has been verified and executed successfully.

Exp 28 MERGE SORT

Aim:
To write a C program for merge sort

Program:

#include<stdlib.h>
#include<stdio.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

int L[n1], R[n2];


for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];

i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1)


{
arr[k] = L[i];
i++;
k++;
}

while (j < n2)


{
arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int l, int r)


{
if (l < r)
{
int m = l+(r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}

void printArray(int A[], int size)


{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}

int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr)/sizeof(arr[0]);

printf("Given array is \n");


printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");


printArray(arr, arr_size);
return 0;
}

Output:
Given array is
12 11 13 5 6 7

Sorted array is
5 6 7 11 12 13
Result:
Thus the C program for merge sort has been verified and executed successfully.

Exp 29 IMPLEMENTATION OF HASHING

Aim:
To write a C program for implementation of hashing

Program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 20
struct DataItem {
int data;
int key;
};

struct DataItem* hashArray[SIZE];


struct DataItem* dummyItem;
struct DataItem* item;

int hashCode(int key) {


return key % SIZE;
}

struct DataItem *search(int key) {


//get the hash
int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

return NULL;
}

void insert(int key,int data) {

struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));


item->data = data;
item->key = key;

//get the hash


int hashIndex = hashCode(key);

//move in array until an empty or deleted cell


while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

hashArray[hashIndex] = item;
}

struct DataItem* delete(struct DataItem* item) {


int key = item->key;

//get the hash


int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];

//assign a dummy item at deleted position


hashArray[hashIndex] = dummyItem;
return temp;
}

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

return NULL;
}

void display() {
int i = 0;

for(i = 0; i<SIZE; i++) {

if(hashArray[i] != NULL)
printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);
else
printf(" ~~ ");
}
printf("\n");
}
int main() {
dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));
dummyItem->data = -1;
dummyItem->key = -1;

insert(1, 20);
insert(2, 70);
insert(42, 80);
insert(4, 25);
insert(12, 44);
insert(14, 32);
insert(17, 11);
insert(13, 78);
insert(37, 97);

display();
item = search(37);

if(item != NULL) {
printf("Element found: %d\n", item->data);
} else {
printf("Element not found\n");
}

delete(item);
item = search(37);

if(item != NULL) {
printf("Element found: %d\n", item->data);
} else {
printf("Element not found\n");
}
}

Output:
~~ (1,20) (2,70) (42,80) (4,25) ~~ ~~ ~~ ~~ ~~ ~~ ~~ (12,44) (13,78) (14,32) ~~ ~~
(17,11) (37,97) ~~
Element found: 97
Element not found
Result:

Thus the C program for implementation of hashing has been verified and executed
successfully.

Exp 30 COLLISION RESOLUTION IN HASH TABLE

Aim:
To write a C program for collision resolution in hash table
Program:

#include<stdio.h>
#include<stdlib.h>
struct hash {
struct hashnode *ptr;
};
struct hashnode {
int val;
struct hashnode *next;
};
struct hash *create_hash(int size)
{
struct hash *htable;
int i;
htable = (struct hash *) malloc(sizeof(struct hash) * size);
if (htable == NULL) {
printf("\n Out of Memory !!");
return NULL;
}
for (i = 0; i < size; i++)
htable[i].ptr = NULL;
return htable;
}
struct hashnode *create_hashnode(int val)
{
struct hashnode *tmp;
tmp = (struct hashnode *) malloc(sizeof(struct hashnode));
if (tmp == NULL)
printf("\n System out of Memory");
tmp->next = NULL;
tmp->val = val;
}
int insert_value(struct hash **htable, int val, int hsize)
{
int slot;
struct hash *htptr = (*htable);
struct hashnode *tmp;
if ((*htable) == NULL) {
printf("\n Hash Table is not created");
return 0;
}
slot = (val) % hsize;
if (htptr[slot].ptr == NULL) {
tmp = create_hashnode(val);
htptr[slot].ptr = tmp;
}
else {
tmp = create_hashnode(val);
tmp->next = htptr[slot].ptr;
htptr[slot].ptr = tmp;
}
return 1;
}
void print_hashtable(struct hash **htable, int size)
{
int i;
struct hashnode *tmp;
struct hash *htmp = (*htable);
for (i = 0; i < size; i++) {
tmp = htmp[i].ptr;
while (tmp != NULL) {
printf(" %d ", tmp->val);
tmp = tmp->next;
}
printf("\n");
}
}
int seach_hashtable(struct hash **htable, int val, int size)
{
int slot, found = 0;
slot = val % size;
struct hashnode *tmp;
tmp = (*htable)[slot].ptr;
while (tmp != NULL) {
if (tmp->val == val) {
found = 1;
break;
}
tmp = tmp->next;
}
return found;
}
int main()
{
struct hash *hashtable1;
int hashtable1size;
printf("\n Enter the size of HashTable : ");
scanf("%d", &hashtable1size);
hashtable1 = create_hash(hashtable1size);
if (hashtable1 != NULL)
printf("\nTable Created Successfully !!");
int key, val, found;
do {
printf("\n Hash Table Collision Resolution by Codingstreet.com");
printf("\n 1. Insert value ");
printf("\n 2. Search Value");
printf("\n 3. Print Table");
printf("\n 4. Exit");
printf("\n Enter the key [1-4] :");
scanf("%d", &key);
switch (key) {
case 1:
printf("\n Enter Val : ");
scanf("%d", &val);
insert_value(&hashtable1, val, hashtable1size);
break;
case 2:
printf("\n Enter Val : ");
scanf("%d", &val);
found = seach_hashtable(&hashtable1, val, hashtable1size);
if (found == 0)
printf("\n Val not found");
else
printf("\n %d found at slot %d ", val,
val % hashtable1size);
break;
case 3:
print_hashtable(&hashtable1, hashtable1size);
break;
case 4:
printf("\n Thank you !!! ");
break;
default:
break;
}
} while (key != 4);
printf("\n Thanks for using Codingstreet.com 's Data structure solution\n");
return 0;
}

Output:
Enter the size of HashTable : 5
Table Created Successfully !!
Hash Table Collision Resolution by Codingstreet.com
1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :1

Enter Val : 10

Hash Table Collision Resolution by Codingstreet.com


1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :1

Enter Val : 20

Hash Table Collision Resolution by Codingstreet.com


1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :1

Enter Val : 30

Hash Table Collision Resolution by Codingstreet.com


1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :1

Enter Val : 40

Hash Table Collision Resolution by Codingstreet.com


1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :3
40 30 20 10
Hash Table Collision Resolution by Codingstreet.com
1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :2

Enter Val : 30

30 found at slot 0
Hash Table Collision Resolution by Codingstreet.com
1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :3
40 30 20 10

Hash Table Collision Resolution by Codingstreet.com


1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :40

Hash Table Collision Resolution by Codingstreet.com


1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :2

Enter Val : 40

40 found at slot 0
Hash Table Collision Resolution by Codingstreet.com
1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :
Result:
Thus the C program for collision resolution in hash table has been verified and executed
successfully.

You might also like