(DS-0)

 1A.Write a program to store the elements in 1-D array and perform the operations like searching, sorting and reversing the elements. [Menu Driven]

#include<stdio.h>

void create();

void print();

void sort();

void search();

void reverse();

int a[5],n,i;

int main()

{

int ch;

do

{

printf("\n 1.create \n 2.print \n 3.sort \n 4.search \n 5.reverse \n 6.exit \n");

printf("\n Enter an option :");

scanf("%d",&ch);

switch(ch)

{

case 1:create();

break;

case 2:print();

break;

case 3:sort();

break;

case 4:search();

break;

case 5:reverse();

break;

case 6:break;

}

}while(ch!=6);

return 0;

}

void create()

{

printf("\n Enter the no of element :");

scanf("%d",&n);

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

{

printf("\n Enter element for index %d :",i);

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

}

}

void print()

{

printf("\n The array is :");

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

{

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

}

}

void search()

{

int val;

printf("\n enter the value for searching : ");

scanf("%d",&val);

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

{

if(a[i]==val)

{

printf("%d is found at index %d ",val,i);

break;

}

}

if(i==n)

{

printf("\n %d is not found",val);

}

}

void reverse()

{

int temp;

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

{

temp=a[i];

a[i]=a[n-i-1];

a[n-i-1]=temp;

}

}

void sort()

{

int j,temp;

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;

}

}

}

}

1B.Read the two arrays from the user and merge them and display the elements in sorted order. [Menu Driven]

#include<stdio.h>

void create();

void create2();

void print();

void sort();

void merge();

int a[5],b[5],c[10],n1,n2,i,k;

int main()

{

int ch;

do

{

printf("\n 1.create \n 2.create2 \n 3.print \n 4.sort \n 5.merge \n 6.exit \n");

printf("\n enter a option :");

scanf("%d",&ch);

switch(ch)

{

case 1:create();

break;

case 2:create2();

break;

case 3: print();

break;

case 4: sort();

break;

case 5: merge();

break;

case 6: break;

}

}while(ch!=6);

return 0;

}

void create()

{

printf("\n enter the no of element :");

scanf("%d",&n1);

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

{

printf("\n enter the value for index %d :",i);

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

}

}

void create2()

{

printf("\n enter the no of element :");

scanf("%d",&n2);

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

{

printf("\n enter the value for index %d:",i);

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

}

}

void print()

{

printf("\n The array is:");

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

{

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

}

}

void sort()

{

int j,temp;

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

{

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

{

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

{

temp=c[i];

c[i]=c[j];

c[j]=temp;

}

}

}

}

void merge()

{

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

{

c[i]=a[i];

}

k=n1;

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

{

c[k]=b[i];

k++;

}

}

1C.Write a program to perform the Matrix addition, Multiplication and Transpose Operation. [Menu Driven]

 #include<stdio.h>

void create();

void create2();

void add();

void multiplication();

void transpose();

int a[5][5],b[5][5],c[10][10],i,j,k,r1,r2,c1,c2;

int main()

{

int ch;

do

{

printf("\n 1.create \n 2.create2 \n 3.add \n 4.multiplication \n 5.transpose \n 6.Exit\n");

printf("\n Enter an option :");

scanf("%d",&ch);

switch(ch)

{

case 1:create();

break;

case 2:create2();

break;

case 3: add();

break;

case 4:multiplication();

break;

case 5: transpose();

break;

case 6: break;

}

}while(ch!=6);

return 0;

}

void create()

{

printf("\n Enter the no. of rows :");

scanf("%d",&r1);

printf("\n Enter the no. of columns :");

scanf("%d",&c1);

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

{

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

{

printf("\n Enter the value for index %d%d :",i,j);

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

}

}

}

void create2()

{

printf("\n Enter the no of rows:");

scanf("%d",&r2);

printf("\n Enter the no of columns:");

scanf("%d",&c2);

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

{

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

{

printf("\n Enter the value for index %d%d:",i,j);

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

}

}

}

void print()

{

printf("\nThe array is C:\n");

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

{

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

{

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

}

}

}

void add()

{

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

{

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

{

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

{

c[i][j]=a[i][j]+b[i][j];

}

}

}

printf("\nThe array is C:\n");

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

{

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

{

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

}

printf("\n");

}

}

void multiplication()

{

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

{

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

{

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

{

c[i][j]=0;

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

{

c[i][j]+=a[i][k]*b[k][j];

}

}

}

}

printf("\n the array is C:\n");

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

{

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

{

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

}

printf("\n");

}

}

