Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views55 pages

Data Structures Lab Record-1

The document is a laboratory record for the Data Structures Laboratory course at KPR Institute of Engineering and Technology. It includes the vision and mission statements of the institution and the department, program educational objectives, program outcomes, and specific outcomes for Computer Science and Engineering graduates. Additionally, it outlines various experiments and assessments related to data structures, including linked lists, sorting algorithms, and search techniques.

Uploaded by

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

Data Structures Lab Record-1

The document is a laboratory record for the Data Structures Laboratory course at KPR Institute of Engineering and Technology. It includes the vision and mission statements of the institution and the department, program educational objectives, program outcomes, and specific outcomes for Computer Science and Engineering graduates. Additionally, it outlines various experiments and assessments related to data structures, including linked lists, sorting algorithms, and search techniques.

Uploaded by

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

Department of Computer Science and Engineering

KPR Institute of Engineering and Technology

LABORATORY RECORD

U21CSG03
DATA STRUCTURES LABORATORY
KPR INSTITUTE OF ENGINEERING AND TECHNOLOGY
(Autonomous) COIMBATORE – 641 407

LABORATORY RECORD

Name : ……………………………………………………………….

Roll Number : ……………………………………………………………….

Subject Code & Title: ……………………………………………………………….

Department : ……………………………………………………………….

Year & Semester : ……………………………………………………………….

This is the certified record of work done by………………………………………….

Register Number………………………………………………………

Staff In- Charge Head of the Department

Place:

Date:

He/ She has submitted the record for the End Semester
PracticalExamination held on ……………………

Internal Examiner External Examiner

2
VISION AND MISSION OF THE INSTITUTION
Vision
To become a premier institute of academic excellence by imparting technical, intellectual and
professional skills to students for meeting the diverse need of the industry, society, the nation and the
world at large
Mission
 Commitment to offer value-based education and enhancement of practical skills
 Continuous assessment of teaching and learning process through scholarly activities
 Enriching research and innovation activities in collaboration with industry and institute of repute
 Ensuring the academic process to uphold culture, ethics and social responsibility

VISION AND MISSION OF THE DEPARTMENT


Vision
To be a premier centre for education, dissemination of knowledge and research in the frontier areas
of computer science and engineering to serve the community with moral values.
Mission
The Department of Computer Science And Engineering is committed
 Provide holistic education incorporating the state-of-the-art technologies to produce successful
professionals.
 Facilitate the students to pursue higher education and research in the areas related to Computer
Science and Engineering.
 Promote strong collaborations with the industries and steer the students to nurture their interest in
continuous learning to meet the changing needs of the society.
Program Educational Objectives (PEOs)
 Acquire knowledge and skills on cutting edge technologies in the field of computer science and
engineering.
 Inculcate a passion for continuous learning through further studies and research in the field of
computer science and engineering.
 Develop innovative ideas upholding the rich value systems to solve the changing technological
needs.
Program Outcomes (POs)
Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering problems
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences
3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
3
consideration for the public health and safety, and the cultural, societal, and environmental
considerations
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the engineering practice
9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective
reports and design documentation, make effective presentations, and give and receive clear
instructions
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one's own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change

Program Specific Outcomes (PSOs)


A graduate of Computer Science and Engineering will be able to
1. PSO 1: Solve complex engineering problems by building integrated systems across various
domains of Computational Intelligence.
2. PSO 2: Apply technical skills to produce solutions that meet the requirements of the emerging
technologies in Computer Science and Engineering.

4
RUBRICS FOR ASSESSMENT

Excellent Good Adequate Inadequate


Criteria
(4 Marks) (3 Marks) (2 Marks) (1 Mark)
Unable to identify
Able to develop Able to use
theoretical
A. Preparation & Observation

contingency or theoretical
Criterion #1 Able to develop framework,
alternative plans framework,
Ability to setup contingency or measurement
and anticipate measurement
and conduct alternative plans. techniques,
problems during techniques, testing
experiments testing apparatus
experiment. apparatus or model.
or model.
Able to formulate,
controls and Able to apply
Criterion #2 evaluate constraint and
Able to evaluate
Ability to take alternatives of the assumption into the Unable to discuss
data and relate to
measurements experiment. Able to experimental experimental
engineering
/ readings and evaluate data and design. Able to processes and
phenomena for
present data relate to conduct experiment protocols
decision-making.
engineering correctly and collect
phenomena for data.
decision-making.
Criterion #3
Ability to
Able to combine Able to select and Unable to select
analyze the
B. Results & Interpretation

