Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 66

Department Of Computer Applications

A
PRACTICAL
FILE
ON
DATA STRUCTURE

SUBMITTED TO: SUBMITTED BY:

Data Structure
Department Of Computer Applications

INDEX
Sr. No. PROGRAMS Page No. Remarks
1. Program for array Traversal
2. Program for array Insertion
3. Program for array Deletion
4. Program for matrix Transpose
5. Program for matrix Addition
6. Program for matrix Multiplication
7. Program for single linked list Traversal
8. Program for single linked list Insertion
9. Program for single linked list Deletion
10. Program for doubly linked list Insertion
& Deletion
11. Program for Push & Pop into stack
12. Program for Infix to Postfix conversion
13. Program for Linear queue
14. Program for Circular queue
15. Program for implementation of Sparse
Array
16. Program for Polynomial Addition &
Multiplication
17. Program for Tree Traversal
18. Program for BST(Binary Search Tree)
19. Program for Bubble Sort
20. Program for quick Sort
21. Program for Selection Sort
22. Program for Binary search
23. Program for Linear search

Data Structure
Department Of Computer Applications

1.Program for Array traversal.


#include<stdio.h>
#include<conio.h>
void main()
{
int k,i,n,LB,UB,A[10];
clrscr();
printf(“Enter the number of elements:”);
scanf(“%d”,&n);
printf(“Enter the elements of array:”);
for(i=1;i<=n;i++)
{
printf(“Element number [%d] is:”,i);
scanf(“%d”,&A[i]);
}
LB=1;
UB=n;
k=LB;
printf(“\n”);
printf(“Traversing of array is:\n”);
while(k<=UB)
{
printf(“Element number [%d] is %d \n”,k,A[k]);
k=k+1;
}
getch();
}

Output:

Data Structure
Department Of Computer Applications

2.Program for array insertion.


#include<stdio.h>
#include<conio.h>
main()
{
int array[100],position,c,n,value;
clrscr();
printf(“Enter number of elements: \n”);
scanf(“%d”, &n);
printf(“Enter %d eginnin:\n”,n);
for(c=0;c<n;c++)
scanf(“%d”, &array[c]);
printf(“Enter the location where you wish to insert an element:\n”);
scanf(“%d”,&position);
printf(“Enter the value to insert:\n”);
scanf(“%d”,&value);
for(c=n-1;c>=position-1;c--)
array[c+1]=array[c];
array[position-1]=value;
printf(“Resultant array is:\n”);
for(c=0;c<=n;c++)
printf(“%d\n”, array[c]);
getch();
}

Output:

Data Structure
Department Of Computer Applications

3. Program for array deletion.


#include<stdio.h>
main()
{
int array[100],loc,c,n;
clrscr();
printf(“Enter number of elements \n”);
scanf(“%d”,&n);
printf(“Enter %d elements \n”,n);
for(c=0;c<n;c++)
scanf(“%d”,&array[c]);
printf(“Enter the location where you want to delete element \n”);
scanf(“%d”,&loc);
if(loc>=n-1)
printf(“Deletion not possible \n”);
else
{
for(c=loc-1;c<n-1;c++)
array[c]=array[c+1];
printf(“Resultant array is: \n”);
for(c=0;c<n-1;c++)
printf(“%d \n”,array[c]);
}
getch();
}

Data Structure
Department Of Computer Applications

Output:

4. Program for matrix transpose.


#include<stdio.h>
#include<conio.h>
main()
{
int A[2][3],B[3][2];
int i,j;
clrscr();
printf(“Enter the elements of A:\n”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&A[i][j]);
}
}
printf(“Matrix is:\n”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,A[i][j]);
}
printf(“\n”);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{

Data Structure
Department Of Computer Applications

B[i][j]=A[j][i];
}
}
printf(“After transpose:\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“%d\t”,B[i][j]);
}
printf(“\n”);
}
getch();
}

Output:

Data Structure
Department Of Computer Applications

5. Program for matrix addition.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],s[2][2];
int i,j;
clrscr();
printf(“Enter the first matrix \n”);
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf(“Enter %d%d \n”,i,j, “element:”);
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter second matrix: \n”);
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf(“Enter %d%d \n”,i+1,j+1,”element:”);
scanf(“%d”,&b[i][j]);
}
}
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{

Data Structure
Department Of Computer Applications

s[i][j] = a[i][j]+b[i][j];
}
}
printf(“The addition of matrix is: \n”);
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf(“%d \n”, s[i][j]);
}
}
getch();
}

Output:

Data Structure
Department Of Computer Applications

