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

DATA STRUCTURE EN_NO:226010307097

() A.Y.D.T.I

PRACTICAL: 2
AIM:IMPLEMENT ARRAY USING ROW COLUMN MAJOR ORDER
AND COLUMN MAJOR ORDER

INPUT:
#include<stdio.h>
#include<conio.h>
#define max 10
int main()
{
inta[max][max],m,n,i,j;
clrscr();
printf("Enter total no of rows");
scanf("%d",&m);
printf("Enter total no of columne");
scanf("%d",&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter the no:");
scanf("%d",&a[i][j]);
}
}
printf("row major order");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf("");
}
printf("columne major order");
for(j=0;j<n;j++)
{
printf("%d",a[j][i]);
}
}
printf("");
getch();
}

Page | 1
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

OUTPUT:

Page | 2
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 3
AIM:IMPLEMENT SEQENTIAL SEARCH ALGORITHMS.

INPUT:
#include <stdio.h>
#include <conio.h>
main()
{
int arr[]={12,23,78,98,67,56,45,19,65,9},key,i,flag=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&key);
for(i=0;i<10;i++)
{
if(key==arr[i])
flag=1;
}
if(flag==1)
printf("\nTHE NUMBER %d EXISTS IN THE ARRAY",key);
else
printf("\nTHE NUMBER %d DOES NOT EXIST IN THE ARRAY",key);
getch();
}

OUTPUT:

Page | 3
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 4

AIM:IMPLEMENT BINARY SEARCH ALGORITHMS.

INPUT:
#include <stdio.h>
int binarySearch(int a[], int beg, int end, int val)
{
int mid;
if(end >= beg)
{ mid = (beg + end)/2;
/* if the item to be searched is present at middle */
if(a[mid] == val)
{
return mid+1;
}
/* if the item to be searched is smaller than middle, then it can only be in left subarray */
else if(a[mid] <val)
{
return binarySearch(a, mid+1, end, val);
}
/* if the item to be searched is greater than middle, then it can only be in right subarray */
else
{
return binarySearch(a, beg, mid-1, val);
}
}
return -1;
}
int main() {
int a[] = {49, 14, 85, 90, 990, 11, 72, 99, 90}; // given array
int val = 40; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = binarySearch(a, 0, n-1, val); // Store result
printf("The elements of the array are - ");
for (int i = 0; i< n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");

Page | 4
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

else
printf("\nElement is present at %d position of array", res);
return 0;
}

OUTPUT:

Page | 5
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL 5
AIM:IMPLEMENT VARIOUS STRING ALGORITTHMS.

PRACTICAL 5.1

AIM:WRITE A PROGRAM TO COPY STRING

#include<stdio.h>
#include<conio.h>
#include<string.h>
void strcpy(char[],char[]);
void main()
{
char str1[30],str2[30]="10";
clrscr();
printf("\n enter source string");
gets(str1);
strcpy(str1,str2);
printf("\n after copy str1 into str2 the str2 is:");
puts(str2);
getch();
}
void strcpy(char*s1,char*s2)
{
while(*s1!='10')
{
*s2=*s1;
s1++;
s2++;
}
*s2='10';
}

OUTPUT:~

Page | 6
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL 5.2

AIM:WRITE A PROGRAM TO REVERSE A STRING.


#include<stdio.h>

#include<conio.h>

#include<string.h>

void str_rev(char[],char[]);

void main()

char org[50],rev[50];

clrscr();

printf("enter string");

gets(org);

str_rev(org,rev);

printf("reverse string %s",rev);

getch();

OUTPUT:~

Page | 7
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 5.3
AIM:WRITE A PROGRAM FOR COMPERSION OF STING TWO
STRING.
INPUT:-

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int values;
char s1[30],s2[30];
clrscr();
printf("enter string1:");
gets(s1);
printf("enter string2:");
gets(s2);
values=strcmp(s1,s2);
if(values==0)
{
printf("same string");
}
else
{
printf("not same string");
}
getch();
}

OUTPUT:-

Page | 8
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 5.4
AIM:WRITE A PROGRAM FOR CONVERT LOWERCASE TO
UPPERCASE.