/organize more than Able to evaluate or use and apply and describe the
data
one set of data, compare data and appropriate techniques or
theoretically
interpret data and make meaningful techniques or methods of
and logically to
make meaningful conclusion methods to analyse analyzing the
conclude
conclusion. the data. data.
experimental
results
Criterion #4
Ability to
Able to verify and/or Able to verify and/or
interpret and Able to identify and Unable to identify
validate several sets validate data and
discuss any verify how results how results
of data and relates relate to
discrepancies relate/differ from relate/differ from
to engineering engineering
between theory or previous theory or previous
phenomena for phenomena for
theoretical and results results.
decision making. decision making.
experimental
results
Able to listen Misunderstand the Unable to listen
C. Viva Voce

Criterion #5 carefully and questions and does carefully to


Able to listen
Demonstrate respond to not respond questions and
carefully and
the ability to questions appropriately to the does not provide
respond to
respond appropriately; is teacher, or has an appropriate
questions
effectively to able to explain and some trouble in answer, or is
appropriately
questions interpret results to answering unable to answer
the teacher questions questions

5
INDEX

Page
Ex. No. Date Experiment Name No. Marks Signature

Write a function program to perform the


following operations on a singly linked list
i. Create a list cube.
ii. Insert an element to the list
1 iii. Delete the maximum element from the list
iv. Arrange the list as sorted order
v. Display the elements of the list
Write a main method to demonstrate the above
functionalities.

Creation of Array and linked list implementation


2 of Stack and Queue ADTs.

3 Implementation of quick, heap and shell sort.

Program to sort the elements in ascending order


4 using selection sort and bubble sort.

5 Implementation of hashing technique.

Develop a program to perform linear and binary


6 search.

Program to construct an expression tree for a


7 given expression and perform various tree
traversal methods.

Implement Prims algorithm with the following


functionalities
i. Read a set of vertices minimum of six from the
keyboard
ii. Get the number of edges and form the graph
iii. Find the value of each edge by using distance
8 formula for two points.
iv. Develop a Minimum Spanning Tree for the
graph

v. Find the total length of all edges. Write a main


method to execute the above functionalities

3
Choose an appropriate data structure and create
a token system for banking service (withdrawal,
9 deposit, and money transfer).

Create a food delivering system which allocates


10 the path for delivery of food using appropriate
data structures.

Create a book rack allocation system in a library,


which allocates appropriate space for the books
11 based on category using appropriate data
structures.

Average(A) /20

Final marks = (A*75) / 20

4
Ex. No.: 1 SINGLY LINKED LIST
Date:

AIM

To write a function program to perform the following operations on a singly linked list
i. Create a list cube.
ii. Insert an element to the list
iii. Delete the maximum element from the list
iv. Arrange the list as sorted order
v. Display the elements of the list

ALGORITHM

Step 1: Create a linked list in name of cube


Step 2: Insert an element to the list
Step 3: Arrange the list as sorted order
Step 4: Display the elements of the list
Step 5: Delete the maximum element from the list
Step 6: Display the remaining elements of the list.

PROGRAM

/* Singly Linked List Example - Insert,Delete,Display and Count Operations*/


#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void insert();
void display();
void delete();
int count();
typedef struct node DATA_NODE;
DATA_NODE *head_node, *first_node, *temp_node = 0,*prev_node,next_node;
int data;
int main() {
int option = 0;
printf("Singly Linked List Example - All Operations\n");
while (option < 5) {
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");

8
printf("3 : Display Linked List\n");
printf("4 : Count Linked List\n");
printf("Others : Exit()\n");
printf("Enter your option:");
scanf("%d", &option);
switch (option) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
count();
break;
default:
break;
}
}
return 0;
}