6. Program for matrix multiplication.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2], b[2][2], s[2][2];
int i,j,k;
clrscr();
printf(“Enter the first matrix: \n”);
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf(“Enter %d%d \n”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter second matrix: \n”);
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf(“Enter %d%d \n”,i,j);
scanf(“%d”,&b[i][j]);
}
}
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
s[i][j]=0;
for(k=1;k<=2;k++)

Data Structure
Department Of Computer Applications

{
s[i][j] = s[i][j]+a[i][k]*b[k][j];
}
}
}
printf(“Matrix multiplication is: \n”);
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf(“%d \n”, s[i][j]);
}
}
getch();
}

Output:

Data Structure
Department Of Computer Applications

7.Program for single linked list traversal.


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *link;
};
struct node *n,*first,*this1;
int main()
{
char ch;
int item;
clrscr();
first=NULL;
printf(“Enter q to quite”);
while((ch=getch())!=’q’)
{
n=(struct node *) malloc (sizeof(struct node));
printf(“\nEnter info=”);
scanf(“%d”,&n->info);
n->link=NULL;
if(first==NULL)
first=n;
else
{
this1=first;
while(this1->link != NULL)
this1=this1->link;
this1->link=n;
}
printf(“Enter q to quite:”);

Data Structure
Department Of Computer Applications

}
this1=first;
while(this1 != NULL)
{
printf(“\n %d”,this1->info);
this1=this1->link;
}
getch();
return 0;
}

Output:

Data Structure
Department Of Computer Applications

8. Program for single linked list insertion.


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NODE;
NODE *start;
void createmptylist(NODE **start)
{
*start=NULL;
}
void traversinorder(NODE *start)
{
while(start!=NULL)
{
printf("%d\n",start->info);
start=start->next;
}
}
void insert_at_begin(int item)
{
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
if(start==NULL)
ptr->next=NULL;
else
ptr->next=start;
start=ptr;

Data Structure
Department Of Computer Applications

}
void insert_at_end(int item)
{
NODE *ptr,*loc;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr->next=NULL;
if(start==NULL)
{
start=ptr;
}
else
{
loc=start;
while(loc->next!=NULL)
loc=loc->next;
loc->next=ptr;
}
}
void insert_spe(NODE *start,int item)
{
NODE *ptr,*loc;
int temp,k;
loc=start;
for(k=0;k<temp;k++)
{
loc=loc->next;
if(loc==NULL)
{
printf("node in the list at less than one\n");
return;
}
}
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr->next=loc->next;
loc->next=ptr;
}
void main()
{
int choice,item,after,ch;
clrscr();
createmptylist(start);
while(1)
{
printf(" 1. Insert element at begin:\n");
printf(" 2. insert element at end:\n");
printf(" 3. Insert at specific location:\n");
printf(" 4. Traverse the list inorder:\n");

Data Structure
Department Of Computer Applications

printf(" 5. Exit.\n");
printf(" Enter your choice\n");
scanf(" %d",&choice);
switch(choice)
{
case 1: printf(" Enter the item\n");
scanf(" %d",&item);
insert_at_begin(item);
break;
case 2: printf(" Enter the item\n");
scanf(" %d",&item);
insert_at_end(item);
break;
case 3: printf(" Enter the value of item\n");
scanf(" %d",&item);
insert_spe(start,item);
break;
case 4: printf(" Traverse the list:\n");
traversinorder(start);
break;
case 5: return;
}
}
getch();
}

Output:

Data Structure
Department Of Computer Applications

Data Structure
Department Of Computer Applications

Data Structure
Department Of Computer Applications

9. Program for single linked list deletion.


