Sunday, 17 June 2012

Implementation of Simple Queue in c


#include<stdio.h>
#include<conio.h>
#include<string.h>
#define size 5
int f=-1,r=-1,a[5];
void enqueue(int);
int dequeue(void);
void main()
{
 int i,j,k,l,m,n;
 clrscr();
 enqueue(10);
 enqueue(20);
 enqueue(30);
 enqueue(40);
 enqueue(50);

            i=dequeue();
printf("\nDequeued value is %d",i);
            j=dequeue();
printf("\nDequeued value is %d",j);
            k=dequeue();
printf("\nDequeued value is %d",k);
            enqueue(60);
            l=dequeue();
printf("\nDequeued value is %d",l);
getch();
}


void enqueue(int y)
{
            if(r==size)
            {
                        printf("\nQueue is overflow");
            }
            else if(f==-1 && r==-1)
            {
                        f=0;r=0;
                        a[r]=y;
                        printf("\nValue enqueued is %d",a[r++]);
            }
            else
            {
                        a[r]=y;
                        printf("\nValue Enqueued is %d",a[r++]);
            }
}


int dequeue()
{
            if(f==r)
            {
                        f=-1;r=-1;
                        printf("\nQueue is underflow");
                        return 0;
            }
            else
            {
                        return a[f++];
            }
}

Implementation of Circular Queue in c

Implementation of Circular Queue in c



#include<stdio.h>
#include<conio.h>
#include<string.h>
#define size 5
int f=-1,r=-1,a[5];
void enqueue(int);
int dequeue(void);
void main()
{
 clrscr();
 enqueue(10);
 enqueue(20);
 enqueue(30);
 enqueue(40);
 enqueue(50);
dequeue();
dequeue();
 enqueue(60);
 enqueue(70);
getch();
}

void enqueue(int y)
{
            if(f==0 && r==size)
            {
                        printf("\nQueue is full");
            }
            else if(f!=0 && r==size)
            {
                        r=0;
                        a[r]=y;
                        printf("\nValue enqueued is %d at a[%d]",a[r++],r);
            }
            else if(f==-1 && r==-1)          
            {                         
                        f=0;r=0;                   
                        a[r]=y;
                        printf("\nValue enqueued is %d at a[%d]",a[r++],r);
            }
            else if(f==r)
            {
                        printf("\nQueue is full");
            }
            else
            {
                        a[r]=y;
                        printf("\nValue enqueued is %d at a[%d]",a[r++],r);
            }
}

int dequeue()
{
            if(f==r)
            {
                        f=-1;r=-1;
                        printf("\nQueue is underflow");
                        return 0;
            }
            else
            {
                        printf("\nDequeued value is %d at a[%d]",a[f],f);
                        return a[f++];              
            }                                 
}                          

Stack program in c with pop(), push() and display() functions


Stack program in c with pop(), push() and display() function.



#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 5
int top=-1;
void push();
int pop();
void display();
int stack_arr[max];
void main()
{
int choice;
clrscr();
while(1)
{
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Wrong choice\n");
}
}
}

void push()
{
int pushed_item;
if(top==(max-1))
{
printf("Stack is overflow\n");
}
else
{
printf("Enter the item to pushed on stack\n");
scanf("%d",&pushed_item);
top=top+1;
stack_arr[top]=pushed_item;
}
}



int pop()
{
int e;
if(top==-1)
printf("Stack underflow\n");
else
{
e=stack_arr[top];
printf("popped element is %d\n",e);
top=top-1;
}
return(e);
}


void display()
{
int i;
if(top==-1)
printf("Stack is empty\n");
else
{
printf("Stack elements\n");
for(i=top;i>=0;i--)
{
printf("%d\n",stack_arr[i]);
}
}
}
This Program is for serial port


#include<stdio.h>
#include<conio.h>
#define base 0x3f8

void main()
{
char ch,ch1;
clrscr();

       //printf("\n enter data");
while(ch!='$')
{
//printf("\n");
scanf("%c",&ch);
outportb (base+1,0x00);
outportb (base+3,0x80);
outportb (base,0x30);
outportb (base+1,0x00);
outportb(base+3,0x03);
outportb (base+4,0x03);
outportb(base, ch);
ch1=inportb(base);
printf("%c",ch);

}

getch();
}


This program will continue running until you have given "$" sign as input.