void insert() {
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);
temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));
temp_node->value = data;
if (first_node == 0) {
first_node = temp_node;
}
else {
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
fflush(stdin);
}
void delete() {
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
printf("\nDisplay Linked List : \n");
printf("\nEnter Position for Delete Element : \n");
scanf("%d", &pos);
if (pos > 0 && pos <= countvalue) {
9
if (pos == 1) {
temp_node = temp_node -> next;
first_node = temp_node;
printf("\nDeleted Successfully \n\n");
}

else {
while (temp_node != 0) {
if (i == (pos - 1)) {
prev_node->next=temp_node->next;
if(i == (countvalue - 1)){
head_node = prev_node;
}
printf("\nDeleted\n\n");
break;
}
else {
i++;
prev_node = temp_node;
temp_node = temp_node -> next;
}
}
}
}
else{
printf("\nInvalid Position \n\n");
}
void display() {
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
}

int count() {
int count = 0;
temp_node = first_node;
while (temp_node != 0) {
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
10
}

OUTPUT:

Singly Linked List Example - All Operations


Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
100
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
12
200
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
300
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
400
Options
1 : Insert into Linked List
2 : Delete from Linked List
11
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
500
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:3
Display Linked List :
# 100 # # 200 # # 300 # # 400 # # 500 #
No Of Items In Linked List : 5
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:4
No Of Items In Linked List : 5
Options
1 : Insert into Linked List
13
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:2
No Of Items In Linked List : 5
Display Linked List :
Enter Position for Delete Element :
3
Deleted Successfully
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:3
Display Linked List :
# 100 # # 200 # # 400 # # 500 #
No Of Items In Linked List : 4
Options
12
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:2
No Of Items In Linked List : 4
Display Linked List :
Enter Position for Delete Element :
6
Invalid Position
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:
5
(program exited with code: 0)
Press any key to continue . . .

Criteria Score

Criterion #1 Ability to setup and conduct experiments.


A. Preparation &
Observation Criterion #2 Ability to take measurements / readings and present
data
Criterion #3 Ability to analyze the data theoretically and
B. Results & logically to conclude experimental results
Interpretation Criterion #4 Ability to interpret and discuss any discrepancies
between theoretical and experimental results
Criterion #5 Demonstrate the ability to effectively respond to
C. Viva Voce
questions
Total

RESULT
Thus to write a function program to perform the following operations on a singly
linked list was executed and output was verified successfully.

13
Ex. No.: 2 STACK AND QUEUE ADTS
Date:

AIM

To Create an Array and linked list implementation using Stack and Queue ADTs.

ALGORITHM

Step 1: Create a Stack using Linked List


Step 2: Insert an element to the list
Step 3: Display the elements of the stack
Step 4: Delete the element from stack.
Step 6: Display the remaining elements of the list.

PROGRAM:

i. C Program to Implement a Stack using Linked List.

#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
14
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL){
printf("No elements in stack");
}
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
/* Create empty stack */

15
void create()
{
top = NULL;
}
/* Count stack elements */
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
/* Push data into stack */
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
/* Display stack elements */

void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
/* Pop Operation on stack */
void pop()
{
top1 = top;
16
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else{
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
}
top = top1;
count--;
}
/* Return top element */
int topelement()
{
return(top->info);
}
/* Check if stack is empty or not */
void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}
/* Destroy entire stack */
void destroy()
{
top1 = top;
while (top1 != NULL)
{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;
printf("\n All stack elements destroyed");
count = 0;
}

OUTPUT

$ cc pgm2.c
$ a.out
1 - Push
17
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Dipslay
18
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56
Enter choice : 1
Enter data : 80
Enter choice : 2
Popped value : 80
Enter choice : 3
Top element : 56
Enter choice : 1
Enter data : 78
Enter choice : 1
Enter data : 90
Enter choice : 6
90 78 56
Enter choice : 7
No. of elements in stack : 3
Enter choice : 8
All stack elements destroyed
Enter choice : 4
Stack is empty
Enter choice : 5

ii. C Program to Implement Queue Data Structure using Linked List

#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
18
{
int no, ch, e;
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
19
/* Create an empty queue */
void create()
{
front = rear = NULL;
}
/* Returns queue size */
void queuesize()
{
printf("\n Queue size : %d", count);
}
/* Enqueing the queue */
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
/* Displaying the queue elements */
void display()
{
front1 = front;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}
/* Dequeing the queue */
20
void deq()
{
front1 = front;
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
/* Returns the front element of queue */
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
/* Display if queue is empty or not */
void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}

OUTPUT

$ cc pgm4.c
$ a.out
1 - Enque
2 - Deque
21
3 - Front element
4 - Empty
5 - Exit
6 - Display
7 - Queue size
Enter choice : 1
Enter data : 14
Enter choice : 1
Enter data : 85
Enter choice : 1
Enter data : 38
Enter choice : 3
Front element : 14
Enter choice : 6
14 85 38
Enter choice : 7
Queue size : 3
Enter choice : 2
Dequed value : 14
Enter choice : 6
85 38
Enter choice : 7
Queue size : 2
Enter choice : 4
Queue not empty
Enter choice : 5

Criteria Score

Criterion #1 Ability to setup and conduct experiments.


A. Preparation &
Observation Criterion #2 Ability to take measurements / readings and present
data
Criterion #3 Ability to analyze the data theoretically and
B. Results & logically to conclude experimental results
Interpretation Criterion #4 Ability to interpret and discuss any discrepancies
between theoretical and experimental results
Criterion #5 Demonstrate the ability to effectively respond to
C. Viva Voce
questions
Total

RESULT

Thus the program to Creation of Array and linked list implementation of Stack and Queue
ADTs was executed and output was verified successfully.

22
Ex. No.: 3 IMPLEMENTATION OF QUICK, HEAP AND SHELL SORT
Date:

AIM

To Create an Array and linked list implementation using Stack and Queue ADTs.

ALGORITHM

Step :1 Create a List of elements .


Step :2 Sort the elements using shell sort ,quick sort and heap sort.
Step :3 Display the elements after the sorting .

PROGRAM:

i. Quick sort

#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);
23
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?: 10


Enter 10 elements: 2 3 5 7 1 9 3 8 0 4
Order of Sorted elements: 0 1 2 3 3 4 5 7 8 9

ii. Heap Sort

#include <stdio.h>
/* function to heapify a subtree. Here 'i' is the
index of root node in array a[], and 'n' is the size of heap.*/
void heapify(int a[], int n, int i)
{
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child
int right = 2 * i + 2; // right child
// If left child is larger than root
if (left < n && a[left] > a[largest])
largest = left;
// If right child is larger than root
if (right < n && a[right] > a[largest])
largest = right;
// If root is not largest
if (largest != i) {
// swap a[i] with a[largest]
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
heapify(a, n, largest);
}
}
/*Function to implement the heap sort*/
void heapSort(int a[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, n, i);
// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
/* Move current root element to end*/
24
// swap a[0] with a[i]
int temp = a[0];
a[0] = a[i];
a[i] = temp;
heapify(a, i, 0);
}
}
/* function to print the array elements */
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
}
}
int main()
{
int a[] = {48, 10, 23, 43, 28, 26, 1};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}