INPUT:
#include<stdio.h>
#include<conio.h>
int strlen(char[]);
void upper_case(char[],char[]);
void main()
{
char str1[15],str2[15];
clrscr();
printf("\n enter original string :");
gets(str1);
*str2='\0';
upper_case(str1,str2);
printf("printf upper case string :");
puts(str2);
getch();
}
void upper_case(char *s1,char *s2)
{
int l;
l=strlen(s1);
l=l-1;
while(*s1!='\0')
{
if(*s1>='a' && *s1<='z'){
*s2=*s1 - 32;
}
else{
*s2=*s1;
}
s1++;
s2++;
}
*s2='\0';
}
int strlen(char *s)
{
int m=0;
while(*s!='\0')

Page | 9
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

{
m=m+1;
s++;
}
return m;
}
OUTPUT:

Page | 10
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 5.5
AIM:WRITE A PROGRAM FOR CONCATE TWO STRING INTO THIRD
ONE.

INPUT:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void strcat(char[],char[],char[]);
void main()
{
char str1[30],str2[30],str3[40]="10";
clrscr();
printf("\n enter first string:");
gets(str1);
printf("\n enter second string:");
gets(str2);
strcat(str1,str2,str3);
printf("after concatenating two string str1 and str2 the str3 is:");
puts(str3);
getch();
}
void strcat(char*s1,char*s2,char*s3)
{
while(*s1!='\0')
{
*s3=*s1;
s1++;
s3++;
}
while(*s2!='\0')
{
*s3=*s2;
s2++;
s3++;
*s3='\0';
}
}
OUTPUT:

Page | 11
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 6
AIM:IMPLEMENT PUSH AND POP ALGORITHMA OF STACK USING
ARRAY.

INPUT:
#include<stdio.h>
#include<conio.h>
void push();
void pop();
void display();
int s[10], top=-1,x;
void main()
{
int choice;
printf("stack operation");
printf("\n 1 for push");
printf("\n 2 for pop");
printf("\n 3 for display");
scanf("%d",&choice);
do
{
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default: exit(0);
}
printf("stack operation");
printf("\n 1 for push");
printf("\n 2 for pop");
printf("\n 3 for display");
printf("\nEnter your choice");
scanf("%d",&choice);
}
while((choice>0)||(choice<=3));
getch();

Page | 12
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

}
void push()
{
if(top>=9)
{
printf("stack is overflow");
}
else
{
printf("Enter the value of insert");
scanf("%d",&x);
top=top+1;
s[top]=x;
}
}
void pop()
{
if(top<0)
{
printf("stack is underflow");
}
else
{
x=s[top];
top=top-1;
printf("Delete element is %d\n",x);
}
}
void display()
{
int i;
for (i=top; i>0; i--)
{
printf("stack is %d\n",&s[i]);
}
}

OUTPUT:

Page | 13
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 7
AIM:WRITE A PROGARM TO IMPLEMENT RECURSIVE FUNCTIONS.

INPUT:
#include <stdio.h>
#include <conio.h>
int fibonacci(int num)
{
if (num == 0)
{
return 0; // retuning 0, if condition meets
}

else if (num == 1)
{
return 1; // returning 1, if condition meets
}
else
{
return fibonacci(num - 1) + fibonacci(num - 2);
}
}

int main()
{
int num,i;
clrscr();
printf("Enter the number of elements to be in the series : ");
scanf("%d", &num);
for (i = 0; i<num; i++)
{
printf("%d, ", fibonacci(i));
}
getch();
return 0;
}
OUTPUT:

Page | 14
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL 8
AIM:IMPLEMENT INSERT ALGORITHMS OF QUEUE USING ARRAY.

INPUT:
#include<stdio.h>
#include<conio.h>
#define SIZE 10

void enQueue(int);
void deQueue();
void display();

int queue[SIZE], front = -1, rear = -1;

void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue(int value){

Page | 15
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}
OUTPUT:

Page | 16
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 9
AIM:IMPLEMENT DELETE ALGORITHMS OF QUEUE ARRAY.

