0% found this document useful (0 votes)
16 views74 pages

Bhaskar (r19 Ds Lab Manual (Cse) )

This document contains a C program for performing operations on singly and doubly linked lists such as creation, insertion, deletion and traversal. The program contains functions for inserting nodes at the beginning, end and middle of the list, deleting nodes from the beginning, end and middle, and displaying the elements of the list. It demonstrates the implementation of basic linked list operations using functions on both singly and doubly linked lists.

Uploaded by

sri
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)
16 views74 pages

Bhaskar (r19 Ds Lab Manual (Cse) )

This document contains a C program for performing operations on singly and doubly linked lists such as creation, insertion, deletion and traversal. The program contains functions for inserting nodes at the beginning, end and middle of the list, deleting nodes from the beginning, end and middle, and displaying the elements of the list. It demonstrates the implementation of basic linked list operations using functions on both singly and doubly linked lists.

Uploaded by

sri
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/ 74

CMR TECHNICAL CAMPUS

Kandlakoya(V), Medchal Road, Hyderabad – 501 401


An UGC Autonomous Institute
Accredited by NBA and NAAC with A Grade
Approved by AICTE, New Delhi and Affiliated to JNTU, Hyderabad

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

DATA STRUCTURES

LAB MANUAL

(R19)
WEEK-1

Write a program that uses functions to perform the following operations on singly linked
list.:

i)Creation ii) Insertion iii) Deletion iv) Traversal

PROGRAM:
// Single Linked List

#include<stdio.h>

#include<stdlib.h>

struct node //Each node in list will contain data and next pointer

int data;

struct node *next;

}*start=NULL;

void insertbeg()

struct node *nn;

int a;

nn=(struct node *)malloc(sizeof(struct node));

printf("enter data:");

scanf("%d",&nn->data);

a=nn->data;

nn->next=NULL;

if(start==NULL) //checking if List is empty

printf("\n list is empty, so new node inserted as start node\n");


start=nn;

else //list contains elements

nn->next=start;

start=nn;

printf("\n %d succesfully inserted\n",a);

void insertend()

struct node *nn,*t;

int b;

nn=(struct node *)malloc(sizeof(struct node));

printf("\n enter data:");

scanf("%d",&nn->data);

b=nn->data;

nn->next=NULL;

if(start==NULL) //checking if List is empty

printf("\n list is empty, so new node inserted as start node\n");

start=nn;

else //list contains elements

t=start;
while(t->next!=NULL)

t=t->next;

t->next=nn;

printf("%d is succesfully inserted\n",b);

void insertmid()

struct node *nn,*t=start;

int x,v;

nn=(struct node *)malloc(sizeof(struct node));

if(start==NULL) //checking if List is empty

printf("sll is empty\n");

printf("\n enter data:");

scanf("%d",&nn->data);

v=nn->data;

printf("enter data after which no. is to be inserted:\n");

scanf("%d",&x);

while(t!=NULL && t->data!=x)

t=t->next;
}

if(t==NULL)

printf("\n data does not exist",x);

return;

else

nn->next=t->next;

t->next=nn;

printf("%d succesfully inserted\n",v);

void deletebeg()

struct node *t=start;

if(start==NULL) //list is empty

printf("\n sll is empty");

return;

else

if(start->next==NULL) //list contains single element

start=NULL;
}

else //list contains more than one element

start=start->next;

t->next=NULL;

printf("\n%d is successfully deleted",t->data);

free(t);

void deleteend()

struct node *t=start,*t1;

int x;

if(start==NULL) //list is empty

printf("\n sll is empty");

else

if(start->next==NULL) //list contains single element

start=NULL;

else // list contains more elements

{
t1=t->next;

while(t1->next!=NULL)

t=t->next;

t1=t1->next;

t->next=NULL;

printf("\n%d is successfully deleted",t1->data);

free(t1);

void deletemid()

struct node *t=start,*t1;

int x;

if(start==NULL) //list is empty

printf("\n sll is empty");

else

if(start->next==NULL) //list contains single element

start=NULL;

}
else

printf("enter data to be deleted:");

scanf("%d",&x);

