Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Practical_File_Of_Data_Structure.pdf
1. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
PRACTICAL FILE
Of
“Data Structure”
SUBMITTED TO: SUBMITTED BY :
Mrs Anjana Sharma Abhilash Thakur
Roll no- 6970
Mca 2nd
sem
2. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
TABLE OF CONTANT
S.NO CONTENT SIGNATURE
1 Write a program to insertion and deletion of element on array.
2 A Program to check whether the given number is palindrome or not .
3 Write a program to implement Stack operation using an array.
4 Write a program to implementation of the operations on Queue using array.
5 Write a program to converting Infix expression into Postfix.
6 A Program to sort array element in ascending and descending order.
7 Write a program to evaluations of postfix arithmetic expression.
8 Write a program for tree traversal : in-order ,post-order .
9 Write a program to implement Heap Sort algorithm.
10 Write a program to implement Insertion Sort algorithm.
3. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
1. Write a program to insertion and deletion of element on array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[5],n;
clrscr();
printf(" Insert the elemrnt in Array");
for(i=1;i<=5;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the Deletion element in array ");
scanf("%d",&n);
printf("Element in array ");
for(i=1;i<=5;i++)
{
if (n==a[i])
{
}
else
printf("n %d",a[i]);
}
getch();
}
OUT PUT :-
4. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
2. Write a program to perform various operations on Linked-List.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
struct emp_data
{
int empno;
char empName[MAX];
char designation[MAX];
struct emp_data *next;
};
/*
*******************************************************************
*/
/* Function to insert a node at the front of the linked list. */
/* front: front pointer, id: employee ID, name: employee name */
/* desg: Employee designation */
/* Returns the new front pointer. */
/*
*******************************************************************
*/
struct emp_data *insert(struct emp_data *front, int id, char name[],
char desg[])
{
struct emp_data *newnode;
newnode = (struct emp_data*)malloc(sizeof(struct emp_data));
if (newnode == NULL)
{
printf("n Allocation failed n");
exit(2);
}
newnode->empno = id;
strcpy(newnode->empName, name);
5. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
strcpy(newnode->designation, desg);
newnode->next = front;
front = newnode;
return(front);
}
/* End of insert() */
/* Function to display a node in a linked list */
void printNode(struct emp_data *p)
{
printf("n Employee Details...n");
printf("n Emp No : %d", p->empno);
printf("n Name : %s", p->empName);
printf("n Designation : %sn", p->designation);
printf("-------------------------------------n");
}
/* End of printNode() */
/* ********************************************************/
/* Function to deleteNode a node based on employee number */
/* front: front pointer, id: Key value */
/* Returns: the modified list. */
/* ********************************************************/
struct emp_data* deleteNode(struct emp_data *front, int id)
{
struct emp_data *ptr;
struct emp_data *bptr;
if (front->empno == id)
{
ptr = front;
printf("n Node deleted:");
printNode(front);
front = front->next;
free(ptr);
return(front);
}
for (ptr = front->next, bptr = front; ptr != NULL; ptr = ptr->next,
bptr = bptr->next)
6. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
{
if (ptr->empno == id)
{
printf("n Node deleted:");
printNode(ptr);
bptr->next = ptr->next;
free(ptr);
return(front);
}
}
printf("n Employee Number %d not found ", id);
return(front);
}
/* End of deleteNode() */
/* ****************************************************************/
/* Function to search the nodes in a linear fashion based emp ID */
/* front: front pointer, key: key ID. */
/* ****************************************************************/
void search(struct emp_data *front, int key)
{
struct emp_data *ptr;
for (ptr = front; ptr != NULL; ptr = ptr -> next)
{
if (ptr->empno == key)
{
printf("n Key found:");
printNode(ptr);
return;
}
}
printf("n Employee Number %d not found ", key);
}
/* End of search() */
/* Function to display the linked list */
void display(struct emp_data *front)
{
7. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
struct emp_data *ptr;
for (ptr = front; ptr != NULL; ptr = ptr->next)
{
printNode(ptr);
}
}
/* End of display() */
/* Function to display the menu of operations on a linked list */
void menu()
{
printf("---------------------------------------------n");
printf("Press 1 to INSERT a node into the list n");
printf("Press 2 to DELETE a node from the list n");
printf("Press 3 to DISPLAY the list n");
printf("Press 4 to SEARCH the list n");
printf("Press 5 to EXIT n");
printf("---------------------------------------------n");
}
/* End of menu() */
/* Function to select the option */
char option()
{
char choice;
printf("nn>> Enter your choice: ");
switch(choice=getche())
{
case '1':
case '2':
case '3':
case '4':
case '5': return(choice);
default : printf("n Invalid choice.");
}
return choice;
}
8. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
/* End of option() */
/* The main() program begins */
void main()
{
struct emp_data *linkList;
char name[21], desig[51];
char choice;
int eno;
linkList = NULL;
printf("n Welcome to demonstration of singly linked list n");
menu();
do
{
/* choose oeration to be performed */
choice = option();
switch(choice)
{
case '1':
printf("n Enter the Employee Number : ");
scanf("%d", &eno);
printf("Enter the Employee name : ");
fflush(stdin);
gets(name);
printf("Enter the Employee Designation : ");
gets(desig);
linkList = insert(linkList, eno, name, desig);
break;
case '2':
printf("nn Enter the employee number to be deleted: ");
scanf("%d", &eno);
linkList = deleteNode(linkList, eno);
break;
case '3':
if (linkList == NULL)
{
printf("n List empty.");
break;
9. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
}
display(linkList);
break;
case '4':
printf("nn Enter the employee number to be searched: ");
scanf("%d", &eno);
search(linkList, eno);
break;
case '5': break;
}
} while (choice != '5');
}
OUTPUT:-
10. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
3. Write a program to implement Stack operation using an array.
#include<stdio.h>
#include<conio.h>
main()
{
int a[10]={0},i,top=-1,max=10,n,x;
clescr();
printf("ntMENUn1.PUSHn2.POPn3.DISPLAYn4.EXITn");
do
{
printf("nEnter your choicen");
scanf("%d",&n);
switch(n)
{
case 1:
if(top==max-1)
printf("Overflown");
else
{
printf("Enter the elementn");
scanf("%d",&x);
a[++top]=x;
}
break;
case 2:
if(top<0)
printf("Underflown");
else
printf("The deleted item =%d",a[top--]);
break;
case 3:
if(top<0)
printf("The stack is emptyn");
else
{
printf("The elements of the stack are :");
11. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
for(i=0;i<=top;i++)
printf("%dn",a[i]);
}
break;
case 4:
break;
default:
printf("Invalid choicen");
break;
}
}
while(n!=4);
}
Output:-
12. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
4. Write a program to implementation of the operations on Queue using array.
#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
13. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice n");
} /*End of switch*/
} /*End of while*/
} /*End of main()*/
insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow n");
14. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /*End of insert()*/
delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow n");
return ;
}
15. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
else
{
printf("Element deleted from queue is : %dn", queue_array[front]);
front = front + 1;
}
} /*End of delete() */
display()
{
int i;
if (front == - 1)
printf("Queue is empty n");
else
{
printf("Queue is : n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
} /*End of display() */
16. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
Output:-
17. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
5. Write a program to converting Infix expression into Postfix.
#include<stdio.h>
#include<conio.h>
#define SIZE 100
int top = -1;
char stack[SIZE];
void push(char item);
char pop();
int is_operator(char symbol);
int precedence(char symbol);
void main()
{
int i;
int j;
char infix_exp[SIZE], postfix_exp[SIZE];
char item;
char x;
clrscr();
printf("nEnter Infix expression in parentheses: n");
gets(infix_exp);
i=0;
j=0;
item=infix_exp[i++];
while(item != '0')
{
if(item == '(')
{
push(item);
}
else if((item >= 'A' && item <= 'Z') ||
(item >= 'a' && item <= 'z'))
{
postfix_exp[j++] = item;
}
else if(is_operator(item) == 1)
{
x=pop();
while(is_operator(x) == 1 && precedence(x)
21. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
6. Write a program to evaluations of postfix arithmetic expression.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50
int stack[MAX];
char post[MAX];
int top=-1;
void pushstack(int tmp);
void calculator(char c);
void main()
{
int i;
clrscr();
printf("Insert a postfix notation :: ");
gets(post);
for(i=0;i<strlen(post);i++)
{
if(post[i]>='0' && post[i]<='9')
{
pushstack(i);
}
if(post[i]=='+' || post[i]=='-' || post[i]=='*' ||
post[i]=='/' || post[i]=='^')
{
calculator(post[i]);
}
}
printf("nnResult :: %d",stack[top]);
getch();
}
void pushstack(int tmp)
{
top++;
stack[top]=(int)(post[tmp]-48);
}
void calculator(char c)
{
22. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
int a,b,ans;
a=stack[top];
stack[top]='0';
top--;
b=stack[top];
stack[top]='0';
top--;
switch(c)
{
case '+':
ans=b+a;
break;
case '-':
ans=b-a;
break;
case '*':
ans=b*a;
break;
case '/':
ans=b/a;
break;
case '^':
ans=b^a;
break;
default:
ans=0;
}
top++;
stack[top]=ans;
}
Output:-
23. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
7. Write a program to implement Bubble Sort algorithm.
#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], n, c, d, swap;
clrscr();
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:n");
for ( c = 0 ; c < n ; c++ )
printf("%dn", array[c]);
getch();
}
Output:-
24. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
8. Write a program for tree traversal : in-order ,post-order .
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *left,*right;
};
struct node *root;
void ins(struct node *n,int val,int opt)
{
struct node *t;
t=(struct node *)malloc(sizeof(struct node));
t->data=val;
t->right=t->left=NULL;
if (opt==1)
n->left=t;
else
n->right=t;
printf("n %d is inserted",val);
if (opt==1)
25. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
{
printf("tat the leftn");
getch();
}
else
{
printf("tat the rightn");
getch();
}
}
void inser(struct node *t,int x)
{
if (t->data >x)
if (t->left==NULL)
ins(t,x,1);
else
inser(t->left,x);
else if (t->data < x)
26. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
if (t->right==NULL)
ins(t,x,2);
else
inser(t->right,x);
else
printf("n Element is already present in the listn");
}
void inorder(struct node *p)
{
if (p!=NULL)
{
inorder(p->left);
printf("n %5d",p->data);
inorder (p->right);
}
}
void preorder(struct node *p)
{
27. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
if (p!=NULL)
{
printf("n %5d",p->data);
preorder(p->left);
preorder (p->right);
}
}
void postorder(struct node *p)
{
if (p!=NULL)
{
preorder(p->left);
preorder (p->right);
printf("n %5d",p->data);
}
}
void main()
{
int op,n;
28. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
root=(struct node *)malloc(sizeof(struct node));
root->data=30;
root->right=root->left=NULL;
clrscr();
do
{
printf("n 1.Insertion");
printf("n 2.Preorder");
printf("n 3.Inorder");
printf("n 4.Postorder");
printf("n 5.Quit");
printf("n Enter your choicen");
scanf("%d",&op);
switch (op)
{
case 1: printf("n Enter the element to insertn");
scanf("%d",&n);
inser(root,n);
break;
29. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
case 2: printf("n The preorder elements aren");
preorder(root);
getch();
break;
case 3: printf("n The inorder elements aren");
inorder(root);
getch();
break;
case 4: printf("n The postorder elements aren");
postorder(root);
getch();
break;
0
default: exit(0);
}
}while(op<5);
getch();
}
Output:-
30. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
31. Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
9. Write a program to implement Heap Sort algorithm.
#include<stdio.h>
void heapsort(int[],int);
void heapify(int[],int);
void adjust(int[],int);
main() {
int n,i,a[50];
system("clear");
printf("nEnter the limit:");
scanf("%d",&n);
printf("nEnter the elements:");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("nThe Sorted Elements Are:n");
for (i=0;i<n;i++)
printf("t%d",a[i]);
printf("n");
}
void heapsort(int a[],int n) {
int i,t;
heapify(a,n);
for (i=n-1;i>0;i--) {
t = a[0];
a[0] = a[i];
a[i] = t;
adjust(a,i);
}
}
void heapify(int a[],int n) {
int k,i,j,item;
for (k=1;k<n;k++) {
item = a[k];
i = k;
j = (i-1)/2;
while((i>0)&&(item>a[j])) {
a[i] = a[j];
i = j;