Académique Documents
Professionnel Documents
Culture Documents
#include<stdio.h>
#include<conio.h>
int temp;
temp=*x;
*x = *y;
*y = temp;
int i,s,f;
for(i=1;i< n;++i)
s=i;
f=(s-1)/2;
swap(&a[f],&a[s]);
s=f;
if(s==0)
break;
f=(s-1)/2;
for(i=n-1;i>=1;--i)
swap(&a[0],&a[i]);
f=0;
s=1;
if(i==1)
break;
if(i>2)if(a[2]>a[1])s=2;
swap(&a[f],&a[s]);
f=s;
s=2*f+1;
if(i>s+1 )if(a[s+1]>a[s])s=s+1;
if (s>=i)
break;
}
void main()
int a[100],n,i;
clrscr();
printf("\t\tHEAP SORT\n");
scanf("%d",&n);
printf("\nEnter Elements\n");
for(i=0;i< n;++i)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\n\t\t\tSorted List\n");
for(i=0;i< n;++i)
printf("\t%d",a[i]);
getche();
#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
}*front=NULL,*rear=NULL;
void main()
int choice;
clrscr();
while(1)
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
scanf("%d", &choice);
switch(choice)
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
insert()
int added_item;
scanf("%d",&added_item);
tmp->info = added_item;
tmp->link=NULL;
front=tmp;
else
rear->link=tmp;
rear=tmp;
}/*End of insert()*/
del()
if(front == NULL)
printf("Queue Underflow\n");
else
tmp=front;
front=front->link;
free(tmp);
}/*End of del()*/
display()
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{
while(ptr != NULL)
printf("%d ",ptr->info);
ptr = ptr->link;
printf("\n");
}/*End of else*/
}/*End of display()*/
QUICKSORT
#include<stdio.h>
#include<conio.h>
main()
{
int a[20],p,q,i,n;
clrscr();
p=0;
q=n-1;
printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
quicksort(a,p,q);
if(i< j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
while(i< j);
a[m] =a[j];
a[j] = v;
return j;
}
STACK USING ARRAY
#include<stdio.h>
#include<conio.h>
#define size 5
char stack[size][15],ele[20];
int tos;
void push();
char* pop();
void show();
int isempty();
int isfull();
void main()
int choice;
clrscr();
tos=0;
do
scanf("%d",&choice);
switch(choice)
{
case 1:
if (isfull())
else
scanf("%s",ele);
push(ele);
break;
case 2:
if(isempty())
else
break;
case 3:
if(isempty())
else
show();
break;
case 4:
exit(1);
default:
printf("\nDo you understand english and numbers??");
}while(1);
int isempty()
return(tos==0);
int isfull()
return(tos==size);
void push(ele)
strcpy(stack[tos],ele);
tos++;
char* pop()
tos--;
return(stack[tos]);
void show()
{
int x=tos;
while(x!=0)
printf("\t%s\n",stack[--x]);
#include<stdio.h>
#include<malloc.h>
#define maxsize 10
void push();
void pop();
void display();
struct node
int info;
}*start=NULL, *new,*temp,*p;
int ch,a;
clrscr();
do
printf("\t\t\tLinked stack");
printf("\n 1.Push");
printf("\n 2.Pop");
printf("\n 3.Display");
printf("\n 4.Exit");
scanf("%d",&ch);
switch(ch)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nInvalid choice");
break;
while(ch<=3);
}
void push()
new=(N*)malloc(sizeof(N));
scanf("%d",&new->info);
new->link=NULL;
if(start==NULL)
start=new;
else
p=start;
while(p->link!=NULL)
p=p->link;
p->link=new;
getch();
}
void pop()
if(start==NULL)
printf("\nStack is empty");
else if(start->link==NULL)
free(start);
start=NULL;
else
p=start;
while(p->link!=NULL)
temp=p;
p=p->link;
temp->link=NULL;
free(p);
void display()
if(start==NULL)
printf("\nStack is empty");
else
p=start;
while(p!=NULL)
{
printf("\t%d",p->info);
p=p->link;
printf("\n");
# include <stdio.h>
# include <conio.h>
# define SIZE 10
int arr[SIZE], front = -1, rear = -1, i ;
void enqueue() ;
void dequeue() ;
void display() ;
void main()
{
int ch ;
clrscr() ;
do
{
printf("QUEUE USING ARRAY\n");
printf("[1].ENQUEUE\n [2].DEQUEUE\n [3].Display\n [4].Exit\n") ;
scanf("%d", &ch) ;
switch(ch)
{
case 1 :
enqueue() ;
break ;
case 2 :
dequeue() ;
break ;
case 3 :
display() ;
break ;
case 4 :
break ;
default :
printf("Invalid option") ;
getch() ;
} while(ch != 4) ;
getch() ;
void enqueue()
if(rear == SIZE - 1)
getch() ;
return ;
rear++ ;
if(front == -1)
front++ ;
void dequeue()
if(front == -1)
getch() ;
return ;
getch() ;
if(front == rear)
front = rear = -1 ;
else
front++ ;
void display()
if(front == -1)
getch() ;
return ;
}
printf("The elements in queue are :FRONT ->") ;
getch() ;
BALANCED PARENTHESIS
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include"stack.c"
void main()
{
char expr[20];
char ch;
int i,n;
clrscr();
printf("Enter the expression");
scanf("%s",expr);
n=strlen(expr);
for(i=0;i<n;i++)
{
if(expr[i]=='(')
push(expr[i]);
if(expr[i]==')')
pop();
}
if(top==-1)
printf("Balanced expression");
else
printf("Unbalanced expression");
getch();
}
INFIX TO POSTFIX
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
char inf[40],post[40];
int top=0,st[20];
void postfix();
void push(int);
char pop();
void main()
{
clrscr();
printf("\n Enter infix expression:");
scanf("%s",&inf);
postfix();
getch();
}
void postfix()
{
int i,j=0;
for(i=0;inf[i]!='\0';i++)
{
switch(inf[i])
{
case '+':
while(st[top]>=1)
post[j++]=pop();
push(1);
break;
case '-':
while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case '*':
while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case '/':
while(st[top]>=3)
post[j++]=pop();
push(4);
break;
case '^':
while(st[top]>=4)
post[j++]=pop();
push(5);
break;
case '(':
push(0);
break;
case ')':
while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:
post[j++]=inf[i];
}
}
while(top>0)
post[j++]=pop();
printf("\n\t Postfix expression is %s",post);
}
void push(int ele)
{
top++;
st[top]=ele;
}
char pop()
{
int el;
char e;
el=st[top];
top--;
switch(el)
{
case 1:
e='+';
break;
case 2:
e='-';
break;
case 3:
e='*';
break;
case 4:
e='/';
break;
case 5:
e='^';
break;
}
return(e);
}
#include<stdio.h>
#include<conio.h>
#define max 100
struct list
{
int a[max];
int last;
}s;
void main()
{
int x,p,loc,c;
void insert(int,int);
int locate(int);
void delete(int);
clrscr();
s.last==0;
while(1)
{
printf("LIST USING ARRAY \n");
printf("\n.1.INSERT\n 2.locate\n3.DELETE\n4.EXIT\n");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter the elements & It's Location\n");
scanf("%d%d",&x,&p);
insert(x,p);
break;
case 2:
printf("Enter elements to be located\n");
scanf("%d",&x);
loc=locate(x);
if(loc==0)
printf("Elements not found");
else
printf("location of x--%d\n",loc);
break;
case 3:
printf("Enter position to delete elements\n");
scanf("%d",&p);
delete(p);
break;
case 4:
exit(0);
getch();
}
}
}
void insert(int x,int p)
{
int q;
if(s.last>=max)
{
printf("LIST FULL");
}
else if((p>s.last+1)||(p<1))
printf("position does not exist");
else
{
for(q=s.last;q>=p;q--)
s.a[q+1]=s.a[q];
s.last=s.last+1;
s.a[p]=x;
}
}
int locate(int x)
{
int q;
for(q-1;q<=s.last;q++)
{
if(s.a[q]==x)
return q;
}
return(0);
}
void delete(int p)
{
int q;
if((p>s.last)||(p>1))
{
printf("position does not exist");
}
else
{
s.last=s.last-1;
for(q=p;q<=s.last;q++)
s.a[q]=s.a[q+1];
}
}
Binary Search Tree
#include< stdio.h>
#include< conio.h>
#include< string.h>
#include< dos.h>
#include< stdlib.h>
struct node
{
char data[15];
struct node *left,*right;
};
1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 1
/****** Data Entry *******
1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 4
1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 1
Lionel Dylan Malcolm Cyril Daniel Jason
1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 2
Malcolm Cyril Dylan Lionel Jason Daniel
1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 3
Cyril Malcolm Dylan Lionel Jason Daniel
1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 4
Malcolm Cyril Dylan Lionel Jason Daniel
1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 5
1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 3
1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 5
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i;
struct node
{
int data;
struct node *next;
}*head,*temp,*temp1;
void main()
{
int ch,p,c,n;
clrscr();
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
while(1)
{
printf("\n1.create2.display3.make null4.insert5.delete6.locate7.exit\n");
printf("\n enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1;
printf("\nenter element");
if(head->next==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%d",&temp->data);
head->next=temp;
temp->next=NULL;
}
else
{
temp1=(struct node*)malloc(sizeof(struct node));
scanf("%d",&temp1->data);
temp->next=temp1;
temp1->next=NULL;
temp=temp1;
}
break;
case 2:
temp=head;
if((temp->next)==NULL)
printf("\n list empty\n");
else
{
printf("\t\t\n\n elements in list\t\n\n");
while(temp->next!=NULL)
{
printf("->%d",temp->next->data);
temp=temp->next;
}
}
break;
case 3:
temp=head;
temp->next=NULL;
break;
case 4:
temp1=(struct node*)malloc(sizeof(struct node));
printf("\n to insert 1.first2.last3.anywhere\n");
scanf("%d",&c);
printf("enter element to be inserted\n");
scanf("%d",&temp1->data);
switch(c)
{
case 1:
temp1->next=head->next;
head->next=temp1;
break;
case 2:
temp=head->next;
while(temp->next!=NULL)
temp=temp->next;
temp->next=temp1;
temp1->next=NULL;
break;
case 3:
printf("enter position\n");
scanf("%d",&p);
if(p==1)
{
temp1->next=head->next;
head->next=temp1;
}
else
{
temp=head->next;
for(i=1;i<p-1;i++)
temp=temp->next;
temp1->next=temp->next;
temp->next=temp1;
}
}break;
case 5:
temp=head ;
printf("enter position to be deleted ");
scanf("%d",&p);
for(i=1;i<p;i++)
temp=temp->next;
temp->next=temp->next->next;
break;
case 6:
temp=head->next;
printf("enter element to be located\n");
scanf("%d",&n);
for(i=1;temp->data!=n;i++)
temp=temp->next;
if(temp->data==n)
printf("element is at position %d\n",i);
else
printf("elements not found\n");
break;
case 7:
exit(0);
}
}
getch();
}