Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 49

EXPERIMENT NO: 1

/* 1) Write a C program to create a sequential file with


atleast 5 record of students obtaining information
like usn,name,marks un 3 subjects & perform the
display & search operations */

#include<stdio.h>
FILE *fp;
struct stud
{
int usn;
char name[10];
int m1,m2,m3;
}s;
int n,i,choice;
char ch;
void display();
void search();
void main()
{
printf("enter the number of records\n");
scanf("%d",&n);
printf("enter %d students usn,name,marks in 3 subjects\n",n);
fp=fopen("stud.c","w");
for(i=0;i<n;i++)
{
scanf("%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
fprintf(fp,"%d\t%s\t%d\t%d\t%d\n",s.usn,s.name,s.m1,s.m2,s.m3);
}
fclose(fp);
do
{
printf("1:display\n 2:search");
printf("enter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:display();
break;
case 2:search();
break;
}
printf("do u want to continue\n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y');
getch();
}

void display()
{
fp=fopen("stud.c","r");
for(i=0;i<n;i++)
{
fscanf(fp,"%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
printf("%d\t%s\t%d\t%d\t%d\n",s.usn,s.name,s.m1,s.m2,s.m3);
}
fclose(fp);
}

void search()
{
int num;
fp=fopen("stud.c","r");
printf("enter the usn whose record is needed\n");
scanf("%d",&num);
for(i=0;i<n;i++)
{
fscanf(fp,"%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
if(s.usn==num)
{
printf("search is sucessful & details of that student
is\n");
printf("%d\t%s\t%d\t%d\t%d\n",s.usn,s.name,s.m1,s.m2,s.m3);
fclose(fp);
return;
}
}
printf("record does not exist\n");
fclose(fp);
}

/*
*************************OUTPUT******************************
enter the number of records
3
enter 3 students usn,name,marks in 3 subjects
11 PRAJWAL 90 90 90
22 DUVAN 85 90 95
33 MANJU 79 99 99
1:display
2:searchenter ur choice
1
11 PRAJWAL 90 90 90
22 DUVAN 85 90 95
33 MANJU 79 99 99
do u want to continue
y
1:display
2:searchenter ur choice
2
enter the usn whose record is needed
22
search is sucessful & details of that student is
22 DUVAN 85 90 95
do u want to continue
n*/
EXPERIMENT NO: 2(a)
/* 2) Write and demonstrate the following C functions.

a. newStrcpy that does the same job as strcpy

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char name1[10];
char name2[10];
clrscr();
printf("enter the name2\n");
gets(name2);
newstrcpy(name1,name2);
printf("name1=%s",name1);
getch();
}
newstrcpy(char name1[],char name2[])
{
int i;
i=0;
while(name2[i]!='\0')
{
name1[i]=name2[i];
i++;
}
name1[i]='\0';
return;
}

/**************************output****************************

Enter name2

BMS

Name1=BMS

*************************************************************/
EXPERIMENT NO: 2(b)
/* 2b.newStrcpy that does the same job as strcat without using
and library functions. */

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char name1[10];
char name2[10];
clrscr();
printf("enter the name1\n");
gets(name1);
printf("enter the name2\n");
gets(name2);
newstrcat(name1,name2);
printf("%s",name1);
getch();
}
newstrcat(char name1[],char name2[])
{
int i=0,j=0;
while(name1[i]!='\0')
{
i++;
}
while(name2[j]!='\0')
{
name1[i]=name2[j];
i++;
j++;
}
return;
}
EXPERIMENT NO: 3

/* 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
strtock libray function and unions. */

#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef union
{
unsigned char chaddress[20];
unsigned long numaddress;
}ipadd;

main()
{
ipadd addr;
char *parser;
char *straddress="153.18.8.105";
int i;
clrscr();
parser=strtok(straddress,".");
addr.chaddress[0]=strtol(parser,(char **)NULL,10);
for(i=1;i<4;i++)
{
parser=strtok(NULL,".");
addr.chaddress[i]=strtol(parser,(char**)NULL,10);
printf("%ld",addr.numaddress);
}
getch();
return;
}

/**************************output****************************

47615290491762136729
************************************************************/

EXPERIMENT NO: 4

/* 4) program to implement a stack (push, pop, display) */

#include<stdio.h>
#include<conio.h>
# define max 3
void push();
void pop();
void display();
int top=-1;
int stack[max];
void main()
{
int choice;
char ch;
clrscr();
do
{
printf("\n 1.push 2.pop 3.diplay \n");
printf("enter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default:printf("invalid choice\n");
}
printf("do u want to continue(y/n)");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y');
getch();
}

void push()
{
int item;
if(top==max-1)
{
printf("STACKOVERFLOW\n");
return;
}
printf(" Enter the elements to be inserted \n");
scanf("%d",&item);
top++;
stack[top]=item;
}

void pop()
{
if(top==-1)
{
printf("STACK UNDERFLOW\n");
return;
}
printf("the deleted element is %d\n",stack[top]);
top--;
}

void display()
{
int i;
if(top==-1)
{
printf("stack underflow\n");
return;
}
printf("STACK CONTENTS\n ");
for(i=top;i>=0;i--)
{
printf("%d\t", stack[i]);
}
}

*************************************OUTPUT*******************
1.push 2.pop 3.diplay
enter your choice 1
Enter the elements to be inserted
11
do u want to continue(y/n)y

1.push 2.pop 3.diplay


enter your choice 1
Enter the elements to be inserted
22
do u want to continue(y/n)y

1.push 2.pop 3.diplay


enter your choice 1
Enter the elements to be inserted
33
do u want to continue(y/n)y

1.push 2.pop 3.diplay


enter your choice 1

STACK OVERFLOW

do u want to continue(y/n)y

1.push 2.pop 3.diplay


enter your choice 3

STACK CONTENTS
***************
11 22 33
do u want to continue(y/n)y

1.push 2.pop 3.diplay


enter your choice 2
the deleted element is 33
do u want to continue(y/n)y

1.push 2.pop 3.diplay


enter your choice 2
the deleted element is 22
do u want to continue(y/n)y

1.push 2.pop 3.diplay


enter your choice 2
the deleted element is 11
do u want to continue(y/n)y

1.push 2.pop 3.diplay


enter your choice 2

STACK UNDERFLOW

do u want to continue(y/n)n
*/
EXPERIMENT NO: 5

/* 5) Write a C program to convert valid infix expression


to postfix */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void convert();
int preced(char);
char pop();
void push(char);
int length,top=-1,index=0;
char stack[20],infix[20],postfix[20],symbol;
void main()
{
clrscr();
printf("\nEnter infix expression\n");
scanf("%s",infix);
convert();
printf("Equivalent postfix expression=%s",postfix);
getch();
}

void convert()
{
int i,pos=0;
char temp;
push('#');
length=strlen(infix);
while(index<length)
{
symbol=infix[index];
switch(symbol)
{
case '(':push(symbol);
break;
case ')':temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(preced(stack[top])>=preced(symbol))
{
postfix[pos]=pop();
pos++;
}
push(symbol);
break;
default :postfix[pos]=symbol;
pos++;
}
index++;
}
while(top>0)
{
postfix[pos]=pop();
pos++;
}
}

int preced(char symbol)


{
int p;
switch(symbol)
{
case '^':p=3;
break;
case '*':
case '/':p=2;
break;
case '+':
case '-':p=1;
break;
case '(':p=0;
break;
case '#':p=-1;
break;
}
return(p);
}

void push(char symbol)


{
top++;
stack[top]=symbol;
}

char pop(void)
{
char item;
item=stack[top];
top--;
return(item);
}

/***********************************OUTPUT*******************

Enter infix expression


A+B*C
Equivalent postfix expression=ABC*+

*************************************************************/
EXPERIMENT NO: 6
/* 6) Write a C program to evaluate the valid postfix
expression */

#include<stdio.h>
#include<math.h>
#include<string.h>
int stack[10],value[10],top=-1,i=0;
char postfix[10];
void push(int);
void main()
{
int result;
clrscr();
printf("enter the postfix expression ");
scanf("%s",postfix);
while(postfix[i]!='\0')
{
if(isalpha(postfix[i]))
{
printf("enter the value of %c ",postfix[i]);
scanf("%d",&value[i]);
}
i++;
}
result=evaluate();
printf("Result=%d",result);
getch();
}

int evaluate()
{
int i=0,res;
char op1,op2,ch;
while(postfix[i]!='\0')
{
ch=postfix[i];
if(isalpha(ch))
{
push(value[i]);
}
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);
break;
case '-':push(op1-op2);
break;
case '*':push(op1*op2);
break;
case '/':push(op1/op2);
break;
case '^':push(pow(op1,op2));
break;
}
}
i++;
}
res=pop();
return(res);
}

void push(int num)


{
top++;
stack[top]=num;
}

int pop()
{
int item;
item=stack[top];
top--;
return(item);
}
/************************OUTPUT*********************
enter the postfix expression
abc*+
enter the value of a 1
enter the value of b 2
enter the value of c 3
Result=7
***************************************************/
EXPERIMENT NO:7
/* 7) Write a C program to simulate the working of linear
queue with the following operations
a)Insert
b)Delete
c)Display */

#include<stdio.h>
#include<conio.h>
#define MAX 3
void insert();
void del();
void display();
int queue[MAX],front=-1,rear=-1;
void main()
{
int choice;
char ch;
clrscr();
do
{
printf("1.Insert\t2.Delete\t3.Display\n");
printf("Enter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
default:printf("Invalid input!\n");
}
printf("Do u want to continue?\n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y'||ch=='Y');
getch();
}

void insert()
{
int item;
if(rear==MAX-1)
{
printf("Queue is full\n");
return;
}
if(front==-1)
front=rear=0;
else
rear++;
printf("Enter the element to be inserted\n");
scanf("%d",&item);
queue[rear]=item;
}

void del()
{
if(front==-1)
{
printf("Queue is empty\n");
return;
}
printf("The deleted element is %d\n",queue[front]);
if(front==rear)
front=rear=-1;
else
front++;
}

void display()
{
int i;
if(front==-1)
printf("Queue is empty\n");
else
{
printf("QUEUE CONTENTS\n");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
}

/*
1.Insert 2.Delete 3.Display
Enter ur choice
1
Enter the element to be inserted
11
Do u want to continue?
Y
1.Insert 2.Delete 3.Display
Enter ur choice
1
Enter the element to be inserted
22
Do u want to continue?
Y
1.Insert 2.Delete 3.Display
Enter ur choice
1
Enter the element to be inserted
33
Do u want to continue?
Y
1.Insert 2.Delete 3.Display
Enter ur choice
1
Queue is full
Do u want to continue?
Y
1.Insert 2.Delete 3.Display
Enter ur choice
3
QUEUE CONTENTS
11 22 33
Do u want to continue?
Y
1.Insert 2.Delete 3.Display
Enter ur choice
2
The deleted element is 11
Do u want to continue?
Y
1.Insert 2.Delete 3.Display
Enter ur choice
2
The deleted element is 22
Do u want to continue?
Y
1.Insert 2.Delete 3.Display
Enter ur choice
2
The deleted element is 33
Do u want to continue?
N

*/
EXPERIMENT NO: 8
/* 8) Write a C program to simulate the working of a
Circular queue with the following operations
a)insert b)delete c)display */

#include<stdio.h>
#include<conio.h>
#define MAX 3
void cqinsert();
void cqdel();
void cqdisplay();
int front=-1,rear=-1;
char queue[MAX];
void main()
{
int choice;
char ch;
clrscr();
do
{
printf("1.Insert\t2.Delete\t3.Display\n");
printf("Enter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:cqinsert();
break;
case 2:cqdel();
break;
case 3:cqdisplay();
break;
default:printf("Invalid input!\n");
}
printf("Do u want to continue?\n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y'||ch=='Y');
getch();
}

void cqinsert()
{
int item;
if( (front==0 && rear==MAX-1) || (front==rear+1) )
{
printf("Queue is full\n");
return;
}
printf("Enter the element to be inserted\n");
scanf("%d",&item);
if(front==-1)
front=rear=0;
else
if(rear==MAX-1)
rear=0;
else
rear++;
queue[rear]=item;
}
void cqdel()
{
if(front==-1)
{
printf("Queue is empty\n");
return;
}
printf("The deleted element is %d\n",queue[front]);
if(front==rear)
front=rear=-1;
else
if(front==MAX-1)
front=0;
else
front++;
}
void cqdisplay()
{
int i;
if(front==-1)
{
printf("Queue is empty\n");
return;
}
printf("Contents of the queue are\n");
if(front<=rear)
{
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}
else
{
for(i=front;i<=MAX-1;i++)
printf("%d\n",queue[i]);
for(i=0;i<=rear;i++)
printf("%d\n",queue[i]);
} }

/*
1.Insert 2.Delete 3.Display
Enter ur choice
1
Enter the element to be inserted
11
Do u want to continue?
y
1.Insert 2.Delete 3.Display
Enter ur choice
1
Enter the element to be inserted
22
Do u want to continue?
y
1.Insert 2.Delete 3.Display
Enter ur choice
1
Enter the element to be inserted
33
Do u want to continue?
y
1.Insert 2.Delete 3.Display
Enter ur choice
3
Contents of the queue are
11
22
33
Do u want to continue?
y
1.Insert 2.Delete 3.Display
Enter ur choice
2
The deleted element is 11
Do u want to continue?
y
1.Insert 2.Delete 3.Display
Enter ur choice
3
Contents of the queue are
22
33
Do u want to continue?
y
1.Insert 2.Delete 3.Display
Enter ur choice
1
Enter the element to be inserted
44
Do u want to continue?
y
1.Insert 2.Delete 3.Display
Enter ur choice
3
Contents of the queue are
22
33
44
Do u want to continue?
N

*/
EXPERIMENT NO: 9
/* 9) Write a C program to construct a singly linked list consist
of student information id,name,sem & perform the following
operations a)insert at front of the list b)insert at end of the
list c)insert at any required position d)delete student node
whose id ia read as input e)search a required student by reading
student id,if it is present update that student information f)
display all students information*/

#include<stdio.h>
void ins_front();
void ins_end();
void ins_pos();
void del();
void search();
void display();
struct NODE
{
int id,sem;
char name[10];
struct NODE *link;
};
typedef struct NODE node;
node *start=NULL,*temp,*new1,*cptr,*prev;
int i,choice;
char ch;
void main()
{
clrscr();
do
{
printf("1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search
6:display\n");
printf("enter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:ins_front();
break;
case 2:ins_end();
break;
case 3:ins_pos();
break;
case 4:del();
break;
case 5:search();
break;
case 6: display();
break;
}
printf("do u want to continue\n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y');
getch();
}
void ins_front()
{
new1=(node*) malloc(sizeof(node));
printf("enter student id,name,sem\n");
scanf("%d%s%d",&new1->id,new1->name,&new1->sem);
if(start==NULL)
{
new1->link=NULL;
start=new1;
}
else
{
new1->link=start;
start=new1;
}
//return(0);
}
/*return(0)*/

void ins_end()
{
new1=(node*) malloc(sizeof(node));
printf("enter student id,name,sem\n");
scanf("%d%s%d",&new1->id,new1->name,&new1->sem);
if(start==NULL)
{
new1->link=NULL;
start=new1;
}
temp=start;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=new1;
new1->link=NULL;
//return(0);
}

void ins_pos()
{
int pos,count;
new1=(node*) malloc(sizeof(node));
printf("enter student id,name,sem\n");
scanf("%d%s%d",&new1->id,new1->name,&new1->sem);
printf("enter the position to be insert\n");
scanf("%d",&pos);
if(pos==1)
{
new1->link=start;
start=new1;
}
count=2;
prev=start;
temp=start->link;
while(count<pos && temp!=NULL)
{
prev=temp;
temp=temp->link;
count++;
}
new1->link=temp;
prev->link=new1;
//return(0);
}

void del()
{
int key;
printf("enter the student id to be delete\n");
scanf("%d",&key);
if(start->id==key)
{
temp=start;
start=start->link;
free(temp);
}
prev=start;
temp=start->link;
while(temp!=NULL && temp->id!=key)
{
prev=temp;
temp=temp->link;
}
if(temp==NULL)
{
printf("student with that id does not exist\n");
//return 0;
}
prev->link=temp->link;
}

void search()
{
int key;
printf("enter the student id to be search\n");
scanf("%d",&key);
temp=start;
while(temp!=NULL)
{ if(temp->id==key)
{
printf("student with that id exist & his detals are\n");
printf("%d%s%d",temp->id,temp->name,temp->sem);
printf("do u want to update(y/n)\n");
ch=getche();
if(ch=='y')
{
printf("enter new id,name,sem\n");
scanf("%d%s%d",&temp->id,temp->name,& temp->sem);
}
// return;
}
temp=temp->link;

}
}

void display()
{
printf("STUDENTS INFORMATION\n");
printf("ID NAME SEM\n");
temp=start;
while(temp!=NULL)
{
printf("%d\t%s\t%d\n",temp->id,temp->name,temp->sem);
temp=temp->link;
}
}
/*
1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display
enter ur choice
1
enter student id,name,sem
11 DUVAN 3
do u want to continue
y
1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display
enter ur choice
1
enter student id,name,sem
22 PRAJWAL 4
do u want to continue
y
1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display
enter ur choice
6
STUDENTS INFORMATION
--------------------
ID NAME SEM
---------------------
22 PRAJWAL 4
11 DUVAN 3
do u want to continue
y
1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display
enter ur choice
3
enter student id,name,sem
33 SACHIN 3
enter the position to be insert
2
do u want to continue
y
1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display
enter ur choice
6
STUDENTS INFORMATION
ID NAME SEM
22 PRAJWAL 4
33 SACHIN 3
11 DUVAN 3
do u want to continue
n
*/
EXPERIMENT NO: 10
/* 10) Write a C program using dynamic variables &
pointers to construct a stack using singly linked
list */

#include<stdio.h>
void push();
void pop();
void display();
struct NODE
{
int info;
struct NODE *link;
};
typedef struct NODE node;
node *top=NULL,*new1,*temp;
void main()
{
char ch;
int choice;
clrscr();
do
{
printf("1:push\t2:pop\t3:display\n");
printf("enter u r choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
}
printf("do u want to continue\n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y');
getch();
}

void push()
{
new1=(node*)malloc(sizeof(node));
printf("enter an element to be push\n");
scanf("%d",&new1->info);
new1->link=top;
top=new1;
}

void pop()
{
if(top==NULL)
printf("STACK UNDERFLOW\n");
else
{
printf("popped element=%d\n",top->info);
temp=top;
top=top->link;
free(temp);
}
}

void display()
{
if(top==NULL)
printf("STACK IS EMPTY\n");
else
{
printf("STACK CONTENTS\n");
temp=top;
while(temp!=NULL)
{
printf("%d\n",temp->info);
temp=temp->link;
}
}
}

*
******************************OUTPUT**************************
1:push 2:pop 3:display
enter u r choice
1
enter an element to be push
11
do u want to continue
y
1:push 2:pop 3:display
enter u r choice
1
enter an element to be push
22
do u want to continue
y
1:push 2:pop 3:display
enter u r choice
3
STACK CONTENTS
22
11
do u want to continue
y
1:push 2:pop 3:display
enter u r choice
2
popped element=22
do u want to continue
y
1:push 2:pop 3:display
enter u r choice
2
popped element=11
do u want to continue
y
1:push 2:pop 3:display
enter u r choice
2
STACK UNDERFLOW
do u want to continue
n
*/
EXPERIMENT NO: 11

11) Write a C program using dynamic variables & pointers to


construct queue using singly linked list a.Insert
b.Delete c.Display*/

#include<stdio.h>
void insert();
void del();
void display();
struct NODE
{
int info;
struct NODE *link;
};
typedef struct NODE node;
node *front=NULL,*rear=NULL,*temp,*new1;
void main()
{
int choice;
char ch;
clrscr();
do
{
printf("1:insert\t2:delete\t3:display\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
}
printf("do u want to continue\n");
fflush(stdin);
ch=getchar();
}
while(ch=='y');
getch();
}

void insert()
{
new1=(node*)malloc(sizeof(node));
printf("enter an element\n");
scanf("%d",&new1->info);
new1->link=NULL;
if(front==NULL)
front=rear=new1;
else
{
rear->link=new1;
rear=new1;
}
}

void del()
{
if(front==NULL)
{
printf("QUEUE IS EMPTY\n");
return;
}
printf("deleted element=%d",front->info);
temp=front;
if(front==rear)
front=rear=NULL;
else
front=front->link;
free(temp);
}

void display()
{
if(front==NULL)
{
printf("QUEUE IS EMPTY\n");
return;
}
printf("QUEUE CONTENTS\n");
temp=front;
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
/*******************************OUTPUT************************
1:insert 2:delete 3:display
1
enter an element
11
do u want to continue
y
1:insert 2:delete 3:display
1
enter an element
22
do u want to continue
y
1:insert 2:delete 3:display
3
QUEUE CONTENTS
11 22
do u want to continue
y
1:insert 2:delete 3:display
2
deleted element=11
do u want to continue
y
1:insert 2:delete 3:display
2
deleted element=22
do u want to continue
y
1:insert 2:delete 3:display
2
QUEUE IS EMPTY
do u want to continue
n

*/
EXPERIMENT NO:12
/* 12) Write a C program to support the following operations on a
doubly linked list 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 contents of the list */

#include<stdio.h>
#include<alloc.h>
void create();
void insert();
void del();
void display();
struct NODE
{
struct NODE *llink;
int info;
struct NODE *rlink;
};
typedef struct NODE node;
node *left=NULL,*right=NULL,*new1,*temp;
int choice,key;
char ch;
void main()
{
clrscr();
create();
do
{
printf("1:insert\n2:delete\n3:display\n");
printf("enter u r choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
}
printf("do u want to continue\n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y');
getch();
}

void create()
{
do
{
new1=(node*)malloc(sizeof(node));
printf("enter an element\n");
scanf("%d",&new1->info);
new1->llink=NULL;
new1->rlink=NULL;
if(left==NULL)
{
left=right=new1;
}
else
{
new1->rlink=left;
left->llink=new1;
left=new1;
}
printf("do u want to add one more element\n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y');
}

void insert()
{
printf("enter key value \n");
scanf("%d",&key);
if(left->info==key)
{
new1=(node*)malloc(sizeof(node));
printf("enter an element\n");
scanf("%d",&new1->info);
new1->rlink=left;
left->llink=new1;
new1->llink=NULL;
left=new1;
return;
}
temp=left;
while(temp!=NULL && temp->info!=key)
temp=temp->rlink;
if(temp==NULL)
printf("key element not found");
else
{
new1=(node*)malloc(sizeof(node));
printf("enter an element\n");
scanf("%d",&new1->info);
new1->llink=temp->llink;
new1->rlink=temp;
temp->llink->rlink=new1;
temp->llink=new1;
}
}

void del()
{
if(left==NULL)
{
printf("list is empty\n");
return;
}

printf("enter key value \n");


scanf("%d",&key);
temp=left;
while(temp!=NULL && temp->info!=key)
temp=temp->rlink;

if(temp==NULL)
{
printf("key element not found");
return;
}
printf("deleted element=%d",temp->info);

if(left==right)
{
left=right=NULL;
free(temp);
return;
}

if(temp==left)
{
left=left->rlink;
left->llink=NULL;
free(temp);
return;
}

if(temp==right)
{
right=right->llink;
right->rlink=NULL;
free(temp);
return;
}
else
{
(temp->llink)->rlink=temp->rlink;
(temp->rlink)->llink=temp->llink;
free(temp);
}
}

void display()
{
if(left==NULL)
{
printf("list is empty\n");
return;
}
temp=left;
while(temp!=NULL)
{
printf("%d---->",temp->info);
temp=temp->rlink;
}
}
/*
*******************************OUTPUT*************************
enter an element
10
do u want to add one more element
y
enter an element
20
do u want to add one more element
y
enter an element
30
do u want to add one more element
n
1:insert
2:delete
3:display
enter u r choice
3
30---->20---->10---->
do u want to continue
y

1:insert
2:delete
3:display
enter u r choice
1
enter key value
20
enter an element
15
do u want to continue
y
1:insert
2:delete
3:display
enter u r choice
3
30---->15---->20---->10---->
do u want to continue
y
1:insert
2:delete
3:display
enter u r choice
2
enter key value
20
deleted element=20
do u want to continue
y
1:insert
2:delete
3:display
enter u r choice
3
30---->15---->10---->
do u want to continue
n

*/
EXPERIMENT NO: 13

/* 13) Write a C program


a) To construct a binary tree of integers
b) To traverse the tree in preorder, inorder, postorder
c) To display the elements in the tree
*/

#include<stdio.h>
struct TREE
{
struct TREE *lptr;
int info;
struct TREE *rptr;
};
typedef struct TREE tree;
tree *root=NULL,*temp,*prev,*cptr,*new1;
void create();
void preorder(tree *);
void inorder(tree *);
void postorder(tree *);
void main()
{
int choice;
char ch;
clrscr();
do
{
printf("1:CREATE\n2:PREORDER\n3:INORDER\n4:POSTORDER\n");
printf("ENTER U R CHOICE\n");
scanf("%d",&choice);
switch(choice)
{
case 1:create();
break;
case 2:preorder(root);
break;
case 3:inorder(root);
break;
case 4:postorder(root);
break;
}
printf("DO U WANT TO CONTINUE\n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y');
getch();
}
void create()
{
int data;
root=(tree*)malloc(sizeof(tree));
printf("enter an element\n");
scanf("%d",&root->info);
root->lptr=root->rptr=NULL;
printf("enter the data & 0 to stop\n");
scanf("%d",&data);
while(data!=0)
{
new1=(tree*)malloc(sizeof(tree));
new1->info=data;
new1->lptr=new1->rptr=NULL;
cptr=root;
while(cptr!=NULL)
{
prev=cptr;
cptr=data<cptr->info ? cptr->lptr : cptr->rptr;
}
if(data<prev->info)
prev->lptr=new1;
else
prev->rptr=new1;
printf("enter next data\n");
scanf("%d",&data);
}
}
void preorder(tree *temp)
{
if(temp!=NULL)
{
printf("%d\t",temp->info);
preorder(temp->lptr);
preorder(temp->rptr);
}
}
void inorder(tree *temp)
{
if(temp!=NULL)
{
inorder(temp->lptr);
printf("%d\t",temp->info);
inorder(temp->rptr);
}
}
void postorder(tree *temp)
{
if(temp!=NULL)
{
postorder(temp->lptr);
postorder(temp->rptr);
printf("%d\t",temp->info);
}
}
/*
********************************OUTPUT***********************
1:CREATE
2:PREORDER
3:INORDER
4:POSTORDER
ENTER U R CHOICE
1
enter an element
10
enter the data & 0 to stop
5
enter next data
20
enter next data
25
enter next data
3
enter next data
15
enter next data
0
DO U WANT TO CONTINUE
y
1:CREATE
2:PREORDER
3:INORDER
4:POSTORDER
ENTER U R CHOICE
2
10 5 3 20 15 25
DO U WANT TO CONTINUE
y
1:CREATE
2:PREORDER
3:INORDER
4:POSTORDER
ENTER U R CHOICE
3
3 5 10 15 20 25
DO U WANT TO CONTINUE
y
1:CREATE
2:PREORDER
3:INORDER
4:POSTORDER
ENTER U R CHOICE
4
3 5 15 25 20 10
DO U WANT TO CONTINUE
N
*/
EXPERIMENT NO:14 (a)
/* 14.
a) Write a recursive C program for searching an element
on a given list using binary search method
*/

#include<stdio.h>
void main()
{
int a[10],n,p,i,key,low,high;
clrscr();
printf("enter number of elements\n");
scanf("%d",&n);
printf("enter the elements in ascending order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter key element\n");
scanf("%d",&key);
low=0;
high=n-1;
p=search(a,key,low,high);
if(p==-1)
printf("unsucessful search");
else
printf("sucessful search,key element is present at the
position %d",p+1);
getch();
}

int search(int a[],int key,int low,int high)


{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
return(mid);
else
if(key<a[mid])
return(search(a,key,low,mid-1));
else
return(search(a,key,mid+1,high));
}
return(-1);
}

/******************************OUTPUT*************************
enter number of elements
6
enter the elements in ascending order
10
15
20
25
30
35
enter key element
15
sucessful search,key element is present at the position 2

*************************************************************/
EXPERIMENT NO: 14(b)
/* 14.b)Write recursive programs for solving the Towers
of Honoi problem */

#include<stdio.h>
void move(int,char,char,char);
void main()
{
int n;
clrscr();
printf("enter number of discs\n");
scanf("%d",&n);
move(n,'s','t','d');
getch();
}

void move(int n,char src,char temp,char dest)


{
if(n==1)
{
printf(" \n1 %c--->%c\n",src,dest);
return;
}
move(n-1,src,dest,temp);
printf(" \n%d %c--->%c\n",n,src,dest);
move(n-1,temp,src,dest);
return;
}
/*********************************OUTPUT*********************
enter number of discs
3

1 s--->d

2 s--->t

1 d--->t

3 s--->d

1 t--->s

2 t--->d

1 s--->d
*/

You might also like