Académique Documents
Professionnel Documents
Culture Documents
#include<stdio.h>
#include<conio.h>
struct
{
int usn;
char name[25];
int marks[3];
}s;
FILE *fp;
char filename[20];
void create_file()
{
fp=fopen("stud.txt","w");
if(fp==NULL)
printf("File could not be created\n");
else
printf("The file \"stud.txt\" is created in the current directory");
}
void read_store_details()
{
int i;
for(i=1;i<=5;i++)
{
printf("Enter details of student-%d\n",i);
printf("Usn:\t" );
scanf("%d",&s.usn);
printf("Name:\t");
scanf("%s",s.name);
printf("Marks1:\t");
scanf("%d",&s.marks[0]);
printf("Marks2:\t");
scanf("%d",&s.marks[1]);
printf("Marks3:\t");
scanf("%d",&s.marks[2]);
fprintf(fp,"%d %s %d %d %d\n",s.usn,s.name,s.marks[0],s.marks[1],s.marks[2]);
}
fclose(fp);
}
void display_all()
{
fp=fopen("stud.txt","r");
Department of Computer Science & Engineering / Information Science & Engineering
SBM Jain College of Engineering, Bangalore(R) - 562 112 4
printf("All the records in a file are:\n");
while(feof(fp)==0)
{
fscanf(fp,"%d %s %d %d
%d\n",&s.usn,s.name,&s.marks[0],&s.marks[1],&s.marks[2]);
printf("%d %s %d %d %d\n",s.usn,s.name,s.marks[0],s.marks[1],s.marks[2]);
}
fclose(fp);
}
void search()
{
int reg_no;
fp=fopen("stud.txt","r");
printf("Enter the USN:\t");
scanf("%d",®_no);
while(feof(fp)==0)
{
fscanf(fp,"%d %s %d %d
%d\n",&s.usn,s.name,&s.marks[0],&s.marks[1],&s.marks[2]);
if(reg_no==s.usn)
{
printf("Record found, details are:\t");
printf("%d %s %d %d %d\n",s.usn,s.name,s.marks[0],s.marks[1],s.marks[2]);
fclose(fp);
return;
}
}
printf("Record not found\n");
fclose(fp);
}
void main()
{
int choice;
while(1)
{
clrscr();
printf("\t MENU \n");
printf("1->Create a file\n");
printf("2->Read and store details of 5 students\n");
printf("3->Display all records\n");
printf("4->Search for a particular USN number\n");
printf("5->Exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: create_file();
break;
case 2: read_store_details();
break;
case 3: display_all();
break;
case 4: search();
break;
Department of Computer Science & Engineering / Information Science & Engineering
SBM Jain College of Engineering, Bangalore(R) - 562 112 5
default: exit(0);
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
void newStrCat(char *dest,const char *src)
{
while(*dest!='\0')
++dest;
while(*src!='\0')
{
*dest=*src++;
dest++;
}
*dest='\0';
}
void main()
{
char src[20],dest[20];
clrscr();
printf("Enter the first string\n");
gets(src);
printf("Enter the second string\n");
Department of Computer Science & Engineering / Information Science & Engineering
SBM Jain College of Engineering, Bangalore(R) - 562 112 6
gets(dest);
newStrCat(dest,src);
printf("The concatenated string is\n");
puts(dest);
getch();
}
#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 5
struct
{
int items[MAX_SIZE];
int top;
Department of Computer Science & Engineering / Information Science & Engineering
SBM Jain College of Engineering, Bangalore(R) - 562 112 7
}s={{0},-1};
void push(int item)
{
s.items[++s.top]=item;
}
int pop()
{
return(s.items[s.top--]);
}
void display()
{
int i;
if(s.top==-1)
printf("Stack empty\n");
else
{
printf("Stack contents:\t");
for(i=0;i<=s.top;i++)
printf("%d",s.items[i]);
}
}
void main()
{
int choice,item;
while(1)
{
clrscr();
printf("1->Push\n2->Pop\n3->Display\n4->Exit\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:if(s.top==MAX_SIZE-1)
printf("Stack overflow\n");
else
{
printf("Enter the Item:\t");
scanf("%d",&item);
push(item);
}
break;
case 2:if(s.top==-1)
printf("Stack underflow\n");
else
printf("Poped element:\t%d",pop());
break;
case 3:display();
break;
default:exit(0);
}
getch();
Department of Computer Science & Engineering / Information Science & Engineering
SBM Jain College of Engineering, Bangalore(R) - 562 112 8
}
}
#include<ctype.h>
#include<stdio.h>
#include<conio.h>
int top=-1;
char s[40];
int empty()
{
if(top==-1)return 1;
return 0;
}
int prec(char op)
{
switch(op)
{
case '+':
case '-':if(s[top]=='(')return 0;
return 1;
case '*':
case '/':if(s[top]=='+'||s[top]=='-'||s[top]=='(')return 0;
return 1;
case '(':return 0;
case ')':if(s[top]=='(')return 0;
return 1;
case '$':return 0;
}
}
void push(char item)
{
s[++top]=item;
}
char pop()
{
return(s[top--]);
}
void convert(char *in)
{
char post[20];
int i=0;
while(*in!='\0')
{
if(isalpha(*in))
post[i++]=*in;
else
{
while(top!=-1&&prec(*in))
Department of Computer Science & Engineering / Information Science & Engineering
SBM Jain College of Engineering, Bangalore(R) - 562 112 9
{
post[i++]=pop();
}
if(*in==')')
pop();
else push(*in);
}
in++;
}
while(top!=-1)
{
post[i++]=pop();
}
post[i]='\0';
printf(" The postfix Expression is ...\n");
printf(" %s",post);
}
void main()
{
char in[40];clrscr();
printf(" Enter the Expression ");
scanf("%s",in);
convert(in);
getch();
}
#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<math.h>
#define MAX 20
int stack[MAX],top=-1;
void push(int item)
{
stack[++top]=item;
}
int pop()
{
return stack[top--];
}
void main()
{
char e[20],*p;
int op1,op2;
p=e;
clrscr();
printf("Enter the postfix expression\n");
scanf("%s",e);
while(*p)
{
Department of Computer Science & Engineering / Information Science & Engineering
SBM Jain College of Engineering, Bangalore(R) - 562 112 10
if(isdigit(*p))
push(*p-48);
else
{
op1=pop();
op2=pop();
switch(*p)
{
case '+': push(op1+op2);break;
case '-': push(op2-op1);break;
case '*': push(op1*op2);break;
case '/': push(op2/op1);break;
case '$': push(pow(op2,op1));break;
default: printf("Illegal operation\n");
exit(0);
}
}
p++;
}
printf("Res=%d",pop());
getch();
}
7. Write a C program to simulate the working of a queue of
integers using an array. Provide the
following operations.
a) Insert
b) Delete
c) Display
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 2
int q[MAXSIZE],f=0,r=-1;
void insert(int item)
{
q[++r]=item;
}
int del()
{
int item;
item=q[f++];
if(f>r)
{
f=0;r=-1;
}
return item;
}
void display()
{
int i;
printf("Elements in queue are:\t");
for(i=f;i<=r;i++)
printf("%d\t",q[i]);
}
void main()
{
int choice,item;
while(1)
{
clrscr();
printf("1->Insert\n2->Delete\n3->Display\n4->Exit\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: if(r==MAXSIZE-1)
printf("Queue full\n");
else
{
printf("Enter the Item:\t");
scanf("%d",&item);
insert(item);
}
break;
case 2: if(f>r)
{
printf("Queue empty\n");
}
else
printf("Deleted element:\t%d",del());
break;
case 3: if(f>r)
printf("Queue empty\n");
else
display();
break;
default: exit(0);
}
getch();
}
}
8. Write a C program to simulate the working of a circular
queue of integers using an array.
Provide the following operations.
a) Insert
b) Delete
c) Display
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 2
int q[MAXSIZE],f=0,r=-1,count=0;
void insert(int item)
{
r=(r+1)%MAXSIZE;
q[r]=item;
count++;
}
int del()
{
int item;
item=q[f];
f=(f+1)%MAXSIZE;
count--;
return item;
}
void display()
{
int i,j=f;
printf("Elements in queue are:\t");
for(i=1;i<=count;i++)
{
printf("%d\t",q[j]);
j=(j+1)%MAXSIZE;
}
}
void main()
{
int choice,item;
while(1)
{
clrscr();
printf("1->Insert\n2->Delete\n3->Display\n4->Exit\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: if(count==MAXSIZE)
printf("Queue full\n");
else
{
printf("Enter the Item:\t");
scanf("%d",&item);
insert(item);
}
break;
case 2: if(count==0)
{
printf("Queue empty\n");
}
else
printf("Deleted element:\t%d",del());
break;
case 3: if(count==0)
printf("Queue empty\n");
else
display();
break;
default: exit(0);
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
struct node
{
int id,sem;
char name[20];
struct node *next;
}*first=0,*temp=0,*rear=0;
void accept_data()
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the student details\n");
printf("Id:");
scanf("%d",&temp->id);
printf("Name:");
scanf("%s",temp->name);
printf("sem:");
scanf("%d",&temp->sem);
temp->next=NULL;
}
void insert_front()
{
accept_data();
temp->next=first;
first=temp;
if(first->next==0)rear=first;
}
void insert_rear()
{
if(rear==0)
insert_front();
else
{
accept_data();
rear->next=temp;
rear=temp;
}
}
void insert_at_pos()
{
int pos,count=1;
struct node *prev=0,*cur=first;
printf("Enter the Position\n");
scanf("%d",&pos);
if(pos>1&&first==0)
{
printf("Invalid position\n");
return;
}
if(pos==1)
insert_front();
else
{
while(count!=pos && cur->next!=NULL)
{
count++;
prev=cur;
cur=cur->next;
}
if(count+1==pos)
insert_rear();
else
if(count==pos)
{
accept_data();
prev->next=temp;
temp->next=cur;
}
else
printf("Invalid position\n");
}
}
void del()
{
int usn;
struct node *prev=0,*cur;
printf("Enter the student ID\n");
scanf("%d",&usn);
cur=first;
if(cur->id==usn)
{
if(cur->next==0)rear=0;
first=first->next;
printf("Deleted!!!\n");
free(cur);
}
else
{
while(cur->next!=NULL && cur->id!=usn)
{
prev=cur;
cur=cur->next;
}
if(cur->next==NULL&& cur->id!=usn)
printf("Id does not exist\n");
else
{
if(cur->id==usn)rear=prev;
prev->next=cur->next;
free(cur);
printf("Deleted!!!!\n");
}
}
}
void display()
{
temp=first;
while(temp!=NULL)
{
printf("Id:\t%d\n",temp->id);
printf("Name:\t%s\n",temp->name);
printf("Sem:\t%d\n\n",temp->sem);
temp=temp->next;
}
}
void search()
{
int reg;
temp=first;
printf("Enter the id to be searched for\n");
scanf("%d",®);
while(temp!=0 )
{
if(reg==temp->id)
{
printf("search successfull\n");
printf("Enter new id\n");
scanf("%d",&temp->id);
printf("Enter new name\n");
scanf("%s",temp->name);
printf("Enter new usn\n");
scanf("%d",&temp->sem);
return;
}
temp=temp->next;
}
printf("search unsuccessfull\n");
}
void main()
{
int ch;
while(1)
{
clrscr();
printf("0-> Insrert at any position\n1->Insert front\n2->Insert rear\n3->Delete\n4-
>Display\n5->search\n6->exit\n");
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 0: insert_at_pos();break;
case 1:insert_front();break;
case 2: insert_rear();break;
case 3: if(first==NULL)
printf("List empty\n");
else
del();
break;
case 4: if(first==NULL)
printf("List empty\n");
else
display();
break;
case 5: search();
break;
case 6: exit(0);
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
{
int info;
struct node *next;
}*first=0,*temp;
void push()
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter item:\t");
scanf("%d",&temp->info);
temp->next=first;
first=temp;
}
void pop()
{
temp=first;
first=first->next;
printf("poped item: %d",temp->info);
free(temp);
}
void display()
{
temp=first;
printf("Stack contents:\t");
while(temp!=0)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
void main()
{
int choice;
while(1)
{
clrscr();
printf("1->Push\n2->Pop\n3->Display\n4->Exit\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: if(first==0)
printf("Stack underflow\n");
else
pop();
break;
case 3: if(first==0)
printf("List empty\n");
else
display();
break;
default: exit(0);
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
{
int info;
struct node *next;
}*first=0,*temp,*rear=0;
void insert()
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter item:\t");
scanf("%d",&temp->info);
temp->next=0;
if(rear==0)rear=first=temp;
else
{
rear->next=temp;
rear=temp;
}
}
void del()
{
temp=first;
if(first->next==0)rear=0;
first=first->next;
printf("Deleted item: %d",temp->info);
free(temp);
}
void display()
{
temp=first;
printf("Stack contents:\t");
while(temp!=0)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
void main()
{
int choice;
while(1)
{
clrscr();
printf("1->Insert\n2->delete\n3->Display\n4->Exit\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: if(first==0)
printf("Queue empty\n");
else
del();
break;
case 3: if(first==0)
printf("Queue empty\n");
else
display();
break;
default: exit(0);
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int info;
struct node *rlink,*llink;
}*first=0,*temp;
void insert_front()
{
temp=(struct node*)malloc(sizeof(struct node));
temp->rlink=temp->llink=0;
printf("Enter the item\n");
scanf("%d",&temp->info);
if(first==0) first=temp;
else
{
temp->rlink=first;
first->llink=temp;
first=temp;
}
}
void display()
{
temp=first;
while(temp!=0)
{
printf("%d\t",temp->info);
temp=temp->rlink;
}
}
void insert_left()
{
struct node *cur=first;
int key;
printf("Enter the key\n");
scanf("%d",&key);
if(key==first->info)
insert_front();
else
{
while(cur->rlink!=0&&key!=cur->info)
cur=cur->rlink;
if(key==cur->info)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->rlink=temp->llink=0;
printf("Enter the item\n");
scanf("%d",&temp->info);
temp->llink=cur->llink;
temp->rlink=cur;
cur->llink->rlink=temp;
cur->llink=temp;
}
else printf("Key does not exist\n");
}
}
void delete()
{
int item;
temp=first;
printf("Enter the item to be deleted\n");
scanf("%d",&item);
if(first->info==item)
first=first->rlink;
else
{
while(temp->rlink!=0&&item!=temp->info)
temp=temp->rlink;
if(temp->info==item&&temp->rlink==0)
temp->llink->rlink=0;
else
{
if(temp->info==item)
{
temp->llink->rlink=temp->rlink;
temp->rlink->llink=temp->llink;
}
else
{
printf("Item does not exist\n");
return;
}
}
}
free(temp);
}
void main()
{
int ch;
while(1)
{
clrscr();
printf("1.Insert Front\n2.Display\n3.Insert Left\n4.Delete\n5.Exit\nEnter your
choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_front();
break;
case 2: if(first==0) printf("List is empty\n");
else
display();
break;
case 3: if(first==0) printf("List is empty\n");
else
insert_left();
break;
case 4: if(first==0) printf("List is empty\n");
else
delete();
break;
default: exit(0);
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
struct node
{
int info;
struct node *left, *right;
}*prev=0,*cur=0,*root=0,*temp;
void create()
{
int item;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter the info\n");
scanf("%d",&item);
temp->info=item;
temp->left=temp->right=0;
if(root==0) root=temp;
else
{
cur=root;
while(cur!=0)
{
prev=cur;
cur=(item<cur->info)?cur->left:cur->right;
}
if(item<prev->info) prev->left=temp;
else prev->right=temp;
}
}
void pre(struct node *root)
{
temp=root;
if(root!=0)
{
printf("%d\t",root->info);
pre(root->left);
pre(root->right);
}
}
void post(struct node *root)
{
temp=root;
if(root!=0)
{
post(root->left);
post(root->right);
printf("%d\t",root->info);
}
}
void in(struct node *root)
{
temp=root;
if(root!=0)
{
in(root->left);
printf("%d\t",root->info);
in(root->right);
}
}
void main()
{
int ch;
while(1)
{
clrscr();
printf("1.Create a tree\n2.Preorder\n3.Inorder\n4.Postorder\n5.Exit\nEnter your
choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: pre(root);
break;
case 3: in(root);
break;
case 4: post(root);
break;
default: exit(0);
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
#include<process.h>
int a[10],l,h,key;
int bin_search(int l,int h)
{
int mid;
if(l>h)
return -1;
else
{
mid=(l+h)/2;
if(key==a[mid])return mid+1;
else
if(key>a[mid])
return bin_search(mid+1,h);
else
return bin_search(l,mid-1);
}
}
void main()
{
int n,i,pos;
clrscr();
printf("Enter the value for n\n");
scanf("%d",&n);
printf("Enter the elements:\t");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key element\n");
scanf("%d",&key);
pos=bin_search(0,n-1);
if(pos==-1)printf("Search unsuccessfull\n");
else
printf("Element found at position:\t%d",pos);
getch();
}