Wednesday, 12 February 2014

Stack using linked list cprogram

Aim: Stack Using Linked List In Cprogram

Program:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

void push(int);

void pop();

void display();

struct list

{

int data;

struct list *next;

};

typedef struct list node;

node *header,*top;



void main()

{

int ch,item;

clrscr();

header=(node *)malloc(sizeof(node));

header->data=NULL;

header->next=NULL;

top=header->next;

while(1)

{

printf("1.push\t 2.pop\t 3.display\t 4.exit\n");

printf("enter your choice\n");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("enter the item you want to push\n");

scanf("%d",&item);

push(item);

break;

case 2:

pop();

break;

case 3:

display();

break;

default:

exit(0);

}

}

}

void push(int item)

{

node *new;

new=(node *)malloc(sizeof(node));

new->data=item;

new->next=top;

top=new;

header->next=new;

}

void pop()

{

top=header->next;

if(top==NULL)

printf("underflow\n");

else

{

header->next=top->next;

free(top);

top=header->next;

}

}

void display()

{

node *ptr;

ptr=header->next;

printf("header->");

while(ptr!=NULL)

{

printf("%d->",ptr->data);

ptr=ptr->next;

}

printf("NULL\n");


}

Output:


Circular Queue using array cprogram

Aim: To Implement Circular Queue using array in C program

program:

#include<stdio.h>

#include<conio.h>

#define maxsize 5

void insertion();

int deletion();

void display();

int rear=0,front=0;

int cqueue[maxsize];

void main()

{

int item,n,ch;

clrscr();

while(1)

{

printf("1.insertion\t 2.deletion 3.display\t 4.exit\n");

printf("enter your choice");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("enter the item to insert:");

scanf("%d",&item);

insertion(item);

break;

case 2:

item=deletion();

printf("item  deletion from the cqueue is %d\n",item);

break;

case 3:

display();

break;

default :

exit(0);

}

}

}

void insertion(int item)

{

if(front==0)

{

front=1;

rear=1;

cqueue[rear]=item;

}

else

{

rear=(rear%maxsize)+1;

if(rear==front)

printf("overflow");

else

cqueue[rear]=item;

}

}

int deletion(int item)

{

if(rear==0)

printf("under flow");

else

{

item=cqueue[front];

if(front==rear)

{

front=0;

rear=0;

}

else

front=(front%maxsize)+1;

return item;

}

}

void display()

{

int i;

printf("elements of cqueue are\n");

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

printf("%d ",cqueue[i]);

printf("\n");

}

output:


Queue using array cprogram

Aim: To Implement queue using arrays in cprogram

program:

#include<stdio.h>

#define MAXSIZE 3

void Insert(int);

void delete();

void Display();

int queue[MAXSIZE],rear=0,front=0;

main()

{

int item,n,ch;

clrscr();

while(1)

{

printf("1.Insert\t");

printf("2.Delete\t");

printf("3.Dispiay\t");

printf("4.Exit\n");

printf("Enter your chioce : ");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("Enter the item you want to Insert : ");

      scanf("%d",&item);

      Insert(item);

      break;

case 2:item=Delete();

      if(item>0)

      printf("Item deleted from queue is %d\n",item);

      break;

case 3:Display();

      break;

      default:exit(0);

}

}

}

void Insert(int item)

{

if(rear==MAXSIZE)

printf("overflow\n");

else

{

rear=rear+1;

queue[rear]=item;

if(front==0)

front=1;

}

}

int Delete()

{

int item;

if(front==0)

{

printf("underflow\n");

return -1;

}

else

{

item=queue[front];

if(front==rear)

{

front=0;

rear=0;

}

else

front=front+1;

return item;

}

}

void Display()

{

int i;

printf("Elements of queue are :  ");

if(front==rear)

printf("NULL\n");

else

{

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

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

printf("\n");

}

}

output:


Stack using array c program

Aim: To implement stack using array in c program
program:

#include<stdio.h>

#define MAXSIZE 3

void push(int);

int pop();

void display();

int top=0;stack[MAXSIZE];

void main()

{

int item,n,ch;

clrscr();

while(1)

{

printf("1.push\n");

printf("2.pop\n");

printf("3.display\n");

printf("4.exit\n");

printf("enter your choice:");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("enter the item you want to push:");

scanf("%d",&item);

push(item);

break;

case 2:item=pop();

printf("item deleted from stack is %d\n",item);

break;

case 3:display();

break;

default:exit(0);

}

}

}

void push(int item)

{

if(top==MAXSIZE)

printf("overflow");

else

{

top=top+1;

stack[top]=item;

}

}

int pop()

{

int item;

if(top==0)

{

printf("underflow");

exit(0);

}

else

{

item=stack[top];

top=top-1;

return item;

}

}

void display()

