Sunteți pe pagina 1din 74

VISION OF THE DEPARTMENT:

To spark the imagination of the Computer Science Engineers with values, skills and creativity to
solve the real world problems.

MISSION OF THE DEPARTMENT:

MISSION 1: To inculcate creative thinking and problem solving skills through effective teaching,
learning and research.

MISSION 2: To empower professionals with core competency in the field of Computer Science
and Engineering.

MISSION 3: To foster independent and life-long learning with ethical and social responsibilities.

PROGRAMME EDUCATIONAL OBJECTIVES:

PEO 1: To empower students with effective computational and problem solving skills.

PEO 2: To enable students with core skills for employment and entrepreneurship.

PEO 3: To imbibe students with ethical values and leadership qualities.

PEO 4: To foster students with research oriented ability which helps them in analyzing and solving
real life problems and motivate them for pursuing higher studies.
PROGRAMME OUTCOMES:
Engineering Graduates will be able to:
PO1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
PO2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
PO3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
PO4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions.
PO5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
PO6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
PO7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.
PO8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
PO9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
PO10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
PO11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
PO12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change
PROGRAMME SPECIFIC OUTCOMES:
PSO 1: An ability to apply foundation of Computer Science and Engineering, algorithmic principles and
theory in designing and modelling computation based systems.
PSO 2: The ability to demonstrate software development skills.
Data Structure Lab (RCS-355)

COURSE OUTCOMES:

By the end of the course students will be able to,

C209.1: Implement elementary data structures such as arrays and linked lists.

C209.2: Implement various data structures using arrays.

C209.3: Implement various data structures using linked lists.

C209.4: Implement sorting and searching techniques.


PROGRAM 1
AIM : TO IMPLEMENT INSERTION ,DELETION AND SEARCHING IN AN
ARRAY
CODE:

#include<stdio.h>

void create(int[],int);

void insertion_begin(int[]);

void insertion_loc(int[],int);

void deletion_loc(int[],int);

void search_ele(int[],int);

void display(int[]);

int n;

int main()

int arr[100],t=0;

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

scanf("%d",&n);

create(arr,n);

while(t==0)

printf("Enter your choice\n");

printf("Enter 1 for insertion at beginning\n");

printf("Enter 2 for insertion at location\n");

printf("Enter 3 for deletion\n");

printf("Enter 4 for searching\n");

printf("Enter 5 to exit\n");

int ch;
scanf("%d",&ch);

switch(ch)

case 1:

insertion_begin(arr);

break;

case 2:

printf("Enter the location where the element to be inserted\n");

int l;

scanf("%d",&l);

insertion_loc(arr,l-1);

break;

case 3:

printf("Enter the location where the element to be DELETED\n");

scanf("%d",&l);

deletion_loc(arr,l-1);

break;

case 4:

printf("Enter the the element to be searched\n");

int e;

scanf("%d",&e);

search_ele(arr,e);

break;

case 5:

t=1;

break;

default:

printf("wrong choice\n");

}
}

return 0;

void create(int a[],int n)

int i,y;

printf("ENTER THE ELEMENTS \n");

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

scanf("%d",&y);

a[i]=y;

display(a);

void insertion_begin(int a[])

int e,i;

printf("enter the element \n");

scanf("%d",&e);

for(i=n-1;i>=0;i--)

a[i+1]=a[i];

a[0]=e;

n=n+1;

display(a);

void insertion_loc(int a[],int l)