void transpose()

{

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

{

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

{

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

}

printf("\n");

}

}

 2A.Write a program to create a single linked list and display the node elements in reverse order.

#include<stdio.h>

#include<stdlib.h>

struct node 

{

int data;

struct node *next;

};

struct node* create(struct node*);

struct node* print(struct node*);

struct node* reverse(struct node*);

int main()

{

int op;

struct node *start=NULL;

printf("--------Practical 2A [Menu Driven]-------\n");

do

{

printf("\n1->Create the linked list \n2->Print the linked list \n3->reverse the linked list \n4->Exit");

printf("\nEnter a choice : ");

scanf("%d",&op);

switch(op)

{

case 1:start=create(start);

break;

case 2:start=print(start);

break;

case 3:start=reverse(start);

break;

case 4:

break;

}

}while (op!=4);

return 0;

}

struct node *create(struct node *start)

{

int val;

struct node *newnode, *temp, *pre, *post;

printf("Enter value :");

scanf("%d",&val);

do

{

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

newnode->data=val;

if(start==NULL)

{

start=newnode;

newnode->next=NULL;

}

else

{

temp=start;

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=newnode;

newnode->next=NULL;

}

printf("\nEnter val or -1 to Exit :");

scanf("%d",&val);

}while(val!=-1);

return start;

}

//Reversing the linked list..

struct node *reverse(struct node *start)

{

struct node *pre=NULL, *temp=start, *post;

while(temp!=NULL)

{

post=temp->next;

temp->next=pre;

pre=temp;

temp=post;

}

post=NULL;

temp=NULL;

start=pre;

return start;

}

//Printing the linked list..

struct node *print(struct node *start)

{

struct node *temp=start;

printf("\nThe linked list is :");

while(temp!=NULL)

{

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

temp=temp->next;

}

return start;

}

2B.Write a program to search the elements in the linked list and display the same

#include<stdio.h>

#include<stdlib.h>

struct node 

{

int data;

struct node *next;

};

struct node* create(struct node*);

struct node* print(struct node*);

struct node* search(struct node*);

int main()

{

int op;

struct node* start=NULL; 

printf("-----------Menu Driven---------\n");

do

{

printf("\n1.Create the linked list \n2.Display the linked list \n3.Search the element in linked list \n4.Exit");

printf("\nEnter a choice : ");

scanf("%d",&op);

switch(op)

{

case 1:start=create(start);

break;

case 2:start=print(start);

break;

case 3:start=search(start);

break;

case 4:

break;

}

}

while(op!=4);

return 0;

}

//creating the linked list

struct node *create(struct node *start)

{

int val;

struct node *newnode, *temp;

printf("\nEnter value :");

scanf("%d",&val);

do

{

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

newnode->data=val;

if(start==NULL)

{

start=newnode;

newnode->next=NULL;

}

else

{

temp=start;

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=newnode;

newnode->next=NULL;

}

printf("\nEnter val or -1 to Exit :");

scanf("%d",&val);

}while(val!=-1);

return start;

}

//printing created linked list..

struct node *print(struct node *start)

{

struct node *temp=start;

printf("\nThe linked list is :");

while(temp!=NULL)

{

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

temp=temp->next;

}

return start;

}

//searching the element in linked list...

struct node *search(struct node *start)

{

int val,count=1;

struct node *temp=start;

printf("\nEnter the element you want to search in linked list:");

scanf("%d",&val);

while(temp->data!=val)

{

if(temp->next==NULL)

{

break;

}

else

{

count++;

temp=temp->next;

}

}

if(temp->data==val)

{

printf("\nThe element %d is found at node %d ",val,count);

}

else

{

printf("\nThe element %d is not found in the linked list !!",val);

}

return start;

}

2C.Write a program to create double linked list and sort the elements in the linked list.

#include<stdlib.h>

#include<stdio.h>

struct node

{

int data;

struct node *next,*prev;

};

struct node* create(struct node*);

struct node* display(struct node*);

struct node* sort(struct node*);

int main()

{

int ch;

struct node *start=NULL;

do

{

printf("\n 1.create \n 2.display \n 3.sort \n 4.exit");

printf("\n enter an option:");

scanf("%d",&ch);

switch(ch)

{

case 1:start=create(start);

break;

case 2:start=display(start);

break;

case 3:start=sort(start);

break;

case 4:break;

}

}while(ch!=4);

return 0;

}

struct node* create(struct node*start)