t1=start->next;

while(t1!=NULL && t1->data!=x)

t=t->next;

t1=t1->next;

if(t1==NULL)

printf("\n data does not exist",x);

else

t->next=t1->next;

t1->next=NULL;

printf("\n%d is successfully deleted",t1->data);

free(t1);

}
void display()

struct node *t;

if(start==NULL)

printf("sll is empty\n");

return;

printf("elements are:\n");

t=start;

while(t!=NULL)

printf("--> %d",t->data);

t=t->next;

return;

void main()

int c,a;

do

printf("\n1:insert\n2:delete\n3:display\n4:exit");

printf("\nenter choice:");

scanf("%d",&c);

switch(c)
{

case 1:

printf("1:insert at beg\n2:insert at end\n3:insert at mid");

printf("\nenter choice:");

scanf("%d",&a);

switch(a)

case 1:insertbeg();break;

case 2:insertend();break;

case 3:insertmid();break;

break;

case 2:

printf("1:delete at beg\n2:delete at end\n3:delete at mid");

printf("\nenter choice:");

scanf("%d",&a);

switch(a)

case 1:deletebeg();break;

case 2:deleteend();break;

case 3:deletemid();break;

break;

case 3:display();

break;

case 4:printf("program ends\n");


break;

default:printf("wrong choice\n");

break;

}while(c!=4);

OUTPUT:
WEEK-2

Write a program that uses functions to perform the following operations on doubly
linked list.:

i)Creation ii) Insertion iii) Deletion iv) Traversal

PROGRAM:

// Double Linked List

#include<stdio.h>

#include<stdlib.h>

struct node //Each node in list will contain data, previous and next pointer

struct node *prev;

int data;

struct node *next;

}*start=NULL;

void insertbeg()

struct node *nn;

int a;

nn=(struct node *)malloc(sizeof(struct node));

printf("enter data:");

scanf("%d",&nn->data);

a=nn->data;

nn->next=nn->prev=NULL;

if(start==NULL) //checking if List is empty

{
printf("\n dll is empty, so new node inserted as start node\n");

start=nn;

else //list contains elements

nn->next=start;

start->prev=nn;

start=nn;

printf("\n %d succesfully inserted\n",a);

void insertend()

struct node *nn,*t;

int b;

nn=(struct node *)malloc(sizeof(struct node));

printf("\n enter data:");

scanf("%d",&nn->data);

b=nn->data;

nn->next=nn->prev=NULL;

if(start==NULL) //checking if List is empty

printf("\n dll is empty, so new node inserted as start node\n");

start=nn;

else //list contains elements


{

t=start;

while(t->next!=NULL)

t=t->next;

t->next=nn;

nn->prev=t;

printf("%d is succesfully inserted\n",b);

void insertmid()

struct node *nn,*t=start;

int x,v;

nn=(struct node *)malloc(sizeof(struct node));

if(start==NULL) //checking if List is empty

printf("dll is empty\n");

return;

printf("\n enter data:");

scanf("%d",&nn->data);

v=nn->data;
nn->next=NULL;

nn->prev=NULL;

printf("enter data after which no. is to be inserted:\n");

scanf("%d",&x);

while(t!=NULL && t->data!=x)

t=t->next;

if(t==NULL)

printf("\n data does not exist",x);

return;

else

if(t->next==NULL) // dll contains one element

t->next=nn;

nn->prev=t;

else

t->next->prev=nn;

nn->prev=t;

nn->next=t->next;

t->next=nn;
}

printf("%d succesfully inserted\n",v);

void deletebeg()

struct node *t=start;

if(start==NULL) //list is empty

printf("\n dll is empty");

return;

else

if(start->next==NULL) //list contains single element

start=NULL;

else //list contains more than one element

start=start->next;

t->next=NULL;

start->prev=NULL;

printf("\n%d is successfully deleted",t->data);


free(t);

void deleteend()

struct node *t=start;

int x;

if(start==NULL) //list is empty

printf("\n dll is empty");

return;

else

if(start->next==NULL) //list contains single element

start=NULL;

printf("dll contains only one element and %d is deleted",t->data);

free(t);

else // list contains more elements

while(t->next!=NULL)

t=t->next;

t->prev->next=NULL;
t->prev=NULL;

printf("\n%d is successfully deleted",t->data);

free(t);

void deletemid()

struct node *t=start;

int x;

if(start==NULL) //list is empty

printf("\n dll is empty");

return;

else

if(start->next==NULL) //list contains single element

start=NULL;

printf("dll contains only one element and %d is deleted",t->data);

else

printf("enter data to be deleted:");

scanf("%d",&x);
while(t!=NULL && t->data!=x)

t=t->next;

if(t==NULL)

printf("\n data does not exist",x);

else

if(t->next==NULL) // deleting last element in dll

printf("\ndeleting last element in dll");

deleteend();

return;

if(t->prev==NULL) //deleting first element in dll

printf("\ndeleting first element in dll");

deletebeg();

return;

else //deleting middle element in dll

t->prev->next=t->next;

t->next->prev=t->prev;
t->next=t->prev=NULL;

printf("\n%d is successfully deleted",t->data);

free(t);

void display()

struct node *t;

if(start==NULL)

printf("dll is empty\n");

return;

printf("elements are:\n");

t=start;

while(t!=NULL)

printf("<--> %d",t->data);

t=t->next;

}
void main()

