Professional Documents
Culture Documents
Data Structures With C Lab
Data Structures With C Lab
/*1. Write a C Program to create a sequential file with atleast 5 records, each record having the
structure shown below:
USN - Positive number : Name - 25 Char : Marks1,marks 2,marks 3 are positive Numbers
Write necessary functions
a. To display all the records in the file.
b. To search for a specific record based on the USN. In case the record is not found, suitable
message should be displayed.
Both the options in this case must be demonstrated.*/
#include<stdio.h>
#include<conio.h>
typedef struct st
{
int USN,M1,M2,M3;
char Name[25];
}record;
record stu;
void read_data(FILE *fp)
{
printf("\nEnter the USN\t");
scanf("%d",&stu.USN);
printf("Enter the Name\t");
scanf("%s",&stu.Name);
printf("Enter the Marks 1\t");
scanf("%d",&stu.M1);
printf("Enter the Marks 2\t");
scanf("%d",&stu.M2);
printf("Enter the Marks 3\t");
scanf("%d",&stu.M3);
fprintf(fp,"%d\t%s\t%d\t%d\t%d\n",stu.USN,stu.Name,stu.M1,stu.M2,stu.M3);
fclose(fp);
}
void disp_data(record st)
{
printf("%d%18s\t%5d\t%5d\t%5d\n",st.USN,st.Name,st.M1,st.M2,st.M3);
}
void main()
{
FILE *fp;
int i,j,n,ch,key,flag=1,found=0;
clrscr();
printf("\n Enter the Number of students details\t");
scanf("%d",&n);
if(n>0)
fp=fopen("student.dat","w");
printf("\n Enter the students Details\n");
for(i=0;i<n;i++)
{
printf("\n Enter the Details of %d Student",i+1);
read_data(fp);
}
fclose(fp);
while(flag)
{
clrscr();
printf("\n1 Add Record\n2 Search\n3 Display all\n4 Exit\n Enter the choice\t");
scanf("%d",&ch);
switch(ch)
{
case 1: fp=fopen("student.dat","a");
read_data(fp);
break;
case 3:
fp=fopen("student.dat","r");
printf("\nUSN\t\tName\tMarks 1\tMarks 2\tMarks
3\n");
while(1)
{
fscanf(fp,"%d%s%d%d
%d",&stu.USN,stu.Name,&stu.M1,&stu.M2,&stu.M3);
if(feof(fp)) break;
disp_data(stu);
}
fclose(fp);
break;
case 2:
fp=fopen("student.dat","r");
printf("\n Enter the USN Number to search\t");
scanf("%d",&key);
while(!feof(fp))
{
fscanf(fp,"%d%s%d%d
%d",&stu.USN,stu.Name,&stu.M1,&stu.M2,&stu.M3);
if(feof(fp)) break;
if(stu.USN==key)
{
printf("\n Record Found\n");
printf("\nUSN\t\tName\tMarks
1\tMarks 2\tMarks 3\n");
disp_data(stu);
found=1;
}
}
fclose(fp);
if(found==0)
{
printf("\nThe record with USN %d is not
found\n",key);
found=0;
}
break;
case 4:
default: flag=0;
}
getch();
}
}
/*3. Write a C Program, which accepts the Internet Protocol (IP) address in
decimal dot format (ex. 153.18.8.105) and converts it into 32-bit long
integer (ex. 2568095849) using strtok library function and unions.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
typedef union ip
{
unsigned long num;
unsigned char ar[3];
}IP;
void main()
{
IP ipaddr;
int i=3,j;
char *ip_add="153.18.8.105";
char *p,*x;
clrscr();
p=strtok(ip_add,".");
ipaddr.ar[i--]=strtol(p,&x,10);
while(p!=NULL)
{
p=strtok(NULL,".");
ipaddr.ar[i--]=strtol(p,&x,10);
}
printf("IP Address in Doted Decimal System\t");
for(j=3;j>=0;j--)
{
if(j!=0)
printf("%d.",ipaddr.ar[j]);
else
printf("%d",ipaddr.ar[j]);
}
printf("\nIP Binary Address %lu",ipaddr.num);
getch();
}
return ret;
}
void display(STACK x)
{
int i;
if(isempty(x.top))
printf("\n Stack is Empty\n");
else
{
printf ("\n Elements of Stack are\n");
for(i=x.top;i>=0;i--)
printf("%d\t",x.s[i]);
}
}
void main()
{
int ch,ele,flag=1;
STACK a;
a.top=-1;
while(flag)
{
clrscr();
printf("\n 1 Push\n2 Pop\n3 Display\n4 Exit\nEnter the Choice\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the element to push\t");
scanf("%d",&ele);
push(&a,ele);
break;
case 2:
ele=pop(&a);
if(ele!=-1)
printf("\n Element poped is \t%d",ele);
break;
case 3:
display(a);
break;
case 4:
default: flag=0;
}
getch();
}}
a.top=-1;
push('#');
clrscr();
printf("Enter the infix Expn\t");
scanf("%s",infix);
convert(infix,postfix);
printf("The postfix expn of %s is %s",infix,postfix);
getch();
}
/*6. Write a C Program to evaluate a valid suffix/postfix expression using stack. Assume that the
suffix/postfix expression is read as a single line consisting of non-negative single digit operands
and binary arithmetic operators. The arithmetic operators are + (add), - (subtract), * (multiply)
and / (divide).*/
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
typedef struct stack
{
double s[10];
int top;
}STACK;
STACK a;
void push(double ele)
{
a.top++;
a.s[a.top]=ele;
}
double pop()
{
double ret;
ret=a.s[a.top];
a.top--;
return ret;
}
double eval(char postfix[])
{
int i;
double x,y;
char c;
for(i=0;postfix[i]!='\0';i++)
{
c=postfix[i];
if(isdigit(c))
push(c-'0');
else
{
x=pop();
y=pop();
switch(c)
{
case '+':push(x+y);
break;
case '-':push(y-x);
break;
case '*':push(x*y);
break;
case '/':push(y/x);
}
}
}
return(pop());
}
void main()
{
char postfix[20];
double res;
clrscr();
printf("Enter the Postfix expn\t");
gets(postfix);
res=eval(postfix);
printf("The Result is %lf",res);
getch();
}
if(x->f==x->r)
{
x->f=-1;
x->r=0;
}
}
return ret;
}
void display(QUEUE x)
{
int i;
if(isempty(x.f))
printf("\n Stack is Empty\n");
else
{
printf ("\n Elements of Stack are\n");
for(i=x.f;i<x.r;i++)
printf("%d\t",x.a[i]);
}
}
void main()
{
int ch,ele,flag=1;
QUEUE x;
x.f=-1;
x.r=0;
while(flag)
{
clrscr();
printf("\n 1 Insert\n2 Delete\n3 Display\n4 Exit\nEnter the Choice\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the element to Insert\t");
scanf("%d",&ele);
insert(&x,ele);
break;
case 2:
ele=qdelete(&x);
if(ele!=-1)
printf("\n Element Deleted is \t%d",ele);
break;
case 3:
display(x);
break;
case 4:
default: flag=0;
}
getch();
}
}
int isfull(int x)
{
return (x==MAX);
}
int isempty(int x)
{
return (x==0);
}
void insert(QUEUE *x,int ele)
{
if(isfull(x->count))
printf("\nQueue Full\n");
else
{
x->a[x->r]=ele;
x->r=(x->r+1)%MAX;
x->count++;
}
}
int qdelete(QUEUE *x)
{
int ret=-1;
if(isempty(x->count))
printf("\n Queue is empty\n");
else
{
ret=x->a[x->f];
x->f=(x->f+1)%MAX;
x->count--;
}
return ret;
}
void display(QUEUE x)
{
int i,j;
if(isempty(x.count))
printf("\n Stack is Empty\n");
else
{
printf ("\n Elements of Stack are\n");
j=x.f;
for(i=0;i<x.count;i++)
{
printf("%d\t",x.a[j]);
j=(j+1)%MAX;
}
}
}
void main()
{
int ch,ele,flag=1;
QUEUE x;
x.f=0;
x.r=0;
x.count=0;
while(flag)
{
clrscr();
printf("\n1 Insert\n2 Delete\n3 Display\n4 Exit\nEnter the Choice\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the element to Insert\t");
scanf("%d",&ele);
insert(&x,ele);
break;
case 2:
ele=qdelete(&x);
if(ele!=-1)
printf("\n Element Deleted is \t%d",ele);
break;
case 3:
display(x);
break;
case 4:
default: flag=0;
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct stud
{
int id;
char name[20];
int sem;
}stu;
typedef struct node
{
stu info;
struct node *link;
}N;
stu getdata()
{
stu t;
printf("\nEnter the Student ID\t");
scanf("%d",&t.id);
fflush(stdin);
printf("\nEnter the Student Name\t");
scanf("%s",t.name);
printf("\nEnter the Student Sem\t");
scanf("%d",&t.sem);
return t;
}
void dispdata(stu x)
{
printf("\n%d\t%s\t%d",x.id,x.name,x.sem);
}
N* getnode(stu x)
{
N *t;
t=(N *) malloc(sizeof(struct node));
t->info=x;
t->link=NULL;
return t;
}
N *finsert(N *f,stu x)
{
N *t=getnode(x);
if(f==NULL)
f=t;
else
{
t->link=f;
f=t;
}
return f;
}
N *einsert(N *f,stu x)
{
N *t,*t1;
t=getnode(x);
if(f==NULL)
f=t;
else
{
t1=f;
while(t1->link!=NULL)
t1=t1->link;
t1->link=t;
}
return f;
}
N *posinsert(N *f,stu x,int pos)
{
int i=1;
N *cur,*t,*prev;
t=getnode(x);
else
{
cur=f;
prev=f;
while(cur!=NULL && cur->info.id!=key)
{
prev=cur;
cur=cur->link;
}
if(cur==NULL)
printf("\nInvalid ID\n");
else
{
printf("\n Node Deleted\n");
printf("\nID\tName\tSem\n");
dispdata(cur->info);
prev->link=cur->link;
}
}
return f;
}
N *id_update(N *f,int key)
{
N *cur,*prev;
if(f==NULL)
printf("\nInvalid Key\n");
else if(f->info.id==key)
{
printf("New Data\n");
f->info=getdata();
}
else
{
cur=f;
prev=f;
while(cur!=NULL && cur->info.id!=key)
{
prev=cur;
cur=cur->link;
}
if(cur==NULL)
printf("\nInvalid ID\n");
else
{
printf("New Data\n");
cur->info=getdata();
}
}
return f;
}
case 3:
printf("Enter the Student date to Node\n");
x=getdata();
printf("Enter the Position \t");
scanf("%d",&pos);
first=posinsert(first,x,pos);
break;
case 4: display(first);
break;
case 5:
printf("Enter the ID \t");
scanf("%d",&id);
first=id_delete(first,id);
break;
case 6:
printf("Enter the ID \t");
scanf("%d",&id);
first=id_update(first,id);
break;
case 7:
default: flag=0;
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define MAX 5
typedef struct node
{
int info;
struct node *link;
}N;
int count=0;
N* getnode(int x)
{
N *t;
t=(N *) malloc(sizeof(struct node));
t->info=x;
t->link=NULL;
return t;
}
N *push(N *f,int x)
{
if(count==MAX)
printf("\n Stack Oveflow");
else
{
N *t=getnode(x);
if(f==NULL)
f=t;
else
{
t->link=f;
f=t;
}
count++;
}
return f;
}
N *pop(N *f,int &ele)
{
ele =-1;
if(count==0)
printf("\n Stack Empty");
else
{
N *t=f;
ele=f->info;
f=f->link;
free(t);
count--;
}
return f;
}
void display(N *f)
{
N *t=f;
if(f==NULL)
printf(" List is Empty\n");
else
{
printf("\nElement of List is \n");
while(t!=NULL)
{
printf("%d\t",t->info);
t=t->link;
}
}
}
void main()
{
N *first=NULL;
int ch,flag=1,ele;
while(flag)
{
clrscr();
printf("\n1 Push\n2 Pop\n3 Display\n4 Exit\n");
printf("Enter the Choice\t");
scanf("%d",&ch);
switch(ch)
{
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define MAX 5
typedef struct node
{
int info;
struct node *link;
}N;
int count=0;
N *front,*rear;
N* getnode(int x)
{
N *t;
t=(N *) malloc(sizeof(struct node));
t->info=x;
t->link=NULL;
return t;
}
void insert(int x)
{
if(count==MAX)
printf("\n Queue Oveflow");
else
{
N *t=getnode(x);
if(rear==NULL)
front=rear=t;
else
{
rear->link=t;
rear=t;
}
count++;
}
}
void Qdelete(int &ele)
{
ele =-1;
if(count==0)
printf("\n Queue Empty");
else
{
N *t=front;
ele=front->info;
front=front->link;
if(front==NULL)
rear=front;
free(t);
count--;
}
}
void display()
{
N *t=front;
if(t==NULL)
printf(" \nQueue is Empty\n");
else
{
printf("\nElement of Queue is \n");
while(t!=NULL)
{
printf("%d\t",t->info);
t=t->link;
}
}
}
void main()
{
front=rear=NULL;
int ch,flag=1,ele;
while(flag)
{
clrscr();
printf("\n1 Insert\n2 Delete\n3 Display\n4 Exit\n");
printf("Enter the Choice\t");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the Info to Insert\n");
scanf("%d",&ele);
insert(ele);
break;
case 2: Qdelete(ele);
if(ele!=-1)
printf("\n Element Deleted is %d",ele);
break;
case 3:
display();
break;
case 4:
default: flag=0;
}
getch();
}
}
N *finsert(N *f,int x)
{
N *t;
t=getnode(x);
if(f==NULL)
f=t;
else
{
t->rptr=f;
f->lptr=t;
f=t;
}
return f;
}
N *Insert_left(N *f,int x,int key)
{
N *t,*cur;
if(f->info==key)
f=finsert(f,x);
else
{
cur=f;
while(cur!=NULL && cur->info!=key)
cur=cur->rptr;
if(cur==NULL)
printf("\n Invalid Key\n");
else
{
N *t=getnode(x);
t->rptr=cur;
t->lptr=cur->lptr;
cur->lptr=t;
t->lptr->rptr=t;
}
}
return f;
}
N *Delete_key(N *f,int key)
{
N *t,*cur;
if(f->info==key)
{
printf("\n Key Element %d is found and deleted",key);
t=f;
f=f->rptr;
f->lptr=NULL;
free(t);
}
else
{
cur=f;
while(cur!=NULL && cur->info!=key)
cur=cur->rptr;
if(cur==NULL)
printf("\n Key Element %d not found",key);
else
{
printf("\n Key Element %d is found and deleted",key);
cur->lptr->rptr=cur->rptr;
cur->rptr->lptr=cur->lptr;
free(cur);
}
}
return f;
}
case 3:
printf("\nEnter the Key to delete\t");
scanf("%d",&key);
first=Delete_key(first,key);
break;
case 4:
display(first);
break;
case 5:
default: flag=0;
}
getch();
}
}
else
p->rptr=t;
}
}
return r;
}
void inorder(N *r)
{
if(r)
{
inorder(r->lptr);
printf("%d\t",r->info);
inorder(r->rptr);
}
}
void preorder(N *r)
{
if(r)
{
printf("%d\t",r->info);
preorder(r->lptr);
preorder(r->rptr);
}
}
void postorder(N *r)
{
if(r)
{
postorder(r->lptr);
postorder(r->rptr);
printf("%d\t",r->info);
}
}
void display(N *f)
{
printf("\n Inorder\n");
inorder(f);
printf("\n Preorder\n");
preorder(f);
printf("\n Postorder\n");
postorder(f);
}
void main()
{
N *root=NULL;
int ch,ele,flag=1;
clrscr();
while(flag)
{
printf("Enter The Value of Node\t");
scanf("%d",&ele);
root=insert(root,ele);
printf("Press 1 to Continue");
scanf("%d",&ch);
if(ch!=1)
flag=0;
}
printf("\n Elements of Tree are\n");
display(root);
getch();
}