Data Structure Lab Programs
Data Structure Lab Programs
#include<stdio.h>
#include<stdlib.h>
int A[100],n,key;
int 1search (int A[],int n,int key)
{
int i=-1;
while (i<n) {/*Traversing till the end of table*/
if (A[++i] == key) /* element is found */
return i;
}
return -1; /* item is not found */
}
void main()
{
int k,temp,loc,ch;
while(1)
{
printf("\n Sorting Techiques");
printf("\n*********************");
printf("\n 1. Insertion Sort ");
printf("\n 2. Selection Sort ");
printf("\n 3. Exit ");
printf("\n Enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1: acceptInput();
insertion_sort(a,n);
display();
break;
case 2: acceptInput();
for(k=0; k<n; k++)
{
loc=max(a,k,n);/* find the largest element location */
temp=a[k];
a[k]=a[loc];
a[loc]=temp;
}
display();
break;
case 3: exit(0);
}
}
}
*******************Output******************
Sorting Techniques
************************
1. Insertion Sort
2. Selection Sort
3. Exit
Enter your choice:1
Enter the number of elements:8
Enter the array elements:75 8 1 16 48 3 7 0
The Sorted Array is:75 48 16 8 7 3 1 0
**************Program 4***********
#include<stdio.h>
#include<stdlib.h>
struct node
{
int INFO;
struct node *LINK;
};
typedef struct node NODE;
NODE *start=NULL;
void main()
{
int ch, item, pos;
while(1)
{
printf("\n 1. Create a Linked List ");
printf("\n 2. Display ");
printf("\n 3. Insert First Node ");
printf("\n 4. Insert at the End ");
printf("\n 5. insert at the Specified Position ");
printf("\n 6. Delete First Node ");
printf("\n 7. Delete Last Node ");
printf("\n 8. Delete at the Specified Position ");
printf("\n 9. Delete a Node When Item is Given ");
printf("\n 10. Exit");
printf("\n Enter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: start=NULL;
create();
break;
case 2: display();
break;
case 3: printf("\n Enter the Item to Insert at the Beginning:
");
scanf("%d", &item);
printf("\n Linked List before Insertion is: \n");
display();
insert_beg(item);
printf("\n Linked List after Insertion is: \n");
display();
break;
case 4: printf("\n Enter the Item to Insert at the End: ");
scanf("%d", &item);
printf("\n Linked List before Insertion is:\n");
display();
insert_end(item);
printf("\n Linked Lst after Insertion is: \n");
display();
break;
case 5: printf("\n Enter an Item to Insert at Certain Position:
");
scanf("%d", &item);
printf("\n Enter a Valid Position: ");
scanf("%d",&pos);
if((pos==0)||(pos>length()+1))
{
printf("\n It is Invalid Position \n");\
break;
}
else
{
printf("\n Linked List before Insertion is: \n");
display();
insert_pos(item, pos);
printf("\n Linked List after Insertion is: \n");
display();
break;
}
case 6: printf("\n Linked List before Deletion is: \n");
display();
delete_beg();
printf("\n Linked List after Deletion is: \n");
display();
break;
case 7: printf("\n Linked List before Deletion is: \n");
display();
delete_end();
printf("\n Linked List after Deletion is: \n");
display();
break;
case 8: printf("\n Enter a Valid Position to Delete: \n");
scanf("%d",&pos);
if((pos==0)||(pos>length()))
printf("\n It is Invalid position \n");
break;
}
else
{
printf("\n Linked List before Deletion is: \n");
display();
delete_pos(pos);
printf("\n Linked List after Deletion is: \n");
display();
break;
}
case 9: printf("\n Linked List before Deletion is: \n");
display();
printf("\n Enter an Item to be Deleted: \n");
scanf("%d",&item);
delete_item(item);
printf("\n Linked list after Deletion is: \n");
display();
break;
case 10: exit(0);
}
}
}
*****************Output:****************
1.Create a Linked List
2.Display
3.Insert First Node
4.Insert at the End
5.insert at the Specified Position
6.Delete First Node
7.Delete Last Node
8.Delete at the Specified Position
9.Delete a Node When Item is Given
10. Exit
Enter Your Choice: 1
Enter the node 1:61
Do you wish to add one more node (Y/N): y
Enter the node 2:16
Do you wish to add one more node (Y/N): y
Enter the node 3 18
Do you wish to add one more node (Y/N): y
Enter the node 4:27
Do you wish to add one more node (Y/N): n
1. Create a Linked List
2. Display
3. Insert First Node
4. Insert at the End
5. insert at the Specified Position
6. Delete First Node
7. Delete Last Node
8. Delete at the Specified Position
9. Delete a Node When Item is Given
10. Exit
Enter Your Choice: 2
6116-8-27-> NULL
1. Create a Linked List
2. Display
3. Insert First Node
4. Insert at the End
5. insert at the Specified Position
6. Delete First Node
7. Delete Last Node
8. Delete at the Specified Position
9. Delete a Node When Item is Given
10. Exit
Enter Your Choice: 8
***************Program 5***************
#include<stdio.h>
#include<stdlib.h>
#define N. 10
int QUEUE[N], FRONT-0, REAR--1, ITEM;
REAR P maximum
not full Full to pr
void main()
{
int ch;
while(1)
{
printf("\n Queue Implementation using Array");
printf("\n***********************************");
printf("\n 1. Insert into Queue ");
printf("\n 2. Delete from Queue ");
printf("\n 3. Display Queue ");
printf("\n 4. Exit ");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: Qinsert();
Qdisplay();
break;
case 2: Qdelete();
Qdisplay();
break;
case 3: Qdisplay();
break;
case 4: exit(0);
}
}
}
***************Output:***************
Queue Implementation using Array
***************************************
1. Insert into Queue
2. Delete from Queue
3. Display Queue
4. Exit
Enter your choice:1
Enter an Item:45
Queue:45
*****************Program 6****************
#include<stdio.h>
#include<stdlib.h>
#define N 10
switch(ch)
{
case 1: CQinsert();
CQdisplay();
break;
case 2: CQdelete();
CQdisplay();
break;
case 3: CQdisplay();
break;
case 4: exit(0);
}
}
}
***************Output***************
Circular Queue implementation using Array
********************************************
1. Circular Queue Insert
2. Circular Queue Delete
3. Circular Queue Display
4. Exit
Enter your Choice:1
Enter the Element to be Inserted:61
Circular Queue:61
Front Element of the CQueue is:61
Rear Element of the CQueue is:61
Circular Queue implementataion using Array
****************Program 7***************
#include<stdio.h>
#include<stdlib.h>
struct node
{
int INFO;
struct node *LINK;
};
typedef struct node NODE;
NODE *start-NULL;
void insertOrdered(int data)
{
NODE *NEWNODE (NODE *)malloc(sizeof(NODE));
NEWNODE-INFO= data;
if(start==NULL)
{
start NEWNODE;
start-> LINK NULL;
}
else if(data start->INFO)
{
NEWNODE->LINK=start;
start=NEWNODE;
}
else
{
NODE PREVPTR start;
NODE CURRPTR start->LINK;
while (CURRPTR != NULL && data > CURRPTR->INFO)
{
PREVPTR CURRPTR;
CURRPTR CURRPTR->LINK;
}
PREVPTR->LINK = NEWNODE;
NEWNODE->LINK = CURRPTR;
}
}
void deleteOrdered(int data)
{
NODE PREVPTR = start;
NODE CURRPTR start->LINK;
if(start--NULL)
printf("\n List is Empty");
else if(data start->INFO)
{
start-CURRPTR;
free(PREVPTR);
}
else
{
while (CURRPTR 1- NULL && CURRPTR->INFO 1= data)
{
PREVPTR CURRPTR;
CURRPTR CURRPTR->LINK;
}
if(CURRPTR != NULL)
{
PREVPTR->LINK CURRPTR->LINK;
free(CURRPTR);
}
else
printf("\n Data Not Found in the List");
}
}
void display()
{
NODE CURRPTR = start;
if (CURRPTR NULL)
printf(" Empty");
else
{
while (CURRPTR 1= NULL)
{
printf("%d", CURRPTR -> INFO);
printf(" -> ");
CURRPTR CURRPTR -> LINK;
}
printf ("NULL");
}
}
int main()
{
int ch, data;
while(1)
{
printf("\n Ordered Linked List Operations");
printf("\n********************************");
printf("\n 1. Insert");
printf("\n 2. Delete");
printf("\n 3. Display");
printf("\n 4. Exit");
printf("\n Enter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter Data to be Inserted: ");
scanf("%d", &data);
printf("\n Linked List before Insertion is: \n");
display();
insertOrdered(data);
printf("\n Linked List after Insertion is: \n");
display();
break;
case 2: printf("\n Enter Data to be Deleted: ");
scanf("%d",&data);
printf("\n Linked List before Deletion is: \n");
display();
deleteOrdered(data);
printf("\n Linked List after Deletion is: \n");
display();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
***************Output****************
Ordered Linked List Operations
*********************************
1. Insert
2. Delete
3. Display
4. Exit
Enter Your Choice:1
Enter Data to be Inserted:61
Linked List before Insertion:Empty
Linked List after Insertion is:61->NULL
***************Program 8***************
#include<stdio.h>
void toh(int,char,char,char);
int count=0;
void main()
{
char source='S'temp='T',dest='D';
int n;
printf ("Enter the number of disks:");
scanf ("%d",&n);
printf("\n Sequence is :");
toh(n,source,temp,dest);
printf("\n The Number of Moves:%d",count);
void toh(int n,char source,char temp,char dest)
{
if (n>0)
{
toh (n-1,source,dest,temp);
printf("\n Move Disk %d %c->%c \n",n,source,dest);
count++;
toh (n-1,temp,source,dest);
}
}
*******************Output*************
Enter the number of disks:3
Sequence is:
Move Disk 1 S->D
Move Disk 2 S->T
Move Disk 1 D->T
Move Disk 3 S->D
Move Disk 1 T->S
Move Disk 2 T->D
Move Disk 1 S->D
The Number of Moves:7
*******************program 9*******************
#include <stdio.h>
if (n==0)
return(m);
else if (n > m)
return(GCD(n, m));
else
void main()
int k, m, n;
*******************Output***********************
GCD (10,20,100) = 10
**********************program10*************************
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
struct stack
int info;
};
void main()
do
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. DISPLAY");
printf("\n 4. EXIT");
scanf("%d", &choice);
switch(choice);
push(item);
break;
if(item!=-1)
break;
case 3: display();
break;
}while(choice!=4);
********************Output***********************
*MAIN MENU*
1. PUSH
2. POP
3. DISPLAY
4. EXIT
*MAIN MENU*
1. PUSH
2. POP
3. DISPLAY
4. EXIT
*MAIN MENU*
1. PUSH
2. POP
3. DISPLAY
4. EXIT
*MAIN MENU*
1. PUSH
2. POP
3. DISPLAY
4. EXIT
30
20
10
********************program11********************
#include<stdio.h>
#include<string.h>
#define MAX 20
Char s[MAX];
Int top=0;
Int pop();
Void main()
int i=0,j=0;
printf(“\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”);
Scanf(“%s”, infix);
Push(‘#’);
Ch=infix[i];
If(isalnum(ch))
Postfix[j++]=ch;
Else if(ch==’(‘)
Push(ch);
Else if(ch==’)’)
{ while(s[top]!=’(‘)
postfix[j++] = pop();
elem = pop();
Else
Postfix[j++] pop();
Push(ch);
While(s[top] !=’#’)
Postfix[j++]-pop();
Postfix[j]=’\0’;
Switch(elem)
Case ‘+’:
Case’*’ :
Case’^’: return(3);
Case ‘(‘:
Case ‘#’ : return(0);
++top;
s[top]=elem;
Int pop()
char elem;
elem=s[top];
--top;
return(elem);
**********************Output**********************
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<string.h>
#include<math.h>
#define MAX 20
void main()
Printf(“\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”);
scanf("%s", &postfix);
for(i=0; i<strlen(postfix);i++)
ch=postfix[i];
if(isdigit(ch))
push(ch-'0');
else
op2=pop();
op1=pop();
switch(ch)
} push(res);
**********************Output***********************
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<stdlib.h>
Struct node
Int info;
};
While(ptr->left != NULL)
Ptr ptr->left;
Return ptr;
NODE *temp;
If(!p) {
Return p;
else {
if(p->left= NULL) {
temp p->right;
free(p);
return temp;
temp = p->left;
free(p);
return temp;
pinfo temp->info;
return p;
void main() {
int item,ch,n,1;
while(1) {
printf("\n----------------------------“);
scanf("%d",&ch);
switch(ch) {
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%d",&item);
create(item);
break;
scanf("%d",&item);
disp(root, 1);
break;
are:\n\n\n\n");
disp(root,1);
break;
case 4: exit(1);
}
}
******************* Output***********************
----------------------------
1. Insert
2. Delete
3. Display
4. Exit
50
40 41
18 30
17
15
***********************program 14*************************
#include<stdio.h>
#include<stdlib.h>
struct node
int info;
};
NODE *root=NULL;
Void main() {
int item,i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&item);
create(item);
disp(root, 1);
in_order(root);
printf("\n Preorder Traversal is: ");
pre_order(root);
post_order(root);
*********************Output*********************
5 6
#include <stdio.h>
int left = 2i + 1
int right = 2i + 2 ;
int temp;
largest = left;
largest right;
if (largest l = i ) {
temp=arr[i];
arr[i]=arr[largest];
arr[largest]=temp;
heapify(arr, n, largest);
int i, temp;
heapify(arr, n,i);
temp=arr[0];
arr[0]=arr[1];
arr[i]=temp;
heapify(arr, i, 0);
int main()
int arr[20];
int n, i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
heapSort(arr, n);
printf("%d", arr[i]);
***********************Output ************************
Sorted array is : 10 20 30 40 50 60 70 80
********************** program16*********************
#include <stdio.h>
Int length 0;
Length++;
Return length;
Int I = 0 j=0;
Result[i] = s 1[i] ;
i++;
Result[1] s2[j];
i++;
j++;
Result[1]= ‘\0’;
Void substringExtract (char str[], int start, int length, char result[]) {
Int i;
}
Result[i] = ‘\0’;
K = 0;
k++;
flag = 1 ;
start = i ;
break;
i++;
if (i == start) {
result[j] = replace[k];
1 + stringLength (find);
} else {
result[j++] str[i++];
}
result[j] = '\0';
int main() {
substringExtract(s1, 1, 3, substringResult);
return 0;
Length of S1: 7
#include <stdio.h>
#define MAX 10
int i, j;
printf("\nAdjacency Matrix:\n");
printf("%d", matrix[i][j]);
printf("\n");
void main() {
scanf("%d", &vertices);
scanf("%d", &edges);
scanf("%d", &choice);
matrix[dest-1][src-1] = 1;
displayMatrix(matrix, vertices);
**********************Output***********************
Adjacency Matrix:
01100
00011
00010
10011
00000
*************************program 18*************************
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 11
#define EMPTY -1
if (index == originalIndex) {
return;
hashTable[index] = key;
return index;
if (index == originalIndex) {
break;
return -1;
void display() {
int i;
printf("\nHash Table:\n");
if (hashTable[i] == EMPTY)
else
printf("\n");
int main() {
hashTable[i] = EMPTY;
}
printf("Hash Table Size is: %d \n", TABLE_SIZE);
scanf("%d", &num);
scanf("%d", &key);
insert(key);
display();
scanf("%d", &key);
if (result != -1)
else
return 0;
*********************Output***********************
Enter 7 elements:
25 46 10 36 18 29 43
Inserted 25 at index 3
Inserted 46 at index 2
Inserted 10 at index 10
Inserted 36 at index 4
Inserted 18 at index 7
Inserted 29 at index 8
Inserted 43 at index 0
Hash Table:
Index 0: 43
Index 1: EMPTY
Index 2: 46
Index 3: 25
Index 4: 36
Index 5: EMPTY
Index 6: EMPTY
Index 7: 18
Index 8: 29
Index 9: EMPTY
Index 10: 10