Monday, May 11, 2009

C-PROGRAM FOR BINARY SEARCH USING RECURSIVE FUNCTION

#include
#include
int key;
void main()
{
int a[50],i,n,loc;
int bin();
printf("\nenter the size of the array");
scanf("%d",&n);
printf("%d\n",n);
printf("\narray elements?\n\n");
for(i=0;i
scanf("%d",&a[i]);
for(i=0;i
printf("%5d",a[i]);
printf("\n");
printf("\nelement to be searched");
scanf("%d",&key);
printf("\n%d",key);
loc=bin(a,0,n);
printf("\n\n");
if(loc==0)
printf("\nunsuccessful search %d not found\n",key);
else
{
printf("\nsuccessful search");
printf("\n%d found at position %d \n",key,loc);
}
getch();
}

C-PROGRAM FOR BINARY SEARCH

#include
#include
void main()
{
int a[100],i,n,low,high,mid,term,flag=1;
printf("\nnumber of elements in the array?");
scanf("%d",&n);
printf("%d",n);
printf("\n\nenter elements in ascending order:\n");
for(i=0;i<=high) { mid=(low+high)/2; if(terma[mid])
low=mid+1;
else
if(term==a[mid])
{
printf("\nsearch successful");
printf("\n%d found at location %d\n",term,mid+1);
flag=0;
break;
}
}
if(flag==1)
printf("\nsearch unsuccessful");
getch();
}

C-PROGRAM FOR QUICK SORT

#include
void quick(int a[],int,int);
void main()
{
int a[20],i,j,n,l,u;
printf("\nenter size of the array");
scanf("%d",&n);
printf("\nenter elements of the array");
for(i=0;ip)
j--;
if(i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[j];
a[j]=a[l];
a[l]=temp;
}
quick(a,l,j-1);
quick(a,j+1,u);
}
}
}

C-PROGRAM FOR MERGE SORT

