Dsa Lab Report
Dsa Lab Report
}
}
return 0;
}
// function to add item above the top of the stack
void push(){
int val; //declaring a variable to store the value of element to be pushed
if(top==capacity-1){ //checks stack has some space or stack is full
printf("overflow\n"); //no space, display overflow and exit
}
else{ // if the stack has space then
printf("enter a value"); // enter element to be pushed/added
scanf("%d",&val); //scan the element
top=top+1; //increase top by 1 to point next empty space
stack[top]=val; //add value to the top of stack
printf("value added\n");
}
}
void pop(){
if(top==-1){ //checks stack has some element or stack is empty
printf("underflow\n"); //no element or is empty, display underflow
and exit
}
else{ //if the stack has some element
printf("%d",stack[top]); //print the value pooped/removed
top=top-1; //decrease the value of top by 1
printf("value added");
}
}
void display(){
if(top==-1){ // checks if stack has some element
printf("empty\n"); //no element, display empty
return;
}
else{ //has some element or is not empty
for(i=0;i<=top;i++){
printf("%d",stack[i]); //returning the element which is
present at the top of stack
}
}
}
#include<stdio.h>
#define capacity 10
int queue[capacity],value,front=-1,rear=-1,option; // initializing front and rare
to -1
void enqueue();
void dequeue();
void display();
int main(){
while(option!=4){
printf("enqueue->1\ndequeue->2\ndisplay->3\nexit->4\n");
printf("enter your option");
scanf("%d",&option);
switch(option){
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
printf("enter a valid option");
}
}
return 0;
}
void enqueue(){ //enter value to be added to be queue
printf("enter a number\n");
scanf("%d",&value);
if(rear==capacity-1){ // check if the queue is already full by comparing
rear to capacity-1.
printf("overflow\n"); // if full display overflow
return;
}
if(front==-1 && rear==-1){ //check if empty
front=0; //if empty increase front and rear by 1
rear=0;
}
else{ // if not empty
rear+=1; //increase rear by 1
}
queue[rear]=value; // set element in rear=value
printf("value added\n");
}
void dequeue(){
if(rear==-1||front>rear){ //check if the queue is empty
printf("underflow"); //if empty display underflow
return; //exit
}
if(front==rear){ //if front is equal to rear than(has only one element)
printf("the deleted item is %d"); //print the element in front of
the queue
front=-1; // set front and rear to -1
rear=-1;
}
#include<stdio.h>
#define capacity 10
void enqueue();
void dequeue();
void display();
int main(){
while(option!=4){
printf("enqueue->1\ndequeue->2\ndisplay->3\nexit->4\n");
scanf("%d",&option);
switch(option){
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
return 0;
void enqueue(){
scanf("%d",&value);
rear=0;
void dequeue(){
return ;
rear==-1;
int i;
return; //exit
for(i=0;i<=rear;i++){
printf("Queue[i]");
for(i=front;i<capacity;i++){
printf("Queue[i]");
else{
for(i=front;i<rear;i++){
printf("Queue[i]");
}
LAB WORK 4: Factorial using recursion
Name:Bhumika Bista
#include<stdio.h>
int factorial(int n) { //recursion function
if(n=0){ //base case
return 1;
}
else{
return n* factorial(n-1); //recursion call
}
}
int main(){
int result;
result=factorial(5); //function call
printf("%d",result);
return 0;
}
#include<stdio.h>
int arr[]={1,2,3,4,5};
int n= 5; //sizeof(arr)/sizeof(arr[0]);
int i,key=3,mid,l,r;
return 0;
}
void ins_beg();
void ins_end();
void ins_pos();
void del_beg();
void del_end();
void del_pos();
void display();
int main(){
ins_beg();
ins_end();
ins_pos();
return 0;
}
void ins_beg(){
int info; //data item to be inserted
struct node *newNode;
newNode=(struct node *) malloc (sizeof(struct node));
if(newNode==NULL){
printf("\nERROR\n");
return;
}
else{
printf("\nEnter an item: \n" );
scanf("%d",&info);
newNode->data=info;
newNode->next=head;
head=newNode;
printf("\n%d is inserted at the beginning\n",info);
}
}
void ins_end(){
int info; //data item to be inserted
struct node *newNode,*temp;
newNode=(struct node *) malloc (sizeof(struct node));
if(newNode==NULL){
printf("\nERROR\n");
return;
}
else{
printf("\nEnter an item: \n" );
scanf("%d",&info);
newNode->data=info;
newNode->next=NULL;
//to travel through the list
temp=head;
while(temp->next=NULL){
temp=temp->next;
}
temp->next=newNode;
printf("\n%d is inserted at the end\n",info);
}
}
void ins_pos(){
int info,pos,i; //data item to be inserted
struct node *newNode,*temp;
newNode=(struct node *) malloc (sizeof(struct node));
if(newNode==NULL){
printf("\nERROR\n");
return;
}
else{
printf("\nEnter an item: \n" );
scanf("%d",&info);
printf("Enter position: \n");
scanf("%d",&pos);
newNode->data=info;
temp=head;
for(i=1;i<pos-1;i++){
temp=temp->next;
if(temp==NULL){
printf("\n ERROR!");
}
else{
temp->next=newNode;
}
}
printf("\n%d is inserted at position %d\n",info,pos);
}
}
#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)
// Function declarations
void ins_start();
void ins_end();
void ins_after();
void del_start();
void del_int();
void del_after();
int main() {
ins_start();
//ins_end();
//ins_after();
//ins_after();
//ins_start();
//del_end();
return 0;
}
#include<stdio.h>
void swap(int * a, int *b){
int temp=*a;
*a=*b;
*b=temp;
}
void bubblesort(int arr[],int n){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n-1-i;j++){
if(arr[j]>arr[j+1]){
swap(&arr[j],&arr[j+1]);
}
}
}
}
void print(int arr[],int n){
int i;
for(i=0;i<n;i++){
printf("\n");
printf("%d",arr[i]);
}
}
int main(){
int arr[]={6,5,4,1,8,9,7},n;
//n is the size of arr
n=sizeof(arr)/sizeof(arr[0]);
bubblesort(arr,n);
print(arr,n);
return 0;
}
}
for(j=0;j<=n2;j++){
right[j]=arr[m+1+j];
}
i=0;
j=0;
k=l;
while(i<n1&& j<n2){
if(left[i]<=right[j]){
arr[k]=left[i];
i++;
}
else{
arr[k]=right[j];
j++;
}
k++;
}
while(i<n1){
arr[k]=left[i];
i++;
k++;
}
while(j<n2){
arr[k]=right[j];
j++;
k++;
}
}
//l and r are the left and right index of original array
void mergesort(int arr[],int l, int r){
int m;
if(l<r){
m=l+(r-1)/2;
mergesort(arr,l,m);
mergesort(arr,m+1,r);
merge(arr,l,m,r);
}
}
//for displaying array items
void print(int arr[],int n){
int i;
for(i=0;i<=n;i++){
printf("%d",arr[i]);
}
}
int main(){
//arr[] is input array to be sorted
//n is size of array
int arr[]={3,2,4,5,7,6,8},n;
n=sizeof(arr)/sizeof(arr[0]);
printf("before the array is:\n");
print(arr,n);
mergesort(arr,0,n-1);
printf("\n After the array is:\n");
print(arr,n);
return 0;
}
int main(){
// array we need to sort and number of items in an array
int arr[]={7,2,3,9,8,6},n;
n=sizeof (arr)/sizeof(arr[0]);
heapsort(arr,n);
print(arr,n);
// printf("%d",n);
return 0;
}
LAB WORK 14: Hashing
Name: Bhumika Bista
int main(){
int opt;
while(1){
printf("\n1->insert\n2->search\n3->display");
printf("enter your choice");
scanf("%d",&opt);
switch(opt){
case 1:
insert();
break;
case 2:
search();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
return 0;
}
void insert(){
int key,index;
struct node *newNode;
newNode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter a key:\n");
scanf("%d",&key);
index=key%size;
newNode->data=key;
newNode->next=NULL;
// no collision
if(head[index]==NULL){
head[index]=newNode;
printf("\nInserted:\n");
}
// collision occurs
else{
temp=head[index];
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newNode;
printf("\nItem inserted:\n");
}
}
void search(){
int key,index;
printf("\nEnter the key you wanna search:\n");
scanf("%d",&key);
index=key%size;
if(head[index]==NULL){
printf("\nNot Found!\n");
return;
}
else{
for(temp=head[index];temp!=NULL;temp->next){
if(temp->data==key){
printf("\nItem found at %d\n",head[index]);
break;
}
else{
printf("\nNot Found: \n");
}
}
}
}
void display(){
int i=0;
for(i=0;i<size;i++){
printf("\n");
if(head[i]==NULL){
continue;
}
else{
for(temp=head[i];temp!=NULL;temp=temp->next){
printf("->%d",temp->data);
}
}
}
}
struct queue{
int size;
int f;
int r;
int *arr;
};
int main(){
struct queue q;
q.size=400;
q.f=q.r=0;
q.arr=(int *)malloc(q.size*sizeof(int));
// BFS implementation
int node;
int i=0;
int visited[5]={0,0,0,0,0};
int a [5][5] = {
{0,1,1,0,0},
{1,0,1,1,0},
{1,1,0,0,1},
{0,1,0,0,1},
{0,0,1,1,0}
};
printf("%d",i);
enqueue(&q,i);
while(!isEmpty(&q)){
int node=dequeue(&q);// remove
visited[i]=1;
int j;
for(j=0;j<5;j++){
if(a[node][j]==1 && visited[j]==0){
printf("\t%d",j);
visited[j]=1;
enqueue(&q,j);
}
}
}
return 0;
}
LAB WORK 16: Depth First Traversal
//Bhumika Bista
// DFS implementation
#include<stdio.h>