int c,a;

do

printf("\n1:insert\n2:delete\n3:display\n4:exit");

printf("\nenter choice:");

scanf("%d",&c);

switch(c)

case 1:

printf("1:insert at beg\n2:insert at end\n3:insert at mid");

printf("\nenter choice:");

scanf("%d",&a);

switch(a)

case 1:insertbeg();break;

case 2:insertend();break;

case 3:insertmid();break;

break;

case 2:

printf("1:delete at beg\n2:delete at end\n3:delete at mid");

printf("\nenter choice:");

scanf("%d",&a);

switch(a)
{

case 1:deletebeg();break;

case 2:deleteend();break;

case 3:deletemid();break;

break;

case 3:display();

break;

case 4:printf("program ends\n");

break;

default:printf("wrong choice\n");

break;

}while(c!=4);

OUTPUT:
WEEK-3

Write a program that uses functions to perform the following operations on circular
linked list.:

i)Creation ii) Insertion iii) Deletion iv) Traversal

PROGRAM:

// Circular Linked List

#include<stdio.h>

#include<stdlib.h>

struct node //Each node in list will contain data and next pointer

int data;

struct node *next;

}*last=NULL;

void insertbeg()

struct node *nn;

int a;

nn=(struct node *)malloc(sizeof(struct node));

printf("enter data:");

scanf("%d",&nn->data);

a=nn->data;

nn->next=NULL;

if(last==NULL) //checking if List is empty

printf("\n list is empty, so new node inserted as start node\n");

last=nn;
last->next=nn;

else //list contains elements

nn->next=last->next;

last->next=nn;

printf("\n %d succesfully inserted\n",a);

void insertend()

struct node *nn,*t;

int b;

nn=(struct node *)malloc(sizeof(struct node));

printf("\n enter data:");

scanf("%d",&nn->data);

b=nn->data;

nn->next=NULL;

if(last==NULL) //checking if List is empty

printf("\n list is empty, so new node inserted as start node\n");

last=nn;

last->next=nn;

else //list contains elements

nn->next=last->next;
last->next=nn;

last=last->next;

printf("%d is succesfully inserted\n",b);

void insertmid()

if(last==NULL) //checking if List is empty

printf("cll is empty\n");

return;

else

if(last->next==last) //list contains one element

printf("cll contains only one node, So insert begin or end of cll");

else // list contains more than one element

struct node *nn,*t=last;

nn=(struct node *)malloc(sizeof(struct node));

int x,v;

printf("\n enter data:");

scanf("%d",&nn->data);

v=nn->data;

printf("enter data after which no. is to be inserted:\n");


scanf("%d",&x);

while(t->next!=last && t->data!=x)

t=t->next;

if(t!=last)

nn->next=t->next;

t->next=nn;

printf("%d succesfully inserted\n",v);