INPUT:
#include<stdio.h>
#include<conio.h>
#define SIZE 10
void enQueue(int);
void deQueue();
void display();
int queue[SIZE], front = -1, rear = -1;

void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue(int value){
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;

Page | 17
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

printf("\nInsertion success!!!");
}
}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}

OUTPUT:

Page | 18
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 10
AIM:IMPLEMENT SIMPLE STRUCTURE PROGRAM USING
POINTERS.

INPUT:
#include<stdio.h>
struct student{
int sno;
char sname[30];
float marks;
};
int main ( )
{
struct student s;
struct student *st;
printf("enter sno, sname, marks:");
scanf ("%d%s%f", &s.sno, s.sname, &s. marks);
st = &s;
printf ("\n details of the student are");
printf ("\n Number = %d", st ->sno);
printf ("\n name = %s", st->sname);
printf ("\n marks =%f", st ->marks);
return 0;
}

OUTPUT:

Page | 19
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACATICAL:-11

AIM:IMPLEMENT YNSERTION OF NODE IN THE BEGINNIG OF THE


LIST IN SINGLY LINKED LIST.

INPUT:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
}*head;
void createlist(int n);
void insert node at beginning(int data);
void display list();
int main()
{
int n,data;
clrscr();
printf("\n enter total no of nodes:");
scanf("%d",&n);
create list(n);
printf("\n data in list\n");
display list();
printf("\n enter data to insert at beginning of list:");
scanf("%d",&data);
insert node at beginning(data);
printf("\n data in the list \n ");
display list();
getch();
return 0;
}
void create list(int n)
{
struct node*newnode,*temp;
int data i;
head=(struct node*)malloc(size of(struct node));
if(head ==null)
{
printf("unable to alternate memory");
}
else

Page | 20
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

{
printf("enter data of node1:");
scanf("%d",&data);
head->data=data;
head->next=null;
temp=head;
for(i=2;i<=n;i++)
{
newnode=(struct node*)molloc(size of(struct node));
if(newnode==null)
{
printf("unable to alternate memory");
break;
}
else
{
printf("enter the data of node %d",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=null;
temp->next-newnode;
temp=temp->next;
}
}
printf("singly linked list created successfully\n")
}
}
void insert node at beginning(int data)
{
struct node*newnode;
newnode=(struct node*)malloc(size of(struct node));
if(newnode==null)
{
printf("unable to alternate memory");
}
else
{
newnode->data=data;
Snewnode->next=head;
head=newnode;
printf("data inserted successfully\n");
}
}
void displaylist()
{
struct node*temp;
if(head==null)
{
printf("list is empty");

Page | 21
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

}
else
{
temp=head;
while(temp!=null)
{
printf("data %d\n",temp->data);
temp=temp->next;
}
}
}

OUTPUT:

Page | 22
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 12
AIM:IMPLEMENT INSERTION OF NODE AT THE END OF LIST IN
SINGLY LINKEDLIST.

INPUT:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
struct node *next;
}*head;
void createList(int n);
void insertNodeAtEnd(int data);
void displayList();
int main()
{
int n, data;
clrscr();
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nEnter data to insert at end of the list: ");
scanf("%d", &data);
insertNodeAtEnd(data);
printf("\nData in the list \n");
displayList();
getch();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}

Page | 23
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data; // Link the data field with data
head->next = NULL; // Link the address field to NULL
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void insertNodeAtEnd(int data)
{
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data;
newNode->next = NULL;
temp = head;
while(temp != NULL && temp->next != NULL)
temp = temp->next;
temp->next = newNode;
printf("DATA INSERTED SUCCESSFULLY\n");

Page | 24
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}

OUTPUT:

Page | 25
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRCTICAL: 13
AIM:IMPLEMENT INSERTION OF NODE IN SORTED KINKED LIST.

