BCS3L1-Data Structure lab
BCS3L1-Data Structure lab
PREPARED BY
Mrs.S Jenita Christy
SCHOOL OF COMPUTING
1|Page
LAB MANUAL
Regualtion R 2015
(2015-2016)
2|Page
BCS3L1 DATASTRUCTURES USING C LAB L T P C
Total Contact Hours - 30 0 0 3 2
Prerequisite –Fundamental of Computing and Programming, Data Structures, C
Programming.
Lab Manual Prepared by – Dept. of Computer Science & Engineering
OBJECTIVES
This course demonstrates familiarity with major algorithms and data structures and analyzes
performance of algorithms. It is used to choose the appropriate data structure and algorithm design
method for a specified application and determine which algorithm or data structure to use in different
scenarios.
COURSE OUTCOMES (COs)
CO1 Implement various basic data structures and its operations.
CO2 Implement various sorting and searching algorithms.
CO3 Implement various tree operations.
CO4 Implement various graphs algorithms.
CO5 Develop simple applications using various data structures.
CO6 Develop algorithms using various searching and sorting techniques.
MAPPING BETWEEN COURSE OUTCOMES & PROGRAM OUTCOMES
(3/2/1 INDICATES STRENGTH OF CORRELATION) 3- High, 2- Medium, 1-Low
COs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1 2 2 2 3 2 2 3 1 1 2 2 3
CO2 3 2 2 3 3 2 2 1 1 2 2 3
CO3 3 3 2 2 3
CO4 3 2 1 2 3
CO5 2 2 3 3 2 3 1 1 2 2 3
CO6 3 3 3 2 2 3 1 1 2 2 3
Category Professional Core (PC)
Approval 37th Meeting of Academic Council, May 2015
LIST OF EXPERIMENTS:
1. Simple Cprograms -Control Structures -Functions - Aggregate data types-File handling
2. Implementation of-Lists, Stacks, Queues (Using Arrays, linked lists)-Trees -
Searching andSorting algorithms
3|Page
[BCS3L1]-[DATA STRUCTURES LABORATORY]
LIST OF EXPERIMENTS
Sl.No Title
1. Simple C programs
Control Structures
Functions
Aggregate data types
File handling
2.
Implementation of
Lists, Stacks, Queues (Using Arrays, linked lists)
Trees
Searching and Sorting algorithms
4|Page
CONTENT
5|Page
Ex No :1 CONVERT THE CELSIUS VALUE TO FAHRENHEIT
AIM:
Write a Program in C to convert the Celsius value to Fahrenheit using Functions
ALGORITHM:
STEP 6: Stop
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
float celsius;
float fahrenheit;
printf("Enter the Celsius value: ");
scanf("%f", &celsius);
fahrenheit = (1.8 * celsius) + 32.0;
printf("The Fahrenheit value of the given %f celsius value is %f", celsius, fahrenheit);
6|Page
getch();
}
OUTPUT:
RESULT:
Thus the C program to convert the Celsius value to Fahrenheit using Functions was
written,executed and the output was verified successfully.
7|Page
Ex No :2 FIND THE FACTORIAL OF A GIVEN NUMBER USING RECURSIVE
FUNCTION
AIM:
Write a Program in C to find the Factorial of a given number using Recursive function.
ALGORITHM:
STEP 1: Start the Program
STEP 2: Declare and Initialize the input variables
STEP 3: Enter the number
STEP 4: Call the Recursive Function passing the number to the recursive function as an
Argument.
STEP 5: If the entered number is equal to one, then return one to Main function
STEP 6: If the number is less greater than one then call Recursive
STEP 7: Print the Factorial value of the number
STEP 8: Stop
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
int recur(int b);
void main()
{
8|Page
clrscr();
int num, a;
printf("Enter the Number");
scanf("%d", &num);
a = recur(num);
printf("The Factorial of the number %d is %d", num, a);
getch();
}
int recur(int no)
{
int fact;
if(no == 1)
return(1);
else
fact = no * recur(no - 1);
return(fact);
}
OUTPUT:
RESULT :
Thus the C program to find the Factorial of a given number using Recursive function was
written,executed and the output was verified successfully.
9|Page
Ex No :3
AIM:
ALGORITHM:
STEP 3: Open the File in the Write Mode Using File Pointer
STEP 5: Store the input data in the file using the putc() statement
STEP 7: Open the File in the Read Mode using the File Pointer
SOURCE CODE :
#include <stdio.h>
#include <stdlib.h>
struct person
{
int id;
char fname[20];
char lname[20];
};
int main ()
{
FILE *infile;
struct person input;
10 | P a g e
// read file contents till end of file
while(fread(&input, sizeof(struct person), 1, infile))
printf ("id = %d name = %s %s\n", input.id,
input.fname, input.lname);
// close file
fclose (infile);
return 0;
}
OUTPUT:
RESULT:
Result: Thus the C program to Read and Write the Contents of a File was written,executed and
the output was verified successfully.
11 | P a g e
Ex No :4
AIM :
To write a Program in C Using Standard I/O Library Function with Arguments and Return Value.
ALGORITHM:
STEP 6: Return the addition value to the called function from the calling function
STEP 8: Stop
#include <stdio.h>
#include <conio.h>
int add(int x, int y);
void main()
{
clrscr();
int a, b, c;
12 | P a g e
printf("Enter the Two Numbers:");
scanf("%d %d", &a, &b);
c = add(a, b);
printf("The Addition of Two Numbers %d and %d is %d", a, b, c);
getch();
}
OUTPUT :
RESULT:
Thus the C program using Standard I/O Library Function with Arguments and Return Value.was
written,executed and the output was verified successfully.
13 | P a g e
Ex No :5
PROGRAM TO FIND THE SIZE OF DATA TYPES
AIM:
ALGORITHM:
STEP 3: Print the size of the data types using the statement sizeof()
STEP 4: Stop
SOURCE CODE
#include <stdio.h>
#include <conio.h>
void main()
{
Clrscr();
int i = 10;
float f = 25.005;
char name[] = “Welcome”;
printf(“\n The Size of Integer is %d”, sizeof(i));
printf(“\n The Size of Float is %d”, sizeof(f));
printf(“\n The Size of Character is %d”, sizeof(name));
getch();
}
14 | P a g e
OUTPUT
RESULT:
Thus the C program to find the Size of Data Types was written,executed and the output was
verified successfully.
15 | P a g e
Ex No :6
AIM:
STEP 1: Start
STEP 5: Top<-New
STEP 6: Stack_Head.Link<-Top
STEP 7: Stop
STEP 1: Start
STEP 3: Else
16 | P a g e
STEP 4: Stop
SOURCE CODE :
#include<stdio.h>
17 | P a g e
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
}*top = NULL; // Initially the list is empty
void push(int);
void pop();
void display();
int main()
{
int choice, value;
printf("\nIMPLEMENTING STACKS USING LINKED LISTS\n");
while(1){
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nInvalid Choice\n");
}}}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value; // get value for the node
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top; // Make the node as TOP
top = newNode;
printf("Node is Inserted\n\n");
}
void pop()
{
if(top == NULL)
printf("\nEMPTY STACK\n");
else{
struct Node *temp = top;
18 | P a g e
printf("\nPopped Element : %d", temp->data);
printf("\n");
top = temp->next; // After popping, make the next node as TOP
free(temp);
}}
void display()
{
// Print the stack
if(top == NULL)
printf("\nEMPTY STACK\n");
else
{
printf("The stack is \n");
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n\n",temp->data);
}}
OUTPUT:
RESULT:
Thus the C program for Implementation of Stacks Using Linked Lists was written,executed and
the output was verified successfully.
19 | P a g e
Ex No :7
AIM:
ALGORITHM:
INSERTION:
PUSH(item)
STEP 1. if(item=max of stack)
Print “Overflow”
Return
STEP 2. Top = Top + 1
STEP 3. Stack[Top]=item
STEP 4. Return
DELETION
POP(item)
STEP 1. If(top = -1)
Print “Underflow”
Return
STEP 2. Item = Stack[Top]
STEP 3. Top = Top – 1
STEP 4. Return1
DISPLAY
STEP 1. If Top = - 1
Print “Underflow”
STEP 2. Repeat Step 3 for I = top to I >= 0
STEP 3. Print Stack[I]
STEP 4. Return
20 | P a g e
21 | P a g e
SOURCE CODE :
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
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)");
}
22 | P a g e
}
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:
23 | P a g e
STACK OPERATIONS USING ARRAY
--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:12
98
24
12
Press Next Choice
Enter the Choice:2
24
12
Press Next Choice
Enter the Choice:4
EXIT POINT
RESULT:
Thus the C program for Implementation of Stacks Using Arrays was written,executed and the
output was verified successfully.
24 | P a g e
Ex No :8
AIM:
ALGORITHM:
25 | P a g e
SOURCE CODE:
#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");
}
26 | P a g e
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue 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");
}
}}
return 0;}
OUTPUT:
Enter no 1:10
Enter no 2:54
Enter no 3:98
Enter no 4:234
27 | P a g e
Enter the Choice:3
Deleted Element is 10
Enter the Choice:3
RESULT:
Thus the C program for Implementation of Queue Using Arrays was written,executed and the
output was verified successfully.
28 | P a g e
Ex No :9
BINARY-TREE ALGORITHM
AIM:
ALGORITHM:
STEP 3: Insert the Key-Element Pair into the Tree, overwriting Existing pair, if any, with same
key
STEP 6: If Pair First Element is Less Than Element of first P, assign P as Left Child.
STEP 7: If Pair First Element is Greater than Element of first P, assign P as Right Child
STEP 9: Retrieve a Node for the Pair and assign to Pair Pointer Element
STEP 11: Now, Handle Input Operation – Insert, Delete, ListOrder Accordingly
STEP 12: If Pair First Element is Less Than Pair Pointer First Element; LeftChildNode = NewNode
STEP 13: Else, Pair Pointer First Element RightChildNode = NewNode. Else, Root = NewNode
29 | P a g e
30 | P a g e
# include <stdio.h>
# include <malloc.h>
struct node
{
int info;
struct node *lchild;
struct node *rchild;
}*root;
while(ptr!=NULL)
{
if(item==ptr->info)
{ *loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}/*End of while */
*loc=NULL; /*item not found*/
31 | P a g e
*par=ptrsave;
}/*End of find()*/
if(parent==NULL)
root=tmp;
else
if(item<parent->info)
parent->lchild=tmp;
else
parent->rchild=tmp;
}/*End of insert()*/
/*Initialize child*/
if(loc->lchild!=NULL) /*item to be deleted has lchild */
child=loc->lchild;
else /*item to be deleted has rchild */
child=loc->rchild;
32 | P a g e
root=child;
else
if( loc==par->lchild) /*item is lchild of its parent*/
par->lchild=child;
else /*item is rchild of its parent*/
par->rchild=child;
}/*End of case_b()*/
suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}/*End of case_c()*/
int del(int item)
{
struct node *parent,*location;
if(root==NULL)
{
printf("Tree empty");
return 0;
}
find(item,&parent,&location);
33 | P a g e
if(location==NULL)
{
printf("Item not present in tree");
return 0;
}
34 | P a g e
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
printf("%d ",ptr->info);
}
}/*End of postorder()*/
switch(choice)
{
case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);
insert(num);
35 | P a g e
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
display(root,1);
break;
case 7:
break;
default:
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main()*/
OUTPUT:
36 | P a g e
RESULT:
Thus the C program for Implementation of Implement Binary-Tree Algorithm was
written,executed and the output was verified successful
37 | P a g e
Ex No :10
QSORT Function To Implement Linear Sorting
AIM:
Write a Program in C using the QSORT function to Implement Linear Sorting on the given Array
Object.
ALGORITHM:
a) temp = a[low]
b) a[low] = a[high]
c) a[high] = temp
d) low = low + 1
e) high = high + 1
38 | P a g e
STEP 9. if (h > low) quicksort(a, low, h)
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int comparator(const void* str1, const void* str2) {
if(strcmp(*(const char**)str1, *(const char**)str2) >= 0)
return 1;
else return 0;
}
int main() {
const char* arr[] = {"Rishabh", "Jyoti", "Palak", "Akash"};
int n = sizeof(arr) / sizeof(arr[0]);
printf("\nGiven array of names: \t");
for (int i = 0; i < n; i++) printf("%s \t", arr[i]);
qsort(arr, n, sizeof(const char*), comparator);
printf("\nSorted array of names: \t");
for (int i = 0; i < n; i++)
printf("%s \t", arr[i]);
return 0;
}
OUTPUT:
Given array of names: Rishabh Jyoti Palak Akash
Sorted array of names: Akash Jyoti Palak Rishabh
39 | P a g e
RESULT:
Thus the C program for the QSORT function to Implement Linear Sorting was written,executed
and the output was verified successfully
40 | P a g e