else

printf("\n data does not exist or it is last node please choose other node",v);

void deletebeg()

if(last==NULL) //list is empty

printf("\n cll is empty");

else

{
if(last->next==last) //list contains single element

struct node *t=last;

last=NULL;

t->next=NULL;

printf("\ncll contains only one element and %d is successfully deleted",t->data);

free(t);

else //list contains more than one element

struct node *t=last->next;

last->next=t->next;

t->next=NULL;

printf("\n%d is successfully deleted",t->data);

free(t);

void deleteend()

if(last==NULL) //list is empty

printf("\n cll is empty");

else

if(last->next==last) //list contains single element


{

struct node *t=last;

last=NULL;

t->next=NULL;

printf("\ncll contains one element and %d is successfully deleted",t->data);

free(t);

else // list contains more elements

struct node *t=last->next,*t1;

while(t->next!=last)

t=t->next;

t->next=last->next;

t1=last;

last=t;

t1->next=NULL;

printf("\n%d is successfully deleted",t1->data);

free(t1);

void deletemid()

if(last==NULL) //list is empty

printf("\n cll is empty");

}
else

struct node *t=last,*t1;

if(last->next==last) //list contains single element

last=NULL;

t->next=NULL;

printf("\ncll contains one element and %d is successfully deleted",t->data);

free(t);

else //list contains more elements

int x;

printf("enter data to be deleted:");

scanf("%d",&x);

t1=t->next;

while(t1->data!=x && t1!=last)

t=t->next;

t1=t1->next;

if(t->next==last)

printf("\n data does not exist or it is last node please choose other node",x);

else

{
t->next=t1->next;

t1->next=NULL;

printf("\n%d is successfully deleted",t1->data);

free(t1);

void display()

if(last==NULL)

printf("\ncll is empty");

else

struct node *t,*t1;

t=last->next;

t1=t;

printf("elements are:\n");

while(t->next!=t1)

printf("--> %d",t->data);

t=t->next;

printf("--> %d",t->data);
}

void main()

int c,a;

do

printf("\n1:insert\n2:delete\n3:display\n4:exit");

printf("\nenter choice:");

scanf("%d",&c);

switch(c)

case 1:

printf("1:insert at beg\n2:insert at end\n3:insert at mid");

printf("\nenter choice:");

scanf("%d",&a);

switch(a)

case 1:insertbeg();break;

case 2:insertend();break;

case 3:insertmid();break;

break;

case 2:

printf("1:delete at beg\n2:delete at end\n3:delete at mid");

printf("\nenter choice:");
scanf("%d",&a);

switch(a)

case 1:deletebeg();break;

case 2:deleteend();break;

case 3:deletemid();break;

break;

case 3:display();

break;

case 4:printf("program ends\n");

break;

default:printf("wrong choice\n");

break;

}while(c!=4);

OUTPUT:

Original List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) ]
Deleted value: (6,56)
Deleted value: (5,40)
Deleted value: (4,1)
Deleted value: (3,30)
Deleted value: (2,20)
Deleted value: (1,10)
List after deleting all items:
[]
WEEK-4

Write a program that implement stack (its operations) using

i) Arrays ii) Pointers

PROGRAM:

// Stack Implementation Using Array

#include<stdio.h>

#include<stdlib.h>

#define max 10

int top=-1, stack[max];

void push();

void pop();

void display();

void main()

int choice;

while(1)

printf("\n\n***** Stack MENU *****\n");

printf("1.push\n2.pop\n3.display\n4.exit\n");

printf("enter your choice:\n");

scanf("%d",&choice);

switch(choice)

case 1: push();break;

case 2: pop();break;

case 3: display();break;

case 4:exit(0);
default: printf("Wrong selection!!! Try again!!!\n");

void push()

int element;

if(top==max-1) //stack is full

printf("Stack is Full!!! Insertion is not possible!!!(stack overflows)\n");

else //stack is not full

printf("enter the element\n");

scanf("%d",&element);

stack[++top]=element;

printf("\nInsertion success!!!");

void pop()

int element;

if(top==-1) //stack is empty