#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
struct node *header, *ptr, *ptr1, *temp;
void insert_end();
void delete_front();
void delete_end();
void delete_any();
void display();
void main()
{
int choice;
int cont = 1;
header = (struct node *) malloc(sizeof(struct node));
clrscr();
header->data = NULL;
header->link = NULL;
while(cont == 1)
{
printf(“\n1. Insert at end\n”);
printf(“\n2. Delete from front\n”);
printf(“\n3. Delete from end\n”);
printf(“\n4. Delete from anywhere\n”);
printf(“\n5. Display linked list\n”);
printf(“\nEnter your choice: “);
scanf(“%d”, &choice);
switch(choice)
{
case 1:
insert_end();
break;
case 2:
delete_front();
break;
case 3:
delete_end();
break;
case 4:
delete_any();
break;
case 5:
display();
break;

Data Structure
Department Of Computer Applications

}
printf(“\n\nDo you want to continue? (1 / 0): “);
scanf(“%d”, &cont);
}
getch();
}
void insert_end()
{
int data_value;
printf(“\nEnter data of the node: “);
scanf(“%d”, &data_value);
temp = (struct node *) malloc(sizeof(struct node));
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
temp->data = data_value;
temp->link = ptr->link;
ptr->link = temp;
}
void delete_front()
{
if(header->link == NULL)
{
printf(“\nEmpty Linked List. Deletion not possible.\n”);
}
else
{
ptr = header->link;
header->link= ptr->link;
free(ptr);
printf(“\nNode deleted from the front.\n”);
}
}
void delete_end()
{
if(header->link == NULL)
{
printf(“\nEmpty Linked List. Deletion not possible.\n”);
}
else
{
ptr = header;
while(ptr->link != NULL)
{
ptr1 = ptr;
ptr = ptr->link;
}

Data Structure
Department Of Computer Applications

ptr1->link = ptr->link;
free(ptr);
printf(“\nNode deleted from the end.\n”);
}
}
void delete_any()
{
int key;
if(header->link == NULL)
{
printf(“\nEmpty Linked List. Deletion not possible.\n”);
}
else
{
printf(“\nEnter the data of the node to be deleted: “);
scanf(“%d”, &key);
ptr = header;
while((ptr->link != NULL) && (ptr->data != key))
{
ptr1 = ptr;
ptr = ptr->link;
}
if(ptr->data == key)
{
ptr1->link = ptr->link;
free(ptr);
printf(“\nNode with data %d deleted.\n”, key);
}
else
{
printf(“\nValue %d not found. Deletion not possible.\n”, key);
}
}
}
void display()
{
printf(“\nContents of the linked list are: \n”);
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
printf(“%d “, ptr->data);
}
}

Data Structure
Department Of Computer Applications

Output:

Data Structure
Department Of Computer Applications

9. Program for doubly linked list Insertion & Deletion.