OUTPUT
Before Sorting the array elements are
48 10 23 43 28 26 1
After Sorting the array elements are
1 10 23 26 28 43 48

iii. Shell Sort

#include <stdio.h>
void shellsort(int arr[], int num)
{
int i, j, k, tmp;
for (i = num / 2; i > 0; i = i / 2)
{
for (j = i; j < num; j++)
{
for(k = j - i; k >= 0; k = k - i)
{
if (arr[k+i] >= arr[k])
break;
25
else
{
tmp = arr[k];
arr[k] = arr[k+i];
arr[k+i] = tmp;
}
}
}
}
}
int main()
{
int arr[30];
int k, num;
printf("Enter total no. of elements : ");
scanf("%d", &num);
printf("\nEnter %d numbers: ", num);
for (k = 0 ; k < num; k++)
{
scanf("%d", &arr[k]);
}
shellsort(arr, num);
printf("\n Sorted array is: ");
for (k = 0; k < num; k++)
printf("%d ", arr[k]);
return;
}

Criteria Score

Criterion #1 Ability to setup and conduct experiments.


A. Preparation &
Observation Criterion #2 Ability to take measurements / readings and present
data
Criterion #3 Ability to analyze the data theoretically and
B. Results & logically to conclude experimental results
Interpretation Criterion #4 Ability to interpret and discuss any discrepancies
between theoretical and experimental results
Criterion #5 Demonstrate the ability to effectively respond to
C. Viva Voce
questions
Total

RESULT

Thus, the program to Implement quick, heap and shell sort was executed and output
was verified successfully.

26
Ex. No.: 4 SELECTION SORT AND BUBBLE SORT
Date:

AIM

To develop a c program to sort the elements in ascending order using selection sort and
bubble sort.

ALGORITHM

Step :1 Create a List of elements .


Step :2 Sort the elements using selection sort and bubble sort.
Step :3 Display the elements after the sorting

PROGRAM

i. Selection Sort

#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position=swap;
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", a[i]);
return 0;
}
27
OUTPUT

Enter the number of elements: 5


Enter 5 numbers
35217
Sorted Array
12357

ii.Bubble sort