printf("Stack is Empty!!! Deletion is not possible!!!(stack underflows)\n");

else //stack contains elements

printf("the element %d at position %d is deleted\n",stack[top],top);

top=top-1;

}
}

void display()

int i;

if(top==-1) //stack is empty

printf("Stack is Empty!!!(stack underflows)\n");

else //stack contains elements

printf("***Stack elements are***\n");

for(i=top;i>=0;i--)

printf("%d\n",stack[i]);

OUTPUT:

Enter the size of STACK[MAX=100]:10

STACK OPERATIONS USING ARRAY


--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:12

Enter the Choice:1


Enter a value to be pushed:24

Enter the Choice:1


Enter a value to be pushed:98

Enter the Choice:3


The elements in STACK

98
24
12
Press Next Choice
Enter the Choice:2

The popped elements is 98


Enter the Choice:3

The elements in STACK

24
12
Press Next Choice
Enter the Choice:4

EXIT POINT
ii) Stack Using Pointers:
PROGRAM:

//stack implementation using single linked list

#include <stdio.h>

#include <stdlib.h>

struct node

int data;

struct node *next;

}*top=NULL,*top1,*temp;

void push();

void pop();

void display();

void main()

int ch;

while (1)

printf("1.push\n2.pop\n3.display\n4.exit\n");

printf("Enter choice :");

scanf("%d", &ch);

switch (ch)

case 1: push();break;
case 2: pop(); break;

case 3: display();break;

case 4: exit(0);

default :printf(" you have entered wrong choice\n");

void push()

int element;

printf("enter the element\n");

scanf("%d",&element);

if (top == NULL) //stack is empty

top =(struct node *)malloc(sizeof(struct node));

top->next = NULL;

top->data = element;

else //stack contains elements

temp =(struct node *)malloc(sizeof(struct node));

temp->next = top;

temp->data = element;

top = temp;

}
}

void pop()

if (top == NULL) //stack is empty

printf("Error :stack underflow\n");

return;

else //stack contains elements

top1 = top;

top = top->next;

printf("Popped value : %d\n", top1->data);

top1->next=NULL;

free(top1);

void display()

if (top == NULL) //stack is empty

printf("Stack is empty\n");

return;

top1 = top;

printf("***Stack elements are***\n");


while (top1 != NULL) //stack contains elements

printf("%d\n", top1->data);

top1 = top1->next;

}
WEEK-5

Write a program that implement Queue (its operations) using

i)Arrays ii) Pointers

i)Using Arrays

PROGRAM:

//Queue implementation using Array

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define max 6

int queue[max],front=-1,rear=-1;

void add();

void del();

void display();

void main()

int choice;

while(1)

printf("enter the choice\n");

printf("\n1.Add\n2.Delete\n3.Display\n4.Exit\n");

printf("\nenter the choice:");

scanf("%d",&choice);

switch(choice)

case 1: add();break;

case 2: del();break;
case 3: display();break;

case 4:exit(0);

default: printf("you have entered wrong choice\n");

void add()

int element;

if(rear==max-1)

printf("queue is full\n");

else

if(front==-1)

front=0;

printf("enter the element\n");

scanf("%d",&element);

queue[++rear]=element;

void del()

int element;

if(front==-1)

printf("queue is empty\n");

else

{
printf("the element%d at position%d is deleted\n",queue[front-1],++front);

if(front>rear)

front=-1;

rear=-1;

void display()

int i;

if(front==-1)

printf("queue is an empty\n");

else

printf("Queue elements:\n");

for(i=front;i<=rear;i++)

printf("%d\t",queue[i]);

}
(ii) QUEUE USING POINTERS

PROGRAM:

//Queue implementation using SLL

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct Node

int data;

struct Node *next;

}*front = NULL,*rear = NULL;

void insert(int);

void delete();

void display();

void main()

int choice, value;

printf("\n:: Queue Implementation using Linked List ::\n");