#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *prev;
int n;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void insert1();
void insert2();
void insert3();
void traversebeg();
void traverseend(int);
void sort();
void search();
void update();
void delete();
int count = 0;
void main()
{
int ch;
h = NULL;
temp = temp1 = NULL;
printf(“\n 1 – Insert at beginning”);
printf(“\n 2 – Insert at end”);
printf(“\n 3 – Insert at position i”);
printf(“\n 4 – Delete at i”);
printf(“\n 5 – Display from beginning”);
printf(“\n 6 – Display from end”);
printf(“\n 7 – Search for element”);
printf(“\n 8 – Sort the list”);
printf(“\n 9 – Update an element”);
printf(“\n 10 – Exit”);
while (1)
{
printf(“\n Enter choice : “);
scanf(“%d”, &ch);
switch (ch)
{
case 1:
insert1();
break;
case 2:
insert2();
break;
case 3:
insert3();

Data Structure
Department Of Computer Applications

break;
case 4:
delete();
break;
case 5:
traversebeg();
break;
case 6:
temp2 = h;
if (temp2 == NULL)
printf(“\n Error : List empty to display “);
else
{
printf(“\n Reverse order of linked list is : “);
traverseend(temp2->n);
}
break;
case 7:
search();
break;
case 8:
sort();
break;
case 9:
update();
break;
case 10:
exit(0);
default:
printf(“\n Wrong choice menu”);
}
}
}
void create()
{
int data;
temp =(struct node *)malloc(1*sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf(“\n Enter value to node : “);
scanf(“%d”, &data);
temp->n = data;
count++;
}
void insert1()
{
if (h == NULL)
{
create();

Data Structure
Department Of Computer Applications

h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
void insert2()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
void insert3()
{
int pos, i = 2;
printf(“\n Enter position to be inserted : “);
scanf(“%d”, &pos);
temp2 = h;
if ((pos < 1) || (pos >= count + 1))
{
printf(“\n Position out of range to insert”);
return;
}
if ((h == NULL) && (pos != 1))
{
printf(“\n Empty list cannot insert other than 1 st position”);
return;
}
if ((h == NULL) && (pos == 1))
{
create();
h = temp;
temp1 = h;
return;

Data Structure
Department Of Computer Applications

}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
create();
temp->prev = temp2;
temp->next = temp2->next;
temp2->next->prev = temp;
temp2->next = temp;
}
}
void delete()
{
int i = 1, pos;
printf(“\n Enter position to be deleted : “);
scanf(“%d”, &pos);
temp2 = h;
if ((pos < 1) || (pos >= count + 1))
{
printf(“\n Error : Position out of range to delete”);
return;
}
if (h == NULL)
{
printf(“\n Error : Empty list no elements to delete”);
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
printf(“Node deleted from list”);
free(temp2);
temp2 = h = NULL;
return;
}
}
if (temp2->next == NULL)

Data Structure
Department Of Computer Applications

{
temp2->prev->next = NULL;
free(temp2);
printf(“Node deleted from list”);
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next; /* Might not need this statement if i == 1 check */
if (i == 1)
h = temp2->next;
printf(“\n Node deleted”);
free(temp2);
}
count--;
}
void traversebeg()
{
temp2 = h;
if (temp2 == NULL)
{
printf(“List empty to display \n”);
return;
}
printf(“\n Linked list elements from beginning : “);
while (temp2->next != NULL)
{
printf(“ %d “, temp2->n);
temp2 = temp2->next;
}
printf(“ %d “, temp2->n);
}
void traverseend(int i)
{
if (temp2 != NULL)
{
i = temp2->n;
temp2 = temp2->next;
traverseend(i);
printf(“ %d “, i);
}
}
void search()
{
int data, count = 0;
temp2 = h;
if (temp2 == NULL)
{
printf(“\n Error : List empty to search for data”);

Data Structure
Department Of Computer Applications

return;
}
printf(“\n Enter value to search : “);
scanf(“%d”, &data);
while (temp2 != NULL)
{
if (temp2->n == data)
{
printf(“\n Data found in %d position”,count + 1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf(“\n Error : %d not found in list”, data);
}
void update()
{
int data, data1;
printf(“\n Enter node data to be updated : “);
scanf(“%d”, &data);
printf(“\n Enter new data : “);
scanf(“%d”, &data1);
temp2 = h;
if (temp2 == NULL)
{
printf(“\n Error : List empty no node to update”);
return;
}
while (temp2 != NULL)
{
if (temp2->n == data)
{
temp2->n = data1;
traversebeg();
return;
}
else
temp2 = temp2->next;
}
printf(“\n Error : %d not found in list to update”, data);
}
void sort()
{
int i, j, x;
temp2 = h;
temp4 = h;
if (temp2 == NULL)

Data Structure
Department Of Computer Applications

{
printf(“\n List empty to sort”);
return;
}
for (temp2 = h; temp2 != NULL; temp2 = temp2->next)
{
for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)
{
if (temp2->n > temp4->n)
{
x = temp2->n;
temp2->n = temp4->n;
temp4->n = x;
}
}
}
traversebeg();
}

Output:

Data Structure
Department Of Computer Applications

10.Program for PUSH & POP into stack.


#include<stdio.h>
#include<conio.h>
#define MAX 5
int top,status;
void push(int stack[],int item)
{
if(top==(MAX-1))
status=0;
else
{
status=1;
++top;
stack[top]=item;
}
}
int pop(int stack[])
{
int ret;
if(top==-1)
{
ret=0;
status=0;
}
else
{
status=1;
ret=stack[top];
--top;
}
return ret;
}
void display(int stack[])
{
int i;
printf(“\nThe stack is:”);
if(top==-1)
printf(“Empty”);
else
{
for(i=top;i>=0;--i)
printf(“\n-----------\n|%3d|\n---------“,stack[i]);
}
printf(“\n”);
}
void main()
{
int stack[MAX],item;

Data Structure
Department Of Computer Applications

int ch;
clrscr();
top=-1;
do
{
do
{
printf(“\n MAIN MENU”);
printf(“\n1. PUSH(INSERT) in the stack”);
printf(“\n2. POP (DELETE) from the stack”);
printf(“\n3. EXIT (END THE EXECUTION)”);
printf(“\nEnter your choice:”);
scanf(“%d”,&ch);
if(ch<1||ch>3)
printf(“\nInvalid choice,try again”);
}
while(ch<1||ch>3);
switch(ch)
{
case 1:
printf(“\nEnter the element to be pushed:”);
scanf(“%d”,&item);
printf(“%d”,item);
push(stack,item);
if (status)
{
printf(“\n After pushing”);
display(stack);
if(top==(MAX-1))
printf(“\nThe stack is full”);
}
else
printf(“\nStack overflow on push”);
break;
case 2:
item=pop(stack);
if(status)
{
printf(“\nThe poped item if %d. After poping:”);
display(stack);
}
else
printf(“\nStack underflow on pop”);
break;
default:
printf(“\nEND OF EXECUTION”);
}
}
while(ch!=3);

Data Structure
Department Of Computer Applications

}
getch();

Output:

Data Structure
Department Of Computer Applications

11.Program for Infix to Postfix conversion.


#include<stdio.h>
#include<conio.h>
#include<string.h>
char stack[50];
int top=-1;
void in_to_post(char infix[]);
void push(char);
char pop();
void main()
{
char infix[25];
clrscr();
printf("Enter the infix expression:");
gets(infix);
in_to_post(infix);
getch();
}
void push(char symb)
{
if(top>=49)
{
printf("stack overflow.");
getch();
return;
}
else
{
top=top+1;
stack[top]=symb;
}
}
char pop()
{
char item;
if(top==-1)
{
printf("stack empty.");
getch();
return(0);
}
else
{
item=stack[top];
top--;
}
return(item);
}

Data Structure
Department Of Computer Applications

int preced(char ch)


{
if(ch==47)
{
return(5);
}
else if(ch==42)
{
return(4);
}
else if(ch==43)
{
return(3);
}
else
return(2);
}
void in_to_post(char infix[])
{
int length;
static int index=0,pos=0;
char symbol,temp;
char postfix[40];
length=strlen(infix);
push('#');
while(index<length)
{
symbol=infix[index];
switch(symbol)
{
case '(':push (symbol);
break;
case ')':temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(preced(stack[top])>=preced(symbol))
{
temp=pop();
postfix[pos]=temp;

Data Structure
Department Of Computer Applications

pos++;
}
push (symbol);
break;
default:postfix[pos++]=symbol;
break;
}
index++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos++]='\0';
puts(postfix);
return;
}

Output:

Data Structure
Department Of Computer Applications

12. program for linear queue operations using array.


#include<stdio.h>
#include<conio.h>
#define MAX 50
int queue_array[MAX];
int rear=-1;
int front=-1;
main()
{
int choice;
clrscr();
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:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:printf("Wrong choice\n");
}
}
int add_item;
if(rear==MAX-1)
printf("Queue overflow\n");
else
{
if(front==-1)
front=0;
printf("Insert the element in queue:");
scanf("%d",&add_item);
rear=rear+1;
queue_array[rear]=add_item;
return;

Data Structure
Department Of Computer Applications

}
}
delete()
{
if(front==-1||front>rear)
{
printf("Queue underflow\n");
return;
}
else
{
printf("Element deleted from queue is:%d\n");
front=front+1;
}
}
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");
}
}