INPUT:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct Node
{
int data;
struct Node *next;
};
void display(struct Node *node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
struct Node* newNode(int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertion_sort(struct Node **head, struct Node *newNode)
{ struct Node *current = *head;
if (*head == NULL || (*head)->data >= newNode->data)
{
newNode->next = *head;
*head = newNode;
return;
}
while (current->next != NULL && current->next->data <newNode->data)

Page | 26
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

current = current->next;
newNode->next = current->next;
current->next = newNode;
}
int main()
{
int k;
struct Node *head = NULL;
struct Node *node2 = NULL;
struct Node *node3 = NULL;
clrscr();
head = (struct Node*)malloc(sizeof(struct Node));
node2 = (struct Node*)malloc(sizeof(struct Node));
node3 = (struct Node*)malloc(sizeof(struct Node));
head->data = 10; // data set for head node
head->next = node2;
node2->data = 15;
node2->next = node3;
node3->data = 20;
node3->next = NULL;
printf("Linked list before insertion : ");
display(head);
printf("\nEnter data you want to insert: ");
scanf("%d",&k);
insertion_sort(&head, newNode(k));
printf("Linked list after insertion : ");
display(head);
getch();
return 0;
}
OUTPUT:

Page | 27
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 14
AIM:IMPLEMENT INSERTION OF NODE AT AYE POSITION IN
LINKED LIST.

INPUT:
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
int getCurrSize(struct Node* node){
int size=0;
while(node!=NULL){
node = node->next;
size++;
}
return size;
}
void insertAfterNthNode(int n, int data, struct Node** head)
{
int size = getCurrSize(*head);
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if(n < 0 || n > size)
printf("Invalid position to insert\n");
else if(n == 0){
newNode->next = *head;
*head = newNode;
}
else
{
struct Node* temp = *head;
while(--n)
temp=temp->next;
newNode->next= temp->next;
temp->next = newNode;
}
}
void display(struct Node* node){
printf("Linked List : ");

Page | 28
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

while(node!=NULL){
printf("%d ",node->data);
node = node->next;
}
printf("\n");
}
int main()
{
struct Node* head = NULL;
struct Node* node2 = NULL;
struct Node* node3 = NULL;
struct Node* node4 = NULL;
clrscr();
head = (struct Node*)malloc(sizeof(struct Node));
node2 = (struct Node*)malloc(sizeof(struct Node));
node3 = (struct Node*)malloc(sizeof(struct Node));
node4 = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = node2;
node2->data = 20;
node2->next = node3;
node3->data = 30;
node3->next = node4;
node4->data = 40;
node4->next = NULL;
display(head);
insertAfterNthNode(1, 15, &head);
insertAfterNthNode(3, 25, &head);
insertAfterNthNode(5, 35, &head);
insertAfterNthNode(7, 45, &head);
display(head);
insertAfterNthNode(-2, 100, &head);
insertAfterNthNode(10, 200, &head);
getch();
return 0;
}
OUTPUT:

Page | 29
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 15
AIM:IMPLEMENT COUNTING NO OF NODE ALGORITHMS IN
SINGLY LINKED LIST.

INPUT:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
struct node *next;
}*head;
void createList(int n);
int countNodes();
void displayList();
int main()
{
int n, total;
clrscr();
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
total = countNodes();
printf("\nTotal number of nodes = %d\n", total);
getch();
return 0;
}

void createList(int n)
{
struct node *newNode, *temp;
int data, i;

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

if(head == NULL)
{
printf("Unable to allocate memory.");

Page | 30
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");


}
}

int countNodes()
{
int count = 0;
struct node *temp;

temp = head;

while(temp != NULL)
{
count++;
temp = temp->next;

Page | 31
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

return count;
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}

OUTPUT:

Page | 32
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 16
AIM:IMPLEMENT SEARCHING OF A NODE ALGORIYHMS IN
SINGLY LINKED LIST.

INPUT:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
struct node *next;
} * head;
void createList(int n);
void displayList();
int searchRecursive(int key, struct node *curNode, int index);
int main()
{
int n, keyToSearch, index;
clrscr();
printf("Enter number of node to create: ");
scanf("%d", &n);
createList(n);
printf("\nData in list: \n");
displayList();
printf("\nEnter element to search: ");
scanf("%d", &keyToSearch);
index = searchRecursive(keyToSearch, head, 0);
if (index >= 0)
printf("%d found in the list at position %d\n", keyToSearch, index + 1);
else
printf("%d not found in the list.\n", keyToSearch);
getch();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
Page | 33
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

int data, i;
head = malloc(sizeof(struct node));
if (head == NULL)
{
printf("Unable to allocate memory. Exiting from app.");
exit(0);
}
printf("Enter data of node 1: ");
scanf("%d", &data);
head->data = data; // Link data field with data
head->next = NULL; // Link address field to NULL
temp = head;
for (i = 2; i<= n; i++)
{
newNode = malloc(sizeof(struct node));
if (newNode == NULL)
{
printf("Unable to allocate memory. Exiting from app.");
exit(0);
}
printf("Enter data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
void displayList()
{
struct node *temp;
if (head == NULL)
{
printf("List is empty.\n");
return; }
temp = head;
while (temp != NULL)
{
printf("%d, ", temp->data);

Page | 34
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

temp = temp->next;
}
printf("\n");
}
int searchRecursive(int key, struct node *curNode, int index)
{
if (curNode == NULL)
return -1;
else if (curNode->data == key)
return index;
else
return searchRecursive(key, curNode->next, index + 1);
}

OUTPUT:

Page | 35
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 17
AIM:IMPLEMENT DELETE A NODE ALGORITHM IN SINGLY LINKED
LIST.

INPUT:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
} *head;
void createList(int n);
void deleteList();
void displayList();
int main()
{
int n, choice;
clrscr();
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nPress 1 to delete entire list: ");
scanf("%d", &choice);
if(choice==1)
{
deleteList();
}
printf("\nData in the list \n");
displayList();
getch();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)

Page | 36
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void deleteList()
{
struct node *temp;
while(head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
printf("SUCCESSFULLY DELETED ALL NODES OF LINKED LIST\n");
}
void displayList()
{
struct node *temp;

Page | 37
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}

OUTPUT:

Page | 38
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 18
AIM:IMPLEMET CONSTUCTION OF BINARY SEARCH TREE.

INPUT:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
void inorder(struct node *root)
{
if(root)
{
inorder(root->left);
printf(" %d",root->data);
inorder(root->right);
}
}
int main()
{
int n ,i;
struct node *p , *q , *root;
printf("Enter the number of nodes to be insert: ");
scanf("%d",&n);
printf("\nPlease enter the numbers to be insert: ");
for(i=0;i<n;i++)
{
p = (struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->left = NULL;
p->right = NULL;
if(i == 0)
{
root = p;
}
else
{
q = root;
while(1)

Page | 39
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

{
if(p->data > q->data)
{
if(q->right == NULL)
{
q->right = p;
break;
}
else
q = q->right;
}
else
{
if(q->left == NULL)
{
q->left = p;
break;
}
else
q = q->left;
}
}
}
}
printf("\nBinary Search Tree nodes in Inorder Traversal: ");
inorder(root);
printf("\n");
return 0;
}

OUTPUT:

Page | 40
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 19
AIM:IMPLEMENT INORDER,PEREORDER AND POSTORDER
TRAVERSAL METHODSIN BINARY SEARCH TREE.

PRACTICAL:19.1

INORDER:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*left;
struct node*right;
};
struct node*newnode(int data)
{
struct node*node=(struct node*)malloc(sizeof(struct node));
node->data=data;
node->left=NULL;
node->right=NULL;
return(node);
}
void printInorder(struct node*node)
{
if(node==NULL)
return;
printInorder(node->left);
printf("%d",node->data);
printInorder(node->right);
}
int main()
{
struct node*root=newnode(20);
clrscr();
root->left=newnode(9); OUTPUT:
root->right=newnode(13);
root->left=newnode(6);
root->right=newnode(10);
printf("\n inorder traversal of binary tree is:\n");
printInorder(root);
getch();
}

Page | 41
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL:19.2
PREORDER:
INPUT:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*left;
struct node*right;
};
struct node*newnode(int data)
{
struct node*node=(struct node*)malloc(sizeof(struct node));
node->data=data;
node->left=NULL;
node->right=NULL;
return(node);
}
void printpreorder(struct node*node)
{
if(node==NULL)
return;
printf("%d",node->data);
printpreorder(node->left);
printpreorder(node->right);
}
int main()
{
struct node*root=newnode(60);
clrscr();
root->left=newnode(6);
root->right=newnode(78);
root->left=newnode(3);
root->right=newnode(10);
printf("\n preorder traversal of binary tree is:\n");
printpreorder(root);

getch();
}
OUTPUT:

Page | 42
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL:19.3
POSTORDER:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* node
= (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
void printPostorder(struct
node* node)
{
if(node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
printf("%d ", node->data);
}
int main()
{
struct node* root = newNode(49);
clrscr();
root->left = newNode(22);
root->right = newNode(55);
root->left->left = newNode(15);
root->left->right = newNode(40);
printf("\nPostorder traversal of binary tree is \n");
printPostorder(root);
getchar();
}
OUTPUT:

Page | 43
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 20

AIM:IMPLEMENT SERCHING ALGORITHM IN BINARY SEARCH


THREE.

INPUT:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data; //node will store some data
struct node *right_child; // right child
struct node *left_child; // left child
};
//function to create a node
struct node* new_node(int x) {
struct node *temp;
temp = malloc(sizeof(struct node));
temp -> data = x;
temp ->left_child = NULL;
temp ->right_child = NULL;
return temp;
}

// searching operation
struct node* search(struct node * root, int x) {
if (root == NULL || root -> data == x) //if root->data is x then the element is found
return root;
else if (x > root -> data) // x is greater, so we will search the right subtree
return search(root ->right_child, x);
else //x is smaller than the data, so we will search the left subtree
return search(root ->left_child, x);
}
// insertion
struct node* insert(struct node * root, int x) {
//searching for the place to insert
if (root == NULL)
return new_node(x);
else if (x > root -> data) // x is greater. Should be inserted to the right
root ->right_child = insert(root ->right_child, x);
else // x is smaller and should be inserted to left
root ->left_child = insert(root ->left_child, x);
return root;
}

Page | 44
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

//function to find the minimum value in a node


struct node* find_minimum(struct node * root) {
if (root == NULL)
return NULL;
else if (root ->left_child != NULL) // node with minimum value will have no left child
return find_minimum(root ->left_child); // left most element will be minimum
return root;
}
// deletion
struct node* delete(struct node * root, int x) {
//searching for the item to be deleted
if (root == NULL)
return NULL;
if (x > root -> data)
root ->right_child = delete(root ->right_child, x);
else if (x < root -> data)
root ->left_child = delete(root ->left_child, x);
else {
//No Child node
if (root ->left_child == NULL && root ->right_child == NULL) {
free(root);
return NULL;
}
//One Child node
else if (root ->left_child == NULL || root ->right_child == NULL) {
struct node *temp;
if (root ->left_child == NULL)
temp = root ->right_child;
else
temp = root ->left_child;
free(root);
return temp;
}

//Two Children
else {
struct node *temp = find_minimum(root ->right_child);
root -> data = temp -> data;
root ->right_child = delete(root ->right_child, temp -> data);
}
}
return root;
}

Page | 45
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) // checking if the root is not null
{
inorder(root ->left_child); // traversing left child
printf(" %d ", root -> data); // printing data at root
inorder(root ->right_child); // traversing right child
}
}
int main() {
struct node *root;
root = new_node(20);
insert(root, 5);
insert(root, 1);
insert(root, 15);
insert(root, 9);
insert(root, 7);
insert(root, 12);
insert(root, 30);
insert(root, 25);
insert(root, 40);
insert(root, 45);
insert(root, 42);
inorder(root);
printf("\n");
root = delete(root, 1);
root = delete(root, 40);
root = delete(root, 45);
root = delete(root, 9);
inorder(root);
printf("\n");
return 0;
}

OUTPUT:

Page | 46
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL 21
AIM:IMPLEMENT BUBBLE SORT ALGORITHM.

INPUT:
#include<conio.h>
#include<stdio.h>
int main()
{
int array[100],n,i,j,swap;
printf("Enter number of elements");
scanf("%d",&n);
printf("Enter %d Numbers:n",n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(array[j]>array[j+1])
{
swap=array[j];
array[j]=array[j+1];
array[j+1]=swap;
}
}
}
printf("sorted array:n");
for(i=0;i<n;i++)printf("%dn",array[i]);
return 0;
}

OUTPUT:

Page | 47
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL:22

AIM:IMPLEMENT SELECTION SORT ALGORITHM.

INPUT:

#include<stdio.h>
#include<conio.h>
void swap(int*i,int*j)
{
int temp=*i;
*i=*j;
*j=temp;
}
void selection(int arr[],int n)
{
int i,j,min_idx;
for(i=0;i<n;i++)
{
min_idx=i;
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[min_idx])
swap(&arr[j],&arr[min_idx]);
if(arr[i]!=arr[min_idx])
swap(&arr[min_idx],&arr[i]);
}
}
printf("sorted array: \n");
for(i=0;i<n;i++)
printf("%d \n",arr[i]);
}
void main()
{
int array[10]={12,2,45,22,33};
clrscr();
selection(array,5);
getch();
}

