0% found this document useful (0 votes)
3 views24 pages

(ArjunBhat) Lab-Report (Data Structure and Algorithm)

This document is a lab report for Data Structure and Algorithm (CACS 201) submitted by Arjun Bhat from Tribhuvan University. It includes implementations of various data structures such as stacks, linear queues, single linked lists, and AVL trees, along with their respective operations and outcomes. Each section concludes with a summary of successful implementation and testing results.

Uploaded by

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

(ArjunBhat) Lab-Report (Data Structure and Algorithm)

This document is a lab report for Data Structure and Algorithm (CACS 201) submitted by Arjun Bhat from Tribhuvan University. It includes implementations of various data structures such as stacks, linear queues, single linked lists, and AVL trees, along with their respective operations and outcomes. Each section concludes with a summary of successful implementation and testing results.

Uploaded by

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

Tribhuvan University

Birendra Multiple Campus


Bharatpur-10, Chitwan

Data Structure and Algorithm(CACS 201) Lab Report


Submitted By:

Name: Arjun Bhat

Roll No: 190041

Lab Sheet #1

Submitted To:
Faculty of Humanities & Social Sciences

Lab Date: Marks & Signature

Submission Date:
Title:
Implementation of different operations related to stack.
Objective:
 Implementing the stack from array
 Checking the stack overflow and underflow
 Performing operations like push, pop and display data.
