Professional Documents
Culture Documents
All Lab Prog
All Lab Prog
1. Write a C program to create a sequential file with at least five records, each record having the
structure shown below:
USN Name Marks1 Marks2 Marks3
Non-zero 25 Characters Positive Positive Positive
Positive integer integer integer
integer
#include<stdio.h>
#include<conio.h>
#include<process.h>
typedef struct
{
int usn;
char name[25];
int m1,m2,m3;
}stu;
fp=fopen(filename,"r");
1
Data Structures Lab - 06CSEL37
if(fp!=NULL)
while(fscanf(fp,"%5d%25s%3d%3d%3d",&s.usn,s.name,&s.m1,&s.m2,&s.m3)!=EOF)
if(id==s.usn)
{ puts("\nusn\tname: \tm1,\tm2,\tm3");
printf("%-5d\t%-25s\t%-3d%-3d\t%-3d\n",s.usn,s.name,s.m1,s.m2,s.m3);
flag=1;
}
if(flag==0)
{
printf("\n No Records");
getch();
}
}
void main()
{
int n,i,ch;
char filename[23];
FILE *fp;
stu s;
clrscr();
printf("\n Enter filename :");
scanf("%s",filename);
fp=fopen(filename,"w");
if(fp!=NULL)
{
puts("\n Enter USN NAME MARKS1 MARKS2 MARKS3 \n");
for(i=0;i<n;i++)
{
scanf("%5d%25s%3d%3d%3d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
fprintf(fp,"%5d%25s%3d%3d%3d",s.usn,s.name,s.m1,s.m2,s.m3);
}
}
fclose(fp);
2
Data Structures Lab - 06CSEL37
while(1)
{
printf("\n 1.Display 2.Search\n");
printf("\n Enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:display(filename);
break;
case 2:search(filename);
break;
default:printf("\n Error in input\n");
exit(0);
} // end Switch
} // end of while
}
/**************************************************************************/
3
Data Structures Lab - 06CSEL37
while(1)
{
printf("\n\n 1: string copy 2:string concat \n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter string to be copied \n");
scanf("%s",s1);
newstrcpy(s2,s1);
printf("\n The original string is %s",s1);
printf("\n The copied string is %s",s2);
break;
default:exit(0);
}
}
}
/**************************************************************************/
3. Write a C program, which accepts the internet protocol (IP) address in decimal dot format
(ex. 1153.18.8.105) and converts it into 32-bit long integer ( ex. 2568095849) using strtok
library functions and unions.
#include<stdio.h>
#include<string.h>
typedef union
{
unsigned char chaddr[4];
unsigned long numaddr;
}IP ;
void main ( )
{
IP addr ;
char * parser ;
char straddr [ 16 ] ;
int i ;
4
Data Structures Lab - 06CSEL37
clrscr() ;
getch();
}
/**************************************************************************/
4. Write a C program to construct a stack of integers and to perform the following operations
on it.
a) Push
b) Pop
c) Display
The program should print appropriate messages for stack overflow, stack underflow and stack
empty.
#define SIZE 5
typedef struct {
int st [SIZE] ;
int top ;
} stack ;
5
Data Structures Lab - 06CSEL37
else
{
s -> top++;
s -> st[ s->top ] = ele ;
}
}
int pop(stack *s)
{
int ele ;
if ( s->top == -1 )
return 0;
else
ele = s-> st [ s->top ] ;
s ->top--;
return ele ;
}
while(1)
{
clrscr();
display(s);
6
Data Structures Lab - 06CSEL37
switch(ch)
{
case 1: printf ( " enter ele to be pushed\n" ) ;
scanf( "%d", &ele ) ;
push ( &s, ele ) ;
break ;
case 2:ele=pop ( &s ) ;
printf ( "poped ele is %d\n", ele ) ;
getch ( ) ;
break ;
default:exit ( 0 ) ;
}
}
}
/**************************************************************************/
5. Write a C program to convert and print a given valid parenthesized infix arithmetic
expression to postfix expression. The expression consists of single character operands and
the binary operators + (plus), - (minus), * (multiply) and / (divide).
#include<conio.h>
7
Data Structures Lab - 06CSEL37
void main()
{
char in[40],pos[40];
int i,j,st[40],top=-1,dont_use;
char sym;
clrscr();
st[++top]='#';
i=j=0;
while(in[i]!='\0')
{
sym=in[i];
if(inp_prec(sym) != stk_prec(st[top]))
st[++top]=sym;
else
dont_use=st[top--];
i++;
}
while(st[top]!='#')
{
pos[j++]=st[top--];
}
pos[j]='\0';
puts("infix :");
puts(in);
puts("postfix :");
puts(pos);
getch();
}
8
Data Structures Lab - 06CSEL37
/**************************************************************************/
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<math.h>
#define SIZE 20
typedef struct{
int st [ SIZE ] ;
int top ;
} stack ;
9
Data Structures Lab - 06CSEL37
else
return 0 ;
}
void main( )
{
char post [ 40 ] ,sym ;
int res, op1, op2 , tmp , i ;
stack s ;
clrscr ( ) ;
s.top = -1 ;
printf ( "enter postfix exp\n" ) ;
scanf ( "%s" , post) ;
else
{
op2 = pop ( &s ) ;
op1 = pop ( &s ) ;
res = eval ( sym, op1 , op2 ) ;
push ( &s , res ) ;
}
}
/**************************************************************************/
10
Data Structures Lab - 06CSEL37
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
#define size 5
typedef struct q
{
int qu[size];
int f,r;
}QUE;
int isfull(QUE q)
{
if(q.r==size-1)
return 1;
else
return 0;
}
int isempty(QUE q)
{
if(q.r==-1)
return 1;
else
return 0;
}
void insertq(QUE *q,int e)
{
if(isfull(*q))
{
puts("que full");
return;
}
else
q->qu[++q->r]=e;
}
int deleteq(QUE *q)
{
int e=-1;
if(isempty(*q))
puts("Que empty");
else
11
Data Structures Lab - 06CSEL37
{
e=q->qu[q->f++];
if(q->f>q->r)
{ q->f=0;
q->r=-1;
}
}
return e;
}
void disp(QUE q)
{
int i;
if(isempty(q))
puts("\n Empty QUEUE");
else
{
printf("\n The elements of Que are :\n");
for(i=q.f ; i<=q.r; i++)
{
printf("%d\n",q.qu[i]);
}
}
}
void main()
{
QUE q;
int ch,ele;
q.r=-1;
q.f=0;
clrscr();
while(1)
{
puts("1: insert 2:delete ");
scanf("%d",&ch);
switch(ch)
{
case 1:puts("enter elements");
scanf("%d",&ele);
insertq(&q,ele);
disp(q);
break;
case 2:ele=deleteq(&q);
12
Data Structures Lab - 06CSEL37
if(ele != -1)
printf("Deleted element is %d",ele);
disp(q);
break;
default: exit(0);
}
}
}
/**************************************************************************/
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
#define max 5
typedef struct {
int data [ max ] ;
int front ;
int rare ;
} cqueue ;
13
Data Structures Lab - 06CSEL37
else {
if( cq.front == max-1 )
i = 0;
else
i = cq.front + 1 ;
}
while(1)
{
printf ( "%d => ", cq.data[i] ) ;
if( i == cq.rare)
break;
if( i == max-1 )
i = 0;
else
i++;
}
printf("\n\n");
}
void main( )
{
cqueue cq ;
int ch , ele ;
clrscr ( ) ;
cq.rare = max - 1 ;
cq.front = max -1;
14
Data Structures Lab - 06CSEL37
while ( 1 )
{
clrscr( ) ;
displaycq( cq ) ;
printf ("\n enter 1:insert 2:delete \n" ) ;
scanf( "%d" ,&ch ) ;
switch( ch )
{
case 1 : printf ( "enter the ele to be inserted\n" ) ;
scanf ( "%d", &ele ) ;
insertcq ( &cq , ele ) ;
break ;
/**************************************************************************/
9. Write a C program using dynamic variables and pointers, to construct a singly linked list
consisting of the following information in each node: student id (integer), student name
(character string) and semester (integer). The operations to be supported are:
The insertion operation.
i) At the front of a list.
ii) At the back of the list.
iii) At any position in the list.
Deleting a node based on student id. If the specified node is not present in the list an error
message should be displayed. Both the options should be demonstrated.
Searching a node based on student id and update the information content. If the specified
node is not present in the list an error message should be displayed. Both situations
should be displayed.
15
Data Structures Lab - 06CSEL37
#include<mem.h>
typedef struct n
{
int rno,sem;
char name[20];
struct n *next;
}*NODE;
NODE getnode( )
{
NODE tmp;
tmp=(NODE) malloc(sizeof(struct n));
puts("\n enter rno,name,sem");
scanf("%d%s%d",&tmp->rno,tmp->name,&tmp->sem);
tmp->next=NULL;
return tmp;
}
16
Data Structures Lab - 06CSEL37
17
Data Structures Lab - 06CSEL37
}
tmp->next=tt->next;
tt->next=tmp;
}
if(tmp->next==NULL)
{
puts("NODE DOESNT EXIST");
return;
}
18
Data Structures Lab - 06CSEL37
if(tmp->next->rno == id)
{
tt=tmp->next;
tmp->next=tt->next;
free(tt);
}
}
if(tmp->next==NULL)
{
puts("NODE DOESNT EXIST");
return;
}
if(tmp->next->rno == id)
{
tt=tmp->next;
puts("\n enter changed name and sem");
scanf("%s%d",tmp->name,&tmp->sem);
}
}
19
Data Structures Lab - 06CSEL37
void main( )
{
NODE head;
int ele, ch;
head = NULL;
clrscr( );
while(1)
{
clrscr( );
disp(&head);
puts ("\n\n 1:insertbeg 2:insert end 3:insert pos 4:delete ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_beg(&head);
break;
case 2: insert_end(&head);
break;
case 3: insert_pos(&head);
break;
case 4: deleteid(&head);
break;
case 5: searchid(&head);
break;
default:exit(0);
}
/**************************************************************************/
10. Write a C program using dynamic variables and pointers to construct a stack of integers
using singly linked list and to perform the following operations.
a) Push
b) Pop
c) Display
The program should print appropriate messages for stack overflow
and stack empty.
#include<mem.h>
# define size 5
20
Data Structures Lab - 06CSEL37
typedef struct n
{
int info;
struct n *next;
}*NODE;
printf("%d==>",tmp->info);
tmp=tmp->next;
}
printf("\n-------------------------------------------\n");
}
21
Data Structures Lab - 06CSEL37
if(*head==NULL)
{
puts("\n UNDERFLOW");
return -1;
}
c--; /////// SEE HERE
tmp=*head;
ele=tmp->info;
*head=tmp->next;
free(tmp);
return ele;
}
void main()
{
NODE head;
int ele, ch;
head = NULL;
clrscr();
while(1)
{
puts ("\n\n 1:insert 2:delete ");
scanf("%d",&ch);
switch(ch)
{ case 1: puts("\n Enter ur element\n");
scanf("%d",&ele);
push(&head,ele);
disp(&head);
break;
case 2: ele = pop(&head);
if (ele != -1)
printf("\n The poped element is %d",ele);
disp(&head);
break;
default:exit(0);
}
} }
22
Data Structures Lab - 06CSEL37
/**************************************************************************/
11. Write a C program using dynamic variables and pointers to construct a queue of integers
using singly linked list and to perform the following operations.
a) Insert
b) Delete
c) Display
The program should print appropriate messages for queue full and queue empty.
#include<mem.h>
#define size 5
int c=0; //SEE HERE
typedef struct n
{
int info;
struct n *next;
}*NODE;
NODE getnode(int ele)
{
NODE tmp;
tmp=(NODE) malloc(sizeof(struct n));
tmp->info=ele;
tmp->next=NULL;
return tmp;
}
23
Data Structures Lab - 06CSEL37
if(*rear==NULL)
*rear = *front=tmp;
else
{
(*rear)->next=tmp;
*rear=tmp;
}
}
24
Data Structures Lab - 06CSEL37
if(*rear==*front)
*rear=*front=NULL;
else
*front=(*front)->next;
free(tmp);
return ele;
}
void main()
{
NODE rear,front;
int ele, ch;
rear=front=NULL;
clrscr();
while(1)
{
puts ("\n\n 1:insert 2:delete ");
scanf("%d",&ch);
switch(ch)
{
case 1: puts("\n Enter ur element\n");
scanf("%d",&ele);
insert(&rear,&front,ele);
disp(&front);
break;
case 2: ele = remove(&rear,&front);
if (ele != -1)
printf("\n The poped element is %d",ele);
disp(&front);
break;
default:exit(0);
}
}
}
25
Data Structures Lab - 06CSEL37
/**************************************************************************/
12. Write a C program to support the following operations on a doubly linked list where each
node consists of integers.
a) Create a doubly linked list by adding each node at the front.
b) Insert a new node to the left of the node whose key value is read as an input.
c) Delete the node of a given data, if it is found, otherwise display appropriate message.
d) Display the contents of the list.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<alloc.h>
#include<mem.h>
typedef struct n
{
int info;
struct n *next,*pre;
}*NODE;
NODE getnode()
{
NODE tmp;
tmp=(NODE)malloc(sizeof(struct n));
puts("enter the info\n");
scanf("%d",&tmp->info);
tmp->next=tmp->pre=NULL;
return tmp;
}
if(tmp==NULL)
{
printf("\noverflow.......!!!!!!\n");
return;
}
if(*head==NULL)
{
*head=tmp;
return;
}
26
Data Structures Lab - 06CSEL37
tmp->next=*head;
(*head)->pre=tmp;
*head=tmp;
return;
}
27
Data Structures Lab - 06CSEL37
28
Data Structures Lab - 06CSEL37
if(*head==NULL)
{
puts("!!!!!no elements!!!!!\n");
return;
}
puts("\n--------------------------------------------------------------\n");
while(tmp!=NULL)
{
printf("%d==>",tmp->info);
tmp=tmp->next;
}
puts("\n--------------------------------------------------------------\n");
return;
}
void main()
{
NODE head=NULL;
int c;
clrscr();
while(1)
{
case 1 : insertbeg(&head);
disp(&head);
break;
case 2 : deleteq(&head);
disp(&head);
break;
case 3 : insertleft(&head);
disp(&head);
break;
case 4 :
disp(&head);
break;
default : exit(0);
}
}
}
/**************************************************************************/
29
Data Structures Lab - 06CSEL37
#include<alloc.h>
#include<stdlib.h>
#define max1 30
typedef struct n
{
int info;
struct n *left,*right;
}*NODE;
typedef struct q1
{
NODE q[max1];
int r,f;
}QUE;
tmp= Q->q[Q->f];
(Q->f)++;
return tmp;
}
Q.r=-1;
Q.f=0;
30
Data Structures Lab - 06CSEL37
if(tmp==NULL)
{
printf("\n no elements\n");
return;
}
while(1)
{
tt=deleteq(&Q);
printf("%d ",tt->info);
if(tt->left !=NULL)
insertq(&Q,tt->left);
if(tt->right !=NULL)
insertq(&Q,tt->right);
if(Q.r<Q.f)
break;
}
}
void create(NODE *root,int el)
{
NODE tmp,cur,prev;
tmp=(NODE)malloc(sizeof(struct n));
tmp->info=el;
tmp->left=tmp->right=NULL;
if(*root==NULL)
{
*root=tmp;
return;
}
cur=*root;
prev=NULL;
while(cur != NULL)
{
prev=cur;
31
Data Structures Lab - 06CSEL37
if(el==cur->info)
{
printf("\n duplicate");
return;
}
if(el<cur->info)
cur=cur->left;
else
cur=cur->right;
}
if(el<prev->info)
prev->left=tmp;
else
prev->right=tmp;
}
if(tmp!=NULL)
{
printf("%d ",tmp->info);
preorder(tmp->left);
preorder(tmp->right);
}
}
if(tmp!=NULL)
{
postorder(tmp->left);
postorder(tmp->right);
printf("%d ",tmp->info);
}
}
32
Data Structures Lab - 06CSEL37
if(tmp!=NULL)
{
inorder(tmp->left);
printf("%d ",tmp->info);
inorder(tmp->right);
}
}
int search(NODE root,int el)
{
NODE tmp=root;
int fl=-1;
if(tmp==NULL)
return -1;
while(tmp != NULL)
{
if(tmp->info==el)
{
fl=0;
break;
}
if(el > tmp->info)
tmp=tmp->right;
if(el <tmp->info)
tmp=tmp->left;
}
return fl;
}
void main()
{
NODE r;
int ch,el,c;
r=NULL;
clrscr();
while(1)
{
printf("\n 1:create 2:inorder 3:preorder 4:postorder \n");
33
Data Structures Lab - 06CSEL37
fflush();
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nplease enter element to add to the BST\n");
scanf("%d",&el);
create(&r,el);
disp(r);
break;
case 2:printf("\n the inorder traversal is\n");
inorder(r);
break;
case 3:printf("\n the preorder traversal is\n");
preorder(r);
break;
case 4:printf("\n the postorder traversal is\n");
postorder(r);
break;
default:exit(0);
}
}
/**************************************************************************/
14. Write recursive C programs for
a) Searching an element on a given list of integers using the Binary search method.
b) Solving the Towers of Hanoi problem.
#include<stdio.h>
#include<conio.h>
#include<process.h>
int bs(int a[],int n,int key,int low,int high)
{
int mid;
if(low>high)
{
printf("\n Item is not found\n");
return;
}
34
Data Structures Lab - 06CSEL37
mid=(low+high)/2;
if(key==a[mid])
{
return mid;
}
if(key<a[mid])
{
high=mid-1;
bs(a,n,key,low,high);
}
else
{
low=mid+1;
bs(a,n,key,low,high);
}
}
void main()
{
int a[20],key,n,i;
int low,high,ele;
clrscr();
low=0;
high=n-1;
ele=bs(a,n,key,low,high);
/**************************************************************************/
35
Data Structures Lab - 06CSEL37
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
int count=0;
void main()
{
int n;
char A,B,C;
clrscr();
printf("Enter the number of discs :");
scanf("%d",&n);
tower(n,'A','B','C');
printf("\nTotal no. of disc moves = %d",count);
getch();
}
36