{

int i;

printf("elements of stack are \n");

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

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

printf("\n");

}


Infix to Postfix cprogram

aim: to implement a infix to post fix c-program:

program:

#include<ctype.h>
#include<stdio.h>
char stack[80];
int top=-1;
char pop();
void main()
{
char instring[80],outstring[80];
int l,p=0,i;
clrscr();
printf("Enter the infix expression\n");
scanf("%s",instring);
l=strlen(instring);
for(i=0;i<l;i++)
{
if((toupper(instring[i])>='A')&&(toupper(instring[i])<='Z'))
outstring[p++]=toupper(instring[i]);
else if(instring[i]=='(')
push(instring[i]);
else if(instring[i]==')')
{
while(stack[top]!='(')
outstring[p++]=pop();
pop();
}
else
{
while(stack_preced(stack[top])>=in_preced(instring[i]))
outstring[p++]=pop();
push(instring[i]);
}
}
while(top>=0)
outstring[p++]=pop();
outstring[p--]='\0';
printf("postfix expresion=%s",outstring);
getch();
}
stack_preced(char x)
{
switch(x)
{
case '+':
case '-':return(1);
case '*':
case '/':return(2);
case '$':return(3);
case '(':return(0);
}
}
in_preced(char x)
{
switch(x)
{
case '+':
case '-':return(1);
case '*':
case '/':return(2);
case '$':return(3);
case '(':return(4);
}
}
push(char c)
{
stack[++top]=c;
}
char pop()
{
char c;
c=stack[top];
top--;
return(c);
}


Tuesday, 3 December 2013

FIBONACCI SEARCH WITHOUT RECURSION

FIBONACCI   SEARCH WITHOUT RECURSION BY USING  C PROGRAM

Program:


#include<stdio.h>

void main()

{

   int a[50],i,n,key,loc,fk,p,q,r,m;

   clrscr();

   printf("enter number of elements to be entered");

   scanf("%d",&n);

   printf("enter elements");
   for(i=1;i<=n;i++)
   scanf("%d",&a[i]);
   printf("enter the key element to search");
   scanf("%d",&key);
   fk=fib(n+1);
   p=fib(fk);
   q=fib(p);
   r=fib(q);
   m=(n+1)-(p+q);
   if(key<a[p])
   p=p+m;
   loc=fibsearch(a,p,q,r,key);
   if(loc==0)
     printf("the key is not found");
   else
   printf("%d is found at %d",key,loc);
   getch(); 
}
int fib(int m)
{
   int a,b,c;
   a=0;
   b=1;
   c=a+b;
   while(c<m)
   {
   a=b;
   b=c;
   c=a+b;
   }
   return b;
}
int fibsearch(int a[],int p,int q,int r,int key)
{
   int t;
   while(p!=0)
   {
   if(key==a[p])
   return p;
   else if(key<a[p])
   {
   if(r==0)
   p=0;
   else
   {
   p=p-r;
   t=q;
   q=r;
   r=t-r;
   }
    }
    else
    {
   if(q==1)
   p=0;
   else
   {
   p=p+r;
   q=q-r;
   r=r-q;
   }
    }
   }
   return 0;
}


output:





FIBONACCI SEARCH WITH RECURSION

FIBONACCI  SEARCH WITH RECURSION BY USING               C PROGRAM

Program:

#include<stdio.h>

void main()

{

  int n,a[50],i,key,loc,p,q,r,m,fk;

  clrscr();

  printf("\nenter number elements to be entered");
  scanf("%d",&n);
  printf("enter elements");
  for(i=1;i<=n;i++)
  scanf("%d",&a[i]);
  printf("enter the key element");
  scanf("%d",&key);
  fk=fib(n+1);
  p=fib(fk);
  q=fib(p);
  r=fib(q) ;
  m=(n+1)-(p+q);
  if(key>a[p])
  p=p+m;
  loc=rfibsearch(a,n,p,q,r,key);
  if(loc==0)
  printf("key is not found");
  else
  printf("%d is found at location %d",key,loc);
  getch();
}
int fib(int m)
{
  int a,b,c;
  a=0;
  b=1;
  c=a+b;
  while(c<m)
  {
  a=b;
  b=c;
  c=a+b;
  }
  return b;
}
int rfibsearch(int a[],int n,int p,int q,int r,int key)
{
  int t;
  if(p<1||p>n)
  return 0;
  else if(key==a[p])
  return p;
  else if(key<a[p])
 {
  if(r==0)
  return 0;
  else
  {
  p=p-r;
  t=q;
  q=r;
  r=t-r;
  return rfibsearch(a,n,p,q,r,key);
  }
  }
  else
  {
  if(q==1)
    return 0;
  else
  {
  p=p+r;
  q=q-r;
  r=r-q;
  return rfibsearch(a,n,p,q,r,key);
  }
  }
}