Output:

Data Structure
Department Of Computer Applications

Data Structure
Department Of Computer Applications

13. Program for circular queue operation using array.

#include<stdio.h>
#include<conio.h>
#define MAX 3
int q[10],front=0,rear=-1;
main()
{
int ch;
void insert();
void delet();
void display();
void exit();
clrscr();
printf("\nCircular queue operations\n");
printf("1.Isert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit();
default:printf("Invalid option\n");
}
}
}
void insert()
{
int x;
if((front==0&&rear==MAX-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be inserted:");
scanf("%d",&x);
if(rear==MAX-1&&front>0)

Data Structure
Department Of Computer Applications

{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==MAX-1)
{
a=q[front];
front=0;
}
else
a=q[front++];
printf("Dleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch();
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=MAX-1;j++)
printf("\t%d",q[j]);

Data Structure
Department Of Computer Applications

printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
printf("\n");
}
getch();

Output:

Data Structure
Department Of Computer Applications

14. Program for implementation of Sparse Array.


#include<stdio.h>
#include<conio.h>
void main()
{
static int array[10][10];
int i,j,m,n;
int counter=0;
clrscr();
printf("Enter the order of matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the coefficients of the matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&array[i][j]);
if (array[i][j]==0)
{
++counter;
}
}
}
if (counter>(((m*n)/2)))
{
printf("The given matrix is sparse matrix\n");
}
else
printf("The given matrix is not a sparse matrix\n");
printf("There are %d nmbers of zeros",counter);
getch();
}

Data Structure
Department Of Computer Applications

Output:

Data Structure
Department Of Computer Applications

15.Program for Polynomial Addition & Multiplication.


#include<stdio.h>
#include<stdlib.h>
struct node
{
float coef;
int expo;
struct node *link;
};
struct node *create(struct node*);
struct node *insert_s(struct node*,float,int);
struct node *insert(struct node*,float,int);
void display(struct node *ptr);
void poly_add(struct node*,struct node*);
void poly_multi(struct node*,struct node*);
void main()
{
struct node *start1=NULL,*start2=NULL;
printf("Enter polynomial 1:\n");
start1=create(start1);
printf("Enter polynomial 2:\n");
start2=create(start2);
printf("Polynomial 1 is:");
display(start1);
printf("Polynomial 2 is:");
display(start2);
poly_add(start1,start2);
poly_multi(start1,start2);
}
struct node *create(struct node *start)
{
int i,n,ex;
float co;
printf("Enter the no. of terms:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter coefficient for term %d:",i);
scanf("%f",&co);
printf("Enter exponent for term %d:",i);
scanf("%d",&ex);
start=insert_s(start,co,ex);
}
return start;
}
struct node *insert_s(struct node *start,float co,int ex)
{
struct node *ptr,*tmp;

Data Structure
Department Of Computer Applications

tmp=(struct node*)malloc(sizeof(struct node));


tmp->coef=co;
tmp->expo=ex;
if(start==NULL||ex > start->expo)
{
tmp->link=start;
start=tmp;
}
else
{
ptr=start;
while(ptr->link!=NULL&&ptr->link->expo>=ex)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
}
return start;
}
struct node *insert(struct node*start,float co,int ex)
{
struct node *ptr,*tmp;
tmp=(struct node*)malloc(sizeof(struct node));
tmp->coef=co;
tmp->expo=ex;
if(start==NULL)
{
tmp->link=start;
start=tmp;
}
else
{
ptr=start;
while(ptr->link!=NULL)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
}
return start;
}
void display(struct node*ptr)
{
if(ptr==NULL)
{
printf("zero polynomial");
return;
}
while(ptr!=NULL)
{
printf("%1fx^%d",ptr->coef,ptr->expo);

Data Structure
Department Of Computer Applications

ptr=ptr->link;
if(ptr!=NULL)
printf("+");
else
printf("\n");
}
}
void poly_add(struct node*p1, struct node*p2)
{
struct node* start3;
start3=NULL;
while(p1!=NULL&&p2!=NULL)
{
if(p1->expo>p2->expo)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
else if(p2->expo>p1->expo)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
else if(p1->expo==p2->expo)
{
start3=insert(start3,p1->coef+p2->coef,p1->expo);
p1=p1->link;
p2=p2->link;
}
}
while(p1!=NULL)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
while(p2!=NULL)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
printf("Added polynomial is:");
display(start3);
}
void poly_multi(struct node*p1,struct node*p2)
{
struct node*start3;
struct node*p2_beg=p2;
start3=NULL;
if(p1==NULL||p2==NULL)

Data Structure
Department Of Computer Applications

{
printf("Multiplied polynomial is zero polynomial\n");
return;
}
while(p1!=NULL)
{
p2=p2_beg;
while(p2!=NULL)
{
start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo);
p2=p2->link;
}
p1=p1->link;
}
printf("Multiplied polynomial is:");
display(start3);
}