{

int ch;

struct node *newnode,*temp;

printf("\n Enter value of node and -1 to exit:");

scanf("%d",&ch);

do

{

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

newnode->data=ch;

if(start==NULL)

{

start=newnode;

newnode->next=NULL;

newnode->prev=NULL;

}

else

{

temp=start;

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=newnode;

newnode->next=NULL;

newnode->prev=temp;

}

printf("\n Enter data or enter -1 to exit:");

scanf("%d",&ch);

}while(ch!=-1);

printf("\n Link list is created.");

return start;

};

struct node* display(struct node*start)

{

struct node *temp=NULL;

temp=start;

printf("The Link list is:");

while(temp!=NULL)

{

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

temp=temp->next;

}

return start;

};

struct node* sort(struct node*start)

{

struct node *temp1=start;

struct node *temp2, *temp;

int x;

while(temp1->next!=NULL)

{

temp2=start;

while(temp2->next!=NULL)

{

temp=temp2->next;

if(temp2->data > temp->data)

{

x=temp->data;

temp->data =temp2->data;

temp2->data=x;

}

temp2=temp2->next;

}

temp1=temp1->next;

}

return start;

}

 3A.Write a program to implement the concept of Stack with Push, Pop, Display and Exit operations.

#include<stdio.h>

#define size 3

void push();

void pop();

void peek();

void print();

int stack[size],TOP=-1,val;

int main()

{

int ch;

printf("------Menu Driven--------");

do

{

printf("\n 1 -> PUSH \n 2 -> PRINT\n 3 -> POP\n 4 -> PEEK \n 5 -> EXIT");

printf("\nEnter a choice:");

scanf("%d",&ch);

switch(ch)

{

case 1: push();

break;

case 2: print();

break;

case 3: pop();

break;

case 4: peek();

break;

case 5: 

break;

}

}while(ch!=5);

return 0;

}

void push()

{

printf("\nEnter an element or -1 to Exit:");

scanf("%d",&val);

do

{

if(TOP==size-1)

{

printf("\nStack is full");

break;

}

else

{

stack[++TOP]=val;

printf("\nInserted successfully");

}

printf("\nEnter an element or -1 to Exit:");

scanf("%d",&val);

}while(val!=-1);

}

void pop()

{

printf("\nEnter 1 to delete or -1 to Exit:");

scanf("%d",&val);

do

{

if(TOP==-1)

{

printf("\nstack is empty");

break;

}

else

{

printf("\nDeleted element is %d",stack[TOP]);

TOP--;

}

printf("\nEnter 1 to delete or -1 to Exit:");

scanf("%d",&val);

}

while(val!=-1);

}

void peek()

{

if(TOP==-1)

{

printf("\nstack is empty");

}

else

{

printf("\nThe Topmost element is %d",stack[TOP]);

}

}

void print()

{

int i;

printf("\nThe stack is:");

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

{

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

}

}

3B.Write a program to implement Tower of Hanoi problem.

#include <stdio.h>

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)

{

    if (n == 1)

    {

        printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);

        return;

    }

    towerOfHanoi(n-1, from_rod, aux_rod, to_rod);

    printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod);

    towerOfHanoi(n-1, aux_rod, to_rod, from_rod);

}

int main()

{

    int n = 4; 

    towerOfHanoi(n, 'A', 'C', 'B');  

    return 0;

}

 4A.Write a program to implement the concept of Queue with Insert, Delete, Display and Exit operations.

#include<stdio.h>

#define size 3

void enqueue();

void dequeue();

void display();

int queue[size], front=-1, rear=-1,i;

int main()

{

int ch;

printf("-----Menu Driven-----");

do

{

printf("\n1.ENQUEUE \n2.DEQUEUE\n3.PRINT\n4.EXIT");

printf("\nEnter a choice:");

scanf("%d",&ch);

switch(ch)

{

case 1: enqueue();

break;

case 2: dequeue();

break;

case 3: display();

break;

case 4:

break;

}

}while(ch!=4);

return 0;

}

void enqueue()

{

int val;

printf("\nEnter an element to insert:");

scanf("%d",&val);

if(front==-1)

{

front++,rear++;

queue[rear]=val;

}

else if(rear==size-1)

{

printf("\nQueue is full");

}

else

{

queue[++rear]=val;

}

}

void dequeue()

{

int val;

if(front==-1)

{

printf("\nQueue is empty");

}

else if(rear==front && rear!=-1)

{

val=queue[front];

printf("\nThe value deleted is %d :",val);

front=rear=-1;

}

else

{

val=queue[front];

printf("\nValue deleted is %d ",val);

front++;

}

}