{
int e,i;

printf("enter the element\n");

scanf("%d",&e);

for(i=n-1;i>=l;i--)

a[i+1]=a[i];

a[l]=e;

n=n+1;

display(a);

void deletion_loc(int a[],int l)

int i;

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

a[i]=a[i+1];

n=n-1;

display(a);

void display(int a[])

int i;

printf("THE ELEMENTS ARE\n");

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

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

void search_ele(int a[],int e)


{

int i,t=0,l;

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

if(a[i]==e)

t=1;

l=i;

break;

if(t==1)

printf("THE ELEMENT IS FOUND AT LOCATION %d\n",l+1);

else

printf("THE ELEMENT IS NOT FOUND\n");

}
INPUT AND OUTPUT
Enter the size of the array

ENTER THE ELEMENTS

THE ELEMENTS ARE

1 2 3 4 5 Enter your choice

Enter 1 for insertion at beginning

Enter 2 for insertion at location

Enter 3 for deletion

Enter 4 for searching

Enter 5 to exit

enter the element

THE ELEMENTS ARE

6 1 2 3 4 5 Enter your choice

Enter 1 for insertion at beginning

Enter 2 for insertion at location

Enter 3 for deletion

Enter 4 for searching

Enter 5 to exit

Enter the location where the element to be inserted


7

enter the element

THE ELEMENTS ARE

6 1 2 3 4 5 8 Enter your choice

Enter 1 for insertion at beginning

Enter 2 for insertion at location

Enter 3 for deletion

Enter 4 for searching

Enter 5 to exit

Enter the location where the element to be DELETED

THE ELEMENTS ARE

6 1 2 3 4 5 Enter your choice

Enter 1 for insertion at beginning

Enter 2 for insertion at location

Enter 3 for deletion

Enter 4 for searching

Enter 5 to exit

Enter the the element to be searched

THE ELEMENT IS FOUND AT LOCATION 4

Enter your choice

Enter 1 for insertion at beginning

Enter 2 for insertion at location

Enter 3 for deletion

Enter 4 for searching


Enter 5 to exit

Process returned 0 (0x0) execution time : 379.591 s

Press any key to continue.


PROGRAM 2
AIM: TO IMPLEMENT ADDITION ,MULTIPLICATION AND TRANSPOSE OF A
MATRIX
CODE:

#include<stdio.h>

void input1();

void input2();

void add();

void multiply();

void transpose();

void display(int,int);

int i=4;

int a[50][50],b[50][50],ans[50][50];

int r1,r2,c1,c2;

void main()

int m,n;

for(m=0;m<r1;m++)

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

ans[m][n]=0;

printf("ENTER YOUR CHOICE \n 1)ARRAY ADDITION \n 2)ARRAY MULTIPILCATON \n 3)ARRAY TRANSPOSE \n


0)EXIT \n");

scanf("%d",&i);
while(i!=0)

switch(i)

case 2:

input2();

if(c1==r2)

multiply();

display(r1,c2);

Else

printf("WRONG SIZE OF ARRAYS.INPUT AGAIN \n");

break;

case 1:

input2();

if((r1==r2)&&(c1==c2))

add();

display(r1,c1);

else printf("WRONG SIZE OF ARRAYS.INPUT AGAIN \n");

break;

case 3:

input1();

transpose();

display(c1,r1);

break;

default:
printf("WRONG CHOICE.INPUT AGAIN \n");

printf("ENTER YOUR CHOICE \n 1)ARRAY ADDITION \n 2)ARRAY MULTIPILCATON \n 3)ARRAY TRANSPOSE \n


0)EXIT \n");

scanf("%d",&i);

void input2()

printf("ENTER THE SIZE OF BOTH THE ARRAYS MENTIONING ROWS AND COLUMNS RESPECTIVELY \n");

scanf("%d %d %d %d",&r1,&c1,&r2,&c2);

int m,n;

printf("ENTER FIRST ARRAY \n");

for(m=0;m<r1;m++)

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

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

printf("ENTER SECOND ARRAY \n");

for(m=0;m<r2;m++)

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

scanf("%d",&b[m][n]);

}
void input1()

printf("ENTER THE SIZE OF THE ARRAY MENTIONING ROWS AND COLUMNS RESPECTIVELY \n");

scanf("%d %d",&r1,&c1);

int m,n;

printf("ENTER THE ARRAY \n");

for(m=0;m<r1;m++)

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

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

void add()

int m,n;

for(m=0;m<r1;m++)

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

ans[m][n]+=a[m][n]+b[m][n];

void multiply()

int m,n,o;