OUTPUT:

Page | 48
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTIUCAL 23

AIM:IMPLEMENT QUICK SORT ALGORITHM.

INPUT:
#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
int partition(int array[],int low,int high)
{
int pivot=array[high];
int i=(low-1);
for(int j=low.j<high;j++)
{
if(array[j]<=pivot)
{
i++;
swap(& array[i],&array[j]);
}
}
swap(&array[i+1],&array[high]);
return(i+1);
}
void quicksort(int array[],int low,int high)
{
if (low<high)
{
int pi=partition(array,low,high);
quicksort(array,pi+1,high);
}
}
void printarray(int array[],int size)
{
for(int i=0;i<size;++i)
{
printf("%d",array[i]);
}
printf("\n");
}
int main()
clrscr();
{

Page | 49
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

int data[]={8,7,2,1,0,9,6};
int n=sizeof(data)/sizeof)data[0]);
printf("Unsorted array\n");
printarray(data,n);
quicksort(data,0,n-1);
pintf("sorted array in ascending order:\n");
printarray(data,n);
grtch();
return 0;
}

OUTPUT:

Page | 50
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL:24
AIM:IMPLEMENT INSERTION SORT ALGORITHM.

INPUT:
#include <math.h>
#include <stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i< n; i++) {
key = arr[i];
j=i–1
while (j >= 0 &&arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i< n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = { 12, 78, 46, 88, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}

OUTPUT:

Page | 51
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL:25
AIM:IMPLEMENT SHELL SORT ALGORITHM.

INPUT:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[30];
int i,j,k,tmp,num;
clrscr();
printf("enter total no. of elements: \n");
scanf("%d",&num);
for(k=0;k<num;k++)
{
printf("enter number: \n",k+1);
scanf("%d",&arr[k]);
}
for(i=num/2;i>0;i=i/2)
{
for(j=i; j<num;j++)
{
for(k=j-1;k>=0;k=k-1)
{
if(arr[k+1]>=arr[k])
break;
else
{
tmp=arr[k];
arr[k]=arr[k+i];
arr[k+i]=tmp;
}
}
}
}
printf("\n shell sorting:\n");
for(k=0;k<num;k++)
{
printf("%d\t",arr[k]);
}
getch();
}
OUTPUT:

Page | 52
DATA STRUCTURE EN_NO:226010307097
() A.Y.D.T.I

PRACTICAL: 26

AIM:WRITE A PROGRAM TO IMPLEMENT MERGE SORT


ALGORITM.

INPUT:
#include <stdio.h>
#include<conio.h>
#define max 10
int a[11] = { 1, 144, 13, 26, 57, 21, 23, 35, 92, 44, 0 };
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i<= high; i++)
a[i] = b[i];
}
void sort(int low, int high) {
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return 0;
}
}

OUTPUT:

Page | 53

You might also like