Tuesday, 6 August 2019

Double ended queue

#include<stdio.h>
#include<stdlib.h>
int front=-1,rear=-1,queue[50];
int size=10;
void enqueuerear();
void enqueuefr();
void dequeuefr();
void dequeuerear();
int isempty();
void display();
int isfull();
void main()
{
while(1)
{
int choices;
printf("\n1.Enqueuerear\n2.Enqueuefront\n3.Dequeuefront\n4.Dequeuerear\n5.Display\n6.Exit\n");
printf("ENTER YOUR CHOICE: ");
scanf("%d",&choices);
switch(choices)
{
case 1:enqueuerear();display();
break;
case 2:enqueuefr();display();
break;
case 3:dequeuefr();display();
break;
case 4:dequeuerear();display();
break;
case 5: display();
break;
case 6:exit(1);
break;
default:printf("ENTER THE CORRECT OPTION\n");
break;
}
}
}
int isempty()
{
if(front==-1 && rear==-1)
{
return 1;
}
else
{
return 0;
}
}
int isfull()
{
if(front==0 && rear==size-1)
{
return 1;
}
else
{
return 0;
}
}
void enqueuerear()
{
int x;
printf("Enter the element to be inserted: ");
scanf("%d",&x);
if(front==-1 && rear==-1)
{
front=rear=0;
queue[rear]=x;
}
else
{
if(isfull())
{
printf("The queue is overflow\n");
}
else
{
rear=rear+1;
queue[rear]=x;
}
}
}
void display()
{
int i;
if(isempty())
{
printf("The queue is empty\n");
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
}
}
void dequeuefr()
{
if(isempty())
{
printf("The queue is underflow\n");
}
else
{
if(front==rear)
{
printf("The deleted element is %d",queue[front]);
front=-1;
rear=-1;
}
else
{
printf("The deleted element is %d",queue[front]);
front++;
}
}
}
void dequeuerear()
{
if(isempty())
{
printf("The queue is underflow\n");
}
else
{
if(front==rear)
{
printf("The deleted element is %d\n",queue[rear]);
front=rear=-1;
}
else
{
printf("The deleted element is %d\n",queue[rear]);
rear--;
}
}
}
void enqueuefr()
{
int x,i;
printf("Enter the element to be inserted: ");
scanf("%d",&x);
if(isempty())
{
front=rear=0;
queue[front]=x;
}
else
{
if(isfull())
{
printf("the queue is overflow/n");
}
else
{
for(i=rear+1;i<=front;i++)
{
queue[i+1]=queue[i];
}
front--;
queue[front]=x;
}
}
}

Saturday, 8 December 2018

Assignment 1

#include<stdio.h>
void main()
{
 int i,rank[5],first,student,student1,student2,student3,sub1r,sub2r,sub3r,count1=0,count2=0,count3=0;
 struct stud
 {
  int sub1,sub2,sub3;
 };
 struct svtud s[3];
 printf("Enter student details and marks(max. 100)\n");
 for(i=1;i<4;i++)
 {
  printf("Student [%d]: Enter the sub. marks\n",i);
  scanf("%d %d %d",&s[i].sub1,&s[i].sub2,&s[i].sub3);
 }
 printf("Details :\n");
 printf("Student Sub1    Sub2    Sub3\n");
 for(i=1;i<4;i++)
 {
  printf("   %d    %d\t%d\t%d\t\n",i,s[i].sub1,s[i].sub2,s[i].sub3);
  //scanf("%d %d %d",&s[i].sub1,&s[i].sub2,&s[i].sub3);
 }
 printf("\n");
 printf("Total marks of Students:\n");
 for(i=1;i<4;i++)
 {
  rank[i] = s[i].sub1 + s[i].sub2  + s[i].sub3;
  printf("Student %d : Total %d\n",i,rank[i]);
 }
 printf("\n");
 first=rank[1];
 student=0;
 for(i=1;i<4;i++)
 {
  if(rank[i]>first)
  {
   first = rank[i];
   student = i;
  }
 }
 printf("First mark is %d secured by student %d\n",first,student);
 sub1r=0;
 sub2r=0;
 sub3r=0;
 for(i=1;i<4;i++)
 {
  if(sub1r<s[i].sub1)
  {
   sub1r=s[i].sub1;
   student1 = i;
  }
  if(sub2r<s[i].sub2)
  {
   sub2r=s[i].sub2;
   student2 = i;
  }
  if(sub3r<s[i].sub3)
  {
   sub3r=s[i].sub3;
   student3 = i;
  }
 }
 printf("\n");
 printf("First mark in Sub1 = %d secured by student %d\n",sub1r,student1);
 printf("First mark in Sub2 = %d secured by student %d\n",sub2r,student2);
 printf("First mark in Sub3 = %d secured by student %d\n",sub3r,student3);
 for(i=1;i<4;i++)
 {
  if(s[i].sub1<=50)
   count1++;
  if(s[i].sub2<=50)
   count2++;
  if(s[i].sub3<=50)
   count3++;
 }
 printf("\n");
 printf("No. of failures\n");
 printf("sub1 = %d\nsub1 = %d\nsub1 = %d\n",count1,count2,count3);
}

Sunday, 2 December 2018

String Handling Programs

REVERSE A STRING

char string1[10],temp[100];
int i,count=0,j;
printf("Enter a string\n");
gets(string1);
j=strlen(string1)-1;
printf("You entered %s whose length is %d\n\n",string1,j);
while(i<j)
{
temp[i]=string1[i];
string1[i]=string1[j];
string1[j]=temp[i];
i++;
j--;
}
printf("%s",string1);
}

COMPARE TWO STRINGS

void main()
{
int compare (char[],char[]);
char str1[100],str2[100],temp[100];
int flag;
printf("Enter string1");
scanf("%s",str1);
printf("Enter string2");
scanf("%s",str2);
flag=compare(str1,str2);
if(flag==1)
printf("Both are same");
else
printf("Wrong");
}
int compare (char str1[],char str2[])
{
int i=0,j,flag=0;

while(str1[i]!='\0'&&str2[i]!='\0')
{
if(str1[i]!=str2[i]){
flag=1;
break;
}
i++;
}
if(flag==0 && str1[i]=='\0' && str2[i]=='\0')
return 1;
else return 0;
}

CHECK SUBSTRING PRESENT IN GIVEN STRING

Program 1 : Without using built-in functions:
#include <stdio.h>

int check_subsequence (char [], char[]);

void main () {
   int flag;
   char s1[1000], s2[1000];
 

   printf("Input first string\n");
   gets(s1);

   printf("Input second string\n");
   gets(s2);

      flag = check_subsequence(s2, s1);

   (flag)?printf("YES\n"):printf("NO\n");

   getch();
}
int check_subsequence (char a[], char b[])
{
   int c, d;

   c = d = 0;

   while (a[c] != '\0')
   {
      while ((a[c] != b[d]) && b[d] != '\0')
  {
         d++;
      }
      if (b[d] == '\0')
         break;
      d++;
      c++;
   }
   if (a[c] == '\0')
      return 1;
   else
      return 0;
}

Program 2: Using strcmp()

char list[15][15];
int i,flag=0,a,n;
char search[15];
printf("Enter the no. of strings");
scanf("%d",&n);
printf("Enter your the strings");
for(i=0;i<n;i++)
scanf("%s",&list[i][0]);
printf("Enter the substring to be searched");
scanf("%s",&search);
for(i=0;i<5;i++)
{
a=strcmp(&list[i][0],search);
if(a==0)
{
printf("Found");
flag=1;
break;
}
}
if(flag==0)
printf("Sorry!");
}