SlideShare a Scribd company logo
1 of 33
Download to read offline
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
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.
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 :-
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);
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)
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)
{
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;
}
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;
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:-
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 :");
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:-
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:
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");
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 ;
}
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() */
Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
Output:-
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)
Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
>= precedence(item))
{
postfix_exp[j++] = x;
x = pop();
}
push(x);
push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j++] = x;
x = pop();
}
}
else
{
printf("nInvalid Arithmetic Expression.n");
getch();
exit(0);
}
item = infix_exp[i++];
}
postfix_exp[j++] = '0';
printf("nArithmetic expression in Postfix notation: ");
puts(postfix_exp);
getch();
}
void push(char item)
{
if(top >= SIZE-1)
{
printf("nStack Overflow. Push not possible.n");
}
else
{
top = top+1;
Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
stack[top] = item;
}
}
char pop()
{
char item = NULL;
if(top <= -1)
{
printf("nStack Underflow. Pop not possible.n");
}
else
{
item = stack[top];
stack[top] = NULL;
top = top-1;
}
return(item);
}
int is_operator(char symbol)
{
if(symbol == '^' || symbol == '*' || symbol == '/' ||
symbol == '+' || symbol == '-')
{
return 1;
}
else
{
return 0;
}
}
int precedence(char symbol)
{
if(symbol == '^')
{
return(3);
}
else if(symbol == '*' || symbol == '/')
{
return(2);
Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}
Output:-
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)
{
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:-
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:-
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)
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)
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)
{
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;
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;
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:-
Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
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;
Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
j = (i-1)/2;
}
a[i] = item;
}
}
void adjust(int a[],int n) {
int i,j,item;
j = 0;
item = a[j];
i = 2*j+1;
while(i<=n-1) {
if(i+1 <= n-1)
if(a[i] <a[i+1])
i++;
if(item<a[i]) {
a[j] = a[i];
j = i;
i = 2*j+1;
} else
break;
}
a[j] = item;
}
Output:-
Practical File Of “Data Structure”
Master Of Computer Application (MCA) HPU Shimla-05
10. Write a program to implement Insertion Sort algorithm.
#include<stdio.h>
int main(){
int i,j,s,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=1;i<s;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0;
}
Output:-

More Related Content

Similar to Practical_File_Of_Data_Structure.pdf

Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked ListSayantan Sur
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rathSANTOSH RATH
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 
Write a task that will perform some of the functions performed by a s.docx
 Write a task that will perform some of the functions performed by a s.docx Write a task that will perform some of the functions performed by a s.docx
Write a task that will perform some of the functions performed by a s.docxajoy21
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 
Assignment no39
Assignment no39Assignment no39
Assignment no39Jay Patel
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfamitbagga0808
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfaptcomputerzone
 
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docxIN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docxGordonpACKellyb
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPTTheVerse1
 
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdfC++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdffeelinggift
 

Similar to Practical_File_Of_Data_Structure.pdf (20)

DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
7 functions
7  functions7  functions
7 functions
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Qprgs
QprgsQprgs
Qprgs
 
Write a task that will perform some of the functions performed by a s.docx
 Write a task that will perform some of the functions performed by a s.docx Write a task that will perform some of the functions performed by a s.docx
Write a task that will perform some of the functions performed by a s.docx
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
Assignment no39
Assignment no39Assignment no39
Assignment no39
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
 
Gps c
Gps cGps c
Gps c
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
 
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docxIN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
 
Lab 2
Lab 2Lab 2
Lab 2
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
5 Rmi Print
5  Rmi Print5  Rmi Print
5 Rmi Print
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
 
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdfC++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
 

Recently uploaded

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 

Recently uploaded (20)

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
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)
  • 18. Practical File Of “Data Structure” Master Of Computer Application (MCA) HPU Shimla-05 >= precedence(item)) { postfix_exp[j++] = x; x = pop(); } push(x); push(item); } else if(item == ')') { x = pop(); while(x != '(') { postfix_exp[j++] = x; x = pop(); } } else { printf("nInvalid Arithmetic Expression.n"); getch(); exit(0); } item = infix_exp[i++]; } postfix_exp[j++] = '0'; printf("nArithmetic expression in Postfix notation: "); puts(postfix_exp); getch(); } void push(char item) { if(top >= SIZE-1) { printf("nStack Overflow. Push not possible.n"); } else { top = top+1;
  • 19. Practical File Of “Data Structure” Master Of Computer Application (MCA) HPU Shimla-05 stack[top] = item; } } char pop() { char item = NULL; if(top <= -1) { printf("nStack Underflow. Pop not possible.n"); } else { item = stack[top]; stack[top] = NULL; top = top-1; } return(item); } int is_operator(char symbol) { if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol == '-') { return 1; } else { return 0; } } int precedence(char symbol) { if(symbol == '^') { return(3); } else if(symbol == '*' || symbol == '/') { return(2);
  • 20. Practical File Of “Data Structure” Master Of Computer Application (MCA) HPU Shimla-05 } else if(symbol == '+' || symbol == '-') { return(1); } else { return(0); } } Output:-
  • 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;
  • 32. Practical File Of “Data Structure” Master Of Computer Application (MCA) HPU Shimla-05 j = (i-1)/2; } a[i] = item; } } void adjust(int a[],int n) { int i,j,item; j = 0; item = a[j]; i = 2*j+1; while(i<=n-1) { if(i+1 <= n-1) if(a[i] <a[i+1]) i++; if(item<a[i]) { a[j] = a[i]; j = i; i = 2*j+1; } else break; } a[j] = item; } Output:-
  • 33. Practical File Of “Data Structure” Master Of Computer Application (MCA) HPU Shimla-05 10. Write a program to implement Insertion Sort algorithm. #include<stdio.h> int main(){ int i,j,s,temp,a[20]; printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=1;i<s;i++){ temp=a[i]; j=i-1; while((temp<a[j])&&(j>=0)){ a[j+1]=a[j]; j=j-1; } a[j+1]=temp; } printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; } Output:-