void display()

{

if(front==-1)

{

printf("\nQueue is Empty");

}

else

{

printf("\nThe Queue is :");

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

{

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

}

}

}

4B.Write a program to implement the concept of Circular Queue.

#include<stdio.h>

#define size 3

void enqueue();

void dequeue();

void display();

int queue[size], front=-1, rear=-1,i ;

int main()

{

int ch;

printf("-----Menu Driven-----");

do

{

printf("\n1.ENQUEUE \n2.DEQUEUE\n3.PRINT\n4.EXIT");

printf("\nEnter your choice :");

scanf("%d",&ch);

switch(ch)

{

case 1: enqueue();

break;

case 2: dequeue();

break;

case 3: display();

break;

case 4:

break;

}

}while(ch!=4);

return 0;

}

void enqueue()

{

int val;

printf("\nEnter an element to insert:");

scanf("%d",&val);

if(rear==-1)

{

rear++;

queue[rear]=val;

front++;

}

else if((rear==size-1 && front==0)||(front==rear+1))

{

printf("\nQueue is full");

}

else

{

rear=(rear+1)%size;

queue[rear]=val;

}

}

void dequeue()

{

int val;

if(front==-1)

{

printf("\nQueue is empty");

}

else if(rear==front && rear!=-1)

{

val=queue[front];

printf("\nThe value deleted is %d ",val);

front=rear=-1;

}

else

{

val=queue[front];

printf("\nValue deleted is %d ",val);

front=(front+1)%size;

}

}

void display()

{

if(front==-1)

{

printf("\nQueue is Empty");

}

else

{

printf("\nThe Queue is :");

for(i=front ; i!=rear ; i=(i+1)%size)

{

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

}

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

}

}

4C.Write a program to implement the concept of Deque.

#include<stdio.h>

#define size 5

int queue[size],rear=-1,front=-1,te=0,x,i;


void insert_rear();

void insert_front();

void del_rear();

void del_front();

void display();


int main()

{

int opt;

do

{

printf("\n 1. Insert at rear\n 2. Insert at front \n 3. Delete from rear \n 4. Delete from front \n 5. Display \n 6. Exit");

printf("\n Enter an option:");

scanf("%d",&opt);

switch(opt)

{

case 1:insert_rear();

break;

case 2:insert_front();

break;

case 3:del_rear();

break;

case 4:del_front();

break;

case 5:display();

break;

case 6:

break; 

}

}while(opt!=6);

return 0;

}


void insert_rear()

{

int val;

printf("\n Enter value to insert at rear :");

scanf("%d",&val);

if(te==size)

{

printf("\n Queue is full !!");

}

else if(te==0)

{

rear=front=0;

queue[rear]=val;

te=te+1;

}

else

{

rear=(rear+1)%size;

queue[rear]=val;

te=te+1;

}

}

void insert_front()

{

int val;

printf("\n Enter value to insert at front :");

scanf("%d",&val);

if(te==size)

{

printf("\n Queue is full !!");

}

else if(te==0)

{

rear=front=0;

queue[rear]=val;

te=te+1;

}

else

{

if(front==0)

{

front=size-1;

}

else

{

front=front-1;

}

queue[front]=val;

te=te+1;

}

}


void del_rear()

{

if(te==0)

{

printf("\n Queue is empty !!");

}

else

{

if(rear==-1)

{

rear=size-1;

}

printf("\n Number Deleted fron Rear end is %d \n",queue[rear]);

rear=rear-1;

te=te-1;

}

}


void del_front()

{

if(te==0)

{

printf("\n Queue is empty !!");

}

else

{

printf("\n Number Deleted from front end is %d \n",queue[front]);

front=(front+1)%size;

te=te-1;

}

}


void display()

{

if(te==0)

{

printf("\n Queue is empty !!");

}

else

{

x=front;

printf("\n Queue is :");

for(i=1 ; i<=te ; i++)

{

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

x=(x+1)%size;

}

printf("\n");

}

}

 5A.Write a program to implement bubble sort. 

#include<stdio.h>

int main()

{

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

printf("\nEnter no. of elements for list:");

scanf("%d",&n);

printf("\nEnter %d integers :\n",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;

}

}

}

printf("\nBubble Sorted list in ascending order is :");

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

{

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

}

}

5B.Write a program to implement selection sort.

#include<stdio.h>