16.Program for Tree Traversal.


Data Structure
Department Of Computer Applications

#include<stdio.h>
struct rec
{
long num;
struct rec *left;
struct rec *right;
};
struct rec *tree=NULL;
struct rec *insert(struct rec *tree,long num);
void preorder(struct rec *tree);
void inorder(struct rec *tree);
void postorder(struct rec *tree);
int count=1;
main()
{
int choice;
long digit;
do
{
choice=select();
switch(choice)
{
case 1: puts("Enter integer: To quit enter 0");
scanf("%ld",&digit);
while(digit!=0)
{
tree=insert(tree,digit);
scanf("%ld",&digit);
}continue;
case 2: puts("\npreorder traversing TREE");
preorder(tree);continue;
case 3: puts("\ninorder traversing TREEE");
inorder(tree);continue;
case 4: puts("\npostorder traversing TREE");
postorder(tree);continue;
case 5: puts("END");exit(0);
}
}while(choice!=5);
}
int select()
{
int selection;
do
{
puts("Enter 1: Insert a node in the BT");
puts("Enter 2: Display(preorder)the BT");
puts("Enter 3: Display(inorder)the BT");
puts("Enter 4: Display(postorder)the BT");
puts("Enter 5: END");

Data Structure
Department Of Computer Applications

puts("Enter your choice");


scanf("%d",&selection);
if((selection<1)||(selection>5))
{
puts("wrong choice:Try again");
getch(); }
}while((selection<1)||(selection>5));
return (selection);
}
struct rec *insert(struct rec *tree,long digit)
{
if(tree==NULL)
{
tree=(struct rec *)malloc(sizeof(struct rec));
tree->left=tree->right=NULL;
tree->num=digit;count++;
}
else
if(count%2==0)
tree->left=insert(tree->left,digit);
else
tree->right=insert(tree->right,digit);
return(tree);
}
void preorder(struct rec *tree)
{
if(tree!=NULL)
{
printf("%12ld\n",tree->num);
preorder(tree->left);
preorder(tree->right);
}
}
void inorder(struct rec *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
printf("%12ld\n",tree->num);
inorder(tree->right);
}
}
void postorder(struct rec *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
printf("%12ld\n",tree->num);

Data Structure
Department Of Computer Applications

}
}
int select()
{
int selection;
do
{
puts("Enter 1: Insert a node in the BT");
puts("Enter 2: Display(preorder)the BT");
puts("Enter 3: Display(inorder)the BT");
puts("Enter 4: Display(postorder)the BT");
puts("Enter 5: END");
puts("Enter your choice");
scanf("%d",&selection);
if((selection<1)||(selection>5))
{
puts("wrong choice:Try again");
getch();
}
}
while((selection<1)||(selection>5));
return (selection);
}
struct rec *insert(struct rec *tree,long digit)
{
if(tree==NULL)
{
tree=(struct rec *)malloc(sizeof(struct rec));
tree->left=tree->right=NULL;
tree->num=digit;count++;
}
else
if(count%2==0)
tree->left=insert(tree->left,digit);
else
tree->right=insert(tree->right,digit);
return(tree);
}
void preorder(struct rec *tree)
{
if(tree!=NULL)
{
printf("%12ld\n",tree->num);
preorder(tree->left);
preorder(tree->right);
}
}
void inorder(struct rec *tree)
{

Data Structure
Department Of Computer Applications

if(tree!=NULL)
{
inorder(tree->left);
printf("%12ld\n",tree->num);
inorder(tree->right);
}
}
void postorder(struct rec *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
printf("%12ld\n",tree->num);
}}

