Académique Documents
Professionnel Documents
Culture Documents
0 PROGRAM
#include<conio.h> else
#include<stdio.h> printf("\n Stack is empty...");
#define MAX 6 break;
typedef struct stack case 3:print(&s);
{ break;
int data[MAX]; }
int top; }while(op!=4);
}stack; }
void init(stack *); void init(stack *s)
{
int empty(stack *);
s->top=-1;
int full(stack *); }
int pop(stack *); int empty(stack *s)
void push(stack *,int); {
void print(stack *); if(s->top==-1)
return(1);
void main() else
{ return(0);
stack s; }
int x,op; int full(stack *s)
init(&s); {
clrscr();
if(s->top==MAX-1)
do return(1);
{ return(0);
printf("\n1)push\n2)pop\n3)print\n4)quit\n"); }
printf("\n Enter your choice :"); void push(stack *s,int x)
scanf("%d",&op); {
switch(op) s->top=s->top+1;
{ case 1:printf("\n Enter a number :"); s->data[s->top]=x;
scanf("%d",&x); }
if(!full(&s)) int pop(stack *s)
{ {
push(&s,x); int x;
} x=s->data[s->top]; s->top=s->top-1; return(x);
else }
{ void print(stack *s)
printf("\n Stack is full....."); {
} int i; printf("\n\n");
break; for(i=s->top;i>=0;i--)
case 2:if(!empty(&s)) printf("%d\n",s->data[i]);
{
x=pop(&s);
printf("\n Popped value=%d",x);
}
7.0 Results:
8.0 Conclusion:
Thus we have successfully performed different operations on stack using menu driven
program
5.0 Program:
#include<stdio.h> while(priority(token)<
#include<conio.h> =priority(top(&s)) && !empty(&s))
#include<ctype.h> {
#define MAX 100 x=pop(&s);
typedef struct stack printf("%c",x);
{ }
int data[MAX]; push(&s,token);
int top; }
}stack; }
int priority(char); }
void init(stack *); while(!empty(&s))
int empty(stack *); {
int full(stack *); x=pop(&s);
char pop(stack *); printf("%c",x);
void push(stack *,char); }
char top(stack *); getch();
}
void main() //---------------------------------------------
{ int priority(char x)
stack s; {
char x; if(x == '(')
int token; return(0);
init(&s); if(x == '+' || x == '-')
clrscr(); return(1);
printf("nEnter infix expression:"); if(x == '*' || x == '/' || x == '%')
while((token=getchar())!='n') return(2);
{ return(3);
if(isalnum(token)) }
printf("%c",token); //---------------------------------------------
else void init(stack *s)
if(token == '(') {
push(&s,'('); s->top=-1;
else }
{ //---------------------------------------------
if(token == ')') int empty(stack *s)
while((x=pop(&s))!='(') {
printf("%c",x); if(s->top==-1)
else return(1);
{ else
return(0);
}
//
int full(stack *s)
{
if(s->top==MAX-1)
return(1);
else
return(0);
}
//
void push(stack *s,char x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
//
char pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
//
char top(stack * s)
{
return(s->data[s->top]);
}
//
7.0 Results:
case '-':
5.0 Program:
{
n3 = n2 - n1; break;
#include<stdio.h> }
int stack[20]; case '*':
{
int top = -1;
n3 = n1 * n2; break;
}
void push(int x) case '/':
{
{ n3 = n2 / n1;
stack[++top] = x; break;
} }
int pop() }
{ push(n3);
return stack[top--]; }
} e++;
}
void main() printf("\nThe result of expression %s =
{ %d\n\n",exp,pop());
char exp[20]; getch();
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: "); 6.0 Results:
scanf("%s",exp);
Enter the expression ::53+
e = exp;
while(*e != '\0') The result of expression= 8
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{ n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
5.0 Program:
#include<stdlib.h>
scanf("%d",&choice);
#include<stdio.h>
switch(choice)
#include<conio.h>
{
#define max_size 5
case 1:
int queue[max_size],front=-1,rear=-1;
insert();
/*------ Function Prototype ----------- */
break;
void insert();
case 2:
void del();
del();
void display();
break;
/*-------------------------------------*/
case 3:
int main()
display();
{
break;
int choice;
case 4:
do{
exit(0);
break;
printf("2.Delete\n"); break;
printf("3.Display\n"); }
printf("4.Exit\n"); }while(choice!=4);
printf("\nEnter your }
choice:\t");
void insert() //Inserting an element in printf("\nQueue Underflow:");
to the queue
}
{
else
int item;
{
if(rear==(max_size-1))
item=queue[front];
{
printf("\nThe deleted element:
printf("\nQueue Overflow:"); %d\t",item);
} if(front==rear)
else {
{ front=-1;
else
printf("%d\t",queue[i]);
getch();
}//end of display()
6.0 Results:
--------QUEUE OPERATIONS----------
1.Insert
2.Delete
3.Display
4.Exit
--------QUEUE OPERATIONS----------
1.Insert
2.Delete
3.Display
4.Exit
--------QUEUE OPERATIONS----------
1.Insert
2.Delete
3.Display
4.Exit
--------QUEUE OPERATIONS----------
1.Insert
2.Delete
3.Display
4.Exit
Front -> 0
Items -> 1 2 3 4 5
Rear -> 4
Front -> 1
Items -> 2 3 4 5
Rear -> 4
Inserted -> 7
Front -> 1
Items -> 2 3 4 5 7
Rear -> 0
Queue is full!!
5.0 Program:
#include<conio.h> break;
#include<stdio.h> case 3:printf("\nNo of nodes=%d",count(head));
#include<stdlib.h>. break;
typedef struct node case 4: printf("\n\t 1:beginning\n\t 2:end\n\t 3:in
{ between\n");
int data; printf("\nEnter your chioce:");
struct node *next; scanf("%d",&op1);
}node; printf("\nEnter the data to be inserted:");
node *create(); scanf("%d",&x);
void print(node *head); switch(op1)
int count(node *); {
void search(node*); case 1:head=insert_b(head,x);
node *insert_b(node *head,int x); break;
node *insert_e(node *head,int x); case 2:head=insert_e(head,x);
node *insert_in(node *head,int x); break;
node *delete_b(node *head); case 3:head=insert_in(head,x);
node *delete_e(node *head); break;
node *delete_in(node *head); }
void main() break;
{
int op,op1,x,n; case 5:printf("\n 1:beginning\n 2:end\n
node *head=NULL; 3:inbetween\n");
clrscr(); printf("Enter your choice");
do scanf("%d",&op1);
{ switch(op1)
{
printf("\n 1.create\n 2.print\n 3.count\n 4.insert\n case 1:head=delete_b(head);
5.delete\n 6.search\n 7.quit"); break;
printf("\nEnter your choice:"); case 2:head=delete_e(head);
scanf("%d",&op); break;
switch(op) case 3:head=delete_in(head);
{ break;
case 1:head=create(); }
break; default:printf("Invalid input");
case 2:print(head); break;
case 6:search(head); return(i);
break; }
}
}while(op!=7);
getch(); node *insert_b(node *head,int x)
// while(op<6&&op>0); {
} node *p;
node *create() p=(node*)malloc(sizeof(node));
{ p->data=x;
node *head,*p; p->next=head;
int i,n; head=p;
head=NULL; return(head);
printf("\n Enter no of the data:"); }
scanf("%d",&n);
printf("\n Enter the data:");
for(i=0;i<n;i++) node *insert_e(node *head,int x)
{ {
if(head==NULL) node *p,*q;
{ p=(node*)malloc(sizeof(node));
p=head=(node*)malloc(sizeof(node)); p->data=x;
} p->next=NULL;
else if(head==NULL)
{ return(p);
p->next=(node*)malloc(sizeof(node)); for(q=head;q->next!=NULL;q=q->next);
p=p->next; q->next=p;
} return(head);
p->next=NULL; }
scanf("%d",&(p->data));
}
return(head); node *insert_in(node *head,int x)
} {
node *p,*q;
int y;
void print(node *head) p=(node*)malloc(sizeof(node));
{ p->data=x;
node *p; p->next=NULL;
printf("\n\n"); printf("\n Enter the data after which you want to
for(p=head;p!=NULL;p=p->next) insert new node?");
printf("%d ",p->data); scanf("%d",&y);
} for(q=head;q!=NULL&&q->data!=y;q=q-
>next);
if(q!=NULL)
int count(node *h) {
{ p- >next=q->next;
int i; q->next=p;
for(i=0;h!=NULL;h=h->next) }
i++; else
printf("\n Data not found:"); node *p,*q;int x,i;
return(head); if(head==NULL)
{
}
printf("\n Underflw");
return(head);
node *delete_b(node *head) }
{ printf("\n Enter the data to be delete:");
node *p,*q; scanf("%d",&x);
if(head==NULL) if(head->data==x)
{ {
printf("\n Underflow"); p=head;
return(head); head=head->next;
} free(p);
else return(head);
}
{
p=head; for(q=head;q->next->data!=x&&q-
head=head->next; >next!=NULL;q=q->next);
if(q->next==NULL)
free(p);
}return(head); {
printf("\n Underflow");
}
return(head);
}
node *delete_e(node *head) p=q->next;
q->next=q->next->next;
{
free(p);
node *p,*q;
if(head==NULL) return(head);
{ }
void search(node *head)
printf("\n Underflow");
{
return(head);
node *p;
}
int data,loc=1;
p=head;
printf("\n Enter the data to be search:");
if(head->next==NULL)
scanf("\n%d",&data);
{
p=head;
head=NULL;
while(p!=NULL&&p->data!=data)
free(p);
{
return(head);
loc++;
}
p=p->next;
for(q=head;q->next->next!=NULL;q=q->next);
}
p=q->next;
if(p==NULL)
q- >next=NULL;
{
free(p);
printf("\nData not found:");
return(head);
}
}
node *delete_in(node *head) else
{ {
printf("\n%d",loc);
}}
7.0 Results:
5.0 Program:
#include<conio.h>
#include<stdio.h> int full(stack *s)
#include<stdlib.h> {
#include<string.h> if(s->top==19)
#include<ctype.h> return(0);
else
typedef struct treenode return(0);
{ }
char data;
struct treenode *left,*right; treenode *create();
}treenode; void inorder(treenode *T);
void preorder(treenode *T);
typedef struct stack void postorder(treenode *T);
{
treenode *data[20]; void main()
int top; {
}stack; treenode *root=NULL,*p;
int x,op;
void init(stack *s) clrscr();
{ do
s->top=-1; {
}
printf("\n\n1.Create\n2.Preorder\n3.Inorder\n4.Po
treenode *pop(stack *s) storder\n5.Quit");
{ printf("\nEnter your choice:");
treenode *p; scanf("%d",&op);
p=s->data[s->top]; switch(op)
s->top=s->top-1; {
return(p); case 1:root=create();
} break;
case 2:preorder(root);
void push(stack *s,treenode *p) break;
{ case 3:inorder(root);
s->top=s->top+1; break;
s->data[s->top]=p; case 4:postorder(root);
} break;
}
}while(op!=5);
int empty(stack *s) }
{
if(s->top==-1) void inorder(treenode *T)
return(0); {
else if(T!=NULL)
return(1); {
} inorder(T->left);
printf("%c",T->data); {
inorder(T->right); q=pop(&s);
} p=pop(&s);
} root=(treenode*)malloc(sizeof(treenode));
root->left=p;
root->right=q;
void preorder(treenode *T) root->data=a[i];
{ push(&s,root);
if(T!=NULL) }
0{ }
printf("%c",T->data); root=pop(&s);
preorder(T->left); return(root);
preorder(T->right); }
}
7.0 Result
}
treenode * create()
{
char a[50];
int i;
treenode *p,*q,*root;
stack s;
init(&s);
flushall();
printf("\n Enter a postfix expression:");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(isalnum(a[i]))
{
p=(treenode*)malloc(sizeof(treenode));
p->left=p->right=NULL;
p->data=a[i];
push(&s,p);
5.0 Program:
{
#include<stdio.h>
while(root!=NULL)
#include<conio.h>
{
if(x==root->data) return(root); if(x>root->data)
typedef struct BSTnode root=root->right; else
{ root=root->left;
int data; }
struct BSTnode *left,*right; return(NULL);
}BSTnode; }
BSTnode *init(); BSTnode *insert(BSTnode *T,int x)
BSTnode *find(BSTnode *,int); BSTnode {
*insert(BSTnode *,int); BSTnode *delet(BSTnode
*,int); BSTnode *find_min(BSTnode *); BSTnode BSTnode *p,*q,*r;
*find_max(BSTnode *); BSTnode *create(); r=(BSTnode*)malloc(sizeof(BSTnode)); r-
>data=x;
void inorder(BSTnode *T); void main()
r->left=NULL; r->right=NULL; if(T==NULL)
{
return(r); p=T;
BSTnode *root,*p; int x;
while(p!=NULL)
clrscr();
{
init(); root=create();
q=p;
printf("\n**BST created**"); printf("\ninorder
traversal on the tree"); inorder(root); if(x>p->data) p=p->right; else
p=find_min(root); p=p->left;
printf("\nsamllest key in the tree=%d",p->data); }
p=find_max(root); if(x>q->data) q->right=r; else
printf("\nlargest key in the tree=%d",p->data); q->left=r; return(T);
printf("\n**delete operation**"); printf("\nEnter
}
the key to be deleted:"); scanf("%d",&x);
BSTnode *delet(BSTnode *T,int x)
root=delet(root,x);
{
printf("\ninorder traversal after deletion:");
inorder(root); BSTnode *temp; if(T==NULL)
} {
void inorder(BSTnode *T) printf("\nElement not found:"); return(T);
{ }
if(T!=NULL) if(x<T->data)
{ {
inorder(T->left); printf("%5d",T->data); T->left=delet(T->left,x); return(T);
inorder(T->right); }
} if(x>T->data)
} {
BSTnode *init() T->right=delet(T->right,x); return(T);
{ }
return(NULL); if(T->left==NULL&&T->right==NULL)
} {
BSTnode *find(BSTnode *root,int x) temp=T; free(temp); return(NULL);
} 7.0 Result
if(T->left==NULL)
{
temp=T; T=T->right; free(temp); return(T);
}
if(T->right==NULL)
{
temp=T; T=T->left; free(temp); return(T);
}
temp=find_min(T->right); T->data=temp->data;
T->right=delet(T->right,x); return(T);
}
BSTnode *create()
{
int n,x,i; BSTnode *root; root=NULL;
printf("\nEnter no. of nodes:"); scanf("%d",&n);
printf("\n Enter tree values:"); for(i=0;i<n;i++)
{
scanf("%d",&x); root=insert(root,x);
}
return(root);
}
BSTnode *find_min(BSTnode *T)
{
while(T->left!=NULL) T=T->left;
return(T);
}
BSTnode *find_max(BSTnode *T)
{
while(T->right!=NULL) T=T->right;
return(T);
}
5.0 Program:
#include<stdio.h> for(j=i-1;j>=0&&a[j]>temp;j--)a[j+1]=a[j];
#include<conio.h> a[j+1]=temp;
#include<stdlib.h>
printf("\nafter pass %d",i);
#include<ctype.h>
for(k=0;k<n;k++)
void insertion_sort(int [],int);
void shell_sort(int a[],int n); printf("%5d",a[k]);
void radix_sort(int a[],int n); }
}
void main()
void shell_sort(int a[],int n)
{ {
int a[50],n,i,op; int i,j,step,pass=1,temp;
clrscr(); printf("\nUnsorted data:");
do for(i=0;i<n;i++)
{ printf("%5d",a[i]);
printf("\n1.Insert\n2.Shell\n3.Radix\n4.Quit"); for(step=n/2;step>0;step=ste
printf("\nEnter your choice:"); scanf("%d",&op); p/2)
switch(op) {
{ for(i=step;i<n;i++)
case 1:printf("\nEnter no of elements:"); {
scanf("%d",&n); temp=a[i];
printf("\nEnter array elements:"); for(j=i;j>=step;j=j-
for(i=0;i<n;i++) scanf("%d",&a[i]); step)
insertion_sort(a,n); {
break; if(temp<a[j-
case 2:printf("\nEnter no of elements:"); step])a[j]=a[j-
scanf("%d",&n); step]; else
printf("\nEnter array elements:"); break;
for(i=0;i<n;i++) scanf("%d",&a[i]); }
shell_sort(a,n); a[j]=temp;
break; }
case 3:printf("\nEnter no of elements:"); printf("\nAfter pass
scanf("%d",&n); %d",pass);for(i=0;i<n;i++)
printf("\nEnter array elements:"); printf("%5d",a[i]);
for(i=0;i<n;i++) scanf("%d",&a[i]); pass++;
radix_sort(a,n); }
break; }
}
}while(op!=4);
}
void radix_sort(int a[],int n)
void insertion_sort(int a[],int n) {
{ int buckets[10][10],count[10];
int i,j,temp,k; int
printf("\nUnsorted data:"); passes,large,div,bucketno,i,j,k
for(k=0;k<n;k++) ;large=a[0];
printf("%5d",a[k]); for(i=1;i<n;i
for(i=1;i<n;i++) ++)
{ if(a[i]>larg
temp=a[i]; e)
large=a[i];
passes=0;
while(large>0)
{
passes++; large=large/10;
}
div=1; for(i=1;i<=passes;i++)
{
for(j=0;j<=9;j++)count[j]=0;
for(j=0;j<n;j++)
{
bucketno=(a[j]/div)%10;
buckets[bucketno][count[bucketno]]=a[j];
count[bucketno]++;
}
for(bucketno=0;bucketno<=9;bucketn
o++)
for(k=0;k<count[bucketno];k++)
a[j++]=buckets[bucketno][k];
div=div*10;
}
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
7.0 Resul
5.0 Program:
#include<stdio.h>
#include<conio.h> void quick_sort(int a[],int l,int u)
#include<stdlib.h> {
int j;
void quick_sort(int[],int,int); if(l<u)
int partition(int[],int,int); {
void mergesort(int a[],int i,int j); j=partition(a,l,u);
void merge(int a[],int i1,int j1,int i2,int j2); quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
void main() }
{ }
int a[30],n,i,op;
clrscr(); int partition(int a[],int l,int u)
do {
{ int v,i,j,temp;
printf("\n1.Quick Sort\n2.Merge Sort\n3.Quit"); v=a[l];
printf("\nEnter your choice:"); i=l;
scanf("%d",&op); j=u+1;
switch(op) do
{ {
case 1:printf("\nEnter no of elements:"); do
scanf("%d",&n); {
printf("\n Enter array element:"); i++;
for(i=0;i<n;i++) }while(a[i]<v&&i<=u);
scanf("%d",&a[i]); do
quick_sort(a,0,n-1); {
printf("\n Sorted array is:"); j--;
for(i=0;i<n;i++) }while(a[j]>v);
printf("%d ",a[i]); if(i<j)
break; {
case 2:printf("\nEnter no of elements:"); temp=a[i];
scanf("%d",&n); a[i]=a[j];
printf("\n Enter array element:"); a[j]=temp;
for(i=0;i<n;i++) }
scanf("%d",&a[i]); }while(i<j);
mergesort(a,0,n-1); a[l]=a[j];
printf("\n Sorted array is:"); a[j]=v;
for(i=0;i<n;i++) return(j);
printf("%d ",a[i]); }
break;
void mergesort(int a[],int i,int j)
} {
}while(op!=3); int mid;
} if(i<j)
{ while(i<=j1&&j<=j2)
mid=(i+j)/2; {
mergesort(a,i,mid); if(a[i]<a[j])
mergesort(a,mid+1,j); temp[k++]=a[i++];
merge(a,i,mid,mid+1,j); else
} temp[k++]=a[j++];
} }
while(i<=j1)
void merge(int a[],int i1,int j1,int i2,int j2) temp[k++]=a[i++];
{ while(j<=j2)
int temp[50]; temp[k++]=a[j++];
int i,j,k; for(i=i1,j=0;i<=j2;i++,j++)
i=i1; a[i]=temp[j];
j=i2;
k=0;
7.0 Result