for(m=0;m<r1;m++)
{

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

for(o=0;o<r2;o++)

ans[m][n]+=a[m][o]*b[o][n];

void transpose()

int m,n;

for(m=0;m<c1;m++)

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

ans[m][n]=a[m][n];

void display(int x,int y)

int m,n;

for(m=0;m<x;m++)

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

printf("%d ",ans[m][n]);
}

printf("\n");

}
INPUT AND OUTPUT

ENTER YOUR CHOICE

1)ARRAY ADDITION

2)ARRAY MULTIPILCATON

3)ARRAY TRANSPOSE

0)EXIT

ENTER THE SIZE OF BOTH THE ARRAYS MENTIONING ROWS AND COLUMNS RESPECTIVELY

ENTER FIRST ARRAY

ENTER SECOND ARRAY

24

68

ENTER YOUR CHOICE

1)ARRAY ADDITION

2)ARRAY MULTIPILCATON
3)ARRAY TRANSPOSE

0)EXIT

ENTER THE SIZE OF BOTH THE ARRAYS MENTIONING ROWS AND COLUMNS RESPECTIVELY

ENTER FIRST ARRAY

ENTER SECOND ARRAY

47 47 28 31

ENTER YOUR CHOICE

1)ARRAY ADDITION

2)ARRAY MULTIPILCATON
3)ARRAY TRANSPOSE

0)EXIT

ENTER THE SIZE OF THE ARRAY MENTIONING ROWS AND COLUMNS RESPECTIVELY

ENTER THE ARRAY

ENTER YOUR CHOICE

1)ARRAY ADDITION

2)ARRAY MULTIPILCATON

3)ARRAY TRANSPOSE

0)EXIT

Process returned 0 (0x0) execution time : 285.541 s

Press any key to continue.


PROGRAM 3
AIM: TO IMPLEMENT VARIOUS SORTING TECHNIQUE IN AN
ARRAY

CODE:

#include<stdio.h>

void bubble();

void insertion();

void selection();

int arr[50];

int n;

void main()

int c=4,i;

do

printf("\n ENTER YOUR CHOICE \n 1.BUBBLE SORT \n 2.INSERTION SORT \n 3.SELECTION


SORT \n 4.EXIT \n");

scanf("%d",&c);

if(c==4)

break;

printf("ENTER THE SIZE OF THE ARRAY \n");

scanf("%d",&n);

printf("ENTER THE ELEMENTS OF THE ARRAY \n");

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

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

}
switch(c)

case 1:

bubble();

break;

case 2:

insertion();

break;

case 3:

selection();

break;

case 4:

break;

default:

printf("WRONG INPUT.ENTER AGAIN \n");

printf("SORTED ARRAY IS \n");

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

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

}while(c!=4);

void bubble()

int i,j,t;

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

for(j=0;j<n-i-1;j++)
{

if(arr[j]>arr[j+1])

t=arr[j];

arr[j]=arr[j+1];

arr[j+1]=t;

void selection()

int i,j,t,min,temp;

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

min=arr[i];

t=i;

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

if(min>arr[j])

min=arr[j];

t=j;

temp=arr[t];

arr[t]=arr[i];

arr[i]=temp;
}

void insertion()

int j,y,t;

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

y=j-1;

t=arr[j];

while(y>-1 && t<arr[y])

arr[y+1]=arr[y];

y--;

arr[y+1]=t;

}
INPUT AND OUTPUT
ENTER YOUR CHOICE

1.BUBBLE SORT

2.INSERTION SORT

3.SELECTION SORT

4.EXIT

ENTER THE SIZE OF THE ARRAY

ENTER THE ELEMENTS OF THE ARRAY

SORTED ARRAY IS

345

ENTER YOUR CHOICE

1.BUBBLE SORT

2.INSERTION SORT

3.SELECTION SORT

4.EXIT

ENTER THE SIZE OF THE ARRAY

ENTER THE ELEMENTS OF THE ARRAY

56

47
89

23

SORTED ARRAY IS

1 4 5 23 47 56 89

ENTER YOUR CHOICE

1.BUBBLE SORT