Output:
Data Structure
Department Of Computer Applications

17.Program for BST(Binary Search Tree).


#include<stdio.h>

Data Structure
Department Of Computer Applications

#include<conio.h>
#include<stdlib.h>
struct treenode
{
int data;
struct treenode *left, *right;
};
struct treenode *root=NULL;
struct treenode *createnode(int data)
{
struct treenode *newnode;
newnode=(struct treenode *)malloc(sizeof(struct treenode));
newnode->data=data;
newnode->left=NULL;
newnode->right=NULL;
return(newnode);
}
void insertion(struct treenode**node,int data)
{
if(*node==NULL)
{
*node= createnode(data);
}
else if(data<(*node)->data)
{
insertion(&(*node)->left,data);
}
else if(data>(*node)->data)
{
insertion(&(*node)->right,data);
}
}
void deletion(struct treenode **node,struct treenode **parent,int data)
{
struct treenode *tmpnode,*tmpparent;
if(*node==NULL)
return;
if((*node)->data==data)
{
if(!(*node)->left&&!(*node)->right)
{
if(parent)
{
if((*parent)->left==*node)
(*parent)->left=NULL;
else
(*parent)->right=NULL;
free(*node);
}

Data Structure
Department Of Computer Applications

}
else if(!(*node)->right&&(*node)->left)
{
tmpnode=*node;
(*parent)->right=(*node)->left;
free(tmpnode);
*node=(*parent)->right;
}
else if((*node)->right&&!(*node)->left)
{
tmpnode= *node;
(*parent)->left=(*node)->right;
free(tmpnode);
(*node)=(*parent)->left;
}
else if(!(*node)->right->left)
{
tmpnode= *node;
(*node)->right->left=(*node)->left;
(*parent)->left=(*node)->right;
free(tmpnode);
*node=(*parent)->left;
}
else
{
tmpnode=(*node)->right;
while (tmpnode->left)
{
tmpparent=tmpnode;
tmpnode=tmpnode->left;
}
tmpparent->left= tmpnode->right;
tmpnode->left=(*node)->left;
tmpnode->right=(*node)->right;
free(*node);
*node=tmpnode;
}
}
else if(data<(*node)->data)
{
deletion(&(*node)->left,node,data);
}
else if(data>(*node)->data)
{
deletion(&(*node)->right,node,data);
}
}
void findElement(struct treenode *node,int data)
{

Data Structure
Department Of Computer Applications

if(!node)
return;
else if(data<node->data)
{
findElement(node->left,data);
}
else if(data>node->data)
{
findElement(node->right,data);
}
else
printf("data found:%d\n",node->data);
return;
}