while(1){

printf("\n****** MENU ******\n");

printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");

printf("Enter your choice: ");

scanf("%d",&choice);

switch(choice){

case 1: printf("Enter the value to be insert: ");

scanf("%d", &value);

insert(value);
break;

case 2: delete(); break;

case 3: display(); break;

case 4: exit(0);

default: printf("\nWrong selection!!! Please try again!!!\n");

void insert(int value)

struct Node *newNode;

newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode -> next = NULL;

if(front == NULL)

front = rear = newNode;

else{

rear -> next = newNode;

rear = newNode;

printf("\nInsertion is Success!!!\n");

void delete()

if(front == NULL)

printf("\nQueue is Empty!!!\n");

else{
struct Node *temp = front;

front = front -> next;

printf("\nDeleted element: %d\n", temp->data);

free(temp);

void display()

if(front == NULL)

printf("\nQueue is Empty!!!\n");

else{

struct Node *temp = front;

while(temp->next != NULL){

printf("%d--->",temp->data);

temp = temp -> next;

printf("%d--->NULL\n",temp->data);

}
WEEK-6

Write a program that implements the following sorting methods to sort a


given list of integers in ascending order

i)Bubble sort ii) Selection sort iii) Insertion sort

i) BUBBLE SORT:

PROGRAM:

//Bubble Sorting

#include<stdio.h>

void main()

int a[10],i,j,temp,n;

printf("\n Enter the max no.of Elements to Sort: \n");

scanf("%d",&n);

printf("\n Enter the Elements : \n");

for(i=0; i<n; i++)

scanf("%d",&a[i]);

for(i=0; i<n; i++)

for(j=i+1; j<n; j++)

if(a[i]>a[j])

temp=a[i];

a[i]=a[j];

a[j]=temp;

}
}

for(i=0; i<n; i++)

printf("%d\t",a[i]);

}
ii)SELECTION SORT:

PROGRAM:

//Selection Sort

#include<stdio.h>

void main()

int size,i,j,temp,list[100];

printf("Enter the size of the List: ");

scanf("%d",&size);

printf("Enter %d integer values: ",size);

for(i=0; i<size; i++)

scanf("%d",&list[i]);

//Selection sort logic

for(i=0; i<size; i++){

for(j=i+1; j<size; j++){

if(list[i] > list[j])

temp=list[i];

list[i]=list[j];

list[j]=temp;

printf("List after sorting is: ");

for(i=0; i<size; i++)

printf(" %d",list[i]);

}
OUTPUT:
ii) INSERTION SORT:

PROGRAM:

//Insertion Sorting

#include<stdio.h>

void main(){

int size, i, j, temp, list[100];

printf("Enter the size of the list: ");

scanf("%d", &size);

printf("Enter %d integer values: ", size);

for (i = 0; i < size; i++)

scanf("%d", &list[i]);

//Insertion sort logic

for (i = 1; i < size; i++) {

temp = list[i];

j = i - 1;

while ((temp < list[j]) && (j >= 0)) {

list[j + 1] = list[j];

j = j - 1;

list[j + 1] = temp;

printf("List after Sorting is: ");

for (i = 0; i < size; i++)


printf(" %d", list[i]);

OUTPUT:
WEEK-7
Write a program that use both recursive and non recursive functions to
perform the following searching operations for a Key value in a given list of
integers:

i)Linear search ii) Binary search

i)LINEAR SEARCH

{ LINEAR SEARCH RECURSIVE }

PROGRAM:

/* C Program to implement Linear Search recursively */

#include <stdio.h>

int RecursiveLS(int arr[], int value, int index, int n)

int pos = 0;

if(index >= n)

return 0;

else if (arr[index] == value)

pos = index + 1;

return pos;

else

return RecursiveLS(arr, value, index+1, n);

return pos;
}

int main()

int n, value, pos, m = 0, arr[100];

printf("Enter the total elements in the array ");

scanf("%d", &n);

printf("Enter the array elements\n");

for (int i = 0; i < n; i++)

scanf("%d", &arr[i]);

printf("Enter the element to search ");

scanf("%d", &value);

pos = RecursiveLS(arr, value, 0, n);

if (pos != 0)

printf("Element found at pos %d ", pos);

else

printf("Element not found");

return 0;

}
LINEAR SEARCH NON-RECURSIVE:

