0% found this document useful (0 votes)
108 views33 pages

Practical File of Data Structure

The document provides 10 programs to implement various data structures and algorithms using C language. The programs include: 1. Insertion and deletion of elements in an array 2. Checking if a number is a palindrome 3. Implementing stack operations using an array 4. Implementing queue operations using an array 5. Converting infix to postfix notation 6. Sorting an array in ascending and descending order 7. Evaluating postfix expressions 8. Tree traversals 9. Implementing heap sort 10. Implementing insertion sort

Uploaded by

Blue Ace
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
108 views33 pages

Practical File of Data Structure

The document provides 10 programs to implement various data structures and algorithms using C language. The programs include: 1. Insertion and deletion of elements in an array 2. Checking if a number is a palindrome 3. Implementing stack operations using an array 4. Implementing queue operations using an array 5. Converting infix to postfix notation 6. Sorting an array in ascending and descending order 7. Evaluating postfix expressions 8. Tree traversals 9. Implementing heap sort 10. Implementing insertion sort

Uploaded by

Blue Ace
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 33

Practical File Of “Data Structure”

PRACTICAL FILE
Of
“Data Structure”

SUBMITTED TO: SUBMITTED BY :

Mrs Anjana Sharma Abhilash Thakur

Roll no- 6970

Mca 2nd sem

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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.

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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 :-

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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);
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

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 : %s\n", 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)
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

{
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)
{
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

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("\n\n>> 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;
}
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

/* 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("\n\n 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;
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

}
display(linkList);
break;
case '4':
printf("\n\n Enter the employee number to be searched: ");
scanf("%d", &eno);
search(linkList, eno);
break;
case '5': break;
}
} while (choice != '5');
}

OUTPUT:-

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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("\n\tMENU\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
if(top==max-1)
printf("Overflow\n");
else
{
printf("Enter the element\n");
scanf("%d",&x);
a[++top]=x;
}
break;

case 2:
if(top<0)
printf("Underflow\n");
else
printf("The deleted item =%d",a[top--]);
break;

case 3:
if(top<0)
printf("The stack is empty\n");
else
{
printf("The elements of the stack are :");
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

for(i=0;i<=top;i++)
printf("%d\n",a[i]);
}
break;

case 4:
break;

default:
printf("Invalid choice\n");
break;
}
}
while(n!=4);
}
Output:-

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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:

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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");


Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

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 ;

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

else

printf("Element deleted from queue is : %d\n", 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() */

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

Output:-

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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)
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

>= 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;
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

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);
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}

Output:-

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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("\n\nResult :: %d",stack[top]);
getch();
}
void pushstack(int tmp)
{
top++;
stack[top]=(int)(post[tmp]-48);
}
void calculator(char c)
{
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

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:-

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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 elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", 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("%d\n", array[c]);
getch();
}
Output:-

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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)
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

printf("\tat the left\n");

getch();

else

printf("\tat the right\n");

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)

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

if (t->right==NULL)

ins(t,x,2);

else

inser(t->right,x);

else

printf("\n Element is already present in the list\n");

void inorder(struct node *p)

if (p!=NULL)

inorder(p->left);

printf("\n %5d",p->data);

inorder (p->right);

void preorder(struct node *p)

{
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

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;

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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 choice\n");

scanf("%d",&op);

switch (op)

case 1: printf("\n Enter the element to insert\n");

scanf("%d",&n);

inser(root,n);

break;
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

case 2: printf("\n The preorder elements are\n");

preorder(root);

getch();

break;

case 3: printf("\n The inorder elements are\n");

inorder(root);

getch();

break;

case 4: printf("\n The postorder elements are\n");

postorder(root);

getch();

break;
0
default: exit(0);

}while(op<5);

getch();
}
Output:-

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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;
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”

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:-

Master Of Computer Application (MCA) HPU Shimla-05


Practical File Of “Data Structure”

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:-

Master Of Computer Application (MCA) HPU Shimla-05

You might also like