int main()
{
int data,ch;
clrscr();
while(1)
{
printf("\n1.Insertion in binary search tree");
printf("\n2.deletion in binary search tree");
printf("\n3.search element in binary search tree");
printf("\n4.exit");
printf("\n Enter ur choice:");
scanf("\t%d",&ch);
switch (ch)
{
case 1:
while(1)
{
printf("\n Enter ur data:");
scanf("\n%d",&data);
insertion(&root, data);
printf("continue insertion(0/1):");
scanf("%d",&ch);
if(!ch)
break;
}
break;
case 2:
printf("enter ur data:");
scanf("%d",&data);
deletion(&root, NULL,data);
break;
case 3:
printf("enter value for data");
scanf("%d",&data);

Data Structure
Department Of Computer Applications

findElement(root,data);
break;

case 4:
exit(0);
default:
printf("You have entered wrong opertaion\n");
break;
}
}
return(0);
}

Output:

Data Structure
Department Of Computer Applications

18.Program for Bubble Sort.


#include<stdio.h>

Data Structure
Department Of Computer Applications

#include<conio.h>
void main()
{
int a[100] ,n,i,j,temp;
clrscr();
printf("How many elements?\n");
scanf("%d",&n);
printf("Enter the element of array:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Elelments of array after the sorting are:\n");
for(i=1;i<=n;i++)
{
printf("%d\n",a[i]);
}
getch();
}

Output:

Data Structure
Department Of Computer Applications

19.Program for Selection Sort.


#include<stdio.h>
#include<conio.h>

Data Structure
Department Of Computer Applications

void main()
{
int a[100],n,i,j,temp,loc,min;
clrscr();
printf("How many elements?\n");
scanf("%d",&n);
printf("Enter the elements of array:\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
min=a[0];
for(i=0;i<=n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<=n-1;j++)
{
if(a[j]<min)
{
min=a[j];
loc=j;
}
}
if(loc!=1)
{
temp=a[i];
a[i]=a[loc] ;
a[loc]=temp;
}
}
printf("The number after selection sorting are:\n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
getch();
}

Output:

Data Structure
Department Of Computer Applications

20.Program for Binary search.


#include<stdio.h>
#include<conio.h>
void main()
{

Data Structure
Department Of Computer Applications

int a[100],i,loc,mid,beg,end,n,flag=0,item;
clrscr();
printf("How many elements:");
scanf("%d",&n);
printf("Enter the elements of the array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be search:");
scanf("%d",&item);
loc=0;
beg=0;
end=n-1;
while((beg<=end)&&(item!=a[mid]))
{
mid=((beg+end)/2);
if(item==a[mid])
{
printf("Search is successfull");
loc=mid;
printf("\nPosition of the item is: %d",loc+1);
flag=flag+1;
}
if(item<a[mid])
end=mid-1;
else
beg=mid+1;
}
if(flag==0)
{
printf("Search is not successfull");
}
getch();
}

Output:

Data Structure
Department Of Computer Applications

21.Program for Quick Sort.


#include<stdio.h>
#include<conio.h>
#define max 100
int a[max],n,i,l,h;

Data Structure
Department Of Computer Applications

void main()
{
void input(void);
clrscr();
input();
getch();
}
void input(void)
{
void output(int a[],int n);
void quick_sort(int a[],int l,int h);
printf("How many elements?\n");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0;i<n-1;i++)
{
scanf("%d",&a[i]);
}
l=0;
h=n-1;
quick_sort(a,l,h);
printf("Sorted array:\n");
output(a,n);
}
void quick_sort(int a[],int l,int h)
{
int temp,key,low,high;
low=1;
high=h;
key=a[(low+high)/2];
do
{
while(key>a[low])
{
low++;
}
while(key<a[high])
{
high--;
}
if(low<=high)
{
temp=a[low];
a[low++]=a[high];
a[high--]=temp;
}
}
while(low<=high);
if(1<high)

Data Structure
Department Of Computer Applications

quick_sort(a,l,high);
if(low<h)
quick_sort(a,low,h);
}
void output(int a[],int n)
{
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
getch();
}

22.Program for Linear search.


#include<stdio.h>
#include<conio.h>
void main()
{

Data Structure
Department Of Computer Applications

int a[100],n,i,item,loc=-1;
clrscr();
printf("Enter the number of element:");
scanf("%d",&n);
printf("Enter the number:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the no. to be search:");
scanf("%d",&item);
for(i=0;i<=n;i++)
{
if(item==a[i])
{
loc=i;
break;
}
}
if(loc>=0)
printf("%d is found in position %d",item,loc+1);
else
printf("Item does not exist");
getch();
}

Output:

Data Structure

You might also like