/* C Program to implement Linear Search non-recursively */


#include<stdio.h>
void main()
{
int list[20],size,i,sElement;

printf("Enter size of the list: ");


scanf("%d",&size);

printf("Enter any %d integer values: ",size);


for(i = 0; i < size; i++)
scanf("%d",&list[i]);

printf("Enter the element to be Search: ");


scanf("%d",&sElement);

// Linear Search Logic


for(i = 0; i < size; i++)
{
if(sElement == list[i])
{
printf("Element is found at %d index", i);
break;
}
}
if(i == size)
printf("Given element is not found in the list!!!");
}

ii)BINARY SEARCH

BINARY SEARCH RECURSIVE:


PROGRAM:

/* C Program to implement binary Search recursively */


#include <stdio.h>

int binarysearch(int a[], int low, int high, int x) {


int mid = (low + high) / 2;
if (low > high) return -1;
if (a[mid] == x) return mid;

if (a[mid] < x)
return binarysearch(a, mid + 1, high, x);
else
return binarysearch(a, low, mid-1, x);
}

int main(void)
{
int a[100];
int len, pos, search_item;

printf("Enter the length of the array\n");


scanf("%d", &len);

printf("Enter the array elements in ascending order\n");


for (int i=0; i<len; i++)
scanf("%d", &a[i]);
printf("Enter the element to search\n");
scanf("%d", &search_item);

pos = binarysearch(a,0,len-1,search_item);

if (pos < 0 )
printf("Cannot find the element %d in the array.\n", search_item);

else
printf("The position of %d in the array is %d.\n", search_item, pos+1);

return 0;
}
BINARY SEARCH NON RECURSIVE:
//binary search non-recursive
#include<stdio.h>
int main()
{
int i, n, key, low, high, mid, a[20];
printf("\n Enter the size of the array :");
scanf("%d",&n);
printf("Enter the array elements in ascending order");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key element\n");
scanf("%d", &key);
low = 0;
high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1;
else
high = mid - 1;
}
}
if(key == a[mid])
printf("The key element is found at location %d", mid + 1);
else
printf("the key element is not found");
return 0;
}
WEEK-8

Write a program to implement the tree traversal methods.

PROGRAM:

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


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 sutree */


printPreorder(node->left);

/* now recur on right subtree */


printPreorder(node->right);
}

/* Driver program to test above functions*/


int main()
{
struct node *root = newNode(8);
root->left = newNode(5);
root->right = newNode(9);
root->left->left = newNode(4);
root->left->right = newNode(6);
root->right->right = newNode(10);

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

Write a program to implement the graph traversal methods.

A) TO IMPLEMENT DEPTH FIRST SEARCH


PROGRAM:

//Graph Traversals DFS


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX 5

struct Vertex {
char label;
bool visited;
};

//stack variables

int stack[MAX];
int top = -1;

//graph variables

//array of vertices
struct Vertex* lstVertices[MAX];

//adjacency matrix
int adjMatrix[MAX][MAX];
//vertex count
int vertexCount = 0;

//stack functions

void push(int item) {


stack[++top] = item;
}

int pop() {
return stack[top--];
}

int peek() {
return stack[top];
}

bool isStackEmpty() {
return top == -1;
}

//graph functions

//add vertex to the vertex list


void addVertex(char label) {
struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex));
vertex->label = label;
vertex->visited = false;
lstVertices[vertexCount++] = vertex;
}
//add edge to edge array
void addEdge(int start,int end) {
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}

//display the vertex


void displayVertex(int vertexIndex) {
printf("%c ",lstVertices[vertexIndex]->label);
}

//get the adjacent unvisited vertex


int getAdjUnvisitedVertex(int vertexIndex) {
int i;

for(i = 0; i < vertexCount; i++) {


if(adjMatrix[vertexIndex][i] == 1 && lstVertices[i]->visited == false)
{
return i;
}
}

return -1;
}