int main()

{

int a[20],n,i,j,position,t;

printf("\nEnter no. of elements for list:");

scanf("%d",&n);

printf("\nEnter %d integers :\n",n);

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

{

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

}

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

{

position=i;

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

{

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

position=j;

}

if(position != i)

{

t=a[i];

a[i]=a[position];

a[position]=t;

}

}

printf("\nSelection Sorted list in ascending order is :\n");

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

{

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

}

return 0;

}

5C.Write a program to implement insertion sort.

#include<stdio.h>

int main()

{

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

printf("\nEnter no. of elements for list :");

scanf("%d",&n);

printf("\nEnter %d integers :\n",n);

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

{

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

}

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

{

temp=a[i];

j=i-1;

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

{

a[j+1]=a[j];

j--;

}

a[j+1]=temp;

}

printf("\nInsertion sorted list in ascending order is :\n");

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

{

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

}

return 0;

}

6A.Write a program to implement merge sort.

#include<stdio.h>

void mergesort(int a[],int i, int j);

void merge(int a[],int i1,int j1,int i2,int j2);

int main()

{

int a[30],n,i;

  printf("Enter No of Elements : ");

  scanf("%d", &n);

  printf("Enter Array Elements : ");

 

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

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

 

  mergesort(a,0,n-1);

  printf("\n Sorted Array is : ");

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

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

return 0;

}

void mergesort(int a[],int i, int j)

{

int mid;

if(i<j)

{

mid=(i+j)/2;

mergesort(a,i,mid); 

mergesort(a,mid+1,j);  

merge(a,i,mid,mid+1,j); 

}

}

void merge(int a[],int i1,int j1,int i2,int j2)

{

int temp[50];   

int i,j,k;

i=i1;   

j=i2;  

k=0;

while(i<=j1 && j<=j2)  

{

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

{

temp[k++]=a[i++];

}

else

{

temp[k++]=a[j++];

}

}

while(i<=j1)

{

temp[k++]=a[i++];

}

while(j<=j2) 

{

temp[k++]=a[j++];

}

for(i=i1,j=0 ; i<=j2 ; i++,j++)

{

a[i]=temp[j];

}

}

6B.Write a program to search the element using sequential search.

#include<stdio.h>

int main()

{

int a[10],i,n,val;

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

scanf("%d",&n);

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

{

printf("\nEnter no. at index %d :",i);

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

}

printf("\nEnter the value to be searched :");

scanf("%d",&val);

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

{

if(a[i]==val)

{

printf("\n %d was found at %d index",val,i);

break;

}

}

if(i==n)

{

printf("\n %d was not found in the list !!",val);

}

return 0;

}

6C.Write a program to search the element using binary search. 

//Write a program to search the element using binary search

#include<stdio.h>

int main()

{

int a[100],i,n,val,first,last,middle;

printf("\n enter the no of element :");

scanf("%d",&n);

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

{

printf("\n enter the no of index %d :",i);

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

}


printf("\n enter the value for searching : ");

scanf("%d",&val);

first=0;

last=n-1;

middle=(first+last)/2;

while(first <= last)

{

if(a[middle] < val)

{

first=middle+1;

}

else if(a[middle] == val)

{

printf("%d is found at location %d \n ",val,middle+1);

break;

}

else

{

last=middle-1;

}

middle =(first+last)/2;

}

if(first>last)

{

printf("Not found ! %d isn't present in the list.\n ",val);

}

return 0;

} 

7.Write a program for inorder, postorder and preorder traversal of tree.

#include<stdio.h>

#include<malloc.h>

struct node

{

int data;

struct node *left;

struct node *right;

};

struct node *root=NULL;

struct node *create(struct node*);

struct node *display(struct node*);

void preorder(struct node *temp);

void postorder(struct node *temp);

void inorder(struct node *temp);


void main()

{

int choice,val,count,min,max;

//clrscr();

do

{

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

printf("1.Create a Binary Tree\n");

printf("2.Display a Binary Tree\n");

printf("3.EXIT\n");

printf("Enter Your Choice : ");

scanf("%d", &choice);

printf("\n\n");

switch(choice)

{

case 1:root =create(root);

break;

case 2:root =display(root);

break;

case 3:break;

}

}while(choice!=3);

}

struct node *create(struct node *root)