#include
#include
void main(void)
{
int i,j,k,l,m,n,a[20],b[20],c[20];
printf("\nenter the array1 size");
scanf("%d",&m);
printf("\nenter %d elements",m);
for(i=0;i<=m;i++)
scanf("%d",&a[i]);
sort_array(a,m-1);
printf("\nsorted arra1:");
for(i=0;i<=m;i++)
printf("\t%d",a[i]);
printf("\nenter the array2 size:");
scanf("%d",&n);
printf("\nenter %d elements:",n);
for(i=0;i<=n;i++)
printf("\t%d",&b[i]);
sort_array(b,n-1);
printf("\nsorted array 2:");
for(i=0;i<=n;i++)
printf("\t%d",b[i]);
i=j=k=0;
while((i if(a[i] < b[j])
{
c[k]=a[i];
i++;
k++;
}
else
{
if(a[i]>b[j])
{
c[k]=b[j];
j++;
k++;
}
else
{
c[k]=a[i];
i++;
j++;
k++;
}
}
if(i {
for(i=1;i {
c[k]=a[i];
i++;
k++;
}
}
else
if(j {
for(i=j;i {
c[k]=b[j];
j++;
k++;
}
}
printf("\nmerged array is:");
for(i=0;i printf("\t %d",c[i]);
}

void sort_array(int sa[20],int x)
{
int i,flag,temp;
flag=0;
while(flag==0)
{
flag=1;
for(i=0;i {
if(sa[i]>sa[i+1])
{
temp=sa[i];
sa[i]=sa[i+1];
sa[i+1]=temp;
flag=0;
}
}
}
}

C-PROGRAM FOR BUBBLE SORT

/*bubble sort*/
#include
#include
void main()
{
int i,j,k,n,flag,limit,a[50];
printf("number of array elements?");
scanf("%d",&n);
printf("%d",n);
printf("\nenter array elements");
for(i=0;ia[j+1])
{
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
flag=0;
}
}
if(flag)
break;
else
flag=1;
}
printf("\nsorted array:\n");
for(i=0;i
printf("\n%5d",a[i]);
printf("\n");
getch();
}

Saturday, May 9, 2009

BEST WEBSITES USEFULL TO EVERY ONE

http://ecmrocks.synthasite.com/resources/Best_Websites_list.xls

PROGRAM FOR IMPLEMENTING QUEUE USING LINKED LIST

#include
#include
#include
#define NULL 0
struct queue
{
int info;
struct queue *next;
};
typedef struct queue node;
node *first,*list,*front,*rear;
void add();
void del();
void print();
void main()
{
int ch;
front=rear=NULL;
do
{
printf("\n 1->add");
printf("\n 2->del");
printf("\n 3->exit");
printf("\n Enter choice");
scanf("%d",&ch);
switch(ch)
{
case 1: add();
print();
break;
case 2: del();
print();
break;
case 3: exit(0);
break;
default: printf("Invalid choice");
}
}while(ch!=3);
}
void add()
{
first=(node *)malloc(sizeof(node));
printf("Enter element");
scanf("%d",&first->info);
first->next=NULL;
if(rear==NULL)
{
rear=first;
front=first;
}
else
rear->next=first;
rear=first;
}

void del()
{
if(front==NULL)
{
printf("\nList is empty\n");
getch();
}
else
{
printf("The popped element is %d \n ",front->info);
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
front=front->next;
}
}

void print()
{
list=front;
if(list==NULL)
{
printf("\nList is empty\n");
}
else
{
printf("\nfront->");
while(list!=rear)
{
printf("%d->",list->info);
list=list->next;
}
printf("%d->",list->info);
printf("rear\n");
}
}

PROGRAM FOR IMPLEMENTING STACK USING LINKED LIST

#include
#include
#include
#define NULL 0
struct stack
{
int info;
struct stack *next;
};
typedef struct stack node;
node *first,*list,*root;
void push();
void pop();
void print();
void main()
{
int ch;
root=NULL;
do
{
printf("\n 1->push");
printf("\n 2->pop");
printf("\n 3->print");
printf("\n 4->exit");
printf("\n Enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: print();
break;
case 4: exit(0);
break;
}
}while(ch!=4);
}
void push()
{
first=(node *)malloc(sizeof(node));
printf("Enter element");
scanf("%d",&first->info);
first->next=root;
root=first;
}
void pop()
{
if(root==NULL)
printf("\n List is empty\n");
else
{
printf("The popped element is %d\n",root->info);
root=root->next;
}
}
void print()
{
list=root;
if(list==NULL)
printf("\nList is empty\n");
else
{
printf("\n The stack elements are");
while(list!=NULL)
{
printf("\n %d",list->info);
list=list->next;
}
}
}

PROGRAM FOR IMPLEMENTING CIRCULAR LINKED LIST

#include
#include
#include
struct cir
{
int info;
struct cir *next;
};
typedef struct cir node;
node *first,*temp,*new1,*list,*prev,*cur,*p;
void creat()
{
int ele;
printf("Type -1 to stop");
printf("\n Enter ele \t");
scanf("%d",&ele);
while(ele!=-1)
{
new1=(node *)malloc(sizeof(node));
new1->info=ele;
if(first==NULL)
{
new1->next=new1;
first=new1;
}
else
{
new1->next=first->next;
first->next=new1;
}
printf("enter ele\t");
scanf("%d",&ele);
}
}
void count()
{
int c=0;
temp=first;
first=first->next;
while(first!=temp)
{
++c;
first=first->next;
}
++c;
printf("No of nodes in circular list=%d",c);
}
void print()
{
node *start;
temp=first;
start=first->next;
printf("the circulat list is");
while(start!=temp)
{
printf("%d->",start->info);
start=start->next;
}
printf("%d",start->info);
}

PROGRAM FOR IMPLEMENTING DOUBLE LINKED LIST

#include
#include
#include
#define NULL 0
typedef struct dlist
{
int info;
struct dlist *left;
struct dlist *right;
}node;
node *root,*first,*prev,*list,*start,*temp,*end,*list1,*last;
int ele;
void create()
{
printf("Type -1 to stop");
printf("\n Enter element");
scanf("%d",&ele);
while(ele!=-1)
{
first=(node *)malloc(sizeof(node));
first->info=ele;
first->left=NULL;
first->right=NULL;
if(root==NULL)
root=first;
else
{
prev->right=first;
first->left=prev;
}
prev=first;
end=first;
scanf("%d",&ele);
}
}
void print()
{
list=root;
list1=end;
if(root==NULL)
printf("\n List is empty");
else
{
printf("\n contents of list in forward direction\n");
printf("root->");
while(list!=NULL)
{
printf("%d->",list->info);
list=list->right;
}
printf("NULL\n");
}
if(end==NULL)
printf("\n List is empty");
else
{
printf("\n contents of list in backward direction\n");
printf("end->");
while(list1!=NULL)
{
printf("%d->",list1->info);
list1=list1->left;
}
printf("NULL\n");
}
}
void delet()
{
int item;
temp=start=root;
if(root==NULL)
printf("Empty list");
else
{
printf("Enter the element to be deleted\n");
scanf("%d",&item);
if(root->info==item)
{
root=root->right;
root->left=NULL;
}
else
{
while((item!=temp->info)&&(temp!=NULL))
{
start=temp;
temp=temp->right;
}
start->right=temp->right;
temp->right->left=start;
}
}
}
void main()
{
int ch;
root=NULL;
clrscr();
do
{
printf("\n 1->create");
printf("\n 2->deletion");
printf("\n 3->exit");
printf("Enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
print();
break;
case 2: delet();
print();
break;
case 3: exit(0);
break;
}
}while(ch!=3);
getch();
}

PROGRAM FOR IMPLEMENTING SINGLR LINKED LIST

#include
#include
#include
#define NULL 0
struct slist
{
int info;
struct slist *next;
};
typedef struct slist node;
node *root,*new1,*prev,*list,*start,*temp;
int ele;
void create()
{
printf("Type -999 to stop");
scanf("%d",&ele);
while(ele!=-999)
{
new1=(node *)malloc(sizeof(node));
new1->info=ele;
new1->next=NULL;
if(root==NULL)
root=new1;
else
prev->next=new1;
prev=new1;
scanf("%d",&ele);
}
}
void print()
{
list=root;
if(root==NULL)
printf("list is empty");
else
{
printf("\n contents of list");
printf("root->");
while(list!=NULL)
{
printf("%d->",list->info);
list=list->next;
}
printf("NULL");
}
}

void delet()
{
int item;
node *p;
printf("Enter the element to be deleted");
scanf("%d",&item);
if(root->info==item)
{
p=root;
root=root->next;
free(p);
}
else
{
temp=root;
while((item!=temp->info)&&(temp!=NULL))
{
prev=temp;
temp=temp->next;
}
if(item==temp->info)
{
prev->next=temp->next;
free(temp);
}
}
}



void main()
{
int ch;
root=NULL;
do
{
printf("\n 1->creation");
printf("\n 2->deletion");
printf("\n 3->exit");
printf("\n Enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
print();
break;
case 2: delet();
print();
break;
case 3: exit(0);
break;
default: printf("Invalid choice");
}
} while(ch!=3);
getch();
}

Wednesday, May 6, 2009

PROGRAM FOR EVELUATING SINGLE LINKED LIST

#include
#include
#include
#define NULL 0
struct slist
{
int info;
struct slist *next;
};
typedef struct slist node;
node *root,*nu,*prev,*list,*start,*temp;
int ele;
void create()
{
printf("Enter -1 to stop\n");
scanf("%d",&ele);
while(ele!=-1)
{
nu=(node*)malloc(sizeof(node));
nu->info=ele;
nu->next=NULL;
if(root==NULL)
{
root=nu;
prev=nu;
}
else
{
prev->next=nu;
prev=nu;
}
scanf("%d",&ele);
}
}
void display()
{
list=root;
if(root==NULL)
printf("The list is empty\n");
else
{
printf("\n Contents of list");
while(list!=NULL)
{
printf("%d,",list->info);
list=list->next;
}
printf("NULL\n");
}
}
void deletion()
{
int item;
node *p;
printf("Enter the element to be deleted\n");
scanf("%d",&item);
if(root->info==item)
{
p=root;
root=root->next;
free(p);
}
else
{
temp=root;
while((item!=temp->info)&&(temp!=NULL))
{
prev=temp;
temp=temp->next;
}
if(item==temp->info)
{
prev->next=temp->next;
free(temp);
}
}
}
void main()
{
int ch;
root=NULL;
clrscr();
do
{
printf("\n Press 1 for entering elements into list\n Press 2 to delete an element from the list\n Press 3 to exit\n");
printf(" Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
display();
break;
case 2:deletion();
display();
break;
case 3:exit(0);
break;
}
}while(ch!=3);
}

PROGRAM TO CONVERT INFIX EXPRESSION TO POSTFIX EXPRESSION

/* THIS IS A PROGRAM TO CONVERT INFIX EXPRESSION TO POSTFIX EXPRESSION */

#include
#include
#include
#include
int top=-1;
char s[20];

void push(char c) // STARTING OF PUSH
{
top++;
s[top]=c;
} // END OF PUSH FUNCTION
void pop(char c) //STARTING OF POP
{
if(c==')')
{
while(s[top]!='(')
{
printf("%c",s[top]);
top--;
}
}
else
{ if(s[top]=='(')
top--;
else
{
printf("%c",s[top]);
top--;
}
}
} // END OF POP FUNCTION
void priority(char c) // FUNCTION TO CHECK PRIORITY
{
int l=change1(c); // THE CHARACTER THAT IS INCOMMING
int m=change1(s[top]);
if(top==-1)
push(c);
else
{
if(l>m)
push(c);
else
{
if(m==4)
push(c);
else
{
pop(s[top]);
push(c);
}
}
}
} // END OF PRIORITY FUNCTION

int change1(char c) // FUNCTION TO CHANGE CHARACTERS TO NUMERICALS
{
int a;
a=(c=='('?4:c=='^'?3:c=='*'?2:c=='/'?2:c=='+'?1:c=='-'?1:-1);
return a;
}
int change2(char c) // ANOTHER FUNCTION TO CHANGE CHARACTERS TO NUMERICALS
{
int a;
if(c=='(')
a=0;
else
{
if(c==')')
a=2;
else a=1;
}
return a;
}
void main() // STARTING OF MAIN
{
char exp[20];
int i=0,j;
clrscr();
printf("Enter an infix expression\n");
// getc(exp);
scanf("%s",&exp);
printf("The postfix expression for %s is ",exp);
while(exp[i]!='\0')
{
if(isalpha(exp[i]))
printf("%c",exp[i]);
else
{
j=change2(exp[i]);
switch(j)
{
case 0:push(exp[i]);
break;
case 2:pop(exp[i]);
break;
case 1:priority(exp[i]);
break;
}
}
i++;
}
if(exp[i]=='\0')
{
while(top!=-1)
pop(s[top]);
}
getch();
} // END OF MAIN

PROGRAM FOR SORTING THE GIVEN STRINGS

/* take a set of strings as input from command line and
load them into an array of strings and print
the sorted list of strings.
*/
#include
#include
#define BUFSIZE 64
char *strings[BUFSIZE]; /* array of pointers to hold strings */
main()
{
char temp[BUFSIZE]; /* temporary string to hold user input */
int i=0; /*index into the strings list */
/* this loop runs till user inputs END */
/*initialize the strings to nulls */
for(i=0; istrings[i]=NULL;
i=0;
while(1)
{
/* zerofill temp to avoid any garbage later */
memset(temp, 0, BUFSIZE);
/* ask the user to give string input */
printf("enter a string. enter END to stop input:");
/* take user input into temp */
scanf("%s", temp);
/* compare user input with END.
if
equal to zero exit the loop and go to sort
else
get memory (size=BUFSIZE) to the next string pointer
load the string into the memory
*/
if(!strcmp(temp, "END"))
{
break;
}
if(NULL==(strings[i]=(char *)malloc(BUFSIZE)))
{
/* memory allocation failed */
perror("malloc():");
exit(0);
}
/* copy the string into strings list */
strcpy(strings[i], temp);
i++;
} /* while */
/* strings list */
//for(i=0; strings[i]!=NULL; i++)
// printf("%s\n", strings[i]);
/* now that we have got our list. Need to sort it
*/
for(i=0; strings[i]!=NULL; i++)
{
int j;
char *p; /* temp pointer */
for(j=i+1; strings[j]!=NULL; j++)
{
// printf("%s\n %s\n", strings[i], strings[j]);
if(strcmp(strings[i], strings[j])>0)
{
p=strings[j];
strings[j]=strings[i];
strings[i]=p;
}
}
} /* for */
printf("sorted list is .............\n");
/* print the ordered list */
for(i=0; strings[i]!=NULL; i++)
{
printf("%s\n", strings[i]);
}
}

C PROGRAM FOR GREP

#include
#include
#define BUFSIZE 256
#define FAILURE -1
#define SUCCESS 0
int
main(int argc, char *argv[])
{
void search_pattern(char *buf, char *pattern);
char buf[BUFSIZE];
char pattern[BUFSIZE];
char file_name[BUFSIZE];
FILE *fp;
/*get the command line arguments into local variables */
memset(pattern, 0, BUFSIZE);
memset(buf, 0, BUFSIZE);
memset(file_name, 0, BUFSIZE);
strcpy(pattern, argv[1]);
strcpy(file_name, argv[2]);
/* open the file in read mode */
fp=(FILE *)fopen(file_name, "r");
if(fp==NULL)
{
/* return in case of failure */
perror("fopen():");
return FAILURE;
}
/* read one line from the file till end of file is reached */
while(fgets(buf, BUFSIZE, fp)!=NULL)
{
search_pattern(buf, pattern);
}
fclose(fp);
return SUCCESS;
}
void
search_pattern(char *buf, char *pattern)
{
char *p, *q;
for(p=buf, q=pattern; *p!='\0'; p++)
{
if(*p != *q)
continue;
else
{
for( ; *p==*q; p++, q++);
if(*q=='\0')
{
/* pattern found. print it */
printf("%s\n", buf);
return;
}
q=pattern;
}
}
getch();
}

PROGRAM TO PRINT STARS IN THE FORM OF RHOMBUS

#include

main()
{
int i,j,k,n,a,b,c,x;
printf("enter the # of rows of graphical output");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\t");
for (k=1;k<=(n-i);k++)
{ printf(" ");
}
for(j=0;j>0;i--)
{
printf("\t");
for (k=(n-i);k>0;k--)
{
printf(" ");
}
for(j=i;j>0;j--)
{
printf("*");
printf(" ");
}
for(k=(n-i-1);k>0;k--)
{
printf(" ");
}
printf("\n");
}

PROGRAM TO PRINT MULTIPLICATION TABLE

#include
main()

{
int i,j,r,c;
printf("enter the # of rows of *table");
scanf("%d",&r);
printf("enter the # of columns of *table");
scanf("%d",&c);
printf("\t\t\t MULTIPLICATION TABLE\n");
printf("-------------------------------------------------------------------------------\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("%5d",i*j);
}
printf("\n\n");
}
printf("________________________________________________________________________________\n");
}

PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER

#include
main()
{
int i,j,k,fact=1;
printf("\n\nenter a number");
scanf("%d",&i);
for(j=1;j<=i;j++)
{
for(k=j;k>0;k--)
{
fact=fact*k;
}
printf("%d\t %4d\n",j,fact);
fact=1;
}
getch();
}

PROGRAM TO GENERATE PASCAL TRIANGLE

#include
#include
void value(int n)
{
int i,j,val;
for(i=1;i<=n;i++) { for(j=0;j<=i;j++) { val= fact(i)/(fact(i-j)*fact(j)); printf("%d",val ); } printf("\n"); } } int fact(int k) { int mul=1; for(;k>0;k--)
{
mul*=k;
}
return mul;
}
void main()
{
int n;
clrscr();
printf("Enter the #of rows of graphical output\n");
scanf("%d",&n);
value(n);
getch();
}

PROGRAM TO FIND WHETHER THE GIVEN NUMBER IS PRIME OR NOT

#include
#include
void main()
{
int i,j,k=0,n;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
k++;}
if(k==2)
printf("%d is prime number\n",i);
}
getch();
}

PROGRAM TO FIND GCD OF GIVEN NUMBERS

#include
#include
void main()
{
int i,j,k=0,a=1,min=0;
clrscr();
printf(" enter the values of i,j");
scanf("%d,%d",&i,&j);
if(i{
min=i;
}
else
{
min=j;
}
while (a<=min)
{
if(i%a==0&&j%a==0)
{
k=a;
a+=2;
}
else
{
a+=2;
}
a--;
}
printf("GCD of %d and %d is %d",i,j,k);
getch();
}

Tuesday, May 5, 2009

PROGRAM FOR CIRCULAR QUEUE

#include
#include
#include
#define MAX 5
int q[MAX],ele,temp,ch,f=-1,r=-1;
void insert()
{
if(f==0&&r==MAX-1)
printf("OVERFLOW");
else
{
printf("Enter an element");
scanf("%d",&ele);
if(f==-1)
{
f=0;
r=0;
}
else
if(r==MAX-1)
r=0;
else r++;
q[r]=ele;
}
}
void deletion()
{
if(f==-1)
printf("UNDERFLOW");
else
{
ele=q[f];
if(f==r)
{
f=-1;
r=-1;
}
else if(f==MAX-1)
f=0;
else f++;
printf("Deleted element is %d",ele);
}
}
void display()
{
int i;
if(f==-1)
printf("Empty stack");
else
{
for(i=f;i<=r;i++)
printf("%d",q[i]);
}
}
void main()
{
int option;
clrscr();
do
{
printf("\nPress 1 for insertion\nPress 2 for deletion\nPress 3 to display the elements\nPress 4 to exit\nEnter your option:");
scanf("%d",&option);
switch(option)
{
case 1:insert();
break;
case 2:deletion();
break;
case 3:display();
break;
case 4:exit(0);
break;
}
}while(option!=4);
}

PROGRAM TO CONVERT A GIVEN NUMERICAL TO ALPHABETIC NUMBER

#include
#include
#include
int count(int n);
int rev(int n);
void main()
{
int i,j,n;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
j=rev(n);
while(j>0)
{
i=j%10;
j=j/10;
switch(i)
{
case 0:printf(" ZERO ");
break;
case 1:printf(" ONE ");
break;
case 2:printf(" TWO ");
break;
case 3:printf(" THREE ");
break;
case 4:printf(" FOUR ");
break;
case 5:printf(" FIVE ");
break;
case 6:printf(" SIX ");
break;
case 7:printf(" SEVEN ");
break;
case 8:printf(" EIGHT ");
break;
case 9:printf(" NINE ");
break;
}
}
getch();
}


int count(int n)
{
int count =0;
while (n>0)
{ n=n/10;
count ++;
}
return count;
}



int rev(int n)
{
int i,sum=0;
int l=count(n);
l--;
while(n>0)
{
i=n%10;
n=n/10;
sum+=(i*pow(10,l));
l--;
}
return sum;
}

Monday, May 4, 2009

PROGRAM TO IMPLEMENT STACK

#include
#include
#include
#define MAX 5
int s[MAX],top=-1;
char choice;
void push()
{
do
{
if(top==MAX-1)
{
printf("OVER FLOW\n");
choice='n';
}
else
{
printf("Enter an element\n");
top++;
scanf("%d",&s[top]);
printf("PUSH ANOTHER ELEMENT Y/N:");
fflush(stdin);
scanf("%c",&choice);
}
}while(choice=='Y'choice=='y');
}
void pop()
{
do
{
if(top==-1)
{
printf("\nUNDER FLOW\n");
choice='n';
}
else
{
printf("The poped element is %d\n",s[top]);
top--;
printf("POP ANOTHER ELEMENT Y/N:");
fflush(stdin);
scanf("%c",&choice);
}
}while(choice=='Y'choice=='y');
}
void display()
{
if(top==-1)
printf("\nUNDER FLOW\n");
else
{
printf("The elements present in the stack are\n");
while(top!=-1)
{
printf(" %d\n",s[top]);
top--;
}
}
}
void main()
{
int option;
clrscr();
do
{
printf("\nPress 1 to push an element\nPress 2 to pop an element\nPress 3 to display the elements\nPress 4 to exit\nEnter your option:");
scanf("%d",&option);
switch(option)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
break;
}
}while(option!=4);
}

PROGRAM TO IMPLEMENT TOWERS OF HANOY

#include
#include
void towers(int n,char source,char dest,char auxilary);
void main()
{
int n;
char s='a',d='c',a='b';
clrscr();
printf("\nEnter the value of n\n");
scanf("%d",&n);
towers(n,s,d,a);
getch();
}
void towers(int n,char source,char dest,char auxilary)
{
static int step=0;
printf("Towers(n=%d,source=%c,dest=%c,aux=%c)\n",n,source,dest,auxilary);
if(n==1)
printf("\t\t\t\t\tstep %3d:Move from %c to %c\n",++step,source,dest);
else
{
towers(n-1,source,auxilary,dest);
printf("\t\t\t\t\tstep %3d:Move from %c to %c\n",++step,source,dest);
towers(n-1,auxilary,dest,source);
}
}

C-PROGRAM FOR QUEUE

#include
#include
#include
#define MAX 5
int q[MAX],rear=-1,front=-1;
char choice;
void insertion()
{
do
{
if(rear==MAX-1)
{
printf("OVER FLOW\n");
choice='n';
}
else
{
printf("Enter an element\n");
rear++;
scanf("%d",&q[rear]);
if(front==-1)
front++;
printf("PUSH ANOTHER ELEMENT Y/N:");
fflush(stdin);
scanf("%c",&choice);
}
}while(choice=='Y'choice=='y');
}
void deletion()
{
do
{
if(front==-1)
{
printf("\nUNDER FLOW\n");
choice='n';
}
else
{
if(front==rear)
{
printf("The poped element is %d\n",q[front]);
front=-1;
rear=-1;
}
else
{
printf("The poped element is %d\n",q[front]);
front++;
printf("POP ANOTHER ELEMENT Y/N:");
fflush(stdin);
scanf("%c",&choice);
}
}
}while(choice=='Y'choice=='y');
}
void display()
{
if(front==-1)
printf("\nUNDER FLOW\n");
else
{
printf("The elements present in the stack are\n");
while(front!=rear)
{
printf(" %d\n",q[front]);
front++;
}
if(front==rear)
{
printf(" %d\n",q[front]);
front=-1;
rear=-1;
}
}
}
void main()
{
int option;
clrscr();
do
{
printf("\nPress 1 for insertion\nPress 2 for deletion\nPress 3 to display the elements\nPress 4 to exit\nEnter your option:");
scanf("%d",&option);
switch(option)
{
case 1:insertion();
break;
case 2:deletion();
break;
case 3:display();
break;
case 4:exit(0);
break;
}
}while(option!=4);
}