void depthFirstSearch()
{
int i;
//mark first node as visited
lstVertices[0]->visited = true;

//display the vertex


displayVertex(0);

//push vertex index in stack


push(0);

while(!isStackEmpty())
{
//get the unvisited vertex of vertex which is at top of the stack
int unvisitedVertex = getAdjUnvisitedVertex(peek());

//no adjacent vertex found


if(unvisitedVertex == -1)
{
pop();
}
else
{
lstVertices[unvisitedVertex]->visited = true;
displayVertex(unvisitedVertex);
push(unvisitedVertex);
}
}

//stack is empty, search is complete, reset the visited flag


for(i = 0;i < vertexCount;i++)
{
lstVertices[i]->visited = false;
}
}

int main()
{
int i, j;

for(i = 0; i < MAX; i++) // set adjacency


{
for(j = 0; j < MAX; j++) // matrix to 0
adjMatrix[i][j] = 0;
}

addVertex('A'); // 0
addVertex('B'); // 1
addVertex('C'); // 2
addVertex('D'); // 3
addVertex('E'); // 4

addEdge(0, 1); // A - B
addEdge(0, 2); // A - C
addEdge(0, 3); // A - D
addEdge(1, 3); // B - D
addEdge(1, 4); // B - E
addEdge(2, 3); // C - D
addEdge(3, 4); // D - E

printf("Depth First Search: ");


depthFirstSearch();
return 0;
}

OUTPUT:
b) To implement Breadth First Search
PROGRAM:
//Graph Traversals BFS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 5
struct Vertex
{
char label;
bool visited;
};
//queue variables
int queue[MAX];
int rear = -1;
int front = 0;
int queueItemCount = 0;
//graph variables
//array of vertices
struct Vertex* lstVertices[MAX];
//adjacency matrix
int adjMatrix[MAX][MAX];
//vertex count
int vertexCount = 0;
//queue functions
void insert(int data)
{
queue[++rear] = data;
queueItemCount++;
}
int removeData()
{
queueItemCount--;
return queue[front++];
}
bool isQueueEmpty() {
return queueItemCount == 0;
}
//graph functions
//add vertex to the vertex list
void addVertex(char label)
{
struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex));
vertex->label = label;
vertex->visited = false;
lstVertices[vertexCount++] = vertex;
}
//add edge to edge array
void addEdge(int start,int end)
{
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}
//display the vertex
void displayVertex(int vertexIndex)
{
printf("%c ",lstVertices[vertexIndex]->label);
}
//get the adjacent unvisited vertex
int getAdjUnvisitedVertex(int vertexIndex)
{
int i;
for(i = 0; i<vertexCount; i++) {
if(adjMatrix[vertexIndex][i] == 1 && lstVertices[i]->visited == false)
return i;
}
return -1;
}
void breadthFirstSearch()
{
int i;
//mark first node as visited
lstVertices[0]->visited = true;
//display the vertex
displayVertex(0);
//insert vertex index in queue
insert(0);
int unvisitedVertex;
while(!isQueueEmpty()) {
//get the unvisited vertex of vertex which is at front of the queue
int tempVertex = removeData();
//no adjacent vertex found
while((unvisitedVertex = getAdjUnvisitedVertex(tempVertex)) != -1) {
lstVertices[unvisitedVertex]->visited = true;
displayVertex(unvisitedVertex);
insert(unvisitedVertex);
}
}
//queue is empty, search is complete, reset the visited flag
for(i = 0;i<vertexCount;i++) {
lstVertices[i]->visited = false;
}
}
int main()
{
int i, j;
for(i = 0; i<MAX; i++) // set adjacency
{
for(j = 0; j<MAX; j++) // matrix to 0
adjMatrix[i][j] = 0;
}
addVertex('A'); // 0
addVertex('B'); // 1
addVertex('C'); // 2
addVertex('D'); // 3
addVertex('E'); // 4
addEdge(0, 1); // A - B
addEdge(0, 2); // A - C
//addEdge(0, 3); // A - D
addEdge(1, 3); // B - D
addEdge(1, 4); // B - E
addEdge(2, 3); // C - D
addEdge(3, 4); // D - E

printf("\nBreadth First Search: ");

breadthFirstSearch();

return 0;
}
OUTPUT:

You might also like