Sei sulla pagina 1di 40

The VTU Blogger

Data Structures Lab Programs


PROGRAM1.WRITE PROGRAM TO CREATE A SEQUENTIAL FILE WITH
ATLEAST 5 RECORDS,EACH RECORD HAVING THE STRUCTURE SHOWN BELOW
:
USN
NAME MARKS1
MARKS2
MARKS3
WRITE NECESSARY FUNTIONS
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 DEMONSTRTED.
#include<stdio.h>
#include<stdlib.h>
struct student
{
int usn;
char name[10];
int m1,m2,m3;
};
display(FILE *fp)
{
struct student s;
printf("\n \n" "USN \t name \t m1 \t m2 \t m3 \t \n");
for(;;)
{
fscanf(fp,"%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
if(feof(fp))
break;
printf("%d\t %s\t %d\t %d\t %d\n",s.usn,s.name,s.m1,s.m2,s.m3);
}
}
int search(FILE *fp,int key)
{
struct student s;
for(;;)
{
fscanf(fp,"%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
if(feof(fp))
break;
if(key==s.usn)
return 1;
}
return 0;
}
main()

FILE *fp;
struct student s;
int flag,key,ch;
for(;;)
{
printf("\n 1. Insert");
printf("\n 2. Display");
printf("\n 3. Search");
printf("\n 4. Exit");
printf("\n Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: fp=fopen("student","a+");
if(fp==NULL)
{
printf("\n File can't be created");
exit(0);
}
printf("Enter student usn,name,m1,m2,m3 \n");
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);
break;
case 2:fp=fopen("stud22","r");
if(fp==NULL)
{
printf("\n File can't be opened");
exit(0);
}
display(fp);
fclose(fp
);
break;
case 3:fp=fopen("stud22","r");
if(fp==NULL)
{
printf("File can't be opened");
exit(0);
}
printf("\n Enter the key to be searched");
scanf("%d",&key);
flag=search(fp,key);
if(flag==1)
printf("\n Successful search");
else
printf("\n Unsuccessful search");
fclose(fp);
break;
default:exit(0);
}
}

OUTPUT :
1. Insert
2. Display
3. Search
4. Exit
Enter your choice 1
Enter student usn,name,m1,m2,m3
10
raj
78
90
89
1. Insert
2. Display
3. Search
4. Exit
Enter your choice 1
Enter student usn,name,m1,m2,m3
20
rahul
67
56
40
1. Insert
2. Display
3. Search
4. Exit
Enter your choice 2
USN
10
20

name
raj
rahul

m1
78
67

m2
90
56

1. Insert
2. Display
3. Search
4. Exit
Enter your choice 3
Enter the key to be searched 30
Unsuccessful search
1. Insert
2. Display
3. Search

m3
89
40

4. Exit
Enter your choice 3
Enter the key to be searched 10
Successful search
1. Insert
2. Display
3. Search
4. Exit
Enter your choice 4
PROGRAM 2. WRITE AND DEMONSTRATE THE FOLLOWING C FUNCTIONS :
A. newStrCpy THAT DOES THE SAME JOB AS strcpy
B. newSrtrCat THAT DOES THE SAME JOB AS strcat WITHOUT
USING LIBRARY FUNCTIONS.
#include<stdio.h>
#include<string.h>
char *newstrcpy(char *str2,char *str1)
{
int i=0;
while(str1[i]!='\0')
{
str2[i]=str1[i];
i++;
}
str2[i]='\0';
return str2;
}
char *newstrcat(char *str3,char *str4)
{
int i=0,j=0;
while(str3[i]!='\0')
i++;
while(str4[j]!='\0')
{
str3[i]=str4[j];
i++;
j++;
}
str3[i]='\0';
return str3;
}
main()
{
char *str3,*str4;
char str1[20],str2[20];
str3=str1;
str4=str2;
printf("\n Enter the string to be copied \n");

scanf(%s,str1);
str4=newstrcpy(str4,str3);
printf("\n The copied string is %s \n",str4);
printf("\n Enter the first string \n");
scanf(%s,str3);
printf("\n Enter the second string \n");
scanf(%s,str4);
str3=newstrcat(str3,str4);
printf("\n The combined string is %s \n",str3);
}

OUPTUT :
Enter the string to be copied
AIeMS bangalore
The copied string is AIeMS bangalore
Enter the first string
Information
Enter the second string
science
The combined string is Informationscience
PROGRAM 3. WRITE A C PROGRAM, WHICH ACCEPTS THE INTERNET
PROTOCOL (IP) ADDRESS IN DECIMAL DOT FORMAT(EX. 153.18.8.105)
AND CONVERTS IT ITO32-BIT LONG INTERGER(EX 25668095849) USING
strtok FUNCTION AND UNIONS.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
union ip
{
unsigned char chaddr[4];
unsigned long numaddr;
};
main()
{
char ipaddr[16];
union ip addr;
char *par;
printf("\n Enter the IP address \n");
gets(ipaddr);
par=strtok(ipaddr,".");
addr.chaddr[3]=strtol(par,(char**)NULL,10);
par=strtok(NULL,".");

addr.chaddr[2]=strtol(par,(char**)NULL,10);
par=strtok(NULL,".");
addr.chaddr[1]=strtol(par,(char**)NULL,10);
par=strtok(NULL,".");
addr.chaddr[0]=strtol(par,(char**)NULL,10);
printf("\n The IP address is %d %d %d %d
\n",addr.chaddr[3],addr.chaddr[2],addr.chaddr[1],addr.chaddr[0]);
printf("\n The Long number is %lu \n",addr.numaddr);
}

OUTPUT :
Enter the IP address
153.18.8.105
The IP address is 153.18.8.105
The Long number is 2568095849

PROGRAM 4. WRITE A C PROGRAM TO CONSTRUCT A STACK OF INTEGERS


AND TO PERFROM THE FOLLOWING OPERATIOS ON IT :
A. PUSH
B. POP
C. DISPLAY
THE PROGRAM SHOULD PRINT APPROPRIATE MESSAGES FOR
STACKOVERFLOW AND STACK UNDERFLOW
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
int s[SIZE],top=-1;
push()
{
int item;
if(top==SIZE-1)
printf("\n Stack overflow");
else
{
printf("\n Enter the item to be inserted ");
scanf("%d",&item);
top=top+1;;
s[top]=item;
printf("\n The item %d is successfully inserted",item);
}
}
pop()
{

if(top==-1)
printf("\n Stack underflow");
else
{
printf("\n The popped item is
top=top-1;
}

%d",s[top]);

display()
{
int i;
if(top==-1)
printf("\n Stack is empty");
else
{
printf("\n The content of stack is \n");
for(i=top;i>=0;i--)
printf("%d \n",s[i]);
}
}
{

main()
int choice;
for(;;)
{
printf("\n 1.Push");
printf("\n 2.Pop");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Enter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default:exit(0);
}

OUTPUT :
1.Push
2.Pop
3.Display
4.Exit
Enter your choice 1

Enter the item to be inserted 20


The item 20 is successfully inserted
1.Push
2.Pop
3.Display
4.Exit
Enter your choice 1
Enter the item to be inserted 30
The item 30 is successfully inserted
1.Push
2.Pop
3.Display
4.Exit
Enter your choice 1
Enter the item to be inserted 40
The item 40 is successfully inserted
1.Push
2.Pop
3.Display
4.Exit
Enter your choice 3
The content of stack is
40
30
20
10
1.Push
2.Pop
3.Display
4.Exit
Enter your choice 2
The popped item is 40
1.Push
2.Pop
3.Display
4.Exit
Enter your choice 4
PROGRAM 5. WRITE A C PROGRAM TO CONVERT AND PRINT A GIVEN
VALID

PARENTESIZED
INFIX
ARITHMETIC
EXPRESSION
TO
POSTFIX
EXPRESSION.
THE EXPRESSION CONSISTS OF SINGLE CHARACTER OPERANDS AND THE
BINARY OPERATORS + , - , * , & /
#include<stdio.h>
#include<stdlib.h>

int top=-1;
char s[50];
push(char symbol)
{
s[++top]=symbol;
}
char pop()
{
return s[top--];
}
int preced(char symbol)
{
int value;
switch(symbol)
{
case $:
Case ^:value=3;
case '/':
case '*':value=2;
case '+':
case '-':value=1;
}
return value;
}
int infixtopostfix(char infix[10],char postfix[10])
{
int i,j=0;
char symbol,temp;
for(i=0;infix[i]!='\0';i++)
{
symbol=infix[i];
if(isalnum(symbol))
postfix[j++]=symbol;
else
{
switch(symbol)
{
case '(': push(symbol);
break;

case ')': temp=pop();


while(temp!='(')
{
postfix[j++]=temp;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
if(top==-1 || s[top]== ()
push(symbol);
else
{
while(preced(s[top])>=preced(symbol))
postfix[j++]=pop();
push(symbol);
}
break;
default: printf("invalid expression");
return 0;
}
}
}
while(top!=-1)
postfix[j++]=pop();
postfix[j]='\0';
return 1;
}
main()
{
int flag;
char infix[15],postfix[15];
printf("\n Enter the infix expression : ");
scanf("%s",infix);
flag=infixtopostfix(infix,postfix);
if(flag==1)
printf("\n postfix expression is : %s\n\n",postfix);
else
printf("\n error");
}
OUTPUT :
Enter the infix expression :
(a+b)*c

postfix expression is :
ab+c*
Enter the infix expression :
a+b*c
postfix expression is : abc*+

PROGRAM 6.
WRITE
A
C
PROGRAM
TO
EVALUTAE
A
VALID
SUFFIX/POSTFIX USING STACK. ASSUMING THAT THE SUFFIX/POSTFIX
EXPRESSION IS READ AS A SINGLE LINE CONSISTING OF NON-NEGATIVE
SINGLE DIGIT OPERANDS AND BINARY ARITHMETIC OPERATORS. THE
ARTHMETIC OPERATORS ARE +(ADD), -(SUBTRACT), *(MULTIPLY),AND /
(DIVIDE).
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int top=-1;
float s[50];
float operate(float opr1,float opr2,char symbol )
{
float res;
switch(symbol)
{
case '+' :res = opr1+opr2;
break;
case '-' :res = opr1-opr2;
break;
case '*' :res = opr1*opr2;
break;
case '/' :res = opr1/opr2;
break;
case '^' :res = pow(opr1,opr2);
break;
}
return res;
}
push(float symbol)
{
s[++top]= symbol;
}
float pop()
{
return(s[top--]);
}

float eval(char postfix[50])


{
int i;
char symbol;
float opr1,opr2,value,res;
for(i=0;postfix[i]!='\0';i++)
{
symbol=postfix[i];
if(isdigit(symbol))
push(symbol-48);
else if(isalpha(symbol))
{
printf("\n enter the value for %c
scanf("%f",&value);
push(value);
}
else
{
opr2=pop();
opr1=pop();
res=operate(opr1,opr2,symbol);
push(res);
}
}
return(pop());
}

",symbol);

main()
{
char postfix[80];
float res;
printf("\n Enter The Postfix Expression \n");
gets(postfix);
printf("The Given Expression is :");
puts(postfix);
res=eval(postfix);
printf("\nThe value of the expression is %f\n" ,res);
}

OUTPUT :
Enter The Postfix Expression
45+
The Given Expression is :45+
The value of the expression is
Enter The Postfix Expression
abc*+
The Given Expression is :abc*+
enter the value for a
enter the value for b
enter the value for c

1
2
3

9.000000

The value of

the expression is

7.000000

PROGRAM 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<stdlib.h>
#define SIZE 5
int q[SIZE],f=0,r=-1;
qinsert()
{
int item;
if(r==SIZE-1)
printf("\n Queue is full");
else
{
printf("\n Enter the element to be inserted ");
scanf("%d",&item);
r=r+1;
q[r]=item;
printf("\n %d is inserted succssfully",item);
}
}
{

}
{

qdelete()
if (f>r)
{
printf("\n Queue is empty");
f=0;
r=-1;
}
else
{
printf("\n Element deleted is %d",q[f]);
f=f+1;
}
qdisplay()
int i;
if(f>r)
printf("\n Queue is empty");
else

printf("\n The content of queue is :\n");


for(i=f;i<=r;i++)
printf("%d \t",q[i]);

main()
{
int choice;
for(;;)
{
printf("\n 1. Insert ");
printf("\n 2. Delete ");
printf("\n 3. Display");
printf("\n 4. Exit ");
printf("\n Enter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:qinsert();
break;
case 2:qdelete();
break;
case 3:qdisplay();
break;
default:exit(0);
}
}

OUTPUT :
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice :1
Enter the element to be inserted 10
10 is inserted succssfully
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice :1
Enter the element to be inserted 20
20 is inserted succssfully
1. Insert
2. Delete

3. Display
4. Exit
Enter your choice :1
Enter the element to be inserted 30
30 is inserted succssfully
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice :3
The content of queue is :
10
20
30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice :2
Element deleted is 10
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice :4
PROGRAM 8.WRITE A C PROGRAM
CIRCULAR QUEUE OF INTEGERS
FOLLOWING OPERATIONS :
A. INSERT
B. DELETE
C. DISPLAY

TO SIMULATE THE WORKING OF


USING AN ARRAY.PROVIDE THE

#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
int f=-1,r=-1,cq[SIZE];
{

cqinsert()
int item;
if(f==(r+1)%SIZE)
printf("\n Circular queue is full");
else
{
printf("\n Enter the item to be inserted:");
scanf("%d",&item);

r=(r+1)%SIZE;
cq[r]=item;
if(f==-1)
f=f+1;
printf("\n Item %d is inserted successfully",item);

}
cqdelete()
{

if(f==-1)
printf("\n Circular queue is empty");
else
{
printf("\n Element deleted is %d",cq[f]);
if(f==r)
f=r=-1;
else
f=(f+1)%SIZE;
}

cqdisplay()
{
int i;
if(f==-1)
printf("\n Circular queue is empty");
else if (f<=r)
{
printf("\n The content of circular queue is :\n");
for(i=f;i<=r;i++)
printf("%d \t",cq[i]);
}
else
{
printf("\n The content of circular queue is :\n");
for(i=f;i<SIZE;i++)
printf("%d \t",cq[i]);
for(i=0;i<=r;i++)
printf("%d\t",cq[i]);
}
}
main()
{
int choice;
for(;;)
{
printf("\n 1.Insert ");
printf("\n 2.Delete ");
printf("\n 3.Display ");
printf("\n 4.Exit ");
printf("\n Enter your choice:");
scanf("%d",&choice);

switch(choice)
{
case 1:cqinsert();
break;
case 2:cqdelete();
break;
case 3:cqdisplay();
break;
default:exit(0);
}
}
}

OUTPUT :
Enter the item to be inserted:40
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the item to be inserted:10
Item 50 is inserted successfully
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the item to be inserted:20
Item 20 is inserted successfully
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the item to be inserted:30
Item 30 is inserted successfully
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the item to be inserted:40

Item 40 is inserted successfully


1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the item to be inserted:50
Item 50 is inserted successfully
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
The content of circular queue is :
10
20
30
40
50
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
Element deleted is 10
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the item to be inserted:60
Item 60 is inserted successfully
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
The content of circular queue is :
20
30
40
50
60
1.Insert
2.Delete
3.Display

4.Exit
Enter your choice:4

PROGRAM 9. WRITE A C PROGRAM USING DYNAMIC VARAIBLES AND


POINTERS, TO CONSTRUCT A SINGLY LINKED LIST CONSISTS OF THE
FOLLOWING IN EACH NODE :
STUDENTID(int) STUDENTNAME(string) & SEM(int)
THE OPERATIONS TO BE SUPPORTED ARE :
A. THE INSERTION OPERATION
- AT THE FRONT OF THE LIST
- AT THE BACK OF THE LIST
- AT ANY POSITION IN THE LIST
B. DELETING A NODE BASED ON THE STUDENT ID.
C. SEARCHING A NODE BASED ON THE STUDENT ID AND UPDATE THE
INFORMATIONCONTENT. IF THE NODE IS NOT PRESENT DISPLAY
ERROR MESSAGE
D. DISPLAYING ALL THE NODES IN THE LIST
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
struct student
{
int id;
char name[10];
int sem;
struct student *link;
};
typedef struct student* NODE;
NODE insertfront(int id,char name[15],int sem,NODE first)
{
NODE temp;
temp=(NODE)malloc(sizeof(struct student));
temp->id=id;
strcpy(temp->name,name);
temp->sem=sem;
temp->link=first;
return temp;
}
NODE insertrear(int id,char name[15],int sem,NODE first)
{
NODE temp,cur;
temp=(NODE)malloc(sizeof(struct student));
temp->id=id;
strcpy(temp->name,name);
temp->sem=sem;
temp->link=NULL;
if(first==NULL)

return temp;
cur=first;
while(cur->link!=NULL)
cur=cur->link;
cur->link=temp;
return first;
}
NODE insertpos(int id,char name[15],int sem,int pos,NODE first)
{
NODE temp,prev,cur;
int i;
temp=(NODE)malloc(sizeof(struct student));
temp->id=id;
strcpy(temp->name,name);
temp->sem=sem;
temp->link=NULL;
if(first==NULL && pos==1)
return temp;
if(pos==1)
{
temp->link=first;
return temp;
}
prev=NULL;
cur=first;
for(i=1;i<pos && cur!=NULL;i++)
{
prev=cur;
cur=cur->link;
}
if(i==pos)
{
prev->link=temp;
temp->link=cur;
return first;
}
else
printf("\nPosition invalid");
return first;
}
NODE deleteid(int id,NODE first)
{
NODE temp,cur,prev,next;
if(first==NULL)
{
printf("\nList is empty");
return NULL;
}
if(id==first->id)
{
temp=first;
first=first->link;

printf("\nRecord deleted is %d\t%s\t%d\n",temp->id,temp>name,temp->sem);


free(temp);
return first;
}
prev=NULL;
cur=first;
while(cur!=NULL&&cur->id!=id)
{
prev=cur;
cur=cur->link;
}
next=cur->link;
if(cur==NULL)
{
printf("\nRecord %d not present",id);
return first;
}
prev->link=next;
printf("\nRecord deleted is %d\t%s\t%d\n",cur->id,cur->name,cur>sem);
free(cur);
return first;
}
int search(int id,NODE first)
{
NODE temp;
char name[15];
int sem;
temp=first;
while(temp!=NULL)
{
if(id==temp->id)
{
printf("\nEnter the values of name and sem : ");
scanf("%s%d",name,&sem);
strcpy(temp->name,name);
temp->sem=sem;
return 1;
}
temp=temp->link;
}
return 0;
}
{

display(NODE first)
NODE temp;
if(first==NULL)
printf("\nList is empty");
else
{
temp=first;
printf("\nID\tNAME\t\tSEM\n");

while(temp!=NULL)
{
printf("%d\t%s\t\t%d\n",temp->id,temp->name,temp->sem);
temp=temp->link;
}

}
main()
{

NODE first=NULL;
int ch,id,sem,flag=0,pos;
char name[15];
for(;;)
{

printf("\n1.Insert(front)\n2.Insert(rear)\n3.Insert(position)\n4.Del
ete\n5.Search\n6.Display\n7.Exit");
printf("\nEnter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the id,name and sem : \n");
scanf("%d%s%d",&id,name,&sem);
first=insertfront(id,name,sem,first);
break;
case 2:
printf("\nEnter the id,name and sem : \n");
scanf("%d%s%d",&id,name,&sem);
first=insertrear(id,name,sem,first);
break;
case 3:
printf("\nEnter the id,name and sem : \n");
scanf("%d%s%d",&id,name,&sem);
printf("\nEnter the position : ");
scanf("%d",&pos);
first=insertpos(id,name,sem,pos,first);
break;
case 4:
printf("\nEnter the student id : ");
scanf("%d",&id);
first=deleteid(id,first);
break;
case 5:
printf("\nEnter the student id : ");
scanf("%d",&id);
flag=search(id,first);
if(flag==1)
printf("\nSuccessful search and updated");
else

printf("\nUnsuccessful search");
break;
case 6:
display(first);
break;
default:exit(0);
}
}

OUTPUT :
1.Insert(front)
2.Insert(rear)
3.Insert(position)
4.Delete
5.Search
6.Display
7.Exit
Enter your choice :1
Enter the id,name and sem
10
rahul
3
1.Insert(front)
2.Insert(rear)
3.Insert(position)
4.Delete
5.Search
6.Display
7.Exit
Enter your choice :1
Enter the id,name and sem
20
rani
3
1.Insert(front)
2.Insert(rear)
3.Insert(position)
4.Delete
5.Search
6.Display
7.Exit
Enter your choice :2
Enter the id,name and sem
30
prem

4
1.Insert(front)
2.Insert(rear)
3.Insert(position)
4.Delete
5.Search
6.Display
7.Exit
Enter your choice :6
ID
NAME
SEM
20
rani 3
10
rahul 3
30
prem 4
1.Insert(front)
2.Insert(rear)
3.Insert(position)
4.Delete
5.Search
6.Display
7.Exit
Enter your choice :7

PROGRAM10. 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 APPROPRIATE MESSAGES FOR STACK OVERFLOW AND
UNDERFLOW
#include<stdio.h>
#include<malloc.h>

#include<stdlib.h>
#define SIZE 5
int count=0;
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE push(int item,NODE first)
{
NODE temp;
if(count>=SIZE)
{
printf("\n Stack Overflow\n");
return first;
}
else
{
count++;
temp=(NODE)malloc(sizeof(struct node));
temp->info=item;
temp->link=first;
printf("\n %d inserted successfully",item);
return temp;
}
}
NODE pop(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("\n Stack underflow\n");
return NULL;
}
temp=first;
first=first->link;
printf("\n Element poped is %d",temp->info);
free(temp);
count--;
return first;
}
display(NODE first)
{
NODE temp;
if(first==NULL)
printf("\n Stack is empty\n");
else
{
printf("\n The content of stack is:\n");

temp=first;
while(temp!=NULL)
{
printf("%d \n",temp->info);
temp=temp->link;
}
}

main()
{
int choice,item;
NODE first=NULL;
for(;;)
{
printf("\n 1.Push \n");
printf("\n 2.Pop \n");
printf("\n 3.Display \n");
printf("\n 4.Quit \n");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n Enter the item to be inserted:");
scanf("%d",&item);
first=push(item,first);
break;
case 2:first=pop(first);
break;
case 3:display(first);
break;
default:exit(0);
}
}

OUTPUT :
1.Push
2.Pop
3.Display
4.Exit
Enter your choice 1
Enter the item to be inserted 20
The item 20 is successfully inserted
1.Push
2.Pop
3.Display
4.Exit

Enter your choice 1


Enter the item to be inserted 30
The item 30 is successfully inserted
1.Push
2.Pop
3.Display
4.Exit
Enter your choice 1
Enter the item to be inserted 40
The item 40 is successfully inserted
1.Push
2.Pop
3.Display
4.Exit
Enter your choice 3
The content of stack is
40
30
20
10
1.Push
2.Pop
3.Display
4.Exit
Enter your choice 2
The popped item is 40
1.Push
2.Pop
3.Display
4.Exit
Enter your choice 4
PROGRAM11. 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 APPROPRIATE MESSAGES FOR QUEUE FULL & QUEUE
EMPTY
#include<stdio.h>
#include<malloc.h>

#include<stdlib.h>
#define SIZE 5
int count=0;
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE qdelete(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("\n Queue is empty");
return NULL;
}
temp=first;
first=first->link;
printf("\n Element deleted is %d",temp->info);
free(temp);
count=count-1;
return first;
}
{

display(NODE first)
NODE temp;
if(first==NULL)
printf("\n Queue is empty \n");
else
{
printf("\n The content of queue is :\n");
temp=first;
while(temp!=NULL)
{
printf("%d \n",temp->info);
temp=temp->link;
}
}

NODE qinsert(int item,NODE first)


{
NODE cur,temp;
if(count>=SIZE)
{
printf("\n Queue is full");
return first;
}
count=count+1;
temp=(NODE)malloc(sizeof(struct node));
temp->info=item;
temp->link=NULL;

if(first==NULL)
return temp;
cur=first;
while(cur->link!=NULL)
cur=cur->link;
cur->link=temp;
printf("\n %d inserted successfully",item);
return first;
}
main()
{
int choice,item;
NODE first=NULL;
for(;;)
{
printf("\n 1.Insert \n");
printf("\n 2.Delete \n");
printf("\n 3.Display \n");
printf("\n 4.Quit \n");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n Enter the item to be inserted:");
scanf("%d",&item);
first=qinsert(item,first);
break;
case 2:first=qdelete(first);
break;
case 3:display(first);
break;
default:exit(0);
}
}
}

OUTPUT :
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice :1
Enter the element to be inserted 10
10 is inserted succssfully
1.
2.
3.
4.

Insert
Delete
Display
Exit

Enter your choice :1


Enter the element to be inserted 20
20 is inserted succssfully
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice :1
Enter the element to be inserted 30
30 is inserted succssfully
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice :3
The content of queue is :
10
20
30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice :2
Element deleted is 10
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice :4
PROGRAM12. 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 A NODE AT 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
D. DISPLAY THE CONTENTS OF THE LIST.
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int info;

struct node *rlink;


struct node *llink;
};
typedef struct node *NODE;
NODE insertfront(int item,NODE first)
{
NODE temp;
temp=(NODE)malloc(sizeof(struct node));
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;
if(first==NULL)
return temp;
temp->rlink=first;
first->llink=temp;
return temp;
}
display(NODE first)
{

NODE temp;
if(first==NULL)
printf("\n List is empty \n");
else
{
printf("\n The content of the linked list is:\n");
temp=first;
while(temp!=NULL)
{
printf("%d \t",temp->info);
temp=temp->rlink;
}
}

NODE deleteinfo(int item,NODE first)


{
NODE cur,prev,next;
if(first==NULL)
{
printf("\n List is empty \n");
return NULL;
}
if(item==first->info)
{
cur=first;
first=first->rlink;
first->llink=NULL;
printf("\n Element deleted is %d",cur->info);
free(cur);
return first;
}
cur=first;

while(cur!=NULL&&item!=cur->info)
cur=cur->rlink;
if(cur==NULL)
{
printf("%d is not present in the linked list",item);
return first;
}
prev=cur->llink;
next=cur->rlink;
prev->rlink=next;
next->llink=prev;
printf("\n Element deleted is %d",cur->info);
free(cur);
return first;
}
NODE insertleft(int item,NODE first,int key)
{
NODE temp,cur,prev;
temp=(NODE)malloc(sizeof(struct node));
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;
if(key==first->info)
{
temp->rlink=first;
first->llink=temp;
printf("%d is inserted successfully",item);
return temp;
}
cur=first;
while(cur!=NULL&&key!=cur->info)
cur=cur->rlink;
if(cur==NULL)
{
printf(" key %d is not found",key);
return first;
}
prev=cur->llink;
prev->rlink=temp;
temp->llink=prev;
temp->rlink=cur;
cur->llink=temp;
printf("%d is inserted successfully",item);
return first;
}
main()
{

NODE first=NULL;
int choice,item,key;
for(;;)
{
printf("\n 1.Insert(front)");

printf("\n 2.Insert(left)");
printf("\n 3.Delete");
printf("\n 4.Display");
printf("\n 5.Quit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n Enter the item to be inserted :");
scanf("%d",&item);
first=insertfront(item,first);
break;
case 2:printf("\n Enter the item to be inserted:");
scanf("%d",&item);
printf("\n Enter the key value:");
scanf("%d",&key);
first=insertleft(item,first,key);
break;
case 3:printf("\n Enter the item to be deleted :");
scanf("%d",&item);
first=deleteinfo(item,first);
break;
case 4:display(first);
break;
default:exit(0);
}
}
}

OUTPUT :
1.Insert(front)
2.Insert(left)
3.Delete
4.Display
5.Quit
Enter your choice:1
Enter the item to be inserted :10
1.Insert(front)
2.Insert(left)
3.Delete
4.Display
5.Quit
Enter your choice:1
Enter the item to be inserted :20
1.Insert(front)
2.Insert(left)
3.Delete
4.Display
5.Quit

Enter your choice:1


Enter the item to be inserted :30
1.Insert(front)
2.Insert(left)
3.Delete
4.Display
5.Quit
Enter your choice:4
The content of the linked list is:
30
20
10
1.Insert(front)
2.Insert(left)
3.Delete
4.Display
5.Quit
Enter your choice:2
Enter the item to be inserted:15
Enter the key value:10
15 is inserted successfully
1.Insert(front)
2.Insert(left)
3.Delete
4.Display
5.Quit
Enter your choice:4
The content of the linked list is:
30
20
15
10
1.Insert(front)
2.Insert(left)
3.Delete
4.Display
5.Quit
Enter your choice:5
PROGRAM13. WRITE A C PROGRAM
A. TO CONSTRUCT A BINARY SEARCH TREE OF INTEGERS
B. TO TRAVERSE THE TREE USING ALL THE METHODS i.e
PREOREDER
INORDER, POSTORDER
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node

int info;
struct node *llink;
struct node *rlink;

};
typedef struct node *NODE;
NODE insert(int item,NODE root)
{
NODE prev,cur,temp;
temp=(NODE)malloc(sizeof(struct node));
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
return temp;
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(item<cur->info)
cur=cur->llink;
else
cur=cur->rlink;
}
if(item<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
return root;
}
preorder(NODE root)
{
if(root!=NULL)
{
printf("%d \t",root->info);
preorder(root->llink);
preorder(root->rlink);
}
}
inorder(NODE root)
{
if(root!=NULL)
{
inorder(root->llink);
printf("%d \t",root->info);
inorder(root->rlink);
}
}
postorder(NODE root)
{
if(root!=NULL)
{
postorder(root->llink);

postorder(root->rlink);
printf("%d \t",root->info);
}
}
main()
{
NODE root=NULL;
int choice,item;
for(;;)
{
printf("\n 1.Creation or Insertion \n");
printf("\n 2.Preorder \n");
printf("\n 3.Inorder \n");
printf("\n 4.Postorder \n");
printf("\n 5.Quit \n");
printf("\n Enter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n Enter the item to be inserted :");
scanf("%d",&item);
root=insert(item,root);
break;
case 2:preorder(root);
break;
case 3:inorder(root);
break;
case 4:postorder(root);
break;
default:exit(0);
}
}
}

OUTPUT :
1.Creation or Insertion
2.Preorder
3.Inorder
4.Postorder
5.Quit
Enter your choice :1
Enter the element to be inserted :100
1.Creation or Insertion
2.Preorder
3.Inorder
4.Postorder

5.Quit
Enter your choice :1
Enter the element to be inserted :200
1.Creation or Insertion
2.Preorder
3.Inorder
4.Postorder
5.Quit
Enter your choice :1
Enter the element to be inserted :50
1.Creation or Insertion
2.Preorder
3.Inorder
4.Postorder
5.Quit
Enter your choice :3
50 100 200
1.Creation or Insertion
2.Preorder
3.Inorder
4.Postorder
5.Quit
Enter your choice :5
PROGRAM14.WRITE RECURSIVE C PROGRAM TO PERFROM BINARY SEARCH
#include<stdio.h>
int binarysearch(int a[10],int key,int low,int high)
{
int mid;
if(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
return mid+1;
if(key<a[mid])
return binarysearch(a,key,low,mid-1);
if(key>a[mid])
return binarysearch(a,key,mid+1,high);
}
return -1;
}
{

main()

int a[10],n,key,i,pos;
printf("\n Enter the no. of elements \n");
scanf("%d",&n);
printf("\n Enter the elements in ascending order \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the key to be searched");
scanf("%d",&key);
pos=binarysearch(a,key,0,n-1);
if(pos==-1)
printf("\n Unsuccessful search \n");
else
{
printf("\n Successful search \n");
printf("\n Element found at position %d",pos);
}
}

OUTPUT :
Enter the no. of elements
6
Enter the elements in ascending
order
23
45
66
77
89
90

Enter the key to be searched


89
Successful search

Element found at position 5

PROGRAM14. WRITE RECURSIVE C PROGRAM TO PERFROM TOWER OF HANOI


#include<stdio.h>
int count=0;
tower(int n,char l,char r,char m)
{
if(n>0)
{
tower(n-1,l,m,r);
printf("\n Moved disc %d from %c to %c \n",n,l,r);
count=count+1;
tower(n-1,m,r,l);
}
}
main()
{
int n;
printf("\n Enter the no. of discs \n");
scanf("%d",&n);
tower(n,'L','R','M');
printf("\n No. of moves = %d \n",count);
}

OUTPUT :
Enter the no. of discs
3

Moved disc 1 from L to R

Moved disc 2 from L to M

Moved disc 1 from R to M

Moved disc 3 from L to R

Moved disc 1 from M to L

Moved disc 2 from M to R

Moved disc 1 from L to R

No. of moves = 7
Posted by bloggy
Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest

Links to this post


Create a Link

Home
http://visvesvarayatechnologicaluniversity.blogspot.com/. Picture Window template. Powered
by Blogger.
Ad Info

Potrebbero piacerti anche