Practical File of Data Structure
Practical File of Data Structure
PRACTICAL FILE
Of
“Data Structure”
TABLE OF CONTANT
}
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 :-
#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;
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 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;
/* End of option() */
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:-
#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:-
#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
int choice;
while (1)
printf("4.Quit \n");
scanf("%d", &choice);
switch (choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
} /*End of switch*/
} /*End of while*/
} /*End of main()*/
insert()
int add_item;
if (rear == MAX - 1)
else
if (front == - 1)
front = 0;
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
} /*End of insert()*/
delete()
return ;
else
front = front + 1;
} /*End of delete() */
display()
int i;
if (front == - 1)
else
printf("Queue is : \n");
printf("\n");
} /*End of display() */
Output:-
#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:-
#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:-
#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;
}
}
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
int data;
};
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”
getch();
else
getch();
if (t->data >x)
if (t->left==NULL)
ins(t,x,1);
else
inser(t->left,x);
if (t->right==NULL)
ins(t,x,2);
else
inser(t->right,x);
else
if (p!=NULL)
inorder(p->left);
printf("\n %5d",p->data);
inorder (p->right);
{
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);
if (p!=NULL)
preorder(p->left);
preorder (p->right);
printf("\n %5d",p->data);
void main()
int op,n;
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");
scanf("%d",&op);
switch (op)
scanf("%d",&n);
inser(root,n);
break;
Master Of Computer Application (MCA) HPU Shimla-05
Practical File Of “Data Structure”
preorder(root);
getch();
break;
inorder(root);
getch();
break;
postorder(root);
getch();
break;
0
default: exit(0);
}while(op<5);
getch();
}
Output:-
#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:-
#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:-