Академический Документы
Профессиональный Документы
Культура Документы
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
int i=0;
int a;
struct node
int data;
}*start,*temp,*x,*temp1;
void main()
int ch;
char ans;
void insertbeg();
void insertmid();
void insertend();
void delbeg();
void delmid();
void delend();
void delall();
void view();
clrscr();
do
printf("1:Insert");
printf("\n2:Delete ");
printf("\n3:View");
scanf("%d",&ch);
switch(ch)
case 1:{
printf("\n1:Insert at Beginning");
printf("\n2:Insert in Middle");
printf("\n3:Insert at End");
fflush(stdin);
scanf("%d",&ch);
switch(ch)
case 1: { insertbeg();
break;
case 2: { insertmid();
break;
case 3: { insertend();
break;
}
break;
case 2:{
fflush(stdin);
scanf("%d",&ch);
switch(ch)
case 1: { delbeg();
break;
case 2: { delmid();
break;
case 3: { delend();
break;
case 4: { delall();
break;
break;
}
case 3:{
view();
break;
default:{
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y' || ans=='Y');
getch();
void insertbeg()
if(i==0)
temp=start;
scanf("%d",&x->data);
x->link=NULL;
start=x;
temp=start;
i=i+1;
else
temp=start;
scanf("%d",&x->data);
x->link=start;
start=x;
temp=start;
i=i+1;
void insertmid()
temp=start;
scanf("%d",&a);
while(temp->data!=a)
temp=temp->link;
scanf("%d",&x->data);
x->link=temp->link;
temp->link=x;
temp=start;
i=i+1;
void insertend()
temp=start;
scanf("%d",&x->data);
while(temp->link!=NULL)
temp=temp->link;
temp->link=x;
x->link=NULL;
i=i+1;
void delbeg()
temp=start;
start=start->link;
free(temp);
temp=start;
i=i-1;
void delmid()
temp=start;
scanf("%d",&a);
while(temp->data!=a)
temp1=temp;
temp=temp->link;
temp1->link=temp->link;
free(temp);
i=i-1;
printf("\nElement Deleted...!!");
void delend()
temp=start;
while(temp->link!=NULL)
temp1=temp;
temp=temp->link;
temp1->link=NULL;
free(temp);
i=i-1;
void delall()
temp=start;
while(start!=NULL)
temp1=temp;
temp=temp->link;
free(temp1);
i=i-1;
start=temp;
void view()
if(i==0)
}
else
temp=start;
while(temp->link!=NULL)
temp=temp->link;
}
Bubble Sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],temp,m,i,j;
clrscr();
// BUBBLE SORT
for(i=0;i<m-1;i++)
{
for(j=0;j<m-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
getch();
}
Merge Sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],b[20],c[40],m,n,i,j,k,temp;
clrscr();
// BUBBLE SORT
for(i=0;i<m-1;i++)
{
for(j=0;j<m-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
//SELECTION SORT
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
i=0;j=0;
for(k=0;k<(m+n);k++)
{
if(i==m)
{
c[k]=b[j];
j++;
}
else if(j==n)
{
c[k]=a[i];
i++;
}
else if(a[i]<b[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=b[j];
j++;
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
int b[20],temp,n,i,j;
clrscr();
//SELECTION SORT
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
getch();
}
Binary search
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10]={1,2,4,7,8,9,12,34,57,97};
int mid,lower=0,upper=9,num,flag;
clrscr();
printf("Enter the no to be searched");
scanf("%d",&num);
for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
{
if(arr[mid]==num)
{
printf("the no is at pos %d in the array",mid);
flag=0;
break;
}
if(arr[mid]>num)
upper=mid-1;
else
lower=mid+1;
}
if(flag)
{
printf("Element found in the array");
}
getch();
}
Binary Search Tree
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define TRUE 1
#define FALSE 0
struct btreenode
{
struct btreenode *leftchild;
int data;
struct btreenode *rightchild;
};
void main()
{
struct btreenode *bt;
int req,i=0,num,a[]={11,9,13,8,10,12,14,15,7};
bt=NULL;
clrscr();
while(i<=8)
{
insert(&bt,a[i]);
i++;
}
clrscr();
delete(&bt,10);
printf("\nBinary tree before deletion\n");
inorder(bt);
}
void insert(struct btreenode **sr,int num)
{
if(*sr==NULL)
{
*sr=malloc(sizeof(struct btreenode));
(*sr)->leftchild=NULL;
(*sr)->data=num;
(*sr)->rightchild=NULL;
}
else /*search the node to where it is to be attached*/
{
//if new data is less traverse to left
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num);
else //traverse to right
insert(&((*sr)->rightchild),num);
}
void search(struct btreenode **root,int num,struct btreenode **par,struct btreenode **x,int *found)
{
while(q!=NULL)
{
if(q->data==num)
{
*found=TRUE;
*x=q;
return;
}
*par=q;
if(q->data>num)
q=q->leftchild;
else
q=q->rightchild;
}
}
void inorder(struct btreenode *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
printf("\t%d",sr->data);
inorder(sr->rightchild);
}
if(*root==NULL)
{
printf("Tree is empty");
return;
}
parent=x=NULL;
search(root,num,&parent,&x,&found);
if(found==FALSE)
{
printf("\ndata to be deleted is not found");
return;
}
if(x->leftchild!=NULL&&x->rightchild!=NULL)
{
parent=x;
xsucc=xsucc->leftchild;
x->data=xsucc->data;
x=xsucc;
}
if(x->leftchild==NULL&&x->rightchild==NULL)
{
if(parent->rightchild==x)
parent->rightchild=NULL;
else
parent->leftchild=NULL;
free(x);
return;
}
if(x->leftchild==NULL&&x->rightchild!=NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->rightchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
if(x->leftchild!=NULL&&x->rightchild==NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->leftchild;
else
parent->rightchild=x->leftchild;
free(x);
return;
}
}
Queues
void main()
{
clrscr();
do
{
clrscr();
printf("\n\n What do you want to do? ");
printf("\n\n 1 : INSERT");
printf("\n 2 : DELETE");
printf("\n 3 : DISPLAY");
printf("\n 4 : EXIT");
printf("\n\n Enter your choice here => ");
scanf("%d",&ch);
switch(ch)
{
case 1:{
insertion();
break;
}
case 2:{
deletion();
break;
}
case 3:{
display();
break;
}
case 4: exit();
default: printf("\n\n You have entered a wrong choice.");
}
printf("\n\n Do you want to continue? (y/n): ");
flushall();
scanf("%c",&ans);
}while(ans=='y');
getch();
}
void insertion()
{
if(front==rear)
printf("\n\n Queue is empty.");
printf("\n\n Enter the character to be inserted: ");
flushall();
scanf("%c",&ele);
rear=rear+1;
q[rear]=ele;
}
void deletion()
{
front=front+1;
q[front]=NULL;
printf("\n\n Character %c has been deleted. ",q[front]);
if(front==rear)
printf("\n\n Queue is empty.");
void display()
{
i=front;
if(front==rear)
printf("\n\n Queue is empty.");
front=front+1;
while(front<=rear)
{
printf("%c->",q[front]);
front=front+1;
}
front=i;
}
Stacks
void main()
{
clrscr();
do
{
clrscr();
printf("\n\n What do you want to do? ");
printf("\n\n 1 : INSERT");
printf("\n 2 : DELETE");
printf("\n 3 : DISPLAY");
printf("\n 4 : EXIT");
printf("\n\n Enter your choice here => ");
scanf("%d",&ch);
switch(ch)
{
case 1:{
push();
break;
}
case 2:{
pop();
break;
}
case 3:{
display();
break;
}
case 4: exit();
default: printf("\n\n You have entered a wrong choice.");
}
printf("\n\n Do you want to continue? (y/n): ");
flushall();
scanf("%c",&ans);
}while(ans=='y');
getch();
}
void push()
{
if(top==-1)
printf("\n\n Stack is empty.");
void pop()
{
printf("\n\n Element %d has been deleted.",stack[top]);
top=top-1;
if(top==-1)
printf("\n\n Stack is empty.");
}
void display()
{
if(top==-1)
printf("\n\n Stack is empty.");
else
{
printf("\n\n The Resultant Stack: ");
for(i=0;i<=top;i++)
printf("%d",stack[i]);
}
}