2.INSERTION SORT

3.SELECTION SORT

4.EXIT

ENTER THE SIZE OF THE ARRAY

ENTER THE ELEMENTS OF THE ARRAY

SORTED ARRAY IS

2345

ENTER YOUR CHOICE

1.BUBBLE SORT

2.INSERTION SORT

3.SELECTION SORT

4.EXIT

Process returned 4 (0x4) execution time : 35.117 s


PROGRAM 4
AIM:TO IMPLEMENT QUICK SORT
CODE:

#include<stdio.h>

int pIndex;

void quicksort(int[],int,int);

int partition(int[],int,int);

void main(){

int A[50],n,i;

printf("ENTER THE SIZE OF THE ARRAY \n");

scanf("%d",&n);

printf("ENTER THE ELEMENTS OF THE ARRAY \n");

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

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

quicksort(A,0,n-1);

printf("THE SORTED ARRAY IS \n");

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

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

void quicksort(int A[],int start,int end){

if(start<end){

pIndex=partition(A,start,end);

quicksort(A,start,pIndex-1);

quicksort(A,pIndex+1,end);
}

int partition(int A[],int start,int end){

int pivot,i,temp;

pivot=A[end];

pIndex=start;

for(i=start;i<=end-1;i++){

if(A[i]<=pivot){

temp=A[i];

A[i]=A[pIndex];

A[pIndex]=temp;

pIndex=pIndex+1;

temp=A[pIndex];

A[pIndex]=A[end];

A[end]=temp;

return pIndex;

}
INPUT AND OUTPUT
ENTER THE SIZE OF THE ARRAY

ENTER THE ELEMENTS OF THE ARRAY

12 52 57 6 4 58 95

THE SORTED ARRAY IS

4 6 12 52 57 58 95

Process returned 7 (0x7) execution time : 60.254 s

Press any key to continue.

ENTER THE SIZE OF THE ARRAY

ENTER THE ELEMENTS OF THE ARRAY

465 78 232 2 1 165

THE SORTED ARRAY IS

1 2 78 165 232 465

Process returned 6 (0x6) execution time : 12.816 s

Press any key to continue.


PROGRAM 5
AIM:TO IMPLEMENT MERGE SORT
CODE:

#include<stdio.h>

int A[50], arr_size;

void merge(int arr[], int l, int m, int r)

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

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

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1+ j];

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2)

if (L[i] <=R[j])

arr[k] = L[i];
i++;

else

arr[k]=R[j];

j++;

k++;

while (i < n1)

arr[k] = L[i];

i++;

k++;

while (j < n2)

arr[k] = R[j];

j++;

k++;

void mergeSort(int arr[], int l, int r){

if (l < r){
int m = l+(r-l)/2;

mergeSort(arr, l, m);

mergeSort(arr, m+1, r);

merge(arr, l, m, r);

void printArray(int A[], int size){

int i;

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

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

void main()

int choice,i;

do{

printf("PRESS 1 IF YOU WANT TO IMPLEMENT MERGE SORT \n");

printf("OR PRESS ANY OTHER KEY TO EXIT \n");

scanf("%d",&choice);

if(choice!=1)

break;

printf("ENTER THE SIZE OF THE ARRAY \n");

scanf("%d",&arr_size);

printf("ENTER THE ELEMENTS OF THE ARRAY \n");

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

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

}
printf("\n");

mergeSort(A, 0, arr_size - 1);

printf("\nSorted array is \n");

printArray(A, arr_size);

}while(choice==1);

}
INPUT AND OUTPUT
PROGRAM 6

AIM: TO PERFROM INSERTION, SEARCHING & DELETION IN


LINKED LIST

CODE:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

struct node{
int data;
struct node *link;};
struct node *start=NULL;

void insert(int x){


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

temp->data=x;
temp->link=start;
start=temp;}

void insert_pos(int x,int n){


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

counter=start;
temp->data=x;

int i;
if (n>1){
for(i=1;i<n-1;i++)
counter=counter->link;

temp1=counter->link;
counter->link=temp;
temp->link=temp1;}

else{
temp->link=start;
start=temp;}}
void disp(){
struct node *counter;
counter=start;

while(counter!=NULL){
printf("%d ",counter->data);
counter=counter->link;}}

int serach(int x){


int c,flag;
struct node *counter;
counter=start;
c=1,flag=0;

while(counter!=NULL){
if (x==counter->data){
printf("\nElement found at %d position\n",c);
flag=1;
break;}

c++;
counter=counter->link;}
return flag;}

void del(int x){


int i;
struct node *temp;
struct node *counter;
counter=start;

if (x>1){
for(i=1;i<x-1;i++)
counter=counter->link;

temp=counter->link;
counter->link=temp->link;
free(temp);}

else{
start=start->link;
free(counter);}}

int main (){


int d;
do{
int n,i,x,res,c;
printf("\n1.Creation and insertion");
printf("\n2.Insertion at specific position");
printf("\n3.Searching");
printf("\n4.Deletion ");
printf("\n\nEnter Sno. as response : ");
scanf("%d",&c);

switch(c)
{
case 1: printf("\nEnter no of elements : ");
scanf("%d",&n);
printf("\nEnter %d elements \n",n);
for(i=0;i<n;i++){
printf("Enter element %d : ",i+1);
scanf("%d",&x);
insert(x);}

printf("\nLinked list after insertion :\n ");


disp();
break;

case 2: printf("\nEnter positon where element is to be


inserted : ");
scanf("%d",&n);
printf("Enter element : ");
scanf("%d",&x);
insert_pos(x,n);

printf("\nLinked list after insertion :\n ");


disp();
break;

case 3: printf("\n\nEnter elements to be searched : ");


scanf("%d",&x);
res=serach(x);
if(res==0)
printf("\nElement is not in list !!!\n");
break;

case 4: printf("\nEnter position element to be deleted : ");


scanf("%d",&x);
del(x);

printf("\nLinked list after deletion :\n ");


disp();
break;

default: printf("\n\nEnter valid response !!!");}


printf("\n\n Enter '1' to continue and '0' to exit : ");
scanf("%d",&d);}
while(d==1);
getch();
}
INPUT AND OUTPUT
EXPERIMENT 7

AIM: TO PERFROM PUSH AND POP IN STACK

7.1: ARRAY IMPLEMENTATION OF STACK

CODE:

#include<stdio.h>

void push(int A[],int* top,int size,int item)


{
if (*top==size-1)
printf("Stack is full !!!");
else
{
(*top)++;
A[*top]=item;
}
}
void pop(int* top)
{
if (*top== -1)
printf("Stack is empty !!!");
else
(*top)--;
}

void disp(int A[],int top)


{
int i;
for (i=top;i>=0;i--)
printf("%d ", A[i]);
}
void main()
{
int A[50], top, size, item,i,res,c;
do
{
printf("\n1.Push");
printf("\n2.Pop");
printf("\n\nEnter Sno. as response : ");
scanf("%d",&c);

switch(c)
{

case 1: top=-1;
printf("\nEnter no of elements : ");
scanf("%d",&size);

printf("\nEnter %d elements \n",size);


for(i=0;i<size;i++){
printf("Enter element %d : ",i+1);
scanf("%d",&item);
push(A,&top,size,item);}

printf("\nStack after push :\n ");


disp(A,top);

break;

case 2: pop(&top);

printf("\nStack after pop :\n ");


disp(A,top);

break;

default: printf("\n\nEnter valid response !!!");}


printf("\n\n Enter '1' to continue and '0' to exit : ");
scanf("%d",&res);}

while(res==1);
getch();
}
OUTPUT:
7.2: LINKED LIST IMPLEMENTATION OF STACK

CODE:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

struct node
{
int data;
struct node *link;
};
struct node *start=NULL;

void Push(int x)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));

temp->data=x;
temp->link=start;
start=temp;
}

void Pop()
{
int i;
struct node *temp;
temp=start;

start=start->link;
free(temp);
}
void disp()
{
struct node *counter;
counter=start;

while(counter!=NULL)
{
printf("%d ",counter->data);
counter=counter->link;
}
}

int main ()
{

int d;

do
{
int n,i,x,c;

printf("\n1.Creation and Push");


printf("\n2.Pop");

printf("\n\nEnter Sno. as response : ");


scanf("%d",&c);

switch(c)
{
case 1: printf("\nEnter no of elements : ");
scanf("%d",&n);

printf("\nEnter %d elements \n",n);


for(i=0;i<n;i++){
printf("Enter element %d : ",i+1);
scanf("%d",&x);

Push(x);}

printf("\nStack after Push :\n ");


disp();
break;

case 2: Pop();
printf("\nStack after Pop :\n ");
disp();
break;

default: printf("\n\nEnter valid response !!!");}

printf("\n\n Enter '1' to continue and '0' to exit : ");


scanf("%d",&d);}

while(d==1);
getch();
}
OUTPUT:
EXPERIMENT 8

AIM: TO PERFROM INSERTION AND DELETION IN QUEUE

ARRAY IMPLEMENTATION OF QUEUE

CODE:

#include<stdio.h>
#include<conio.h>

int A[50];

int r=-1;
int f=-1;

void Insert(int x, int n ){


if (r==n-1)
printf("\nOverflow !!!");

else if (r==-1 && f==-1){


r=0;
f=0;
A[r]=x;}

else{
r++;
A[r]=x;}
}

void Disp(){
if(f==-1)
printf("\nQueue is empty !!!");

else{
int i;
printf("\nQueue : ");

for(i=f; i<=r; i++)


printf("%d ",A[i]);}
}

void Delete(){
if(f==-1)
printf("\nUnderflow !!!");

else if (f==r){
f=-1;
r=-1;}

else
f++;
}

void main (){


int n,i,x;

printf("\nEnter no of elements : ");


scanf("%d",&n);

printf("Enter %d elements : \n",n);

for(i=0;i<n;i++){
printf("Enter element %d : ",i+1);
scanf("%d",&x);
Insert(x,n);
}

Disp();

printf("\nPress any key to delete item...");


getch();
Delete();
Disp();

getch();
}

OUTPUT:
LINKED LIST IMPLEMENTATION OF QUEUE

CODE:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

struct node{
int data;
struct node *link;};

struct node *front=NULL;


struct node *rear=NULL;

void Insert(int x){


struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=NULL;

if(front==NULL && rear==NULL){


front=temp;
rear=temp;}

else{
rear->link=temp;
rear=temp;}
}

void Delete(){

struct node *temp;


temp=front;

if(front==NULL)
printf("Queue Empty");

else if(front==rear){
front=NULL;
rear=NULL;
free(temp);}

else{
front=front->link;
free(temp);}
}
void disp(){
struct node *counter;
counter=front;

while(counter!=rear){
printf("%d ",counter->data);
counter=counter->link;}

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

void main (){

int d;

do{
int n,i,x,c;

printf("\n1.Creation and Insertion");


printf("\n2.Deletion");

printf("\n\nEnter Sno. as response : ");


scanf("%d",&c);

switch(c){

case 1: printf("\nEnter no of elements : ");


scanf("%d",&n);

printf("\nEnter %d elements \n",n);

for(i=0;i<n;i++){
printf("Enter element %d : ",i+1);
scanf("%d",&x);

Insert(x);}

printf("\nQueue after insertion :\n ");


disp();
break;

case 2: Delete();

printf("\nQueue after deletion :\n ");


disp();
break;

default: printf("\n\nEnter valid response !!!");}

printf("\n\n Enter '1' to continue and '0' to exit : ");


scanf("%d",&d);}
while(d==1);
getch();}

OUTPUT:
Experiment-9

AIM: To implement Insertion, Deletion and Searching in a Binary Search


Tree.

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

struct node
{
int key;
struct node *left, *right;
};
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
struct node* insert(struct node* node, int key)
{

if (node == NULL) return newNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);

return node;
}
struct node *deletelement(struct node *root,int val)
{
struct node *par,*curr,*psuc,*suc,*ptr;
if(root==NULL)
{
printf("\ntree is empty\n");
return root;
}
par=root;
curr=root;
while(curr!=NULL&&val!=curr->key)
{
par=curr;
curr=(val<curr->key)?curr->left:curr->right;
}
if(curr == NULL)
{
printf("\n The value to be deleted is not present in the tree");
return root;
}
if(curr->left==NULL)
{
ptr=curr->right;
}
else if(curr->right==NULL)
{
ptr=curr->left;
}
else
{
psuc=curr;
suc=curr->right;
while(suc->left!=NULL)
{
psuc=suc;
suc=suc->left;
}
if(psuc==curr)
{
curr->right=suc->right;
curr->key=suc->key;
}
else
{
psuc->left=suc->right;
curr->key=suc->key;
}
curr=suc;
free(curr);
return root;
}
if(par->left==curr)
{
par->left=ptr;
}
else if(par->right==curr)
{
par->right=ptr;
}
free(curr);
return root;
}
void preorder(struct node *root)
{
if(root!=NULL)
{
printf("%d ", root->key);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d ",root->key);
}
}
int min_val(struct node *root)
{
struct node *curr;
curr=root;
while(curr->left!=NULL)
{
curr=curr->left;
}
return curr->key;
}
int max_val(struct node *root)
{
struct node *curr;
curr=root;
while(curr->right!=NULL)
{
curr=curr->right;
}
return curr->key;
}
// Driver Program to test above functions
int main()
{
int val,option;
struct node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 35);
insert(root, 70);
insert(root, 60);
insert(root, 80);
do
{
printf("\n ******MAIN MENU******* \n");
printf("\n 1. Insert Element");
printf("\t 2. Preorder Traversal");
printf("\n 3. Inorder Traversal");
printf("\t 4. Postorder Traversal");
printf("\n 5. smallest element");
printf("\t 6. largest element");
printf("\n 7. Delete an element");
printf("\n 8. Exit");
printf("\n\n Enter your option : ");
scanf("%d",&option);
switch(option)
{
case 1:printf("\nEnter the Element to be inserted ");
scanf("%d",&val);insert(root,val);break;
case 2:printf("\nPreorder traversal is :");
preorder(root);break;
case 3:printf("\nInorder traversal is :");
inorder(root);break;
case 4:printf("\nPostorder traversal is :");
postorder(root);break;
case 5:printf("\nSmallest element is : %d",min_val(root));break;
case 6:printf("\nLargest element is : %d",max_val(root));break;
case 7:printf("\nEnter the element to be deleted\n");
scanf("%d",&val);
root=deletelement(root,val);break;
case 8:break;
default:printf("Sorry.. Wrong Choice! Try again");
}
}
while(option!=8);
return 0;
}

