SlideShare a Scribd company logo
Programming in ‘C’ and Data Structure Lab File
1
Q1 WAP to count the no of spaces, alphabets, words and digits?
#include <stdio.h>
#include<conio.h>
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == 'n' || c == 't')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %dn",
nwhite, nother);
getch();
}
OUTPUT:
Enter a string:
Hi this is c programming language. Contains 100 programs.
Digits are: 3
Alphabets are: 46
White spaces are: 9
Programming in ‘C’ and Data Structure Lab File
2
Q2 WAP to squeeze a string?
#include<stdio.h>
#include<conio.h>
void squeeze()
{
char str[50] = "Hello this is string";
int i,j;
for(i=j=0;str[i]!='0';i++)
if(str[i]!=' ')
str[j++]=str[i];
//str[j]='0';
for(i=0; str[i]!='0';i++)
printf("%c" ,str[i]);
}
void main()
{
squeeze();
getch();
}
OUTPUT:
Enter a string: Hello, this is String.
Squeeze String is: Hello,thisisString.
Programming in ‘C’ and Data Structure Lab File
3
Q3 WAP to swap the values using the pointers?
#include<stdio.h>
#include<conio.h>
int swap(int *a ,int *b)
{
int temp = *a;
*a=*b;
*b=temp;
}
void main()
{
int a,b;
clrscr();
printf("Please enter A's value:");
scanf("%d",&a);
printf("nPlease enter B's value:");
scanf("%d",&b);
swap(&a,&b);
printf("nAfter swapping A's value: %d",a);
printf("nAfter swapping B's value: %d",b);
getch();
}
OUTPUT:
Please enter A's value:5
Please enter B's value:8
After swapping A's value: 8
After swapping B's value: 5
Programming in ‘C’ and Data Structure Lab File
4
Q4 WAP to compare two strings?
#include <stdio.h>
#include<conio.h>
int compare_function(char *a,char *b)
{
while (*a != '0')
{
if (*a == *b)
{
a++;
b++;
}
else
return 1;
}
return 0;
}
int main()
{
char compare_function();
int result;
char inputA[20],inputB[20];
printf("nEnter string A : ");
gets(inputA);
printf("nEnter string B : ");
gets(inputB);
result = compare_function(inputA,inputB);
if (result == 0)
printf("nStrings are same");
else
printf("nStrings are different");
}
OUTPUT:
Enter first String: Hiii
Enter second String:hello
Strings are different
Programming in ‘C’ and Data Structure Lab File
5
Q5 WAP to concatenate the string?
#include<stdio.h>
void concat(char* ,char* );
int main()
{
char str1[25],str2[25];
printf("nEnter First String:");
gets(str1);
printf("nEnter Second String:");
gets(str2);
concat(str1,str2);
#include<stdio.h>
void concat(char* ,char* );
int main()
{
char str1[25],str2[25];
printf("nEnter First String:");
gets(str1);
printf("nEnter Second String:");
gets(str2);
concat(str1,str2); //1
printf("nConcatenated String is %s",str1);
return 0;
}
void concat(char *s1,char *s2)
{
while(*s1!='0') //2
s1++
while(*s2!='0') //3
{
*s1=*s2; //4
s1++;
s2++;
}
*s1='0'; //5
}
OUTPUT:
Enter the first string:
Hello,
Enter the second string:
Good morning..
Concatenated string is:
Hello, Good morning..
Programming in ‘C’ and Data Structure Lab File
6
Q6. WAP which multiply two matrix?
#include<stdio.h>
int main()
{
int i,j,k,m,n,p,q;
int a[10][10],b[10][10],c[10][10];
printf("Enter the order of matrix 1 m * nn");
scanf("%d%d",&m,&n);
printf("Enter the order of matrix 2 p * qn");
scanf("%d%d",&p,&q);
printf("Enter the elements of matrix 1n");
for(i=0;i<m;i++){
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of matrix 2n");
for(i=0;i<p;i++){
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("elements of matrix1 and 2 are:n");
for(i=0;i<m,i<p;i++){
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf("t");
for(j=0;j<n,j<q;j++)
{
printf("%d",b[i][j]);
}
printf("n");
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("multiplication of matrix a and b is :n");
for(i=0;i<m;i++)
Programming in ‘C’ and Data Structure Lab File
7
{
for(j=0;j<m;j++)
{
printf("%d ",c[i][j]);
}
printf("n");
}
return 0;
}
OUTPUT:
Enter the order of matrix 1 m * n
3 3
Enter the order of matrix 2 p * q
3 3
Enter the elements of matrix 1
1 2 3 4 5 6 7 8 9
Enter the elements of matrix 2
1 2 3 4 5 6 7 8 9
Elements of matrix1 and 2 are:
1 2 3 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9
Multiplication of matrix a and b is:
30 36 42
66 81 96
102 126 150
--------------------------------
Process exited with return value 0
Press any key to continue . . .
Programming in ‘C’ and Data Structure Lab File
8
Q7 WAP which reverse the string?
#include <string.h>
#include <stdio.h>
#include<conio.h>
/* reverse: reverse string s in place */
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
printf("%s",s);
}
void main()
{
char s[20]="hello! Good morning";
reverse(s);
getch();
}
OUTPUT:
Enter a String :
Hello! Good Morning..
Reverse String is :
..gninroM doog !olleH
Programming in ‘C’ and Data Structure Lab File
9
Q8 WAP to perform insertion, deletion and traversing in singly link list?
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct nodeType
{
int info;
struct nodeType *next;
}node;
void createEmptyList(node **head);
void traverseInOrder1(node *head);
void traverseInReverse(node *head);
node *searchInUnsortedList(node *head,int item);
void auxillarySearch(node *head,node **ploc,node **loc,int item);
void insertAtBegining(node **head, int item);
void insertAtEnd(node **head, int item);
void insertAfterElement(node *head, int item, int after);
void deleteFromBegining(node **head);
void deleteFromEnd(node **head);
void deleteAfterElement(node *head, int after);
void reverseList(node **head);
void deleteList(node **head);
void main()
{
node *head;
int choice,element,after;
createEmptyList(&head);
while(1)
{
printf("ntOptions availablen");
printf("**********************************nn");
printf("1.Insert Element at beginingn");
printf("2. Insert Element at Endn");
printf("3.Insert Element after at given elementn");
printf("4.Traverse in ordern");
printf("5.Traverse in reverse ordern");
printf("6.Delete from begingign");
printf("7.Delete from endn");
printf("8.Delete from after given elementn");
printf("9.Reverse Link Listn");
printf("10.Exitn");
printf("Enter your choice (1-10)nn");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the Elementn");
Programming in ‘C’ and Data Structure Lab File
10
scanf("%d",&element);
insertAtBegining(&head,element);
break;
case 2:
printf("Enter the Elementn");
scanf("%d",&element);
insertAtEnd(&head,element);
break;
case 3:
printf("Enter the Elementn");
scanf("%d",&element);
printf("Enter the after which to insert:");
scanf("%d",&after);
insertAfterElement(head,element,after);
break;
case 4:
if(head==NULL)
printf("List is Empty");
else
traverseInOrder1(head);
printf("npress any key to continoun");
getch();
break;
case 5:
if(head==NULL)
printf("List is Empty");
else
traverseInReverse(head);
printf("npress any key to continoun");
getch();
break;
case 6:
deleteFromBegining(&head);
break;
case 7:
deleteFromEnd(&head);
break;
case 8:
printf("Enter the element after which element deletedn");
scanf("%d",&after);
deleteAfterElement(head,after);
break;
case 9:
reverseList(&head);
break;
case 10:
deleteList(&head);
exit(1);
}
}
}
Programming in ‘C’ and Data Structure Lab File
11
void createEmptyList(node **head)
{
*head=NULL;
}
void traverseInOrder1(node *head)
{
while(head!=NULL)
{
printf("%d",head->info);
head=head->next;
}
}
void traverseInReverse(node *head)
{
if(head->next!=NULL)
traverseInReverse(head->next);
printf("%d",head->info);
}
node *searchInUnsortedList(node *head,int item)
{
while(head!=NULL && head->info==item)
{
head=head->next;
}
return head;
}
void auxillarySearch(node *head,node **ploc,node **loc, int item)
{
int flag=0;
if(head==NULL)
*ploc=*loc=NULL;
else if(head->info==item)
{
*ploc=NULL;
*loc=head;
}
else
{
*ploc=head;
*loc=head->next;
while((*loc!=NULL)&&(!flag))
{
if((*loc)->info==item)
flag=1;
else
{
*ploc=*loc;
*loc=(*loc)->next;
}
}
Programming in ‘C’ and Data Structure Lab File
12
}
}
void insertAtBegining(node **head,int item)
{
node *ptr;
ptr=(node *)malloc(sizeof(node));
ptr->info=item;
if(*head==NULL)
ptr->next=NULL;
else
ptr->next=*head;
*head=ptr;
}
void insertAtEnd(node **head,int item)
{
node *ptr,*loc;
ptr=(node *)malloc(sizeof(node));
ptr->info=item;
ptr->next=NULL;
if(*head==NULL)
*head=ptr;
else
{
loc=*head;
while(loc->next!=NULL)
loc=loc->next;
loc->next=ptr;
}
}
void insertAfterElement(node *head,int item,int after)
{
node *ptr;
node *loc;
loc=searchInUnsortedList(head,after);
if(loc==(node *)NULL)
return ;
ptr=(node * )malloc(sizeof(node));
ptr->info=item;
ptr->next=loc->next;
loc->next=ptr;
}
void deleteFromBegining(node **head)
{
node *ptr;
if(*head==NULL)
return;
else
{
ptr=*head;
*head=(*head)->next;
Programming in ‘C’ and Data Structure Lab File
13
free(ptr);
}
}
void deleteFromEnd(node **head)
{
node *ptr, *loc;
if(*head==NULL)
return;
else if((*head)->next==(node*)NULL)
{
ptr=*head;
*head=NULL;
free(ptr);
}
else
{
loc=*head;
ptr=(*head)->next;
while(ptr->next!=NULL)
{
loc=ptr;
ptr=ptr->next;
}
loc->next=NULL;
free(ptr);
}
}
void deleteAfterElement(node *head,int after)
{
node *ptr, *loc;
loc=searchInUnsortedList(head,after);
if(loc==(node *)NULL)
return;
ptr=loc->next;
loc->next=ptr->next;
free(ptr);
}
void reverseList(node **head)
{
node *previousNode , *currentNode,*nextNode;
currentNode=*head;
nextNode=currentNode->next;
previousNode=NULL;
currentNode->next=NULL;
while(nextNode!=NULL)
{
previousNode=currentNode;
currentNode=nextNode;
nextNode=currentNode->next;
currentNode->next=previousNode;
}
Programming in ‘C’ and Data Structure Lab File
14
*head=currentNode;
}
void deleteList(node **head)
{
node *ptr;
while(*head !=NULL)
{
ptr=*head;
*head=(*head)->next;
free(ptr);
}
}
Output:
1. Insert elements at beginning
2. Insert element at mid
3. Insert element at end
4. Insert element at after
5. Delete element from beginning
6. Delete element from mid
7. Delete element after element
8. Traverse the link list
9. Exit
Enter your choice: 1
Enter element: 3
Element inserted.
Enter your choice: 3
Enter element: 8
Element inserted.
Enter your choice: 2
Enter element: 2
Element inserted.
Enter your choice: 8
Link list elements are:
3 2 8
Enter your choice: 5
Element 3 is deleted.
Enter your choice: 9
Press any key to continue……
Programming in ‘C’ and Data Structure Lab File
15
Q9 WAP to implement polynomial addition and multiplication?
#include<stdio.h>
#include<stdlib.h>
struct node
{
float coef;
int expo;
struct node *link;
};
struct node *create(struct node *);
struct node *insert_s(struct node *,float,int);
struct node *insert(struct node *,float,int);
void display(struct node *ptr);
void poly_add(struct node *,struct node *);
void poly_mult(struct node *,struct node *);
main( )
{
struct node *start1=NULL,*start2=NULL;
printf("Enter polynomial 1 :n");
start1=create(start1);
printf("Enter polynomial 2 :n");
start2=create(start2);
printf("Polynomial 1 is : ");
display(start1);
printf("Polynomial 2 is : ");
display(start2);
poly_add(start1, start2);
poly_mult(start1, start2);
}
struct node *create(struct node *start)
{
int i,n,ex;
float co;
printf("Enter the number of terms : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter coeficient for term %d : ",i);
scanf("%f",&co);
printf("Enter exponent for term %d : ",i);
scanf("%d",&ex);
start=insert_s(start,co,ex);
}
return start;
}
Programming in ‘C’ and Data Structure Lab File
16
struct node *insert_s(struct node *start,float co,int ex)
{
struct node *ptr,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->coef=co;
tmp->expo=ex;
if(start==NULL || ex > start->expo)
{
tmp->link=start;
start=tmp;
}
else
{
ptr=start;
while(ptr->link!=NULL && ptr->link->expo >= ex)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
}
return start;
}
struct node *insert(struct node *start,float co,int ex)
{
struct node *ptr,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->coef=co;
tmp->expo=ex;
/*If list is empty*/
if(start==NULL)
{
tmp->link=start;
start=tmp;
}
else
{
ptr=start;
while(ptr->link!=NULL)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
}
return start;
}
void display(struct node *ptr)
{
if(ptr==NULL)
{
printf("Zero polynomialn");
return;
}
while(ptr!=NULL)
Programming in ‘C’ and Data Structure Lab File
17
{
printf("(%.1fx^%d)", ptr->coef,ptr->expo);
ptr=ptr->link;
if(ptr!=NULL)
printf(" + ");
else
printf("n");
}
}
void poly_add(struct node *p1,struct node *p2)
{
struct node *start3;
start3=NULL;
while(p1!=NULL && p2!=NULL)
{
if(p1->expo > p2->expo)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
else if(p2->expo > p1->expo)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
else if(p1->expo==p2->expo)
{
start3=insert(start3,p1->coef+p2->coef,p1->expo);
p1=p1->link;
p2=p2->link;
}
}
while(p1!=NULL)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
while(p2!=NULL)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
printf("Added polynomial is : ");
display(start3);
}
void poly_mult(struct node *p1, struct node *p2)
{
struct node *start3;
struct node *p2_beg = p2;
start3=NULL;
if(p1==NULL || p2==NULL)
Programming in ‘C’ and Data Structure Lab File
18
{
printf("Multiplied polynomial is zero polynomialn");
return;
}
while(p1!=NULL)
{
p2=p2_beg;
while(p2!=NULL)
{
start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo);
p2=p2->link;
}
p1=p1->link;
}
printf("Multiplied polynomial is : ");
display(start3);
}
OUTPUT:
Enter polynomial 1 :
Enter the number of terms : 4
Enter coeficient for term 1 : 5
Enter exponent for term 1 : 4
Enter coeficient for term 2 : 7
Enter exponent for term 2 : 3
Enter coeficient for term 3 : 9
Enter exponent for term 3 : 2
Enter coeficient for term 4 : 3
Enter exponent for term 4 : 1
Enter polynomial 2 :
Enter the number of terms : 3
Enter coeficient for term 1 : 5
Enter exponent for term 1 : 3
Enter coeficient for term 2 : 6
Enter exponent for term 2 : 2
Enter coeficient for term 3 : 9
Enter exponent for term 3 : 1
Polynomial 1 is : (5x^4) + (7x^3) + (9x^2) + (3x^1)
Polynomial 2 is : (5x^3) + (6x^2) + (9x^1)
Added polynomial is : (5x^4) + (12x^3) + (15x^2) + (12x^1)
Multiplied polynomial is z: (25x^7) + (30x^6) + (35x^6) + (45x^5) +
(42x^5) + (45x^5) + (63x^4) + (54x^4) + (15x^4) + (81x^3) + (18x^3) +
(27.0x^2)
Programming in ‘C’ and Data Structure Lab File
19
Q10 WAP to implement stack using array?
#include<stdlib.h
#include<stdio.h>
#include<conio.h>
#define MAX 50
void push();
void pop();
void display();
int stack[MAX], top=-1, item;
void main()
{
int ch;
do
{
printf("nnnn1.tPushn2.tPopn3.tDisplayn4.tExitn"
);
printf("nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("nnInvalid entry. Please try
again...n");
}
} while(ch!=4);
getch();
}
void push()
{
if(top == MAX-1)
printf("nnStack is full.");
else
{
printf("nnEnter ITEM: ");
scanf("%d", &item);
top++;
stack[top] = item;
printf("nnITEM inserted = %d", item);
}
}
Programming in ‘C’ and Data Structure Lab File
20
void pop()
{
if(top == -1)
printf("nnStack is empty.");
else
{
item = stack[top];
top--;
printf("nnITEM deleted = %d", item);
}
}
void display()
{
int i;
if(top == -1)
printf("nnStack is empty.");
else
{
for(i=top; i>=0; i--)
printf("n%d", stack[i]);
}
}
OUTPUT:
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 1
ITEM inserted = 1
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 2
ITEM inserted = 2
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
2
1
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
ITEM deleted = 2
Programming in ‘C’ and Data Structure Lab File
21
Q11 WAP to implement stack using link list?
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void push();
void pop();
void display();
struct node
{
int info;
struct node *link;
} *top = NULL;
int item;
void main()
{
int ch;
do
{
printf("nn1. Pushn2. Popn3. Displayn4. Exitn");
printf("nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.n");
}
} while(1);
getch();
}
void push()
{
struct node *ptr;
printf("nnEnter ITEM: ");
scanf("%d", &item);
if (top == NULL)
{
top = (struct node *)malloc(sizeof(struct node));
top->info = item;
top->link = NULL;
Programming in ‘C’ and Data Structure Lab File
22
}
else
{
ptr = top;
top = (struct node *)malloc(sizeof(struct node));
top->info = item;
top->link = ptr;
}
printf("nItem inserted: %dn", item);
}
void pop()
{
struct node *ptr;
if (top == NULL)
printf("nnStack is emptyn");
else
{
ptr = top;
item = top->info;
top = top->link;
free(ptr);
printf("nnItem deleted: %d", item);
}
}
void display()
{
struct node *ptr;
if (top == NULL)
printf("nnStack is emptyn");
else
{
ptr = top;
while(ptr != NULL)
{
printf("nn%d", ptr- >info);
ptr = ptr->link;
}
}
}
Output:
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 1
Item inserted: 1
Programming in ‘C’ and Data Structure Lab File
23
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 2
Item inserted: 2
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 3
Item inserted: 3
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
3
2
1
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
Item deleted: 3
Programming in ‘C’ and Data Structure Lab File
24
Q12 WAP to evaluate postfix expression?
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#define SIZE 40
int stack[SIZE];
int top=-1;
void push(int n)
{
if(top==SIZE-1)
{
printf("Stack is fulln");
return;
}
else
{
top=top+1;
stack[top]=n;
printf("Pushed element is %dn",n);
}
}
int pop()
{
int n;
if(top==-1)
{
printf("Stack is emptyn");
return 0;
}
else
{
n=stack[top];
top=top-1;
printf("The poped element is %dn",n);
return(n);
}
}
int evaluate(int op1, int op2,char ch)
{
printf("op1=%d op2=%d ch=%cn",op1,op2,ch);
int n;
if (op1<op2)
{
n=op1;
op1=op2;
op2=n;
}
if(ch=='+')
n=op1+op2;
else if(ch=='-')
n=op1-op2;
Programming in ‘C’ and Data Structure Lab File
25
else if(ch=='*')
n=op1*op2;
else if(ch=='/')
n=op1/op2;
else if(ch=='%')
n=op1%op2;
else
{
printf("The operator is not identifiedn");
exit(0);
}
printf("n=%dn",n);
return(n);
}
int main()
{
char str[50],ch,ch1;
int i=0,n,op1,op2;
printf("Enter the Postfix stringn");
scanf("%s",str);
ch=str[i];
while(ch!='0')
{
printf("The char is=%cn",ch);
//if(ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5')//
if(isdigit(ch))
{
n=ch-'0';
push(n);
}
else
{
op1=pop();
op2=pop();
n=evaluate(op1,op2,ch);
push(n);
}
ch=str[++i];
}
printf("The value of the arithmetic expression is=%dn",pop());
return 0;
}
OUTPUT:
Enter the expression: 7 5 – 9 2 / *
The value of arithmetic expression is: 9
Programming in ‘C’ and Data Structure Lab File
26
Q13 WAP to implement Queue using Array?
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#define MAX 50
void insert();
void delet();
void display();
int queue[MAX], rear=-1, front=-1, item;
void main()
{
int ch;
do
{
printf("nn1. Insertn2. Deleten3. Displayn4. Exitn");
printf("nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("nnInvalid entry. Please try again...n");
}
} while(1);
getch();
}
void insert()
{
if(rear == MAX-1)
printf("nnQueue is full.");
else
{
printf("nnEnter ITEM: ");
scanf("%d", &item);
if (rear == -1 && front == -1)
{
rear = 0;
front = 0;
}
else
Programming in ‘C’ and Data Structure Lab File
27
rear+ +;
queue[rear] = item;
printf("nnItem inserted: %d", item);
}
}
void delet()
{
if(front == -1)
printf("nnQueue is empty.");
else
{
item = queue[front];
if (front == rear){
front = -1;
rear = -1;}
else
front++;
printf("nnItem deleted: %d", item);
}
}
void display()
{
int i;
if(front == -1)
printf("nnQueue is empty.");
else
{
printf("nn");
for(i=front; i<=rear; i++)
printf(" %d", queue[i]);
}
}
OUTPUT:
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 1
Item inserted: 1
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 2
Item inserted: 2
Enter your choice: 1
Enter ITEM: 3
Item inserted: 3
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
1 2 3
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Item deleted: 1
Programming in ‘C’ and Data Structure Lab File
28
Q14 WAP to queue implement using link list?
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int info;
struct node *link;
}*front = NULL, *rear = NULL;
void insert();
void delet();
void display();
int item;
void main()
{
int ch;
do
{
printf("nn1.tInsertn2.tDeleten3.tDisplayn4.tExitn");
printf("nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("nnInvalid choice. Please try
again...n");
}
} while(1);
getch();
}
void insert()
{
printf("nnEnter ITEM: ");
scanf("%d", &item);
if(rear == NULL)
{
rear = (struct node *)malloc(sizeof(struct node));
rear->info = item;
rear->link = NULL;
front = rear;
Programming in ‘C’ and Data Structure Lab File
29
}
else
{
rear->link = (struct node *)malloc(sizeof(struct
node));
rear = rear->link;
rear->info = item;
rear->link = NULL;
}
}
void delet()
{
struct node *ptr;
if(front == NULL)
printf("nnQueue is empty.n");
else
{
ptr = front;
item = front->info;
front = front->link;
free(ptr);
printf("nItem deleted: %dn", item);
if(front == NULL)
rear = NULL;
}
}
void display()
{
struct node *ptr = front;
if(rear == NULL)
printf("nnQueue is empty.n");
else
{
printf("nn");
while(ptr != NULL)
{
printf("%dt",ptr->info);
ptr = ptr->link;
}
}
}
OUTPUT:
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 1
Programming in ‘C’ and Data Structure Lab File
30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 12
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 13
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
1 12 13
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Item deleted: 1
Programming in ‘C’ and Data Structure Lab File
31
Q15 WAP to implement circular queue using array?
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#define SIZE 5
void insert();
void delet();
void display();
int queue[SIZE], rear=-1, front=-1, item;
void main()
{
int ch;
do
{
printf("n1.tInsertn2.tDeleten3.tDisplayn4.tExitn");
printf("nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("nnInvalid choice. Pleasr try
again...n");
}
} while(1);
getch();
}
void insert()
{
if((front==0 && rear==SIZE-1) || (front==rear+1))
printf("nnQueue is full.");
else
{
printf("nnEnter ITEM: ");
scanf("%d", &item);
if(rear == -1)
{
rear = 0;
front = 0;
Programming in ‘C’ and Data Structure Lab File
32
}
else if(rear == SIZE-1)
rear = 0;
else
rear++;
queue[rear] = item;
printf("nnItem inserted: %dn", item);
}
}
void delet()
{
if(front == -1)
printf("nnQueue is empty.n");
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1;
}
else if(front == SIZE-1)
front = 0;
else
front++;
printf("nnITEM deleted: %d", item);
}
}
void display()
{
int i;
if((front == -1) || (front==rear+1))
printf("nnQueue is empty.n");
else
{
printf("nn");
for(i=front; i<=rear; i++)
printf("t%d",queue[i]);
}
}
OUTPUT:
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 10
Item inserted: 10
Programming in ‘C’ and Data Structure Lab File
33
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 20
Item inserted: 20
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 30
Item inserted: 30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
10 20 30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
ITEM deleted: 10
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
20 30
Programming in ‘C’ and Data Structure Lab File
34
Q16 WAP to perform addition, transpose, and multiplication operations of
sparse matrix?
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void implement(int[][10],int,int,int[][10]);
void add(int[][10],int,int,int[][10],int[][10]);
void sub(int[][10],int,int,int[][10],int[][10]);
void transpose(int[][10],int,int[][10]);
void main()
{
int a[10][10];
int b[10][10];
int c[10][10];
int d[10][10];
int e[50][10];
int row,col,k,l,ch;
char che;
clrscr();
printf("enter the no. of rowst:");
scanf("%d",&row);
printf("n enter the no. of colomst:");
scanf("%d",&col);
printf("n enter the elements of sparse matrix t:");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("n element at [%d][%d]t",k,l);
scanf("%d",&a[k][l]);
}
}
printf("sparse matrix is n");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("%dt",a[k][l]);
}
printf("n");
}
do
{
printf("n CYNET MENUn");
printf("1:REPRESENTATIONn");
printf("2:ADDITIONn");
printf("3:SUBTRACTIONn");
printf("4:TRANSPOSEn");
printf("5:EXITn");
printf("enter your choice:");
Programming in ‘C’ and Data Structure Lab File
35
scanf("%d",&ch);
switch(ch)
{
case 1:
implement(a,row,col,b);
break;
case 2:
printf("n enter the elements of second sparse matrix
t:");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("n element at [%d][%d]t",k,l);
scanf("%d",&c[k][l]);
}
}
printf("sparse matrix is n");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("%dt",c[k][l]);
}
printf("n");
}
implement(a,row,col,b);
implement(c,row,col,d);
add(b,row,col,d,e);
break;
case 3:
printf("n enter the elements of second sparse matrix t:");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("n element at [%d][%d]t",k,l);
scanf("%d",&c[k][l]);
}
}
printf("sparse matrix is n");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("%dt",c[k][l]);
}
printf("n");
}
implement(a,row,col,b);
implement(c,row,col,d);
Programming in ‘C’ and Data Structure Lab File
36
sub(b,row,col,d,e);
break;
case 4:
implement(a,row,col,b);
transpose(b,col,c);
break;
case 5:
exit(0);
default:
printf("you entered wrong choicen");
}
printf("do you want to continue(yY):");
che=getche();
}while(che=='y'||che=='Y');
getch();
}
void implement(int a[][10],int row,int col,int b[][10])
{
int g=1,nz=0,k,l;
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
if(a[k][l]!=0)
{
nz=nz+1;
}
}
}
b[0][0]=row;
b[0][1]=col;
b[0][2]=nz;
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
if(a[k][l]!=0)
{
b[g][0]=k;
b[g][1]=l;
b[g][2]=a[k][l];
g++;
}
}
}
printf("implementation of sparse matrix isn");
for(k=0;k<g;k++)
{
for(l=0;l<3;l++)
{
Programming in ‘C’ and Data Structure Lab File
37
printf("%dt",b[k][l]);
}
printf("n");
}
}
void add(int b[][10],int row,int col,int d[][10],int e[][10])
{
int p1=1,p2=1,i=1;
int k,l;
if(b[0][0]!=d[0][0])
{
printf("addition is not possiblen");
}
else
{
while(p1<=b[0][2]&&p2<=d[0][2])
{
if(b[p1][0]==d[p2][0])
{
if(b[p1][1]<d[p2][1])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
else if(b[p1][1]>d[p2][1])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
else if(b[p1][1]==d[p2][1])
{
e[i][0]=d[p1][0];
e[i][1]=d[p1][1];
e[i][2]=b[p1][2]+d[p2][2];
if(e[i][2]!=0)
{
i++;
}
p1++;
p2++;
}
}
else if(b[p1][0]<d[p2][0])
{
Programming in ‘C’ and Data Structure Lab File
38
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
else if(b[p1][0]>d[p2][0])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
}
if(p1!=b[0][2])
{
while(p1<=b[0][2])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
}
else if(p2!=d[0][2])
{
while(p2<=d[0][2])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
}
e[0][0]=row;
e[0][1]=col;
e[0][2]=i-1;
printf("matrix after additionn");
for(k=0;k<i;k++)
{
for(l=0;l<3;l++)
{
printf("%dt",e[k][l]);
}
printf("n");
}
}
}
Programming in ‘C’ and Data Structure Lab File
39
void sub(int b[][10],int row,int col,int d[][10],int e[][10])
{
int p1=1,p2=1,i=1;
int k,l;
if(b[0][0]!=d[0][0])
{
printf("subtraction is not possiblen");
}
else
{
while(p1<=b[0][2]&&p2<=d[0][2])
{
if(b[p1][0]==d[p2][0])
{
if(b[p1][1]<d[p2][1])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
else if(b[p1][1]>d[p2][1])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
else if(b[p1][1]==d[p2][1])
{
e[i][0]=d[p1][0];
e[i][1]=d[p1][1];
e[i][2]=b[p1][2]-d[p2][2];
if(e[i][2]!=0)
{
i++;
}
p1++;
p2++;
}
}
else if(b[p1][0]<d[p2][0])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
Programming in ‘C’ and Data Structure Lab File
40
}
else if(b[p1][0]>d[p2][0])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
}
if(p1!=b[0][2])
{
while(p1<=b[0][2])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
}
else if(p2!=d[0][2])
{
while(p2<=d[0][2])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
}
e[0][0]=row;
e[0][1]=col;
e[0][2]=i-1;
printf("matrix after subtractionn");
for(k=0;k<=i;k++)
{
for(l=0;l<3;l++)
{
printf("%dt",e[k][l]);
}
printf("n");
}
}
}
void transpose(int b[][10],int col,int c[][10])
{
int i,j,k,temp;
for(i=0;i<=b[0][2];i++)
{
Programming in ‘C’ and Data Structure Lab File
41
c[i][0]=b[i][1];
c[i][1]=b[i][0];
c[i][2]=b[i][2];
}
for(i=1;i<=b[0][2];i++)
{
for(j=i+1;j<=b[0][2];j++)
{
if(c[i][0]>c[j][0])
{
for(k=0;k<3;k++)
{
temp=c[i][k];
c[i][k]=c[j][k];
c[j][k]=temp;
}
}
}
}
printf("transpose of matrix isn");
for(i=0;i<=c[0][2];i++)
{
for(j=0;j<3;j++)
{
printf("%dt",c[i][j]);
}
printf("n");
}
}
OUTPUT:
Enter the no of the rows: 3
Enter the no of the columns: 3
Enter the elements:
0 0 0 4 0 0 0 7 0
The sparse matrix is:
2 1 4
3 2 7
Enter the elements of second matrix:
2 0 0 6 0 0 5 0 0
The second sparse matrix is:
1 1 2
2 1 6
3 1 5
Addition of sparse matrix is: multiplication of sparse matrix is:
0 0 2 2 1 8
0 0 10 3 1 42
2 0 5
Programming in ‘C’ and Data Structure Lab File
42
Q17 WAP to perform insertion, deletion, traversing and searching in Binary
search tree?
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *left,*right;
};
struct node *root;
void insert(int x)
{
struct node *p,*previous,*current;
p=(struct node *)malloc(sizeof(struct node));
if(p==NULL)
{
printf("n Out of memory");
}
p->data=x;
p->left=NULL;
p->right=NULL;
if(root==NULL)
{
root=p;
return;
}
previous=NULL;
current=root;
while(current!=NULL)
{
previous=current;
if(p->data<current->data)
current=current->left;
else
current=current->right;
}
if(p->data<previous->data)
previous->left=p;
else
previous->right=p;
}
void inorder(struct node *t)
{
if (t!=NULL)
{
inorder(t->left);
printf("n %5d",t->data);
Programming in ‘C’ and Data Structure Lab File
43
inorder (t->right);
}
}
void del(int x)
{
int tright=0,tleft=0;
struct node *ptr=root;
struct node *parent=root;
struct node *t1=root;
struct node *temp=root;
while(ptr!=NULL&& ptr->data!=x)
{
parent=ptr;
if (x<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
if (ptr==NULL)
{
printf("n Delete element not found");
return ;
}
else if(t1->data==x && (t1->left ==NULL || t1->right==NULL))
if(t1->left==NULL)
t1=t1->right;
else
t1=t1->left;
else if (ptr->left==NULL)
if (x<parent->data)
parent->left=ptr->right;
else
parent->right=ptr->right;
else if (ptr->right==NULL)
if (x<parent->data)
parent->left=ptr->left;
else
parent->right=ptr->left;
else
{
temp=ptr;
parent=ptr;
if((ptr->left)>=(ptr->right))
{
ptr=ptr->left;
while(ptr->right!=NULL)
{
tright=1;
parent=ptr;
ptr=ptr->right;
}
Programming in ‘C’ and Data Structure Lab File
44
temp->data=ptr->data;
if(tright)
parent->right=ptr->left;
else
parent->left=ptr->left;
}
else
{
ptr=ptr->right;
while (ptr->left!=NULL)
{
tleft=1;
parent=ptr;
ptr=ptr->left;
}
temp->data=ptr->data;
if(tleft)
parent->left=ptr->right;
else
parent->right=ptr->right;
}
free(ptr);
}
}
void main()
{
int op,n,srchno;
root=(struct node *)malloc(sizeof(struct node));
root->data=30;
root->right=root->left=NULL;
clrscr();
do
{
printf("n 1.Insertion");
printf("n 2.Deletion");
printf("n 3.Inorder");
printf("n 4.Quit");
printf("n Enter your choicen");
scanf("%d",&op);
switch (op)
{
case 1: printf("n Enter the element to insertn");
scanf("%d",&n);
insert(n);
break;
case 2: printf("n Enter the element to be deletedn");
scanf("%d",&srchno);
del(srchno);
break;
case 3: printf("n The inorder elements aren");
Programming in ‘C’ and Data Structure Lab File
45
inorder(root);
getch();
break;
default: exit(0);
}
}while(op<4);
getch();
}
OUTPUT:
1. Insertion
2. Deletion
3. Traversing
4. Quit
Enter your choice:
1
Enter the element: 1
Enter the element: 5
Enter the element: 3
Enter the element: 8
Enter your choice
2
Enter the no. to be deleted
8
No is deleted
Enter your choice: 3
Tree element are:
1 5 3
Press any key to continue…
Programming in ‘C’ and Data Structure Lab File
46
Q18 WAP to implement Depth first graph?
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#define TRUE 1
#define FALSE 0
#define MAX 8
struct node
{
int data ;
struct node *next ;
} ;
int visited[MAX] ;
void dfs ( int, struct node ** ) ;
struct node * getnode_write ( int ) ;
void del ( struct node * ) ;
void main( )
{
struct node *arr[MAX] ;
struct node *v1, *v2, *v3, *v4 ;
int i ;
clrscr( ) ;
v1 = getnode_write ( 2 ) ;
arr[0] = v1 ;
v1 -> next = v2 = getnode_write ( 3 ) ;
v2 -> next = NULL ;
v1 = getnode_write ( 1 ) ;
arr[1] = v1 ;
v1 -> next = v2 = getnode_write ( 4 ) ;
v2 -> next = v3 = getnode_write ( 5 ) ;
v3 -> next = NULL ;
v1 = getnode_write ( 1 ) ;
arr[2] = v1 ;
v1 -> next = v2 = getnode_write ( 6 ) ;
v2 -> next = v3 = getnode_write ( 7 ) ;
v3 -> next = NULL ;
v1 = getnode_write ( 2 ) ;
arr[3] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = NULL ;
v1 = getnode_write ( 2 ) ;
arr[4] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = NULL ;
v1 = getnode_write ( 3 ) ;
arr[5] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = NULL ;
v1 = getnode_write ( 3 ) ;
arr[6] = v1 ;
Programming in ‘C’ and Data Structure Lab File
47
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = NULL ;
v1 = getnode_write ( 4 ) ;
arr[7] = v1 ;
v1 -> next = v2 = getnode_write ( 5 ) ;
v2 -> next = v3 = getnode_write ( 6 ) ;
v3 -> next = v4 = getnode_write ( 7 ) ;
v4 -> next = NULL ;
dfs ( 1, arr ) ;
for ( i = 0 ; i < MAX ; i++ )
del ( arr[i] ) ;
getch( ) ;
}
void dfs ( int v, struct node **p )
{
struct node *q ;
visited[v - 1] = TRUE ;
printf ( "%dt", v ) ;
q = * ( p + v - 1 ) ;
while ( q != NULL )
{
if ( visited[q -> data - 1] == FALSE )
dfs ( q -> data, p ) ;
else
q = q -> next ;
}
}
struct node * getnode_write ( int val )
{
struct node *newnode ;
newnode = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
newnode -> data = val ;
return newnode ;
}
void del ( struct node *n )
{
struct node *temp ;
while ( n != NULL )
{
temp = n -> next ;
free ( n ) ;
n = temp ;
}
}
OUTPUT:
Traversing of DFS Graph is : 1 2 3 4 5 6 7 8
Programming in ‘C’ and Data Structure Lab File
48
Q19 WAP to implement Breadth first shortest path?
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#define TRUE 1
#define FALSE 0
#define MAX 8
struct node
{
int data ;
struct node *next ;
} ;
int visited[MAX] ;
int q[8] ;
int front, rear ;
void bfs ( int, struct node ** ) ;
struct node * getnode_write ( int ) ;
void addqueue ( int ) ;
int deletequeue( ) ;
int isempty( ) ;
void del ( struct node * ) ;
void main( )
{
struct node *arr[MAX] ;
struct node *v1, *v2, *v3, *v4 ;
int i ;
clrscr( ) ;
v1 = getnode_write ( 2 ) ;
arr[0] = v1 ;
v1 -> next = v2 = getnode_write ( 3 ) ;
v2 -> next = NULL ;
v1 = getnode_write ( 1 ) ;
arr[1] = v1 ;
v1 -> next = v2 = getnode_write ( 4 ) ;
v2 -> next = v3 = getnode_write ( 5 ) ;
v3 -> next = NULL ;
v1 = getnode_write ( 1 ) ;
arr[2] = v1 ;
v1 -> next = v2 = getnode_write ( 6 ) ;
v2 -> next = v3 = getnode_write ( 7 ) ;
v3 -> next = NULL ;
v1 = getnode_write ( 2 ) ;
arr[3] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = NULL ;
v1 = getnode_write ( 2 ) ;
arr[4] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = NULL ;
Programming in ‘C’ and Data Structure Lab File
49
v1 = getnode_write ( 3 ) ;
arr[5] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = NULL ;
v1 = getnode_write ( 3 ) ;
arr[6] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = NULL ;
v1 = getnode_write ( 4 ) ;
arr[7] = v1 ;
v1 -> next = v2 = getnode_write ( 5 ) ;
v2 -> next = v3 = getnode_write ( 6 ) ;
v3 -> next = v4 = getnode_write ( 7 ) ;
v4 -> next = NULL ;
front = rear = -1 ;
bfs ( 1, arr ) ;
for ( i = 0 ; i < MAX ; i++ )
del ( arr[i] ) ;
getch( ) ;
}
void bfs ( int v, struct node **p )
{
struct node *u ;
visited[v - 1] = TRUE ;
printf ( "%dt", v ) ;
addqueue ( v ) ;
while ( isempty( ) == FALSE )
{
v = deletequeue( ) ;
u = * ( p + v - 1 ) ;
while ( u != NULL )
{
if ( visited [ u -> data - 1 ] == FALSE )
{
addqueue ( u -> data ) ;
visited [ u -> data - 1 ] = TRUE ;
printf ( "%dt", u -> data ) ;
}
u = u -> next ;
}
}
}
struct node * getnode_write ( int val )
{
struct node *newnode ;
newnode = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
newnode -> data = val ;
return newnode ;
}
void addqueue ( int vertex )
{
Programming in ‘C’ and Data Structure Lab File
50
if ( rear == MAX - 1 )
{
printf ( "nQueue Overflow." ) ;
exit( ) ;
}
rear++ ;
q[rear] = vertex ;
if ( front == -1 )
front = 0 ;
}
int deletequeue( )
{
int data ;
if ( front == -1 )
{
printf ( "nQueue Underflow." ) ;
exit( ) ;
}
data = q[front] ;
if ( front == rear )
front = rear = -1 ;
else
front++ ;
return data ;
}
int isempty( )
{
if ( front == -1 )
return TRUE ;
return FALSE ;
}
void del ( struct node *n )
{
struct node *temp ;
while ( n != NULL )
{
temp = n -> next ;
free ( n ) ;
n = temp ;
}
}
Programming in ‘C’ and Data Structure Lab File
51
Q20 WAP to implement heap tree?
#include <stdio.h>
#include <conio.h>
void restoreup ( int, int * ) ;
void restoredown ( int, int *, int ) ;
void makeheap ( int *, int ) ;
void add ( int, int *, int * ) ;
int replace ( int, int *, int ) ;
int del ( int *, int * ) ;
void main( )
{
int arr [20] = { 1000, 7, 10, 25, 17, 23, 27, 16,
19, 37, 42, 4, 33, 1, 5, 11 } ;
int i, n = 15 ;
clrscr( ) ;
makeheap ( arr, n ) ;
printf ( "Heap:n" ) ;
for ( i = 1 ; i <= n ; i++ )
printf ( "%dt", arr [i] ) ;
i = 24 ;
add ( i, arr, &n ) ;
printf ( "nnElement added %d.n", i ) ;
printf ( "nHeap after addition of an element:n" ) ;
for ( i = 1 ; i <= n ; i++ )
printf ( "%dt", arr [i] ) ;
i = replace ( 2, arr, n ) ;
printf ( "nnElement replaced %d.n", i ) ;
printf ( "nHeap after replacement of an element:n" ) ;
for ( i = 1 ; i <= n ; i++ )
printf ( "%dt", arr [i] ) ;
i = del ( arr, &n ) ;
printf ( "nnElement deleted %d.n", i ) ;
printf ( "nHeap after deletion of an element:n" ) ;
for ( i = 1 ; i <= n ; i++ )
printf ( "%dt", arr [i] ) ;
getch( ) ;
}
void restoreup ( int i, int *arr )
{
int val ;
val = arr [i] ;
while ( arr [i / 2] <= val )
{
arr [i] = arr [i / 2] ;
i = i / 2 ;
Programming in ‘C’ and Data Structure Lab File
52
}
arr [i] = val ;
}
void restoredown ( int pos, int *arr, int n )
{
int i, val ;
val = arr [pos] ;
while ( pos <= n / 2 )
{
i = 2 * pos ;
if ( ( i < n ) && ( arr [i] < arr [i + 1] ) )
i++ ;
if ( val >= arr [i] )
break ;
arr [pos] = arr [i] ;
pos = i ;
}
arr [pos] = val ;
}
void makeheap ( int *arr, int n )
{
int i ;
for ( i = n / 2 ; i >= 1 ; i-- )
restoredown ( i, arr, n ) ;
}
void add ( int val, int *arr, int *n )
{
( *n ) ++ ;
arr [*n] = val ;
restoreup ( *n, arr ) ;
}
int replace ( int i, int *arr, int n )
{
int r = arr [1] ;
arr [1] = i ;
for ( i = n / 2 ; i >= 1 ; i-- )
restoredown ( i, arr, n ) ;
return r ;
}
int del ( int *arr, int *n )
{
int val ;
val = arr [1] ;
arr [1] = arr [*n] ;
( *n ) -- ;
restoredown ( 1, arr, *n ) ;
return val ;
}
Programming in ‘C’ and Data Structure Lab File
53
OUTPUT:
Heap:
42 37 33 19 23 27 16 7 17
10
4 25 1 5 11
Element added 24.
Heap after addition of an element:
42 37 33 24 23 27 16 19 17
10
4 25 1 5 11 7
Element replaced 42.
Heap after replacement of an element:
37 24 33 19 23 27 16 7 17
10
4 25 1 5 11 2
Element deleted 37.
Heap after deletion of an element:
33 24 27 19 23 25 16 7 17
10
4 2 1 5 11

More Related Content

PDF
C programs
DOCX
Data structure new lab manual
DOCX
Data Structure Project File
DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
PDF
Data struture lab
PDF
Data structures lab manual
PDF
C lab excellent
DOC
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
C programs
Data structure new lab manual
Data Structure Project File
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Data struture lab
Data structures lab manual
C lab excellent
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

What's hot (20)

PDF
VTU Data Structures Lab Manual
DOC
Ds lab manual by s.k.rath
PDF
C Prog - Array
PDF
Understanding storage class using nm
PDF
Ds lab handouts
PDF
c-programming-using-pointers
PPT
Cquestions
PDF
Data structure lab manual
DOCX
Practical File of C Language
DOCX
Data Structure in C (Lab Programs)
PPTX
Function basics
PDF
Programming Fundamentals Arrays and Strings
PDF
Stl algorithm-Basic types
PPSX
C programming array & shorting
DOCX
C++ file
PDF
Implementing string
PDF
C Prog. - Strings (Updated)
PDF
C programms
VTU Data Structures Lab Manual
Ds lab manual by s.k.rath
C Prog - Array
Understanding storage class using nm
Ds lab handouts
c-programming-using-pointers
Cquestions
Data structure lab manual
Practical File of C Language
Data Structure in C (Lab Programs)
Function basics
Programming Fundamentals Arrays and Strings
Stl algorithm-Basic types
C programming array & shorting
C++ file
Implementing string
C Prog. - Strings (Updated)
C programms
Ad

Viewers also liked (17)

PDF
Ankit_Raman_Resume_With_Sound_Knowledge_Of_Data Structure_C Language_DBMS_Ora...
PDF
Data structure lab 03ameer hamza
PDF
Data Structures and Algorithms made Easy (Cover Page)
DOCX
Assignment
PDF
Data structures lab c programs
PDF
Data structures and algorithms made easy
PDF
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
DOC
Datastructure notes
PDF
Infix prefix postfix expression -conversion
PPTX
Structure in C
PDF
Lab manual of C++
PPT
C Structures And Unions
PDF
C notes.pdf
PPTX
Data structure and its types
PDF
Introduction to programming c and data-structures
PPT
Data structures
PDF
Relational Database Management System
Ankit_Raman_Resume_With_Sound_Knowledge_Of_Data Structure_C Language_DBMS_Ora...
Data structure lab 03ameer hamza
Data Structures and Algorithms made Easy (Cover Page)
Assignment
Data structures lab c programs
Data structures and algorithms made easy
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
Datastructure notes
Infix prefix postfix expression -conversion
Structure in C
Lab manual of C++
C Structures And Unions
C notes.pdf
Data structure and its types
Introduction to programming c and data-structures
Data structures
Relational Database Management System
Ad

Similar to C program (20)

PDF
Most Important C language program
DOC
Final ds record
PDF
C Programming Interview Questions
PDF
Solution#includestdio.h#includeconio.h#includealloc.h.pdf
PPT
Wk11-linkedlist.ppt
PPT
Wk11-linkedlist.ppt
PPT
Linked List.ppt
PPT
Wk11-linkedlist.ppt
PPT
Linked list introduction and different operation
PPT
Wk11-linkedlist.ppt linked list complete iit
PPT
Singly Circular Linked List – Last node points to the first node.
PPT
Wk11-linkedlist.ppt
PPT
Wk11-linkedlist.ppt
PPT
linkedlistqwerwwdderdsddsseddddddsdrr.ppt
PDF
best notes in c language
PDF
String
PPT
Linked list1.ppt
PPTX
C-Arrays & Strings (computer programming).pptx
PPT
linkedlist.ppt
PPT
DS Unit 2.ppt
Most Important C language program
Final ds record
C Programming Interview Questions
Solution#includestdio.h#includeconio.h#includealloc.h.pdf
Wk11-linkedlist.ppt
Wk11-linkedlist.ppt
Linked List.ppt
Wk11-linkedlist.ppt
Linked list introduction and different operation
Wk11-linkedlist.ppt linked list complete iit
Singly Circular Linked List – Last node points to the first node.
Wk11-linkedlist.ppt
Wk11-linkedlist.ppt
linkedlistqwerwwdderdsddsseddddddsdrr.ppt
best notes in c language
String
Linked list1.ppt
C-Arrays & Strings (computer programming).pptx
linkedlist.ppt
DS Unit 2.ppt

Recently uploaded (20)

PPTX
sap open course for s4hana steps from ECC to s4
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
A Presentation on Artificial Intelligence
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Cloud computing and distributed systems.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
cuic standard and advanced reporting.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Spectral efficient network and resource selection model in 5G networks
sap open course for s4hana steps from ECC to s4
MIND Revenue Release Quarter 2 2025 Press Release
Building Integrated photovoltaic BIPV_UPV.pdf
Programs and apps: productivity, graphics, security and other tools
Encapsulation_ Review paper, used for researhc scholars
A Presentation on Artificial Intelligence
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Dropbox Q2 2025 Financial Results & Investor Presentation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Empathic Computing: Creating Shared Understanding
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Cloud computing and distributed systems.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Network Security Unit 5.pdf for BCA BBA.
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
cuic standard and advanced reporting.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectral efficient network and resource selection model in 5G networks

C program

  • 1. Programming in ‘C’ and Data Structure Lab File 1 Q1 WAP to count the no of spaces, alphabets, words and digits? #include <stdio.h> #include<conio.h> main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ++ndigit[c-'0']; else if (c == ' ' || c == 'n' || c == 't') ++nwhite; else ++nother; printf("digits ="); for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]); printf(", white space = %d, other = %dn", nwhite, nother); getch(); } OUTPUT: Enter a string: Hi this is c programming language. Contains 100 programs. Digits are: 3 Alphabets are: 46 White spaces are: 9
  • 2. Programming in ‘C’ and Data Structure Lab File 2 Q2 WAP to squeeze a string? #include<stdio.h> #include<conio.h> void squeeze() { char str[50] = "Hello this is string"; int i,j; for(i=j=0;str[i]!='0';i++) if(str[i]!=' ') str[j++]=str[i]; //str[j]='0'; for(i=0; str[i]!='0';i++) printf("%c" ,str[i]); } void main() { squeeze(); getch(); } OUTPUT: Enter a string: Hello, this is String. Squeeze String is: Hello,thisisString.
  • 3. Programming in ‘C’ and Data Structure Lab File 3 Q3 WAP to swap the values using the pointers? #include<stdio.h> #include<conio.h> int swap(int *a ,int *b) { int temp = *a; *a=*b; *b=temp; } void main() { int a,b; clrscr(); printf("Please enter A's value:"); scanf("%d",&a); printf("nPlease enter B's value:"); scanf("%d",&b); swap(&a,&b); printf("nAfter swapping A's value: %d",a); printf("nAfter swapping B's value: %d",b); getch(); } OUTPUT: Please enter A's value:5 Please enter B's value:8 After swapping A's value: 8 After swapping B's value: 5
  • 4. Programming in ‘C’ and Data Structure Lab File 4 Q4 WAP to compare two strings? #include <stdio.h> #include<conio.h> int compare_function(char *a,char *b) { while (*a != '0') { if (*a == *b) { a++; b++; } else return 1; } return 0; } int main() { char compare_function(); int result; char inputA[20],inputB[20]; printf("nEnter string A : "); gets(inputA); printf("nEnter string B : "); gets(inputB); result = compare_function(inputA,inputB); if (result == 0) printf("nStrings are same"); else printf("nStrings are different"); } OUTPUT: Enter first String: Hiii Enter second String:hello Strings are different
  • 5. Programming in ‘C’ and Data Structure Lab File 5 Q5 WAP to concatenate the string? #include<stdio.h> void concat(char* ,char* ); int main() { char str1[25],str2[25]; printf("nEnter First String:"); gets(str1); printf("nEnter Second String:"); gets(str2); concat(str1,str2); #include<stdio.h> void concat(char* ,char* ); int main() { char str1[25],str2[25]; printf("nEnter First String:"); gets(str1); printf("nEnter Second String:"); gets(str2); concat(str1,str2); //1 printf("nConcatenated String is %s",str1); return 0; } void concat(char *s1,char *s2) { while(*s1!='0') //2 s1++ while(*s2!='0') //3 { *s1=*s2; //4 s1++; s2++; } *s1='0'; //5 } OUTPUT: Enter the first string: Hello, Enter the second string: Good morning.. Concatenated string is: Hello, Good morning..
  • 6. Programming in ‘C’ and Data Structure Lab File 6 Q6. WAP which multiply two matrix? #include<stdio.h> int main() { int i,j,k,m,n,p,q; int a[10][10],b[10][10],c[10][10]; printf("Enter the order of matrix 1 m * nn"); scanf("%d%d",&m,&n); printf("Enter the order of matrix 2 p * qn"); scanf("%d%d",&p,&q); printf("Enter the elements of matrix 1n"); for(i=0;i<m;i++){ for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("Enter the elements of matrix 2n"); for(i=0;i<p;i++){ for(j=0;j<q;j++) { scanf("%d",&b[i][j]); } } printf("elements of matrix1 and 2 are:n"); for(i=0;i<m,i<p;i++){ for(j=0;j<n;j++) { printf("%d",a[i][j]); } printf("t"); for(j=0;j<n,j<q;j++) { printf("%d",b[i][j]); } printf("n"); } for(i=0;i<m;i++) { for(j=0;j<m;j++) { c[i][j]=0; for(k=0;k<m;k++) { c[i][j] += a[i][k] * b[k][j]; } } } printf("multiplication of matrix a and b is :n"); for(i=0;i<m;i++)
  • 7. Programming in ‘C’ and Data Structure Lab File 7 { for(j=0;j<m;j++) { printf("%d ",c[i][j]); } printf("n"); } return 0; } OUTPUT: Enter the order of matrix 1 m * n 3 3 Enter the order of matrix 2 p * q 3 3 Enter the elements of matrix 1 1 2 3 4 5 6 7 8 9 Enter the elements of matrix 2 1 2 3 4 5 6 7 8 9 Elements of matrix1 and 2 are: 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9 Multiplication of matrix a and b is: 30 36 42 66 81 96 102 126 150 -------------------------------- Process exited with return value 0 Press any key to continue . . .
  • 8. Programming in ‘C’ and Data Structure Lab File 8 Q7 WAP which reverse the string? #include <string.h> #include <stdio.h> #include<conio.h> /* reverse: reverse string s in place */ void reverse(char s[]) { int c, i, j; for (i = 0, j = strlen(s)-1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; } printf("%s",s); } void main() { char s[20]="hello! Good morning"; reverse(s); getch(); } OUTPUT: Enter a String : Hello! Good Morning.. Reverse String is : ..gninroM doog !olleH
  • 9. Programming in ‘C’ and Data Structure Lab File 9 Q8 WAP to perform insertion, deletion and traversing in singly link list? #include <stdio.h> #include <conio.h> #include <stdlib.h> typedef struct nodeType { int info; struct nodeType *next; }node; void createEmptyList(node **head); void traverseInOrder1(node *head); void traverseInReverse(node *head); node *searchInUnsortedList(node *head,int item); void auxillarySearch(node *head,node **ploc,node **loc,int item); void insertAtBegining(node **head, int item); void insertAtEnd(node **head, int item); void insertAfterElement(node *head, int item, int after); void deleteFromBegining(node **head); void deleteFromEnd(node **head); void deleteAfterElement(node *head, int after); void reverseList(node **head); void deleteList(node **head); void main() { node *head; int choice,element,after; createEmptyList(&head); while(1) { printf("ntOptions availablen"); printf("**********************************nn"); printf("1.Insert Element at beginingn"); printf("2. Insert Element at Endn"); printf("3.Insert Element after at given elementn"); printf("4.Traverse in ordern"); printf("5.Traverse in reverse ordern"); printf("6.Delete from begingign"); printf("7.Delete from endn"); printf("8.Delete from after given elementn"); printf("9.Reverse Link Listn"); printf("10.Exitn"); printf("Enter your choice (1-10)nn"); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the Elementn");
  • 10. Programming in ‘C’ and Data Structure Lab File 10 scanf("%d",&element); insertAtBegining(&head,element); break; case 2: printf("Enter the Elementn"); scanf("%d",&element); insertAtEnd(&head,element); break; case 3: printf("Enter the Elementn"); scanf("%d",&element); printf("Enter the after which to insert:"); scanf("%d",&after); insertAfterElement(head,element,after); break; case 4: if(head==NULL) printf("List is Empty"); else traverseInOrder1(head); printf("npress any key to continoun"); getch(); break; case 5: if(head==NULL) printf("List is Empty"); else traverseInReverse(head); printf("npress any key to continoun"); getch(); break; case 6: deleteFromBegining(&head); break; case 7: deleteFromEnd(&head); break; case 8: printf("Enter the element after which element deletedn"); scanf("%d",&after); deleteAfterElement(head,after); break; case 9: reverseList(&head); break; case 10: deleteList(&head); exit(1); } } }
  • 11. Programming in ‘C’ and Data Structure Lab File 11 void createEmptyList(node **head) { *head=NULL; } void traverseInOrder1(node *head) { while(head!=NULL) { printf("%d",head->info); head=head->next; } } void traverseInReverse(node *head) { if(head->next!=NULL) traverseInReverse(head->next); printf("%d",head->info); } node *searchInUnsortedList(node *head,int item) { while(head!=NULL && head->info==item) { head=head->next; } return head; } void auxillarySearch(node *head,node **ploc,node **loc, int item) { int flag=0; if(head==NULL) *ploc=*loc=NULL; else if(head->info==item) { *ploc=NULL; *loc=head; } else { *ploc=head; *loc=head->next; while((*loc!=NULL)&&(!flag)) { if((*loc)->info==item) flag=1; else { *ploc=*loc; *loc=(*loc)->next; } }
  • 12. Programming in ‘C’ and Data Structure Lab File 12 } } void insertAtBegining(node **head,int item) { node *ptr; ptr=(node *)malloc(sizeof(node)); ptr->info=item; if(*head==NULL) ptr->next=NULL; else ptr->next=*head; *head=ptr; } void insertAtEnd(node **head,int item) { node *ptr,*loc; ptr=(node *)malloc(sizeof(node)); ptr->info=item; ptr->next=NULL; if(*head==NULL) *head=ptr; else { loc=*head; while(loc->next!=NULL) loc=loc->next; loc->next=ptr; } } void insertAfterElement(node *head,int item,int after) { node *ptr; node *loc; loc=searchInUnsortedList(head,after); if(loc==(node *)NULL) return ; ptr=(node * )malloc(sizeof(node)); ptr->info=item; ptr->next=loc->next; loc->next=ptr; } void deleteFromBegining(node **head) { node *ptr; if(*head==NULL) return; else { ptr=*head; *head=(*head)->next;
  • 13. Programming in ‘C’ and Data Structure Lab File 13 free(ptr); } } void deleteFromEnd(node **head) { node *ptr, *loc; if(*head==NULL) return; else if((*head)->next==(node*)NULL) { ptr=*head; *head=NULL; free(ptr); } else { loc=*head; ptr=(*head)->next; while(ptr->next!=NULL) { loc=ptr; ptr=ptr->next; } loc->next=NULL; free(ptr); } } void deleteAfterElement(node *head,int after) { node *ptr, *loc; loc=searchInUnsortedList(head,after); if(loc==(node *)NULL) return; ptr=loc->next; loc->next=ptr->next; free(ptr); } void reverseList(node **head) { node *previousNode , *currentNode,*nextNode; currentNode=*head; nextNode=currentNode->next; previousNode=NULL; currentNode->next=NULL; while(nextNode!=NULL) { previousNode=currentNode; currentNode=nextNode; nextNode=currentNode->next; currentNode->next=previousNode; }
  • 14. Programming in ‘C’ and Data Structure Lab File 14 *head=currentNode; } void deleteList(node **head) { node *ptr; while(*head !=NULL) { ptr=*head; *head=(*head)->next; free(ptr); } } Output: 1. Insert elements at beginning 2. Insert element at mid 3. Insert element at end 4. Insert element at after 5. Delete element from beginning 6. Delete element from mid 7. Delete element after element 8. Traverse the link list 9. Exit Enter your choice: 1 Enter element: 3 Element inserted. Enter your choice: 3 Enter element: 8 Element inserted. Enter your choice: 2 Enter element: 2 Element inserted. Enter your choice: 8 Link list elements are: 3 2 8 Enter your choice: 5 Element 3 is deleted. Enter your choice: 9 Press any key to continue……
  • 15. Programming in ‘C’ and Data Structure Lab File 15 Q9 WAP to implement polynomial addition and multiplication? #include<stdio.h> #include<stdlib.h> struct node { float coef; int expo; struct node *link; }; struct node *create(struct node *); struct node *insert_s(struct node *,float,int); struct node *insert(struct node *,float,int); void display(struct node *ptr); void poly_add(struct node *,struct node *); void poly_mult(struct node *,struct node *); main( ) { struct node *start1=NULL,*start2=NULL; printf("Enter polynomial 1 :n"); start1=create(start1); printf("Enter polynomial 2 :n"); start2=create(start2); printf("Polynomial 1 is : "); display(start1); printf("Polynomial 2 is : "); display(start2); poly_add(start1, start2); poly_mult(start1, start2); } struct node *create(struct node *start) { int i,n,ex; float co; printf("Enter the number of terms : "); scanf("%d",&n); for(i=1;i<=n;i++) { printf("Enter coeficient for term %d : ",i); scanf("%f",&co); printf("Enter exponent for term %d : ",i); scanf("%d",&ex); start=insert_s(start,co,ex); } return start; }
  • 16. Programming in ‘C’ and Data Structure Lab File 16 struct node *insert_s(struct node *start,float co,int ex) { struct node *ptr,*tmp; tmp=(struct node *)malloc(sizeof(struct node)); tmp->coef=co; tmp->expo=ex; if(start==NULL || ex > start->expo) { tmp->link=start; start=tmp; } else { ptr=start; while(ptr->link!=NULL && ptr->link->expo >= ex) ptr=ptr->link; tmp->link=ptr->link; ptr->link=tmp; } return start; } struct node *insert(struct node *start,float co,int ex) { struct node *ptr,*tmp; tmp=(struct node *)malloc(sizeof(struct node)); tmp->coef=co; tmp->expo=ex; /*If list is empty*/ if(start==NULL) { tmp->link=start; start=tmp; } else { ptr=start; while(ptr->link!=NULL) ptr=ptr->link; tmp->link=ptr->link; ptr->link=tmp; } return start; } void display(struct node *ptr) { if(ptr==NULL) { printf("Zero polynomialn"); return; } while(ptr!=NULL)
  • 17. Programming in ‘C’ and Data Structure Lab File 17 { printf("(%.1fx^%d)", ptr->coef,ptr->expo); ptr=ptr->link; if(ptr!=NULL) printf(" + "); else printf("n"); } } void poly_add(struct node *p1,struct node *p2) { struct node *start3; start3=NULL; while(p1!=NULL && p2!=NULL) { if(p1->expo > p2->expo) { start3=insert(start3,p1->coef,p1->expo); p1=p1->link; } else if(p2->expo > p1->expo) { start3=insert(start3,p2->coef,p2->expo); p2=p2->link; } else if(p1->expo==p2->expo) { start3=insert(start3,p1->coef+p2->coef,p1->expo); p1=p1->link; p2=p2->link; } } while(p1!=NULL) { start3=insert(start3,p1->coef,p1->expo); p1=p1->link; } while(p2!=NULL) { start3=insert(start3,p2->coef,p2->expo); p2=p2->link; } printf("Added polynomial is : "); display(start3); } void poly_mult(struct node *p1, struct node *p2) { struct node *start3; struct node *p2_beg = p2; start3=NULL; if(p1==NULL || p2==NULL)
  • 18. Programming in ‘C’ and Data Structure Lab File 18 { printf("Multiplied polynomial is zero polynomialn"); return; } while(p1!=NULL) { p2=p2_beg; while(p2!=NULL) { start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo); p2=p2->link; } p1=p1->link; } printf("Multiplied polynomial is : "); display(start3); } OUTPUT: Enter polynomial 1 : Enter the number of terms : 4 Enter coeficient for term 1 : 5 Enter exponent for term 1 : 4 Enter coeficient for term 2 : 7 Enter exponent for term 2 : 3 Enter coeficient for term 3 : 9 Enter exponent for term 3 : 2 Enter coeficient for term 4 : 3 Enter exponent for term 4 : 1 Enter polynomial 2 : Enter the number of terms : 3 Enter coeficient for term 1 : 5 Enter exponent for term 1 : 3 Enter coeficient for term 2 : 6 Enter exponent for term 2 : 2 Enter coeficient for term 3 : 9 Enter exponent for term 3 : 1 Polynomial 1 is : (5x^4) + (7x^3) + (9x^2) + (3x^1) Polynomial 2 is : (5x^3) + (6x^2) + (9x^1) Added polynomial is : (5x^4) + (12x^3) + (15x^2) + (12x^1) Multiplied polynomial is z: (25x^7) + (30x^6) + (35x^6) + (45x^5) + (42x^5) + (45x^5) + (63x^4) + (54x^4) + (15x^4) + (81x^3) + (18x^3) + (27.0x^2)
  • 19. Programming in ‘C’ and Data Structure Lab File 19 Q10 WAP to implement stack using array? #include<stdlib.h #include<stdio.h> #include<conio.h> #define MAX 50 void push(); void pop(); void display(); int stack[MAX], top=-1, item; void main() { int ch; do { printf("nnnn1.tPushn2.tPopn3.tDisplayn4.tExitn" ); printf("nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf("nnInvalid entry. Please try again...n"); } } while(ch!=4); getch(); } void push() { if(top == MAX-1) printf("nnStack is full."); else { printf("nnEnter ITEM: "); scanf("%d", &item); top++; stack[top] = item; printf("nnITEM inserted = %d", item); } }
  • 20. Programming in ‘C’ and Data Structure Lab File 20 void pop() { if(top == -1) printf("nnStack is empty."); else { item = stack[top]; top--; printf("nnITEM deleted = %d", item); } } void display() { int i; if(top == -1) printf("nnStack is empty."); else { for(i=top; i>=0; i--) printf("n%d", stack[i]); } } OUTPUT: 1. Push 2. Pop 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 1 ITEM inserted = 1 1. Push 2. Pop 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 2 ITEM inserted = 2 1. Push 2. Pop 3. Display 4. Exit Enter your choice: 3 2 1 1. Push 2. Pop 3. Display 4. Exit Enter your choice: 2 ITEM deleted = 2
  • 21. Programming in ‘C’ and Data Structure Lab File 21 Q11 WAP to implement stack using link list? #include <stdlib.h> #include <stdio.h> #include <conio.h> void push(); void pop(); void display(); struct node { int info; struct node *link; } *top = NULL; int item; void main() { int ch; do { printf("nn1. Pushn2. Popn3. Displayn4. Exitn"); printf("nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf("Invalid choice. Please try again.n"); } } while(1); getch(); } void push() { struct node *ptr; printf("nnEnter ITEM: "); scanf("%d", &item); if (top == NULL) { top = (struct node *)malloc(sizeof(struct node)); top->info = item; top->link = NULL;
  • 22. Programming in ‘C’ and Data Structure Lab File 22 } else { ptr = top; top = (struct node *)malloc(sizeof(struct node)); top->info = item; top->link = ptr; } printf("nItem inserted: %dn", item); } void pop() { struct node *ptr; if (top == NULL) printf("nnStack is emptyn"); else { ptr = top; item = top->info; top = top->link; free(ptr); printf("nnItem deleted: %d", item); } } void display() { struct node *ptr; if (top == NULL) printf("nnStack is emptyn"); else { ptr = top; while(ptr != NULL) { printf("nn%d", ptr- >info); ptr = ptr->link; } } } Output: 1. Push 2. Pop 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 1 Item inserted: 1
  • 23. Programming in ‘C’ and Data Structure Lab File 23 1. Push 2. Pop 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 2 Item inserted: 2 1. Push 2. Pop 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 3 Item inserted: 3 1. Push 2. Pop 3. Display 4. Exit Enter your choice: 3 3 2 1 1. Push 2. Pop 3. Display 4. Exit Enter your choice: 2 Item deleted: 3
  • 24. Programming in ‘C’ and Data Structure Lab File 24 Q12 WAP to evaluate postfix expression? #include<stdio.h> #include<ctype.h> #include<stdlib.h> #define SIZE 40 int stack[SIZE]; int top=-1; void push(int n) { if(top==SIZE-1) { printf("Stack is fulln"); return; } else { top=top+1; stack[top]=n; printf("Pushed element is %dn",n); } } int pop() { int n; if(top==-1) { printf("Stack is emptyn"); return 0; } else { n=stack[top]; top=top-1; printf("The poped element is %dn",n); return(n); } } int evaluate(int op1, int op2,char ch) { printf("op1=%d op2=%d ch=%cn",op1,op2,ch); int n; if (op1<op2) { n=op1; op1=op2; op2=n; } if(ch=='+') n=op1+op2; else if(ch=='-') n=op1-op2;
  • 25. Programming in ‘C’ and Data Structure Lab File 25 else if(ch=='*') n=op1*op2; else if(ch=='/') n=op1/op2; else if(ch=='%') n=op1%op2; else { printf("The operator is not identifiedn"); exit(0); } printf("n=%dn",n); return(n); } int main() { char str[50],ch,ch1; int i=0,n,op1,op2; printf("Enter the Postfix stringn"); scanf("%s",str); ch=str[i]; while(ch!='0') { printf("The char is=%cn",ch); //if(ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5')// if(isdigit(ch)) { n=ch-'0'; push(n); } else { op1=pop(); op2=pop(); n=evaluate(op1,op2,ch); push(n); } ch=str[++i]; } printf("The value of the arithmetic expression is=%dn",pop()); return 0; } OUTPUT: Enter the expression: 7 5 – 9 2 / * The value of arithmetic expression is: 9
  • 26. Programming in ‘C’ and Data Structure Lab File 26 Q13 WAP to implement Queue using Array? #include <stdlib.h> #include <conio.h> #include <stdio.h> #define MAX 50 void insert(); void delet(); void display(); int queue[MAX], rear=-1, front=-1, item; void main() { int ch; do { printf("nn1. Insertn2. Deleten3. Displayn4. Exitn"); printf("nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1: insert(); break; case 2: delet(); break; case 3: display(); break; case 4: exit(0); default: printf("nnInvalid entry. Please try again...n"); } } while(1); getch(); } void insert() { if(rear == MAX-1) printf("nnQueue is full."); else { printf("nnEnter ITEM: "); scanf("%d", &item); if (rear == -1 && front == -1) { rear = 0; front = 0; } else
  • 27. Programming in ‘C’ and Data Structure Lab File 27 rear+ +; queue[rear] = item; printf("nnItem inserted: %d", item); } } void delet() { if(front == -1) printf("nnQueue is empty."); else { item = queue[front]; if (front == rear){ front = -1; rear = -1;} else front++; printf("nnItem deleted: %d", item); } } void display() { int i; if(front == -1) printf("nnQueue is empty."); else { printf("nn"); for(i=front; i<=rear; i++) printf(" %d", queue[i]); } } OUTPUT: 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 1 Item inserted: 1 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 2 Item inserted: 2 Enter your choice: 1 Enter ITEM: 3 Item inserted: 3 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 3 1 2 3 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 2 Item deleted: 1
  • 28. Programming in ‘C’ and Data Structure Lab File 28 Q14 WAP to queue implement using link list? #include<stdio.h> #include<stdlib.h> #include<conio.h> struct node { int info; struct node *link; }*front = NULL, *rear = NULL; void insert(); void delet(); void display(); int item; void main() { int ch; do { printf("nn1.tInsertn2.tDeleten3.tDisplayn4.tExitn"); printf("nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1: insert(); break; case 2: delet(); break; case 3: display(); break; case 4: exit(0); default: printf("nnInvalid choice. Please try again...n"); } } while(1); getch(); } void insert() { printf("nnEnter ITEM: "); scanf("%d", &item); if(rear == NULL) { rear = (struct node *)malloc(sizeof(struct node)); rear->info = item; rear->link = NULL; front = rear;
  • 29. Programming in ‘C’ and Data Structure Lab File 29 } else { rear->link = (struct node *)malloc(sizeof(struct node)); rear = rear->link; rear->info = item; rear->link = NULL; } } void delet() { struct node *ptr; if(front == NULL) printf("nnQueue is empty.n"); else { ptr = front; item = front->info; front = front->link; free(ptr); printf("nItem deleted: %dn", item); if(front == NULL) rear = NULL; } } void display() { struct node *ptr = front; if(rear == NULL) printf("nnQueue is empty.n"); else { printf("nn"); while(ptr != NULL) { printf("%dt",ptr->info); ptr = ptr->link; } } } OUTPUT: 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 1
  • 30. Programming in ‘C’ and Data Structure Lab File 30 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 12 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 13 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 3 1 12 13 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 2 Item deleted: 1
  • 31. Programming in ‘C’ and Data Structure Lab File 31 Q15 WAP to implement circular queue using array? #include<stdlib.h> #include<conio.h> #include<stdio.h> #define SIZE 5 void insert(); void delet(); void display(); int queue[SIZE], rear=-1, front=-1, item; void main() { int ch; do { printf("n1.tInsertn2.tDeleten3.tDisplayn4.tExitn"); printf("nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1: insert(); break; case 2: delet(); break; case 3: display(); break; case 4: exit(0); default: printf("nnInvalid choice. Pleasr try again...n"); } } while(1); getch(); } void insert() { if((front==0 && rear==SIZE-1) || (front==rear+1)) printf("nnQueue is full."); else { printf("nnEnter ITEM: "); scanf("%d", &item); if(rear == -1) { rear = 0; front = 0;
  • 32. Programming in ‘C’ and Data Structure Lab File 32 } else if(rear == SIZE-1) rear = 0; else rear++; queue[rear] = item; printf("nnItem inserted: %dn", item); } } void delet() { if(front == -1) printf("nnQueue is empty.n"); else { item = queue[front]; if(front == rear) { front = -1; rear = -1; } else if(front == SIZE-1) front = 0; else front++; printf("nnITEM deleted: %d", item); } } void display() { int i; if((front == -1) || (front==rear+1)) printf("nnQueue is empty.n"); else { printf("nn"); for(i=front; i<=rear; i++) printf("t%d",queue[i]); } } OUTPUT: 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 10 Item inserted: 10
  • 33. Programming in ‘C’ and Data Structure Lab File 33 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 20 Item inserted: 20 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 30 Item inserted: 30 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 3 10 20 30 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 2 ITEM deleted: 10 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 3 20 30
  • 34. Programming in ‘C’ and Data Structure Lab File 34 Q16 WAP to perform addition, transpose, and multiplication operations of sparse matrix? #include<stdlib.h> #include<stdio.h> #include<conio.h> void implement(int[][10],int,int,int[][10]); void add(int[][10],int,int,int[][10],int[][10]); void sub(int[][10],int,int,int[][10],int[][10]); void transpose(int[][10],int,int[][10]); void main() { int a[10][10]; int b[10][10]; int c[10][10]; int d[10][10]; int e[50][10]; int row,col,k,l,ch; char che; clrscr(); printf("enter the no. of rowst:"); scanf("%d",&row); printf("n enter the no. of colomst:"); scanf("%d",&col); printf("n enter the elements of sparse matrix t:"); for(k=0;k<row;k++) { for(l=0;l<col;l++) { printf("n element at [%d][%d]t",k,l); scanf("%d",&a[k][l]); } } printf("sparse matrix is n"); for(k=0;k<row;k++) { for(l=0;l<col;l++) { printf("%dt",a[k][l]); } printf("n"); } do { printf("n CYNET MENUn"); printf("1:REPRESENTATIONn"); printf("2:ADDITIONn"); printf("3:SUBTRACTIONn"); printf("4:TRANSPOSEn"); printf("5:EXITn"); printf("enter your choice:");
  • 35. Programming in ‘C’ and Data Structure Lab File 35 scanf("%d",&ch); switch(ch) { case 1: implement(a,row,col,b); break; case 2: printf("n enter the elements of second sparse matrix t:"); for(k=0;k<row;k++) { for(l=0;l<col;l++) { printf("n element at [%d][%d]t",k,l); scanf("%d",&c[k][l]); } } printf("sparse matrix is n"); for(k=0;k<row;k++) { for(l=0;l<col;l++) { printf("%dt",c[k][l]); } printf("n"); } implement(a,row,col,b); implement(c,row,col,d); add(b,row,col,d,e); break; case 3: printf("n enter the elements of second sparse matrix t:"); for(k=0;k<row;k++) { for(l=0;l<col;l++) { printf("n element at [%d][%d]t",k,l); scanf("%d",&c[k][l]); } } printf("sparse matrix is n"); for(k=0;k<row;k++) { for(l=0;l<col;l++) { printf("%dt",c[k][l]); } printf("n"); } implement(a,row,col,b); implement(c,row,col,d);
  • 36. Programming in ‘C’ and Data Structure Lab File 36 sub(b,row,col,d,e); break; case 4: implement(a,row,col,b); transpose(b,col,c); break; case 5: exit(0); default: printf("you entered wrong choicen"); } printf("do you want to continue(yY):"); che=getche(); }while(che=='y'||che=='Y'); getch(); } void implement(int a[][10],int row,int col,int b[][10]) { int g=1,nz=0,k,l; for(k=0;k<row;k++) { for(l=0;l<col;l++) { if(a[k][l]!=0) { nz=nz+1; } } } b[0][0]=row; b[0][1]=col; b[0][2]=nz; for(k=0;k<row;k++) { for(l=0;l<col;l++) { if(a[k][l]!=0) { b[g][0]=k; b[g][1]=l; b[g][2]=a[k][l]; g++; } } } printf("implementation of sparse matrix isn"); for(k=0;k<g;k++) { for(l=0;l<3;l++) {
  • 37. Programming in ‘C’ and Data Structure Lab File 37 printf("%dt",b[k][l]); } printf("n"); } } void add(int b[][10],int row,int col,int d[][10],int e[][10]) { int p1=1,p2=1,i=1; int k,l; if(b[0][0]!=d[0][0]) { printf("addition is not possiblen"); } else { while(p1<=b[0][2]&&p2<=d[0][2]) { if(b[p1][0]==d[p2][0]) { if(b[p1][1]<d[p2][1]) { e[i][0]=b[p1][0]; e[i][1]=b[p1][1]; e[i][2]=b[p1][2]; i++; p1++; } else if(b[p1][1]>d[p2][1]) { e[i][0]=d[p2][0]; e[i][1]=d[p2][1]; e[i][2]=d[p2][2]; i++; p2++; } else if(b[p1][1]==d[p2][1]) { e[i][0]=d[p1][0]; e[i][1]=d[p1][1]; e[i][2]=b[p1][2]+d[p2][2]; if(e[i][2]!=0) { i++; } p1++; p2++; } } else if(b[p1][0]<d[p2][0]) {
  • 38. Programming in ‘C’ and Data Structure Lab File 38 e[i][0]=b[p1][0]; e[i][1]=b[p1][1]; e[i][2]=b[p1][2]; i++; p1++; } else if(b[p1][0]>d[p2][0]) { e[i][0]=d[p2][0]; e[i][1]=d[p2][1]; e[i][2]=d[p2][2]; i++; p2++; } } if(p1!=b[0][2]) { while(p1<=b[0][2]) { e[i][0]=b[p1][0]; e[i][1]=b[p1][1]; e[i][2]=b[p1][2]; i++; p1++; } } else if(p2!=d[0][2]) { while(p2<=d[0][2]) { e[i][0]=d[p2][0]; e[i][1]=d[p2][1]; e[i][2]=d[p2][2]; i++; p2++; } } e[0][0]=row; e[0][1]=col; e[0][2]=i-1; printf("matrix after additionn"); for(k=0;k<i;k++) { for(l=0;l<3;l++) { printf("%dt",e[k][l]); } printf("n"); } } }
  • 39. Programming in ‘C’ and Data Structure Lab File 39 void sub(int b[][10],int row,int col,int d[][10],int e[][10]) { int p1=1,p2=1,i=1; int k,l; if(b[0][0]!=d[0][0]) { printf("subtraction is not possiblen"); } else { while(p1<=b[0][2]&&p2<=d[0][2]) { if(b[p1][0]==d[p2][0]) { if(b[p1][1]<d[p2][1]) { e[i][0]=b[p1][0]; e[i][1]=b[p1][1]; e[i][2]=b[p1][2]; i++; p1++; } else if(b[p1][1]>d[p2][1]) { e[i][0]=d[p2][0]; e[i][1]=d[p2][1]; e[i][2]=d[p2][2]; i++; p2++; } else if(b[p1][1]==d[p2][1]) { e[i][0]=d[p1][0]; e[i][1]=d[p1][1]; e[i][2]=b[p1][2]-d[p2][2]; if(e[i][2]!=0) { i++; } p1++; p2++; } } else if(b[p1][0]<d[p2][0]) { e[i][0]=b[p1][0]; e[i][1]=b[p1][1]; e[i][2]=b[p1][2]; i++; p1++;
  • 40. Programming in ‘C’ and Data Structure Lab File 40 } else if(b[p1][0]>d[p2][0]) { e[i][0]=d[p2][0]; e[i][1]=d[p2][1]; e[i][2]=d[p2][2]; i++; p2++; } } if(p1!=b[0][2]) { while(p1<=b[0][2]) { e[i][0]=b[p1][0]; e[i][1]=b[p1][1]; e[i][2]=b[p1][2]; i++; p1++; } } else if(p2!=d[0][2]) { while(p2<=d[0][2]) { e[i][0]=d[p2][0]; e[i][1]=d[p2][1]; e[i][2]=d[p2][2]; i++; p2++; } } e[0][0]=row; e[0][1]=col; e[0][2]=i-1; printf("matrix after subtractionn"); for(k=0;k<=i;k++) { for(l=0;l<3;l++) { printf("%dt",e[k][l]); } printf("n"); } } } void transpose(int b[][10],int col,int c[][10]) { int i,j,k,temp; for(i=0;i<=b[0][2];i++) {
  • 41. Programming in ‘C’ and Data Structure Lab File 41 c[i][0]=b[i][1]; c[i][1]=b[i][0]; c[i][2]=b[i][2]; } for(i=1;i<=b[0][2];i++) { for(j=i+1;j<=b[0][2];j++) { if(c[i][0]>c[j][0]) { for(k=0;k<3;k++) { temp=c[i][k]; c[i][k]=c[j][k]; c[j][k]=temp; } } } } printf("transpose of matrix isn"); for(i=0;i<=c[0][2];i++) { for(j=0;j<3;j++) { printf("%dt",c[i][j]); } printf("n"); } } OUTPUT: Enter the no of the rows: 3 Enter the no of the columns: 3 Enter the elements: 0 0 0 4 0 0 0 7 0 The sparse matrix is: 2 1 4 3 2 7 Enter the elements of second matrix: 2 0 0 6 0 0 5 0 0 The second sparse matrix is: 1 1 2 2 1 6 3 1 5 Addition of sparse matrix is: multiplication of sparse matrix is: 0 0 2 2 1 8 0 0 10 3 1 42 2 0 5
  • 42. Programming in ‘C’ and Data Structure Lab File 42 Q17 WAP to perform insertion, deletion, traversing and searching in Binary search tree? #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node *left,*right; }; struct node *root; void insert(int x) { struct node *p,*previous,*current; p=(struct node *)malloc(sizeof(struct node)); if(p==NULL) { printf("n Out of memory"); } p->data=x; p->left=NULL; p->right=NULL; if(root==NULL) { root=p; return; } previous=NULL; current=root; while(current!=NULL) { previous=current; if(p->data<current->data) current=current->left; else current=current->right; } if(p->data<previous->data) previous->left=p; else previous->right=p; } void inorder(struct node *t) { if (t!=NULL) { inorder(t->left); printf("n %5d",t->data);
  • 43. Programming in ‘C’ and Data Structure Lab File 43 inorder (t->right); } } void del(int x) { int tright=0,tleft=0; struct node *ptr=root; struct node *parent=root; struct node *t1=root; struct node *temp=root; while(ptr!=NULL&& ptr->data!=x) { parent=ptr; if (x<ptr->data) ptr=ptr->left; else ptr=ptr->right; } if (ptr==NULL) { printf("n Delete element not found"); return ; } else if(t1->data==x && (t1->left ==NULL || t1->right==NULL)) if(t1->left==NULL) t1=t1->right; else t1=t1->left; else if (ptr->left==NULL) if (x<parent->data) parent->left=ptr->right; else parent->right=ptr->right; else if (ptr->right==NULL) if (x<parent->data) parent->left=ptr->left; else parent->right=ptr->left; else { temp=ptr; parent=ptr; if((ptr->left)>=(ptr->right)) { ptr=ptr->left; while(ptr->right!=NULL) { tright=1; parent=ptr; ptr=ptr->right; }
  • 44. Programming in ‘C’ and Data Structure Lab File 44 temp->data=ptr->data; if(tright) parent->right=ptr->left; else parent->left=ptr->left; } else { ptr=ptr->right; while (ptr->left!=NULL) { tleft=1; parent=ptr; ptr=ptr->left; } temp->data=ptr->data; if(tleft) parent->left=ptr->right; else parent->right=ptr->right; } free(ptr); } } void main() { int op,n,srchno; root=(struct node *)malloc(sizeof(struct node)); root->data=30; root->right=root->left=NULL; clrscr(); do { printf("n 1.Insertion"); printf("n 2.Deletion"); printf("n 3.Inorder"); printf("n 4.Quit"); printf("n Enter your choicen"); scanf("%d",&op); switch (op) { case 1: printf("n Enter the element to insertn"); scanf("%d",&n); insert(n); break; case 2: printf("n Enter the element to be deletedn"); scanf("%d",&srchno); del(srchno); break; case 3: printf("n The inorder elements aren");
  • 45. Programming in ‘C’ and Data Structure Lab File 45 inorder(root); getch(); break; default: exit(0); } }while(op<4); getch(); } OUTPUT: 1. Insertion 2. Deletion 3. Traversing 4. Quit Enter your choice: 1 Enter the element: 1 Enter the element: 5 Enter the element: 3 Enter the element: 8 Enter your choice 2 Enter the no. to be deleted 8 No is deleted Enter your choice: 3 Tree element are: 1 5 3 Press any key to continue…
  • 46. Programming in ‘C’ and Data Structure Lab File 46 Q18 WAP to implement Depth first graph? #include <stdio.h> #include <conio.h> #include <alloc.h> #define TRUE 1 #define FALSE 0 #define MAX 8 struct node { int data ; struct node *next ; } ; int visited[MAX] ; void dfs ( int, struct node ** ) ; struct node * getnode_write ( int ) ; void del ( struct node * ) ; void main( ) { struct node *arr[MAX] ; struct node *v1, *v2, *v3, *v4 ; int i ; clrscr( ) ; v1 = getnode_write ( 2 ) ; arr[0] = v1 ; v1 -> next = v2 = getnode_write ( 3 ) ; v2 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[1] = v1 ; v1 -> next = v2 = getnode_write ( 4 ) ; v2 -> next = v3 = getnode_write ( 5 ) ; v3 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[2] = v1 ; v1 -> next = v2 = getnode_write ( 6 ) ; v2 -> next = v3 = getnode_write ( 7 ) ; v3 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[3] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[4] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 3 ) ; arr[5] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 3 ) ; arr[6] = v1 ;
  • 47. Programming in ‘C’ and Data Structure Lab File 47 v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 4 ) ; arr[7] = v1 ; v1 -> next = v2 = getnode_write ( 5 ) ; v2 -> next = v3 = getnode_write ( 6 ) ; v3 -> next = v4 = getnode_write ( 7 ) ; v4 -> next = NULL ; dfs ( 1, arr ) ; for ( i = 0 ; i < MAX ; i++ ) del ( arr[i] ) ; getch( ) ; } void dfs ( int v, struct node **p ) { struct node *q ; visited[v - 1] = TRUE ; printf ( "%dt", v ) ; q = * ( p + v - 1 ) ; while ( q != NULL ) { if ( visited[q -> data - 1] == FALSE ) dfs ( q -> data, p ) ; else q = q -> next ; } } struct node * getnode_write ( int val ) { struct node *newnode ; newnode = ( struct node * ) malloc ( sizeof ( struct node ) ) ; newnode -> data = val ; return newnode ; } void del ( struct node *n ) { struct node *temp ; while ( n != NULL ) { temp = n -> next ; free ( n ) ; n = temp ; } } OUTPUT: Traversing of DFS Graph is : 1 2 3 4 5 6 7 8
  • 48. Programming in ‘C’ and Data Structure Lab File 48 Q19 WAP to implement Breadth first shortest path? #include <stdio.h> #include <conio.h> #include <alloc.h> #define TRUE 1 #define FALSE 0 #define MAX 8 struct node { int data ; struct node *next ; } ; int visited[MAX] ; int q[8] ; int front, rear ; void bfs ( int, struct node ** ) ; struct node * getnode_write ( int ) ; void addqueue ( int ) ; int deletequeue( ) ; int isempty( ) ; void del ( struct node * ) ; void main( ) { struct node *arr[MAX] ; struct node *v1, *v2, *v3, *v4 ; int i ; clrscr( ) ; v1 = getnode_write ( 2 ) ; arr[0] = v1 ; v1 -> next = v2 = getnode_write ( 3 ) ; v2 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[1] = v1 ; v1 -> next = v2 = getnode_write ( 4 ) ; v2 -> next = v3 = getnode_write ( 5 ) ; v3 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[2] = v1 ; v1 -> next = v2 = getnode_write ( 6 ) ; v2 -> next = v3 = getnode_write ( 7 ) ; v3 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[3] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[4] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ;
  • 49. Programming in ‘C’ and Data Structure Lab File 49 v1 = getnode_write ( 3 ) ; arr[5] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 3 ) ; arr[6] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 4 ) ; arr[7] = v1 ; v1 -> next = v2 = getnode_write ( 5 ) ; v2 -> next = v3 = getnode_write ( 6 ) ; v3 -> next = v4 = getnode_write ( 7 ) ; v4 -> next = NULL ; front = rear = -1 ; bfs ( 1, arr ) ; for ( i = 0 ; i < MAX ; i++ ) del ( arr[i] ) ; getch( ) ; } void bfs ( int v, struct node **p ) { struct node *u ; visited[v - 1] = TRUE ; printf ( "%dt", v ) ; addqueue ( v ) ; while ( isempty( ) == FALSE ) { v = deletequeue( ) ; u = * ( p + v - 1 ) ; while ( u != NULL ) { if ( visited [ u -> data - 1 ] == FALSE ) { addqueue ( u -> data ) ; visited [ u -> data - 1 ] = TRUE ; printf ( "%dt", u -> data ) ; } u = u -> next ; } } } struct node * getnode_write ( int val ) { struct node *newnode ; newnode = ( struct node * ) malloc ( sizeof ( struct node ) ) ; newnode -> data = val ; return newnode ; } void addqueue ( int vertex ) {
  • 50. Programming in ‘C’ and Data Structure Lab File 50 if ( rear == MAX - 1 ) { printf ( "nQueue Overflow." ) ; exit( ) ; } rear++ ; q[rear] = vertex ; if ( front == -1 ) front = 0 ; } int deletequeue( ) { int data ; if ( front == -1 ) { printf ( "nQueue Underflow." ) ; exit( ) ; } data = q[front] ; if ( front == rear ) front = rear = -1 ; else front++ ; return data ; } int isempty( ) { if ( front == -1 ) return TRUE ; return FALSE ; } void del ( struct node *n ) { struct node *temp ; while ( n != NULL ) { temp = n -> next ; free ( n ) ; n = temp ; } }
  • 51. Programming in ‘C’ and Data Structure Lab File 51 Q20 WAP to implement heap tree? #include <stdio.h> #include <conio.h> void restoreup ( int, int * ) ; void restoredown ( int, int *, int ) ; void makeheap ( int *, int ) ; void add ( int, int *, int * ) ; int replace ( int, int *, int ) ; int del ( int *, int * ) ; void main( ) { int arr [20] = { 1000, 7, 10, 25, 17, 23, 27, 16, 19, 37, 42, 4, 33, 1, 5, 11 } ; int i, n = 15 ; clrscr( ) ; makeheap ( arr, n ) ; printf ( "Heap:n" ) ; for ( i = 1 ; i <= n ; i++ ) printf ( "%dt", arr [i] ) ; i = 24 ; add ( i, arr, &n ) ; printf ( "nnElement added %d.n", i ) ; printf ( "nHeap after addition of an element:n" ) ; for ( i = 1 ; i <= n ; i++ ) printf ( "%dt", arr [i] ) ; i = replace ( 2, arr, n ) ; printf ( "nnElement replaced %d.n", i ) ; printf ( "nHeap after replacement of an element:n" ) ; for ( i = 1 ; i <= n ; i++ ) printf ( "%dt", arr [i] ) ; i = del ( arr, &n ) ; printf ( "nnElement deleted %d.n", i ) ; printf ( "nHeap after deletion of an element:n" ) ; for ( i = 1 ; i <= n ; i++ ) printf ( "%dt", arr [i] ) ; getch( ) ; } void restoreup ( int i, int *arr ) { int val ; val = arr [i] ; while ( arr [i / 2] <= val ) { arr [i] = arr [i / 2] ; i = i / 2 ;
  • 52. Programming in ‘C’ and Data Structure Lab File 52 } arr [i] = val ; } void restoredown ( int pos, int *arr, int n ) { int i, val ; val = arr [pos] ; while ( pos <= n / 2 ) { i = 2 * pos ; if ( ( i < n ) && ( arr [i] < arr [i + 1] ) ) i++ ; if ( val >= arr [i] ) break ; arr [pos] = arr [i] ; pos = i ; } arr [pos] = val ; } void makeheap ( int *arr, int n ) { int i ; for ( i = n / 2 ; i >= 1 ; i-- ) restoredown ( i, arr, n ) ; } void add ( int val, int *arr, int *n ) { ( *n ) ++ ; arr [*n] = val ; restoreup ( *n, arr ) ; } int replace ( int i, int *arr, int n ) { int r = arr [1] ; arr [1] = i ; for ( i = n / 2 ; i >= 1 ; i-- ) restoredown ( i, arr, n ) ; return r ; } int del ( int *arr, int *n ) { int val ; val = arr [1] ; arr [1] = arr [*n] ; ( *n ) -- ; restoredown ( 1, arr, *n ) ; return val ; }
  • 53. Programming in ‘C’ and Data Structure Lab File 53 OUTPUT: Heap: 42 37 33 19 23 27 16 7 17 10 4 25 1 5 11 Element added 24. Heap after addition of an element: 42 37 33 24 23 27 16 19 17 10 4 25 1 5 11 7 Element replaced 42. Heap after replacement of an element: 37 24 33 19 23 27 16 7 17 10 4 25 1 5 11 2 Element deleted 37. Heap after deletion of an element: 33 24 27 19 23 25 16 7 17 10 4 2 1 5 11