0% found this document useful (0 votes)
132 views33 pages

Dsa Lab Report

The document discusses several data structures and algorithms implemented in C programming language. It includes programs for stack, queue, circular queue, recursion, searching and sorting.

Uploaded by

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

Dsa Lab Report

The document discusses several data structures and algorithms implemented in C programming language. It includes programs for stack, queue, circular queue, recursion, searching and sorting.

Uploaded by

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

LAB WORK 1:STACK

Name: Bhumika Bista


//complete menu driven program in c to array implementation of stack//
#include<stdio.h>
#define capacity 10
int stack[capacity],i,n,top=-1,option=0; //declaring an array, and variables
void push();
void pop();
void display();
int main(){
while(option!=4){
printf("push->1\npop-2\ndisplay-3\nexit->4\n");
printf("enter your option\n");
scanf("%d",&option);
switch(option){ // allows us to execute one code block among many
alternatives
case1:
push();
break;
case2:
pop();
break;
case3:
display();
break;
default:
printf("enter valid choice");
break;

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

LAB WORK 2:linear queue


//Name:Bhumika Bista

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

else{ // if queue is not empty


printf("the deleted item is %d, oueue[front]"); // print the element in
front of the queue
front=+1; // increment front by one
}
}
void display(){
int i;
for(i=front;i<=rear;i++){
printf("%d",queue[i]); //display the value in the front
}
}
LAB WORK 3:Circular Queue
Name=Bhumika Bista

// Array implementation of circular queue

#include<stdio.h>

#define capacity 10

int Queue[capacity],value,front=-1,rear=-1,option; //declaring array and


variable

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(){

printf("Enter a number\n"); //enter value to be added to the queue

scanf("%d",&value);

if((rear+1)%capacity==front){ //check if the queue is full

printf("Overflow"); //if full print overflow

if(front==-1&&rear==-1){ //check if empty

front=0; //if empty increase front and rrear


by 1

rear=0;

else{ //if not empty


rear=(rear+1)%capacity; //increament rear by 1

Queue[rear]=value;//set element in rear=value

void dequeue(){

if(front==-1&&rear==-1){//check if the queue is empty

printf("Underflow");//if empty print underflow

return ;

if(front==rear){//if front is equal to rear than(has only one element)

printf("%d",Queue[front]);//print the element in front of the queue

front==-1;//set front and rear to -1

rear==-1;

else{//if the queue is not empty

printf("%d",Queue[front]);//print the element in front of the queue

front=(front+1)%capacity;//increment front by one

void display(){ //to display

int i;

if(front==-1){//if queue is empty


printf("Empty");//display empty

return; //exit

if(front>rear){ //if front is greater than rear

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

//factorial using recursion

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

LAB WORK 5:Fibonacci sequence


Name:Bhumika Bista

// Fibonacci sequence using recursion


#include<stdio.h>
int fibonacci(int);
int main(){
int n,i=0,c;
printf("enter the number of terms/n");
scanf("%d",&n);
printf("fibonacci series/n");
for(c=1;c<=n;c++){
printf("%d\n",fibonacci(i));
i++;
}
return 0;
}
int fibonacci(int n) // recursion function
{
if(n==0) //base case
return 0;
else if(n==1)
return 1;
else
return(fibonacci(n-1)+fibonacci(n-2)); //recursive call
}
LAB WORK 6: Tower of Hanoi
Name:Bhumika Bista
// tower of Hanoi
#include<stdio.h>
void TOH(int n,char source,char source,char aux,char destination);
int main(){
int n;
char A,B,c;
printf("enter the number of disc:/n");
scanf("%d",&n);
TOH(n,'A','B','C');
return 0;
}
void TOH(int n,char source,char aux,char destination);
{
if(n==1){
printf("\nmove 1 from %c to %c", source,destination);
}
else{
TOH(n-1,source,destination,aux);
printf("\n move %d disc from %c to %c",n,source,destination);
TOH(n-1,aux,source,destination);
}
}

LAB WORK 7:Binary search


Name: Bhumika Bista

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

int bSearch(int arr[], int l, int r, int key){


//printf("%d %d",l,r);
if(l<r){
mid=l+(r-l)/2;
if(arr[mid]==key){
return mid;
}
if(arr[mid]>key){
return bSearch(arr, l,mid-1,key);
}
if(arr[mid]<key){
return bSearch(arr, mid+1,r,key);
}
}
return -1;
}
int main(){
int a = bSearch(arr,0,n,key);
printf("%d",a);

return 0;
}

LAB WORK 8: Singly Linked List


Name: Bhumika Bista
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};

struct node *head;

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

LAB WORK 9: Doubly


Name: Bhumika Bista

#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;
}
LAB WORK 10: Doubly Linked List
Name: Bhumika Bista
#include <stdio.h>
#include <stdlib.h>

// Creating the node for doubly linked list


struct node {
struct node *prev;
int data;
struct node *next;
};

struct node *head;

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

// Insertion at the beginning


void ins_start() {
struct node *newNode;
int info;
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL) {
printf("\nCannot insert: Memory allocation failed.");
} else {
printf("\nEnter item: ");
scanf("%d", &info);
if (head == NULL) {
newNode->next = NULL;
newNode->prev = NULL;
newNode->data = info;
head = newNode;
} else {
newNode->data = info;
newNode->prev = NULL;
newNode->next = head;
head->prev = newNode;
head = newNode;
}
printf("\n%d is inserted at the beginning.\n", info);
}
}

// Insertion at the end


void ins_end() {
struct node *newNode, *temp;
int info;
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL) {
printf("\nCannot insert: Memory allocation failed.");
} else {
printf("\nEnter item: ");
scanf("%d", &info);
newNode->data = info;
if (head == NULL) {
newNode->next = NULL;
newNode->prev = NULL;
head = newNode;
} else {
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
newNode->next = NULL;
}
printf("\n%d is inserted at the end.\n", info);
}
}

// Insertion after a specific position


void ins_after() {
struct node *newNode, *temp;
int info, pos, i;
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL) {
printf("\nCannot insert: Memory allocation failed.");
} else {
temp = head;
printf("\nEnter the position: ");
scanf("%d", &pos);
for (i = 0; i < pos - 1; i++) {
temp = temp->next;
if (temp == NULL) {
printf("\nThere are less than %d elements.\n", pos);
return;
}
}
printf("\nEnter item: ");
scanf("%d", &info);
newNode->data = info;
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != NULL) {
temp->next->prev = newNode;
}
temp->next = newNode;
printf("\n%d is inserted at position %d.\n", info, pos);
}
}

LAB WORK 11: Bubble Sort


Name: Bhumika Bista

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

LAB WORK 12: Merge sort


Name: Bhumika Bista
// implementation of merge sort in c
#include<stdio.h>
#include<stdlib.h>
//merging divided array items
//l,m,r are left mid and right indeces respectively
void merge(int arr[],int l,int m,int r){
int i,j,k;
//n1 and n2 are the size of left and right sub array
int n1=m-l+1;
int n2=r-m;
int left[n1];
int right[n2];
//copying the elements of array in left and right sub array
for(i=0;i<n1;i++){
left[i]=arr[l+i];

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

LAB WORK 13: Heap Sort


Name : Bhumika Bista
// implement of heap sort
#include<stdio.h>
void swap(int *a,int *b){
int temp=*a;
*a=*b;
*b=temp;
}
void heapify(int arr[],int n, int i){
//largest is i
int largest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n && arr[left]>arr[largest]){
largest=left;
}
if(right<n && arr[right]>arr[largest]){
largest=right;
}
if(largest!=i){
swap(&arr[i],&arr[largest]);
heapify(arr,n,largest);
}
}
void heapsort(int arr[],int n){
//Build max heap
int i;
for(i=n/2-1;i>=0;i--){
heapify(arr,n,i);
}
for(i=n-1;i>=0;i--){
swap(&arr[0],&arr[i]);
heapify(arr,i,0);
}
}
void print(int arr[],int n){
int i;
for(i=0;i<n;i++){
printf("%d",arr[i]);
}
}

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

//implementation of hash table using seperate chaining


#include<stdio.h>
#include<stdlib.h>
#define size 10
struct node{
int data;
struct node *next;
};
struct node *head[size]={NULL},*temp;
void insert();
void search();
void display();

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

LAB WORK 15: Breadth First Search


//Bhumika Bista
// BFS implementation
#include<stdio.h>
#include<stdlib.h>

struct queue{
int size;
int f;
int r;
int *arr;
};

int isEmpty(struct queue *q){


if(q->f==q->r){
return 1;
}
return 0;
}

int isFull(struct queue *q){


if(q->r==q->size-1){
return 1;
}
return 0;
}

void enqueue(struct queue *q, int value){


if(isFull(q)){
printf("This queue is full:");
}
else{
q->r++;
q->arr[q->r]=value;
//insertion complete
}
}

int dequeue(struct queue *q){


int a=-1;
if(isEmpty(q)){
printf("The Queue is empty:");
}
else{
q->f++;
a=q->arr[q->f];
}
return a;
}

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>

void DFS(int p);


//n is no of vertices and graph is sorted in array G[10][10]
int i,j;
int visited[5]={0,0,0,0,0};
int n=5;
int G[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},
};
int main(){
//read the adjecency matrix
DFS(0);
return 0;
}
void DFS(int i){
int j;
printf("\t%d",i);
visited[i]=1;
for(j=0;j<n;j++){
if(!visited[j] && G[i][j]==1){
DFS(j);
}
}
}

You might also like