Coding:
#include<stdio.h> printf("enter the
#include<process.h> element");
int stack[3],top,element,ch; scanf("%d",&element);
void push(); stack[top]=element;
void pop(); top++;
void display(); }
void main() else
{ {
printf("\nchoose the operation printf("stack overflow\n");
you wnt to do\n"); printf("\nMax size of stack is
printf("1. push\t2. pop\t3. 3\n");
display\n"); }
printf("enter your choice: \ main();
n"); }
scanf("%d",&ch); void pop()
switch(ch) {
{ if(top>0)
case 1: {
push(); top--;
break; element=stack[top];
case 2: printf("popped element
pop(); is: %d\n",element);
break; }
case 3: else
display(); {
break; printf("stack underflow\n");
default: }
printf("wrong main();
choice\n"); }
} void display(){
main(); int i;
} printf("Stack elements are: \
void push() n");
{ if(top<=0)
if (top<=2) printf("Stack is empty\n");
{
else
{
for(i=top-1;i>=0;i--)
{
printf("%d\t",stack[i]);
}
}
main();
}

Output (after compilation, debugging and Testing)

 Push and stack overflow


 Pop and stack underflow

Conclusion:
Hence the stack is implemented by using array and the condition of stack overflow and
underflow were checked successfully.
Title:
Implementation of different operations related to linear queues.
Objective:
 Implement linear queue.
 Performing insert, delete and display operation
Coding:
#include<stdio.h> }
#define max 5 getch();
#include<process.h> }
int queue_arr[max]; void insert()
int rear=-1; {
int front=-1; int added_item;
void insert(); if(rear==max-1)
void delet(); {
void display(); printf("Queue overflow\n");
void main() printf("the max length of
{ queue is 5\n");
int choice; }
printf("\n1.Insert\t"); Else
printf("2.Delete\t"); {
printf("3.Display\t"); if(front==-1)
printf("4.Quit\n"); front=0;
printf("Enter your choice\ printf("Input the element for
n"); adding in queue\n");
scanf("%d",&choice); scanf("%d",&added_item);
switch(choice){ rear=rear+1;
case 1: queue_arr[rear]=added_item;
insert(); }
break; main();
case 2: }
delet(); void delet(){
break; if(front==-1||front>rear)
case 3: printf("Queue underflow\n");
display(); else
break; {
case 4: printf("Element deleted from
exit(0); queue is
default: %d",queue_arr[front]);
printf("Wrong choice\n"); front=front+1;}
main(); {
} printf("Data in Queue is: \
void display() n");
{ for(i=front;i<=rear;i++)
int i; {
if(front==-1) printf("%d\t",queue_arr[i]);
printf("Queue is empty\ }
n"); }
else main();
}

Output (after compilation, debugging and Testing)

Conclusion:
Hence the linear queue is implemented and related operation were performed successfully.
Title:
Solution of TOH and Fibonacci Series using Recursion
Objective:
 To be able to solve the tower of honoi
 Using the concept of recursion function

Coding:
#include<stdio.h>
tofh(int ndisk, char source, char temp,char dest);
void main()
{
char source='s',temp='t',dest='d';
int ndisk;
printf("Enter number of disks\n");
scanf("%d",&ndisk);
printf("sequence is\n");
tofh(ndisk,source,temp,dest);
getch();
}
tofh(int ndisk,char source,char temp, char dest)
{
if(ndisk>0)
{
tofh(ndisk-1,source,dest,temp);
printf("move disk %d %c->%c\n",ndisk,source,dest);
tofh(ndisk-1,temp,source,dest);
}
}
Output (after compilation, debugging and Testing)
Conclusion:
Hence the solution of TOH was done by the help of recursion function
Title:
Implementation of different operations related to single linked list.
Objective:
 Implementing the single linked list in C
 Creating new list
 Adding data at beginning and at between
 Delete, display, search and count the data
 Performing operation using switch case.
Coding:
# include <stdio.h> printf("Enter the element:“ );
# include <malloc.h> scanf("%d",&m);
# include <process.h> create_list(m);
struct node }
{ break;
int info; case 2:
struct node *link; printf("Enter the
}*start; element : ");
main(){ scanf("%d",&m);
int choice,n,m,position,i; addatbeg(m);
clrscr(); break;
start=NULL; case 3:
while(1) { printf("Enter the
printf("1.Create List\ element : ");
t"); scanf("%d",&m);
printf("2.Add at printf("Enter the
begining\t"); position after which this
printf("3.Add after \t"); element is inserted : ");
printf("4.Delete\n"); scanf("%d",&position);
printf("5.Display\t");
printf("6.Count\t\t"); addafter(m,position);
printf("7.Search\t"); break;
printf("8.Quit\n"); case 4:
printf("Enter your choice if(start==NULL){
: "); printf("List is
scanf("%d",&choice); empty\n");
switch(choice) continue; }
{ printf("Enter the
case 1: element for deletion : ");
printf("How many scanf("%d",&m);
nodes you want : "); del(m);
scanf("%d",&n); break;
for(i=0;i<n-1;i++)
{
case 5: addafter(int data,int pos)
display(); {
break; struct node *tmp,*q;
case 6: int i;
count(); q=start;
break; for(i=0;i<pos-1;i++)
case 7: {
printf("Enter the q=q->link;
element to be searched : "); if(q==NULL)
scanf("%d",&m); {
search(m); printf("There are
break; less than %d elements",pos);
case 8: return;
exit(0); }
default: }
printf("Wrong
choice\n"); tmp=malloc(sizeof(struct node)
}}} );
create_list(int data) tmp->link=q->link;
{ tmp->info=data;
struct node *q,*tmp; q->link=tmp;
tmp= malloc(sizeof(struct return;
node)); }
tmp->info=data; del(int data)
tmp->link=NULL; {
if(start==NULL) struct node *tmp,*q;
start=tmp; if(start->info == data)
else {
{ tmp=start;
q=start; start=start->link;
while(q->link!=NULL) free(tmp);
q=q->link; return;
q->link=tmp; }
} q=start;
return; while(q->link->link != NULL)
} {
addatbeg(int data) if(q->link->info==data)
{ {
struct node *tmp; tmp=q->link;
tmp=malloc(sizeof(struct q->link=tmp->link;
node)); free(tmp);
tmp->info=data; return;
tmp->link=start; }
start=tmp; q=q->link;
return;
}
}/*End of while */ count()
if(q->link->info==data) {
{ struct node *q=start;
tmp=q->link; int cnt=0;
free(tmp); while(q!=NULL)
q->link=NULL; {
return; q=q->link;
} cnt++;
printf("Element %d not found\ }
n",data);
return; printf("Number of elements are
} %d\n",cnt);
return;
display() }
{
struct node *q; search(int data)
if(start == NULL) {
{ struct node *ptr = start;
printf("List is empty\ int pos = 1;
n"); while(ptr!=NULL)
return; {
} if(ptr->info==data)
q=start; {
printf("List is :\n"); printf("Item %d
while(q!=NULL) found at position %d\
{ n",data,pos);
printf("%d ", q->info); return;
q=q->link; }
} ptr = ptr->link;
printf("\n"); pos++;
return; }
} if(ptr == NULL)
printf("Item %d not found
in list\n",data);
return;
}
Output (after compilation, debugging and Testing)
 Creating and displaying list

 Adding at beginning and in between


 counting, deleting, searching data

Conclusion:
Hence the single linked list was implemented and operations like creating, displaying, counting,
adding at beginning, deleting and searching data were performed successfully in C.

Title:
Implementation of AVL tree and balancing it
Objective:
 To implement AVL tree
 To make it balance
Coding:
#include<stdio.h> scanf("%d", &info);
#include<malloc.h>
if( search(root,info) ==
typedef enum { FALSE ,TRUE }
NULL ) root = insert(info,
bool;
root, &ht_inc);
struct node
else
{
int info;
printf("Duplicate value
int balance;
ignored\n");
struct node *lchild;
break;
struct node *rchild;
case 2:
};
if(root==NULL)
struct node *insert (int ,
{
struct node *, int *);
printf("Tree is
struct node* search(struct
empty\n");
node *,int);
continue;
main()
}
{
printf("Tree is :\
bool ht_inc;
n");
int info ;
display(root, 1);
int choice;
printf("\n\n");
struct node *root = (struct
printf("Inorder
node *)malloc(sizeof(struct
Traversal is: ");
node));
inorder(root);
root = NULL;
printf("\n");
while(1)
break;
{
case 3:
printf("1.Insert\n");
exit(1);
printf("2.Display\n");
default:
printf("3.Quit\n");
printf("Wrong
printf("Enter your choice
choice\n");
: ");
}/*End of switch*/
scanf("%d",&choice);
}/*End of while*/
switch(choice)
}/*End of main()*/
{
struct node* search(struct
case 1:
node *ptr,int info)
printf("Enter the value to be
{
inserted : ");

if(ptr!=NULL) pptr->balance = 1;
if(info < ptr->info) break;
ptr=search(ptr- case 1: /* Left heavy */
>lchild,info); aptr = pptr->lchild;
else if( info > ptr- if(aptr->balance == 1)
>info) {
ptr=search(ptr- printf("Left to Left Rotation\
>rchild,info); n");
return(ptr); pptr->lchild= aptr->rchild;
}/*End of search()*/ aptr->rchild = pptr;
struct node *insert (int info, pptr->balance = 0;
struct node *pptr, int aptr->balance=0;
*ht_inc) pptr = aptr;
{ }
struct node *aptr; else
struct node *bptr; {
printf("Left to
if(pptr==NULL) right rotation\n");
{ bptr = aptr->rchild;
pptr = (struct node *) aptr->rchild = bptr->lchild;
malloc(sizeof(struct node)); bptr->lchild = aptr;
pptr->info = info; pptr->lchild = bptr->rchild;
pptr->lchild = NULL; bptr->rchild = pptr;
pptr->rchild = NULL; if(bptr->balance == 1 )
pptr->balance = 0; pptr->balance = -1;
*ht_inc = TRUE; else
return (pptr); pptr->balance = 0;
} if(bptr->balance == -1)
if(info < pptr->info) aptr->balance = 1;
{ else
pptr->lchild = aptr->balance = 0;
insert(info, pptr->lchild, bptr->balance=0;
ht_inc); pptr=bptr;
if(*ht_inc==TRUE) }
{ *ht_inc = FALSE;
switch(pptr- }/*End of switch */
>balance) }/*End of if */
{ }/*End of if*/
case -1: /* Right if(info > pptr->info)
heavy */ {
pptr->balance = pptr->rchild =
0; insert(info, pptr->rchild,
*ht_inc = ht_inc);
FALSE; if(*ht_inc==TRUE)
break;
case 0:

{ else
switch(pptr->balance) aptr->balance = 0;
{ bptr->balance=0;
case 1: /* Left heavy */ pptr = bptr;
pptr->balance = 0; }/*End of else*/
*ht_inc = FALSE; *ht_inc =
break; FALSE;
case 0: /* Balanced */ }/*End of switch */
pptr->balance = -1; }/*End of if*/
break; }/*End of if*/
case -1: /* Right heavy */ return(pptr);
aptr = pptr->rchild; }/*End of insert()*/
if(aptr->balance == -1) display(struct node *ptr,int
{ level)
printf("Right to Right {
Rotation\n"); int i;
pptr->rchild= aptr->lchild; if ( ptr!=NULL )
aptr->lchild = pptr; {
pptr->balance = 0; display(ptr->rchild,
aptr->balance=0; level+1);
pptr = aptr; printf("\n");
} for (i = 0; i < level; i++)
else printf(" ");
{ printf("%d", ptr->info);
printf("Right to Left display(ptr->lchild,
Rotation\n"); level+1);
bptr = aptr->lchild; }/*End of if*/
aptr->lchild = bptr->rchild; }/*End of
bptr->rchild = aptr; display()*/inorder(struct node
pptr->rchild = bptr->lchild; *ptr)
bptr->lchild = pptr; {
if(bptr->balance == -1) if(ptr!=NULL)
pptr->balance = 1; {
else inorder(ptr->lchild);
pptr->balance = 0; printf("%d ",ptr->info);
if(bptr->balance == 1) inorder(ptr->rchild);
aptr->balance = -1; }
}
Output (after compilation, debugging and Testing)

Conclusion:
Hence the avl tree is implemented and is balanced .
Objective:
 To implement Fibonacci series
 To use recursive function.

Coding:

#include<stdio.h>
int fib(int n);
void main()
{
int nterms,i=0;
printf("Enter number of terms\n");
scanf("%d",&nterms);
printf("%d\t",i);
for(i=0;i<nterms-1;i++)
{
printf("%d\t",fib(i));
}
getch();
}
int fib(int n)
{
if(n==0||n==1)
return 1;
else
return(fib(n-1)+fib(n-2));
}
Output (after compilation, debugging and Testing)

Conclusion:
Hence the Fibonacci series is implemented successfully.
Tribhuvan University
Birendra Multiple Campus
Bharatpur-10, Chitwan

Data Structure and Algorithm(CACS 201) Lab Report


Submitted By:

Name: Arjun Bhat

Roll No: 190041

Lab Sheet #7

Submitted To:
Faculty of Humanities & Social Sciences

Lab Date: Marks & Signature

Submission Date:
Title:
Implementation of bubble short
Objective:
 To short the data using bubble short method
Coding:

temp = arr[j];
#include <stdio.h> arr[j] = arr[j+1];
#define MAX 20 arr[j+1] = temp;
void main() xchanges++;
{ }/*End of if*/
int }/*End of inner for loop*/
arr[MAX],i,j,k,temp,n,xchanges; if(xchanges==0) /*If list is
printf("Enter the number of sorted*/
elements : "); break;
scanf("%d",&n); printf("After Pass %d elements
for (i = 0; i < n; i++) are : ",i+1);
{ for (k = 0; k < n; k++)
printf("Enter element %d : printf("%d ", arr[k]);
",i+1); printf("\n");
scanf("%d",&arr[i]); }/*End of outer for loop*/
} printf("Sorted list is :\n");
printf("Unsorted list is :\ for (i = 0; i < n; i++)
n"); printf("%d ", arr[i]);
for (i = 0; i < n; i++) printf("\n");
printf("%d ", arr[i]); getch();
printf("\n"); }
/* Bubble sort*/
for (i = 0; i < n-1 ; i++)
{
xchanges=0;
for (j = 0; j <n-1-i; j++)
{
if (arr[j] > arr[j+1])
{
Output (after compilation, debugging and Testing)

Conclusion:
Hence the sorting of data using bubble short method is successfully done.
Title:

Implementation of sequential searching technique


Objective:
 To use sequential searching technique.
Coding:

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[100],i,n,item;
printf("How many elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element: ");
scanf("%d",&arr[i]);
}

printf("Enter an element to be searched: ");


scanf("%d",&item);
for(i=0;i<n;i++)
{
if(item==arr[i])
{
printf("%d is found in %d location",item,i+1);
break;
}
}
if(i==n)
printf("%d is not found in given array",item);
getch();
}
Output (after compilation, debugging and Testing)

Conclusion:
Hence the sequential searching technique is implemented successfuly

You might also like