Data Structure using C Lab Manual Book
Data Structure using C Lab Manual Book
st
1 Semester B.C.A
Data Structure
using C
Lab Manual
1
2. Given {5,3,1,6,0,2,4} order the numbers in ascending order using Bubble Sort Algorithm
3. Perform the Insertion and Selection Sort on the input {75,8,1,16,48,3,7,0} and display the output in
descending order.
4. Write a program to insert the elements {61,16,8,27} into singly linked list and delete 8,61,27 from the list.
Display your list after each insertion and deletion.
5. Write a program to insert the elements {61,16,8,27} into linear queue and delete three elements from the
list. Display your list after each insertion and deletion.
6. Write a program to insert the elements {61,16,8,27} into circular queue and delete 4 elements from the list.
Display your list after each insertion and deletion.
7. Write a program to insert the elements {61,16,8,27} into ordered singly linked list and delete 8,61,27 from
the list. Display your list after each insertion and deletion.
9. Write a program to push 5,9,34,17,32 into stack and pop 3 times from the stack, also display the popped
numbers.
11. Write a program to inert the elements {5,7,0,6,3,9} into circular queue and delete 6,9&5 from it(using
linked list implementation)..
12. Write a program to convert an infix expression x^y/(5*z)+2 to its postfix expression
14. Write a program to create a binary tree with the elements {18,15,40,50,30,17,41} after creation insert 45
and 19 into tree and delete 15,17 and 41 from tree. Display the tree on each insertion and deletion operation
15. Write a program to create binary search tree with the elements {2,5,1,3,9,0,6} and perform inorder,
preorder and post order traversal.
16. Write a program to Sort the following elements using heap sort {9.16,32,8,4,1,5,8,0}
3
17. Given S1={“Flowers”} ; S2={“are beautiful”} I. Find the length of S1 II. Concatenate S1 and S2 III. Extract the
substring “low” from S1 IV. Find “are” in S2 and replace it with “is”
# include<stdio.h>
# include<conio.h>
void main()
{
int a[8] = {4,7,3,2,1,7,9,0};
int sa[8] = {0,1,2,3,4,7,7,9};
int n=8;
int key=7;
int choice;
clrscr();
printf("\n 1. Linear Search \n");
printf("\n 2. Binary Search \n");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : DISPLAY_ARRAY(a,n);
LINEAR_SEARCH(a,n,key);
break;
case 2 : DISPLAY_ARRAY(sa,n);
BINARY_SEARCH(sa,key,0,n-1);
break;
# include<stdio.h>
# include<conio.h>
void main()
{
int a[7]={5,3,1,6,0,2,4};
int n=7;
clrscr();
printf("\n Input arrays : 5 3 1 6 0 2 4");
BUBBLE_SORT(a,n);
getch();
}
# include<stdio.h>
# include<conio.h>
k=a[pass];
for(j=pass-1;j>=0 && k<a[j];j--)
{
a[j+1] = a[j];
}
a[j+1]=k;
printf("\n\n Sorted arrays after %d pass -->",pass);
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
}
printf("\n\n Sorted arrays using Insertion sort in Descending Order\n");
for(i=n-1;i>=0;i--)
{
printf("%3d",a[i]);
}
}
void main()
{
int a[8]={75,8,1,16,48,3,7,0};
int n=8;
clrscr();
printf("\n Input arrays : 75,8,1,16,48,3,7,0");
INSERTION_SORT(a,n); 7
getch();
}
# include<stdio.h>
# include<stdlib.h>
# include<conio.h>
# include<alloc.h>
# include<ctype.h>
NODE *header=NULL;
10
if(header==NULL)
{
printf("\n EMPTY LIST");
}
else
{
if(header->info==item)
{
header=header->link;
free(curptr);
}
else
{
while(curptr!=NULL)
{
if(curptr->info==item)
{
prevptr->link=curptr->link;
free(curptr);
curptr=curptr->link->link;
}
else
{
prevptr=curptr;
curptr=curptr->link;
}
}
}
}
}
11
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 50
void insert();
void remove();
void display();
int a[MAX];
int rear = - 1;
int front = - 1;
void main()
{
int choice;
clrscr();
while (1)
{
printf("\n 1. Insert element to queue \n");
printf("\n 2. Delete element from queue \n");
printf("\n 3. Display all elements of queue \n");
printf("\n 4. Quit \n");
printf("\n Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1: insert();
break;
case 2: remove();
break;
case 3: display();
break;
case 4: exit(1);
default:printf("\n Wrong choice");
}
}
}
14
void remove()
{
if(front == - 1)
{
printf("\n Sorry, Queue Empty... \n");
return;
}
else
{
if(front == rear)
{
front = -1;
rear = -1;
}
else
{
front++;
}
} 15
}
16
#include<stdio.h>
# define MAX 5
int cq[MAX];
int front = -1;
int rear = -1;
void deletion()
{
if(front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cq[front]);
if(front == rear) 17
{
front = -1;
rear=-1;
}
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
{
while(front_pos <= rear_pos)
{
printf("%3d ",cq[front_pos]);
front_pos++;
}
}
else
{
while(front_pos <= MAX-1)
{
printf("%3d ",cq[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cq[front_pos]);
front_pos++;
}
}
printf("\n"); 18
}
19
struct node
{
int INFO;
struct node *LINK;
};
typedef struct node NODE;
NODE *start=NULL;
20
void display()
{
NODE * CURRPTR = start;
if(CURRPTR == NULL)
{
printf("Empty");
}
else
{
while(CURRPTR != NULL)
{
printf("%d", CURRPTR->INFO);
printf(" -> ");
CURRPTR = CURRPTR->LINK; 21
}
printf("NULL");
}
}
Jaihind Degree College | Megharaj, Computer Science Lecturer
void main()
{
int choice,item;
clrscr();
while(1)
{
printf("\n\n ORDERED LINKED LIST OPERATIONS");
printf("\n ******************************");
printf("\n 1.Insert");
printf("\n 2.Delete");
printf("\n 3.Display");
printf("\n 4.Quit");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\n Enter item to be inserted : ");
scanf("%d", &item);
printf("\n Linked list before insertion is : \n");
display();
insert(item);
printf("\n Linked list after insertion is : \n");
display();
break;
case 2 : printf("\n Enter item to be deleted : ");
scanf("%d",&item);
printf("\n Linked list before deletion is : \n");
display();
remov(item);
printf("\n Linked list after deletion is : \n");
display();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("Wrong choice\n");
}
}
getch();
}
22
struct poly
{
int coeff;
int expo;
};
void main()
{
int t1,t2,t3;
clrscr();
t1=readPoly(p1);
printf(" \n First polynomial : ");
displayPoly(p1,t1);
t2=readPoly(p2);
printf(" \n Second polynomial : ");
displayPoly(p2,t2);
t3=addPoly(p1,p2,t1,t2,p3);
printf(" \n\n Resultant polynomial after addition : ");
displayPoly(p3,t3);
printf("\n");
getch();
}
while(i<t1)
{
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
}
while(j<t2)
{
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
} 25
return(k);
}
26
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int top=-1,stack[MAX];
void push();
void pop();
void display();
void main()
{
int ch;
clrscr();
while(1)
{
clrscr();
printf("\n** Stack Menu **\n");
printf("\n 1.Push \n 2.Pop \n 3.Display \n 4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong Choice!!");
}
}
}
27
void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
getch();
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
{
printf("%d\n",stack[i]);
} 28
}
getch();
}
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int n1, n2;
clrscr();
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, gcf(n1, n2));
getch();
}
30
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int INFO;
struct node *LINK;
};
struct node *front = NULL, *rear = NULL;
31
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL & rear == NULL)
{
printf("\n Queue is Empty");
}
else
{
printf("\n The Queue elements are : ");
do 32
{
printf("%d",ptr->INFO);
ptr=ptr->LINK;
void main()
{
int choice,item;
clrscr();
while(1)
{
printf("\n ****** MAIN MANU ******");
printf("\n 1. Insert");
printf("\n 2. Delete");
printf("\n 3. Display");
printf("\n 4. Quit");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\n Enter the number to insert into Queue : ");
scanf("%d",&item);
QInsert(item);
break;
case 2 : QDelete();
break;
case 3 : display();
break;
case 4 : exit(0);
break;
default: printf("\n Invalid Option");
break;
}
}
}
33
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define SIZE 100
char stack[SIZE];
int top = -1;
char pop()
{
char item ;
if(top < 0)
{
printf("stack under flow: invalid infix expression");
getchar();
exit(1);
}
else
{
item = stack[top];
top = top-1; 35
return(item);
}
}
void main()
{
char infix[SIZE], postfix[SIZE];
clrscr();
printf("ASSUMPTION: The infix expression contains single letter variables and single digit
constants only.\n");
printf("\nEnter Infix expression : ");
gets(infix);
InfixToPostfix(infix,postfix);
printf("Postfix Expression: ");
puts(postfix);
getch();
}
38
#include<stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
void main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
clrscr();
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
39
{
case '+':
{
n3 = n1 + n2;
Jaihind Degree College | Megharaj, Computer Science Lecturer
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
getch();
}
40
struct node
{
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root)
{
if (root == NULL)
{
return;
}
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// preorderTraversal traversal
void preorderTraversal(struct node* root)
{
if (root == NULL)
{
return;
}
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// postorderTraversal traversal
void postorderTraversal(struct node* root)
{
if (root == NULL)
{
return;
}
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
} 41
42
#include <stdio.h>
void main()
{
int arr[] = {1, 12, 9, 5, 6, 10};
int n = 6;
clrscr();
heapSort(arr, n);
printf("Sorted array is \n");
printArray(arr, n);
getch();
}
44
void main()
{
char S1[50],S2[50],substr[50],repstr[50];
int len,pos,ch;
clrscr();
while(1)
{
printf("\n String Operation ");
printf("\n -----------------");
printf("\n 1. Find the length of S1");
printf("\n 2. Concatenate S1 & S2");
printf("\n 3. Extract the substring 'low' from S1");
printf("\n 4. Find 'are' in S2 and replace it with 'is'");
printf("\n 5. Exit");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\n Enter the String : ");
scanf("%s",&S1);
printf("\n The length of string S1 is %d",LENGTH(S1));
break;
case 2 : printf("\n Enter the First String : ");
scanf("%s",&S1);
printf("\n Enter the Second String : ");
scanf("%s",&S2);
CONCAT(S1,S2);
printf("\n The concatenate of string S1 and string S2 is %s",S1);
break;
case 3 : printf("\n Enter the String : ");
scanf("%s",&S1);
printf("\n Enter the position of a substring : ");
scanf("%d",&pos);
printf("\n Enter the length of the substring : ");
scanf("%d",&len);
SUBSTR(S1,pos,len);
break;
case 4 : printf("\n Enter the String : ");
scanf("%s",&S2);
printf("\n Enter the string to be removed : ");
scanf("%s",&substr); 45
printf("\n Enter the string to replace : ");
scanf("%s",&repstr);
REPLACE(S2,substr,repstr);
break;
46
for(j=0;j<len;j++)
{
sub[j]=S1[p];
p++;
}
sub[j]='\0';
printf("\n Substring = %s",sub);
return;
}
47
48