OUTPUT:
Experiment 10
AIM : To implement Heap Sort in an array
CODE:
#include<stdio.h>
#define MAX 100
void heapSort(int arr[],int size);
void buildHeap(int arr[],int size);
int delRoot(int arr[],int *size);
void restoreDown(int arr[],int i,int size);
void display(int arr[],int n);
main()
{

int i,n,arr[MAX];
printf("Enter numberof elements: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter elements %d : ",i);
scanf("%d",&arr[i]);

}
printf("Entered list is :\n");
display(arr,n);
heapSort(arr,n);
printf("Sorted List is :\n");
display(arr,n);
}
void heapSort(int arr[],int size)
{
int max;
buildHeap(arr,size);
printf("Heap is :");
display(arr,size);
while(size>1)
{
max=delRoot(arr,&size);
arr[size+1]=max;

}
}
void buildHeap(int arr[],int size)
{
int i;
for(i=size/2;i>=1;i--)
{

restoreDown(arr,i,size);
}
}
int delRoot(int arr[],int (*size))
{

int max =arr[1];


arr[1]=arr[*size];
(*size)--;
restoreDown(arr,1,*size);
return max;

}
void restoreDown(int arr[],int i,int size)
{
int left=2*i,right =left +1;
int num=arr[i];
while(right<=size)
{
if(num>=arr[left]&&num>=arr[right])
{
arr[i]=num;
return;

}
else if(arr[left]>arr[right])
{
arr[i]=arr[left];
i=left;
}
else
{
arr[i]=arr[right];
i=right;
}
left=2*i;
right=left+1;

if(left ==size&& num< arr[left])


{
arr[i]=arr[left];
i=left;

}
arr[i]=num;

}
void display(int arr[],int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
OUTPUT:
Experiment-11

AIM: To implement BFS(Breadth First Search) on a graph.

To perform Breadth First Search (BFS) on the graph using adjacency list.

CODE:

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

#define MAX 100

#define initial 1
#define waiting 2
#define visited 3

int n;
int adj[MAX][MAX];
int state[MAX];
void create_graph();
void BF_Traversal();
void BFS(int v);

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


void insert_queue(int vertex);
int delete_queue();
int isEmpty_queue();

int main()
{
create_graph();
BF_Traversal();
return 0;
}

void BF_Traversal()
{
int v;
for(v=0; v<n; v++)
state[v] = initial;
printf("Enter Start Vertex for BFS: \n");
scanf("%d", &v);
BFS(v);
}
void BFS(int v)
{
int i;
insert_queue(v);
state[v] = waiting;
while(!isEmpty_queue())
{
v = delete_queue( );
printf("%d ",v);
state[v] = visited;

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


{
if(adj[v][i] == 1 && state[i] == initial)
{
insert_queue(i);
state[i] = waiting;
}
}
}
printf("\n");
}

void insert_queue(int vertex)


{
if(rear == MAX-1)
printf("Queue Overflow\n");
else
{
if(front == -1)
front = 0;
rear = rear+1;
queue[rear] = vertex ;
}
}

int isEmpty_queue()
{
if(front == -1 || front > rear)
return 1;
else
return 0;
}

int delete_queue()
{
int delete_item;
if(front == -1 || front > rear)
{
printf("Queue Underflow\n");
exit(1);
}
delete_item = queue[front];
front = front+1;
return delete_item;
}

void create_graph()
{
int count,max_edge,origin,destin;
printf("Enter number of vertices : ");
scanf("%d",&n);
max_edge = n*(n-1);
for(count=1; count<=max_edge; count++)
{
printf("Enter edge %d( -1 -1 to quit ) : ",count);
scanf("%d %d",&origin,&destin);
if((origin == -1) && (destin == -1))
break;
if(origin>=n || destin>=n || origin<0 || destin<0)
{
printf("Invalid edge!\n");
count--;
}
else
{
adj[origin][destin] = 1;
}
}
OUTPUT:
AIM:To implement DFS (Depth Breadth Search) on a graph.

To perform Depth First Search (DFS) on the graph using adjacency list.

CODE:

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

typedef struct node


{
struct node *next;
int vertex;
}node;

node *G[20];
//heads of linked list
int visited[20];
int n;
void read_graph();
//create adjacency list
void insert(int,int);
//insert an edge (vi,vj) in te adjacency list
void DFS(int);

void main()
{
int i;
read_graph();
//initialised visited to 0

for(i=0;i<n;i++)
visited[i]=0;

DFS(0);
}

void DFS(int i)
{
node *p;

printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;

if(!visited[i])
DFS(i);
p=p->next;
}
}

void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");

scanf("%d",&n);

//initialise G[] with a null

for(i=0;i<n;i++)
{
G[i]=NULL;
//read edges and insert them in G[]

printf("Enter number of edges:");


scanf("%d",&no_of_edges);

for(i=0;i<no_of_edges;i++)
{
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
}
}

void insert(int vi,int vj)


{
node *p,*q;

//acquire memory for the new node


q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;

//insert the node in the linked list number vi


if(G[vi]==NULL)
G[vi]=q;
else
{
//go to end of the linked list
p=G[vi];

while(p->next!=NULL)
p=p->next;
p->next=q;
}
}

OUTPUT:

S-ar putea să vă placă și