{

struct node *newnode=NULL,*temp=NULL,*parent=NULL;

int val;

printf("Enter The Data or Enter -1 to Exit : ");

scanf("%d",&val);

while(val!=-1)

{

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

newnode->data=val;

if(root==NULL)

{

root=newnode;

newnode->left=NULL;

newnode->right=NULL;

}

else

{

temp=root;

while(temp!=NULL)

{

parent=temp;

if(val<temp->data)

{

temp=temp->left;

}

else

{

temp=temp->right;

}

}

if(val<parent->data)

{

parent->left=newnode;

newnode->left=NULL;

newnode->right=NULL;

}

else

{

parent->right=newnode;

newnode->left=NULL;

newnode->right=NULL;

}

}

printf("Enter the Data or Enter -1 to Exit : ");

scanf("%d",&val);

}

printf("Successfully Created\n");

return root;

}

struct node *display(struct node *root)

{

int choice;

printf("\t Display Menu\n");

printf("\t1.Pre-Order \n");

printf("\t2.IN-Order \n");

printf("\t3.Post-Order \n");

printf("\t4.EXIT \n");

printf("Enter Your Choice : \n");

scanf("%d",&choice);

switch(choice)

{

case 1:

printf("\t The PreOrder Traversal is : ");

preorder(root);

break;

case 2:

printf("\t The In-Order Traversal is : ");

inorder(root);

break;

case 3:

printf("\t The Post-Order Traversal is : ");

postorder(root);

break;

case 4:

break;

}

printf("\n");

return root;

}

void preorder(struct node *temp)

{

if (temp!=NULL)

{

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

preorder(temp->left);

preorder(temp->right);

}

}

void postorder(struct node *temp)

{

if(temp!=NULL)

{

postorder(temp->left);

postorder(temp->right);

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

}

}

void inorder(struct node *temp)

{

if(temp!=NULL)

{

inorder(temp->left);

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

inorder(temp->right);

}

}

8A. Write a program to insert the element into maximum heap.

#include<stdio.h>

#include<conio.h>

#define SIZE 30

int a[SIZE], n;

void maxheapify(int a[],int i,int n1);

void buildheap(int a[],int n1);

void swap(int i,int j);

int main()

{

int i,j;

printf("\nenter the number of elements: ");

scanf("%d",&n);

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

{

printf("enter a value:");

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

}

buildheap(a,n);

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

{

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

}

printf("\n");

}

void buildheap(int a[],int n1)

{

int i,j;

for(i=(n1/2)-1;i>=0;i--)

{

maxheapify(a,i,n1);

}


}

void maxheapify(int a[],int i,int n1)

{

int max,l,r;

max=i;

l=(2*i)+1;

r=(2*i)+2;

if(l<n1&&r<n1){

if(a[l]>a[max])

{

max=l;

}

if(a[r]>a[max])

{

max=r;

}

}

else if(l<n1&&r>=n1)

{

if(a[l]>a[max])

{

max=l;

}

}

else if(l>=n1&&r<n1)

{

if(a[r]>a[max])

{

max=r;

}

}

if(i!=max)

{

swap(i,max);

maxheapify(a,max,n1);

}

}

void swap(int i,int j)

{

int temp=a[i];

a[i]=a[j];

a[j]=temp;

}

8B. Write a program to insert the element into minimum heap.

#include<stdio.h>

#include<conio.h>

#define SIZE 30

int a[SIZE], n;

void minheapify(int a[],int i,int n1);

void buildheap(int a[],int n1);

void swap(int i,int j);

int main()

{

int i,j;

printf("\nenter the number of elements: ");

scanf("%d",&n);

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

{

printf("enter a value:");

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

}

buildheap(a,n);

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

{

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

}

printf("\n");

}

void buildheap(int a[],int n1)

{

int i,j;

for(i=(n1/2)-1;i>=0;i--)

{

minheapify(a,i,n1);

}


}

void minheapify(int a[],int i,int n1)

{

int min,l,r;

min=i;

l=(2*i)+1;

r=(2*i)+2;

if(l<n1&&r<n1){

if(a[l]<a[min])

{

min=l;

}

if(a[r]<a[min])

{

min=r;

}

}

else if(l<n1&&r>=n1)

{

if(a[l]<a[min])

{

min=l;

}

}

else if(l>=n1&&r<n1)

{

if(a[r]<a[min])

{

min=r;

}

}

if(i!=min)

{

swap(i,min);

minheapify(a,min,n1);

}

}

void swap(int i,int j)

{

int temp=a[i];

a[i]=a[j];

a[j]=temp;

}


Comments

Popular posts from this blog

python(BI)

Prac_8(AMP)

LSA10