#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])
{
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 the number of elements: 5


Enter 5 numbers
35217
Sorted List in ascending order
12357

28
Criteria Score

Criterion #1 Ability to setup and conduct experiments.


A. Preparation &
Observation Criterion #2 Ability to take measurements / readings and present
data
Criterion #3 Ability to analyze the data theoretically and
B. Results & logically to conclude experimental results
Interpretation Criterion #4 Ability to interpret and discuss any discrepancies
between theoretical and experimental results
Criterion #5 Demonstrate the ability to effectively respond to
C. Viva Voce
questions
Total

RESULT

Thus to Program to sort the elements in ascending order using selection sort and
bubble sort was executed and output was verified successfully.

29
Ex. No.: 5 IMPLEMENTATION OF HASHING TECHNIQUE
Date:

AIM

To write a C program to implement the hashing technique.

ALGORITHM

Step :1 Create a List and using hashing function that maps a given value with
a key
Step :2 Display the elements
Step :3 Searching any elements using hashing function
Step :4 Display the searching elements

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

30
void insert(int key,int data) {
struct DataItem*item = (structDataItem*) 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() {
31
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

32
Criteria Score

Criterion #1 Ability to setup and conduct experiments.


A. Preparation &
Observation Criterion #2 Ability to take measurements / readings and present
data
Criterion #3 Ability to analyze the data theoretically and
B. Results & logically to conclude experimental results
Interpretation Criterion #4 Ability to interpret and discuss any discrepancies
between theoretical and experimental results
Criterion #5 Demonstrate the ability to effectively respond to
C. Viva Voce
questions
Total

RESULT

Thus the implementation of hashing technique was executed and output was
verified successfully.

33
Ex. No.: 6 DEVELOP A PROGRAM TO PERFORM LINEAR AND BINARY SEARCH
Date:

AIM

Develop a program to perform linear and binary search

ALGORITHM

Step :1 Create a List


Step :2 Display the elements
Step :3 Searching any elements using linear and binary search.
Step :4 Display the searching elements

PROGRAM

i. Linear search

#include <stdio.h>
int main() {
int array[100], search, c, n;
printf("Enter 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){
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

34
ii.Binary search

#include <stdio.h>
int binarySearch(int array[], int x, int low, int high) {
// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}

OUTPUT

Element is found at index 1

35
Criteria Score

Criterion #1 Ability to setup and conduct experiments.


A. Preparation &
Observation Criterion #2 Ability to take measurements / readings and present
data
Criterion #3 Ability to analyze the data theoretically and
B. Results & logically to conclude experimental results
Interpretation Criterion #4 Ability to interpret and discuss any discrepancies
between theoretical and experimental results
Criterion #5 Demonstrate the ability to effectively respond to
C. Viva Voce
questions
Total

RESULT

Thus, to write Develop a program to perform linear and binary search was
executed and output was verified successfully.

36
Ex. No.: 7 TREE TRAVERSAL METHODS
Date:

AIM

To write a C program to construct an expression tree for a given expression and perform
various tree traversal methods

ALGORITHM

Step :1 Create a tree for given expression


Step :2 Perform the preorder traversal and display the output.
Step :3 Perform the inorder traversal and display the output
Step :4 Perform the postorder traversal and display the output

PROGRAM

// C program for different tree traversals


#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node {
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
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);
}
/* Given a binary tree, print its nodes according to the
"bottom-up" postorder traversal. */
void printPostorder(struct node* node)
{
if (node == NULL)
return;
// first recur on left subtree
printPostorder(node->left);
// then recur on right subtree

37
printPostorder(node->right);
// now deal with the node
printf("%d ", node->data);
}
/* Given a binary tree, print its nodes in inorder*/
void printInorder(struct node* node)
{
if (node == NULL)
return;
/* first recur on left child */
printInorder(node->left);
/* then print the data of node */
printf("%d ", node->data);
/* now recur on right child */
printInorder(node->right);
}
/* Given a binary tree, print its nodes in preorder*/
void printPreorder(struct node* node)
{
if (node == NULL)
return;
/* first print data of node */
printf("%d ", node->data);
/* then recur on left subtree */
printPreorder(node->left);
/* now recur on right subtree */
printPreorder(node->right);
}
/* Driver program to test above functions*/
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;
}

38
OUTPUT

Preorder traversal of binary tree is


12453
Inorder traversal of binary tree is
42513
Postorder traversal of binary tree is
45231

Criteria Score

Criterion #1 Ability to setup and conduct experiments.


A. Preparation &
Observation Criterion #2 Ability to take measurements / readings and present
data
Criterion #3 Ability to analyze the data theoretically and
B. Results & logically to conclude experimental results
Interpretation Criterion #4 Ability to interpret and discuss any discrepancies
between theoretical and experimental results
Criterion #5 Demonstrate the ability to effectively respond to
C. Viva Voce
questions
Total

RESULT

Thus, Program to construct an expression tree for a given expression and


perform various tree traversal methods.was executed and output was verified successfully.

39
Ex. No.: 8 IMPLEMENTATION OF PRIMS ALGORITHM
Date:

AIM

To implement Prims algorithm with the following functionalities


i. Read a set of vertices minimum of six from the keyboard
ii. Get the number of edges and form the graph
iii. Find the value of each edge by using distance formula for two points.
iv. Develop a Minimum Spanning Tree for the graph
v. Find the total length of all edges. Write a main method to execute the above
functionalities

ALGORITHM

Step :1 Read a set of vertices minimum of six from the keyboard


Step :2 Get the number of edges and form the graph
Step :3 Find the value of each edge by using distance formula for two points.
Step :4 Develop a Minimum Spanning Tree for the graph
Step :5 Find the total length of all edges.

PROGRAM

#include <stdio.h>
#include <limits.h>
#define vertices 5 /*Define the number of vertices in the graph*/
/* create minimum_key() method for finding the vertex that has minimum key-value and
that is not added in MST yet */

int minimum_key(int k[], int mst[])


{
int minimum = INT_MAX, min,i;
/*iterate over all vertices to find the vertex with minimum key-value*/
for (i = 0; i < vertices; i++)
if (mst[i] == 0 && k[i] < minimum)
minimum = k[i], min = i;
return min;
}
/* create prim() method for constructing and printing the MST. The g[vertices][vertices] is
an adjacency matrix that defines the graph for MST.*/

void prim(int g[vertices][vertices])


{
/* create array of size equal to total number of vertices for storing the MST*/
40
int parent[vertices];
/* create k[vertices] array for selecting an edge having minimum weight*/
int k[vertices];
int mst[vertices];
int i, count,edge,v; /*Here 'v' is the vertex*/
for (i = 0; i < vertices; i++)
{
k[i] = INT_MAX;
mst[i] = 0;
}
k[0] = 0; /*It select as first vertex*/
parent[0] = -1; /* set first value of parent[] array to -1 to make it root of MST*/
for (count = 0; count < vertices-1; count++)
{
/*select the vertex having minimum key and that is not added in the MST yet from the
set of vertices*/
edge = minimum_key(k, mst);
mst[edge] = 1;
for (v = 0; v < vertices; v++)
{
if (g[edge][v] && mst[v] == 0 && g[edge][v] < k[v])
{
parent[v] = edge, k[v] = g[edge][v];
}
}
}
/*Print the constructed Minimum spanning tree*/
printf("\n Edge \t Weight\n");
for (i = 1; i < vertices; i++)
printf(" %d <-> %d %d \n", parent[i], i, g[i][parent[i]]);
}
int main()
{
int g[vertices][vertices] = {{0, 0, 3, 0, 0},{0, 0, 10, 4, 0},{3, 10, 0, 2, 6},{0, 4, 2, 0,
1},{0, 0, 6, 1, 0},
};
prim(g);
return 0;
}

41
OUTPUT

Criteria Score

Criterion #1 Ability to setup and conduct experiments.


A. Preparation &
Observation Criterion #2 Ability to take measurements / readings and present
data
Criterion #3 Ability to analyze the data theoretically and
B. Results & logically to conclude experimental results
Interpretation Criterion #4 Ability to interpret and discuss any discrepancies
between theoretical and experimental results
Criterion #5 Demonstrate the ability to effectively respond to
C. Viva Voce
questions
Total

RESULT

Thus the program to Implement Prims algorithm was executed and output was
verified successfully.

42
Ex. No.: 9 TOKEN SYSTEM FOR BANKING SERVICE
Date:

AIM

To choose an appropriate data structure and create a token system for banking service
(withdrawal, deposit and money transfer

ALGORITHM

Step :1 Create record for each customer with account number


Step :2 Select the option from the banking services
Step :3 For deposit service ,add the balance amount with deposit amount.
Step :4 Display the balance amount after the withdraw service.

PROGRAM

#include <stdio.h>
struct customer
{
int account_no;
char name[80];
int balance;
};
void accept(struct customer[], int);
void display(struct customer[], int);
int search(struct customer[], int, int);
void deposit(struct customer[], int, int, int);
void withdraw(struct customer[], int, int, int);
int main()
{
struct customer data[20];
int n, choice, account_no, amount, index;
printf("Banking System\n\n");
printf("Number of customer records you want to enter? : ");
scanf("%d", &n);
accept(data, n);
do
{
printf("\nBanking System Menu :\n");
printf("Press 1 to display all records.\n");
printf("Press 2 to search a record.\n");
printf("Press 3 to deposit amount.\n");
printf("Press 4 to withdraw amount.\n");
printf("Press 0 to exit\n");
printf("\nEnter choice(0-4) : ");
scanf("%d", &choice);
43
switch (choice)
{
case 1:
display(data, n);
break;
case 2:
printf("Enter account number to search : ");
scanf("%d", &account_no);
index = search(data, n, account_no);
if (index == - 1)
{
printf("Record not found : ");
}
else
{
printf("A/c Number: %d\nName: %s\nBalance: %d\n",
data[index].account_no, data[index].name,
data[index].balance);
}
break;
case 3:
printf("Enter account number : ");
scanf("%d", &account_no);
printf("Enter amount to deposit : ");
scanf("%d", &amount);
deposit(data, n, account_no, amount);
break;
case 4:
printf("Enter account number : ");
scanf("%d", &account_no);
printf("Enter amount to withdraw : ");
scanf("%d", &amount);
withdraw(data, n, account_no, amount);
}
}
while (choice != 0);
return 0;
}
void accept(struct customer list[80], int s)
{
int i;
for (i = 0; i < s; i++)
{
printf("\nEnter data for Record #%d", i + 1);
printf("\nEnter account_no : ");
scanf("%d", &list[i].account_no);
fflush(stdin);
printf("Enter name : ");
44
gets(list[i].name);
list[i].balance = 0;
}
}
void display(struct customer list[80], int s)
{
int i;
printf("\n\nA/c No\tName\tBalance\n");
for (i = 0; i < s; i++)
{
printf("%d\t%s\t%d\n", list[i].account_no, list[i].name,
list[i].balance);
}
}
int search(struct customer list[80], int s, int number)
{
int i;
for (i = 0; i < s; i++)
{
if (list[i].account_no == number)
{
return i;
}
}
return - 1;
}
void deposit(struct customer list[], int s, int number, int amt)
{
int i = search(list, s, number);
if (i == - 1)
{
printf("Record not found");
}
else
{
list[i].balance += amt;
}
}
void withdraw(struct customer list[], int s, int number, int amt)
{
int i = search(list, s, number);
if (i == - 1)
{
printf("Record not found\n");
}
else if (list[i].balance < amt)
{
printf("Insufficient balance\n");
45
}
else
{
list[i].balance -= amt;
}
}

OUTPUT

Banking System Number of customer records you want to enter? : 3


43
Enter data for Record #1
Enter account_no : 100
Enter name : Alex
Enter data for Record #2
Enter account_no : 101
Enter name : Monica
Enter data for Record #3
Enter account_no : 103
Enter name : Javed
Banking System Menu :
Press 1 to display all records.
Press 2 to search a record.
Press 3 to deposit amount.
Press 4 to withdraw amount.
Press 0 to exit
Enter choice(0-4) : 1
A/c No Name Balance
100 Alex 0
101 Monica 0
103 Javed 0
Banking System Menu :
Press 1 to display all records.
Press 2 to search a record.
Press 3 to deposit amount.
Press 4 to withdraw amount.
Press 0 to exit
Enter choice(0-4) : 3
Enter account number : 101
Enter amount to deposit : 500
Banking System Menu :
Press 1 to display all records.
Press 2 to search a record.
Press 3 to deposit amount.
Press 4 to withdraw amount.
Press 0 to exit
Enter choice(0-4) : 2
Enter account number to search : 101
46
A/c Number: 101
Name: Monica
Balance: 500
Banking System Menu :
Press 1 to display all records.
Press 2 to search a record.
Press 3 to deposit amount.
Press 4 to withdraw amount.
Press 0 to exit
Enter choice(0-4) : 4
Enter account number : 101
Enter amount to withdraw : 150
Banking System Menu :
Press 1 to display all records.
Press 2 to search a record.
Press 3 to deposit amount.
Press 4 to withdraw amount.
Press 0 to exit
Enter choice(0-4) : 1
A/c No Name Balance
100 Alex 0
101 Monica 350
103 Javed 0
Banking System Menu :
Press 1 to display all records.
Press 2 to search a record.
Press 3 to deposit amount.
Press 4 to withdraw amount.
Press 0 to exit
Enter choice(0-4) : 0

47
Criteria Score

Criterion #1 Ability to setup and conduct experiments.


A. Preparation &
Observation Criterion #2 Ability to take measurements / readings and present
data
Criterion #3 Ability to analyze the data theoretically and
B. Results & logically to conclude experimental results
Interpretation Criterion #4 Ability to interpret and discuss any discrepancies
between theoretical and experimental results
Criterion #5 Demonstrate the ability to effectively respond to
C. Viva Voce
questions
Total

RESULT

Thus Choose an appropriate data structure and create a token system for banking
service (withdrawal, deposit and money transfer was executed and output was verified
successfully.

48
Ex. No.: 10 FOOD DELIVERING SYSTEM
Date:

AIM

To create a food delivering system which allocates the path for delivery of food using
appropriate data structures

ALGORITHM

Step :1 Create queue for food ordering system


Step :2 Display the menu items with price details
Step :3 Stored the food order, using enqueue operation
Step :4 Delivered the food order using dequque operation.
Step :5 Display the order with total amount.

PROGRAM

/ C program to initialize the Food


// Ordering System
// Initialization
#include <stdio.h>
// Structure to store the user details
// i.e., Signup details
struct details {
char uname[100];
int age;
char password[100];
char email[100];
char mobile[10];
};
// Structure to store the hotels
// and their food details
struct hotels {
char hotel[100];
char first_food[20];
char second_food[20];
char third_food[20];
char fourth_food[25];
int first, second, third, fourth;
};
// Initialize the hotels and details
struct hotels m[5];
struct details s[100];
// Function to get the input for
// a new account

49
void signup();
// Function to check whether account
// is already existed or not
void account_check();
// Function to validate all input fields
int validate();
void login();
void cart();
void search_by_hotels();
void search_by_food();
void food_order(int food);
// Function to initialize the hotels and
// food structure dynamically
void hotel_initialize();
void hotels(int hotel_choice);
int flag = 1, i, j = 0, count = 0, caps = 0;
int small = 0, special = 0, numbers = 0;
int success = 0, x, choice;
char temp_name[100], temp_password1[100];
char temp_password2[100], temp_email[100];
char temp_mobile[100];
int temp_age, total = 0, food_choice, n;
int hotel_choice, search_choice, confirm;
int ch, food, hotel_id;

OUTPUT

50
51
Criteria Score

Criterion #1 Ability to setup and conduct experiments.


A. Preparation &
Observation Criterion #2 Ability to take measurements / readings and present
data
Criterion #3 Ability to analyze the data theoretically and
B. Results & logically to conclude experimental results
Interpretation Criterion #4 Ability to interpret and discuss any discrepancies
between theoretical and experimental results
Criterion #5 Demonstrate the ability to effectively respond to
C. Viva Voce
questions
Total

RESULT

Thus, to Create a food delivering system which allocates the path for delivery
of food using appropriate data structures was executed and output was verified
successfully.

52
Ex. No.: 11 BOOK RACK ALLOCATION SYSTEM
Date:

AIM

To create a book rack allocation system in a library, which allocates appropriate space for
the books based on category using appropriate data structures

ALGORITHM

Step :1 Create a library system


Step :2 Display the book’s information with category details.
Step :3 Add the new book in library
Step :4 Display the details of total number of books

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct library{
char bookname[50];
char author[50];
int noofpages;
float price;
};
int main(){
struct library lib[100];
char bookname[30];
int i,j, keepcount;
i=j=keepcount = 0;
while(j!=6){
printf("\n1. Add book information\n");
printf("2.Display book information\n");
printf("3. no of books in the library\n");
printf("4. Exit");
printf ("\n\nEnter one of the above : ");
scanf("%d",&j);
switch (j){
/* Add book */
case 1:
printf ("Enter book name = ");
scanf ("%s",lib[i].bookname);
printf ("Enter author name = ");
scanf ("%s",lib[i].author);
printf ("Enter pages = ");
53
scanf ("%d",&lib[i].noofpages);
printf ("Enter price = ");
scanf ("%f",&lib[i].price);
keepcount++;
i++;
break;
case 2:
printf("you have entered the following information\n");
for(i=0; i<keepcount; i++){
printf ("book name = %s\n",lib[i].bookname);
printf ("\t author name = %s\n",lib[i].author);
printf ("\t pages = %d\n",lib[i].noofpages);
printf ("\t price = %f\n",lib[i].price);
}
break;
case 3:
printf("\n No of books in library : %d", keepcount);
break;
case 4:
exit (0);
}
}
return 0;
}

OUTPUT

1. Add book information


2.Display book information
3. no of books in the library
4. Exit
Enter one of the above : 1
Enter book name = HarryPotter
Enter author name = hp
Enter pages = 250
Enter price = 350.6
1. Add book information
2.Display book information
3. no of books in the library
4. Exit
Enter one of the above : 2
you have entered the following information
book name = HarryPotter
author name = hp
pages = 250
price = 350.600006
1. Add book information
2.Display book information
54
3. no of books in the library
4. Exit
Enter one of the above : 3
No of books in library : 1
1. Add book information
2.Display book information
3. no of books in the library
51
4. Exit
Enter one of the above : 4

Criteria Score

Criterion #1 Ability to setup and conduct experiments.


A. Preparation &
Observation Criterion #2 Ability to take measurements / readings and present
data
Criterion #3 Ability to analyze the data theoretically and
B. Results & logically to conclude experimental results
Interpretation Criterion #4 Ability to interpret and discuss any discrepancies
between theoretical and experimental results
Criterion #5 Demonstrate the ability to effectively respond to
C. Viva Voce
questions
Total

RESULT

Thus, to Create a book rack allocation system in a library, which allocates


appropriate space for the books based on category using appropriate data structures.was
executed and output was verified successfully.

55

You might also like