0% found this document useful (0 votes)
104 views90 pages

Data Structures Lab Record

The document contains multiple C programming exercises from CMR College of Engineering & Technology, focusing on various data structures and operations such as 1D arrays, linked lists, and dynamic memory allocation. It includes source code for implementing insertion, deletion, searching, and traversal in arrays, as well as linked list operations like insertion at different positions and reversal of nodes. Each section is structured with an aim, source code, and output examples.

Uploaded by

rnkumar0517
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views90 pages

Data Structures Lab Record

The document contains multiple C programming exercises from CMR College of Engineering & Technology, focusing on various data structures and operations such as 1D arrays, linked lists, and dynamic memory allocation. It includes source code for implementing insertion, deletion, searching, and traversal in arrays, as well as linked list operations like insertion at different positions and reversal of nodes. Each section is structured with an aim, source code, and output examples.

Uploaded by

rnkumar0517
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

CMR COLLEGE OF ENGINEERING & TECHNOLOGY

(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program to implement the following operations on to a 1D Array:


a. INSERT b. DELETE c. SEARCH d. TRAVERSE

SOURCE CODE
#include <stdio.h>
int main() {
int array[20], position, i, n, value, data;
printf("Enter the number of elements in an array: ");
scanf("%d", &n);
printf("Enter %d elements: \n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter the position where to insert the element: ");
scanf("%d", &position);
if (position < 1 || position > n + 1) {
printf("Invalid position!\n");
return 1;
}
printf("Enter the value to insert: ");
scanf("%d", &value);
for (i = n - 1; i >= position - 1; i - )
array[i + 1] = array[i];
array[position - 1] = value;
n++;
printf("Updated array elements are:\n");
for (i = 0; i < n; i++)
printf("%d\n", array[i]);
printf("Enter the element to search: ");
scanf("%d", &data);
for (i = 0; i < n; i++) {
if (array[i] == data) {
printf("%d is present at location %d.\n", data, i + 1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", data);
printf("Enter the position where you wish to the element: ");
scanf("%d", &position);
if (position < 1 || position > n) {
printf("Deletion is not possible\n");

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

} else {
for (i = position - 1; i < n - 1; i++)
array[i] = array[i + 1];
n-;
printf("Resultant array is:\n");
for (i = 0; i < n; i++)
printf("%d\n", array[i]);
}
return 0;
}

OUTPUT

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program to implement Self-referential Structure.


SOURCE CODE
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
struct Node* head = NULL;
head = createNode(10);
head->next = createNode(20);
head->next->next = createNode(30);
printf("Linked list: ");
printList(head);
return 0;
}

OUTPUT

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program to Perform Dynamic Memory Allocation


SOURCE CODE
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
int *array;
printf("Enter the number of elements:\n");
scanf("%d", &n);
array = (int *)malloc(n * sizeof(int));
if (array == NULL) {
printf("Memory allocation failed");
return -1;
}
for (i = 0; i < n; i++) {
printf("Enter element %d: ", i);
scanf("%d", &array[i]);
}
printf("Array elements: ");
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
}

OUTPUT

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program to implement Single linked list i) Insertion ii)Deletion iii)Display

SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_random();
void _begin();
void _end();
void _random();
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
int main() {
int opt;
while (1) {
printf("\n\t MENU");
printf("\n 1.create");
printf("\n 2.display");
printf("\n 3.Insert at begin");
printf("\n 4.Insert at end");
printf("\n 5.Insert at random");
printf("\n 6.Delete at begin");
printf("\n 7.Delete at end");
printf("\n 8.Delete at random");
printf("\n 9.exit");
printf("\n choose any option: ");
scanf("%d", &opt);
switch (opt) {
case 1: create(); break;
case 2: display(); break;
case 3: insert_begin(); break;
case 4: insert_end(); break;
case 5: insert_random(); break;
case 6: _begin(); break;
case 7: _end(); break;
case 8: _random(); break;
case 9: printf("terminated by you"); exit(0);
}} retrun 0; }
Dept of CSE Page No.
CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void create() {
struct node *ptr, *temp;
int i, n, val;
printf("Enter the number of elements: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
ptr = (struct node *)malloc(sizeof(struct node));
printf("Enter the data of the node: ");
scanf("%d", &val);
ptr->data = val;
ptr->next = NULL;
if (head == NULL) {
head = ptr;
temp = head;
} else {
temp->next = ptr;
temp = temp->next;
}}}
void display() {
struct node *temp;
printf("The Elements are: ");
temp = head;
while (temp != NULL) {
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
void insert_begin() {
int num;
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d", &num);
ptr->data = num;
ptr->next = NULL;
if (head == NULL) {
head = ptr;
} else {
ptr->next = head;
head = ptr;
}}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void insert_end() {
int num;
struct node *ptr, *temp;
ptr = (struct node *)malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d", &num);
ptr->data = num;
ptr->next = NULL;
if (head == NULL) {
head = ptr;
} else {
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = ptr;
}}
void insert_random() {
int pos, i = 1, num;
struct node *ptr, *temp;
if (head == NULL) {
printf("List is empty\n");
return;
}
ptr = (struct node *)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d", &num);
printf("Enter position to insert: ");
scanf("%d", &pos);
ptr->data = num;
ptr->next = NULL;
temp = head;
while (i < pos - 1 && temp != NULL) {
temp = temp->next;
i++;
}
if (temp == NULL) {
printf("Position out of range\n");
} else {
ptr->next = temp->next;
temp->next = ptr;
}
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void _begin() {
struct node *temp;
if (head == NULL) {
printf("List is empty\n");
} else {
temp = head;
head = head->next;
printf("Deleted element is %d\n", temp->data);
free(temp);
}
}
void _end() {
struct node *temp, *prev;
if (head == NULL) {
printf("List is empty\n");
} else if (head->next == NULL) {
temp = head;
head = NULL;
printf("Deleted element is %d\n", temp->data);
free(temp);
} else {
temp = head;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
prev->next = NULL;
printf("Deleted element is %d\n", temp->data);
free(temp);
}
}

void _random() {
struct node *temp, *temp1;
int pos, i = 1;
if (head == NULL) {
printf("List is empty\n");
return;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("Enter position to : ");


scanf("%d", &pos);
if (pos == 1) {
temp = head;
head = head->next;
printf("Deleted element is %d\n", temp->data);
free(temp);
} else {
temp = head;
while (i < pos - 1 && temp != NULL) {
temp = temp->next;
i++;
}
if (temp == NULL || temp->next == NULL) {
printf("Position out of range\n");
} else {
temp1 = temp->next;
temp->next = temp1->next;
printf("Deleted element is %d\n", temp1->data);
free(temp1);
}}}
OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a function to reverse the nodes of a Single linked lisT


SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void reverse();
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
int main()
{
int opt;
while(1)
{
printf("\n \t MENU");
printf("\n 1.create");
printf("\n 2.display");
printf("\n 3.Reverse the node");
printf("\n choose any option");
scanf("%d",&opt);
switch(opt)
{
case 1:create();break;
case 2:display();break;
case 3:reverse();break;
case 4:
printf("terminated by you"); exit(0);
break;
}
}
return 0;
}
void create()
{
struct node*ptr,*temp;
int i,n,val;
printf("Enter the number of elements");
scanf("%d",&n);
for(i=0;i<n;i++)

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

{
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter the data of the node\n");
scanf("%d",&val);
ptr->data=val;
ptr->next=NULL;
if(head==NULL)
{
head=ptr;
temp=head;
}
else
{
temp->next=ptr;
temp=temp->next;
}}}
void display()
{
struct node*temp;
printf("The Elements are\n");
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL");
}
void reverse() {
struct node* temp = head;
struct node* previous = NULL;
struct node* next = NULL;

while (temp != NULL) {


next = temp->next;
temp->next = previous;
previous = temp;
temp = next;
}
head = previous;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a program that uses functions to perform the following operations on doubly linked list:
i) Creation ii) Insertion iii) Deletion iv) Traversal
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_random();
void _begin();
void _end();
void _random();
void search();
struct node {
int data;
struct node *next;
struct node *previous;
};
struct node *head = NULL;
int main() {
int opt;
while(1) {
printf("\n \t MENU");
printf("\n 1.create");
printf("\n 2.display");
printf("\n 3.Insert at begin");
printf("\n 4.Insert at end");
printf("\n 5.Insert at random");
printf("\n 6.Delete at begin");
printf("\n 7.Delete at end");
printf("\n 8.Delete at random");
printf("\n 9.search");
printf("\n 10.exit");
printf("\n choose any option: ");
scanf("%d", &opt);
switch(opt) {
case 1: create(); break;
case 2: display(); break;
case 3: insert_begin(); break;
case 4: insert_end(); break;
case 5: insert_random(); break;
case 6: _begin(); break;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

case 7: _end(); break;


case 8: _random(); break;
case 9: search();break;
case 10: printf("terminated by you\n"); exit(0); break;
default: printf("Invalid option\n"); break;
}}
return 0;
}
void create() {
struct node *ptr, *temp;
int i, n, val;
printf("Enter the number of elements: ");
scanf("%d", &n);
for(i = 0; i < n; i++) {
ptr = (struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d", &val);
ptr->data = val;
ptr->previous = NULL;
ptr->next = NULL;
if(head == NULL) {
head = ptr;
temp = head;
} else {
temp->next = ptr;
ptr->previous = temp;
temp = ptr;
}}}
void display() {
if (head == NULL) {
printf("List is empty\n");
} else {
struct node *temp = head;
printf("The linked list is:\n");
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}}
void insert_begin() {
struct node *ptr;
int num;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

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


printf("Enter data: ");
scanf("%d", &num);
ptr->data = num;
ptr->previous = NULL;
if(head == NULL) {
ptr->next = NULL;
head = ptr;
} else {
ptr->next = head;
head->previous = ptr;
head = ptr;
}}
void insert_end() {
struct node *ptr, *temp;
int num;
ptr = (struct node*)malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d", &num);
ptr->data = num;
ptr->next = NULL;
if(head == NULL) {
ptr->previous = NULL;
head = ptr;
} else {
temp = head;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = ptr;
ptr->previous = temp;
}}
void insert_random() {
struct node *ptr, *temp, *temp1, *temp2;
int pos, i = 1, num;
ptr = (struct node*)malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d", &num);
ptr->data = num;
printf("Enter position to insert: ");
scanf("%d", &pos);

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

if(head == NULL) {
ptr->previous = NULL;
ptr->next = NULL;
head = ptr;
} else {
temp = head;
while(i < pos) {
temp1 = temp;
temp = temp->next;
i++;
}
temp2 = temp->next;
temp->next = ptr;
ptr->previous = temp;
ptr->next = temp2;
temp2->previous = ptr;
}}
void _begin() {
if(head == NULL) {
printf("List is Empty!!! Deletion is not possible\n");
} else {
struct node *temp = head;
if(temp->next == NULL) {
head = NULL;
} else {
head = temp->next;
head->previous = NULL;
}
printf("Deleted element is %d\n", temp->data);
free(temp);
}}
void _end() {
if(head == NULL) {
printf("List is Empty!!! Deletion is not possible\n");
} else {
struct node *temp = head, *prev;
while(temp->next != NULL) {
prev = temp;
temp = temp->next;
}
if(temp == head) {
head = NULL;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

} else {
prev->next = NULL;
}
printf("Deleted element is %d\n", temp->data);
free(temp);
}}
void _random() {
int pos, i = 1;
if(head == NULL) {
printf("List is empty\n");
} else {
struct node *temp, *temp1;
temp = head;
printf("Enter position to : ");
scanf("%d", &pos);
if(pos == 1) {
head = temp->next;
head->previous = NULL;
printf("Deleted element is %d\n", temp->data);
free(temp);
} else {
while(i < pos - 1 ) {
temp = temp->next;
i++;
}
temp1 = temp->next;
temp->next = temp1->next;
if(temp1->next != NULL) {
temp1->next->previous = temp;
}
printf("Deleted element is %d\n", temp1->data);
free(temp1);
}}}
void search()
{
struct node*temp=head;
int key,count=1;
printf("\n Enter the element to be searched in the list: ");
scanf("%d",&key);
while(temp!=NULL)
{
if(temp->data==key)

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

{
printf("Element %d found at position %d",key,count);
return 0 ;
}
else
{
count+=1;
temp=temp->next;
}
}
printf(" Element %d is not found in the list\n ",key);
return 0;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a program that uses functions to perform the following operations on circular linked list:
i) Creation ii) Insertion iii) Deletion iv) Traversal
SOURCE CODE:

#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_random();
void _begin();
void _end();
void _random();
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
int main() {
int opt;
while(1) {
printf("\n \t MENU");
printf("\n 1.create");
printf("\n 2.display");
printf("\n 3.Insert at begin");
printf("\n 4.Insert at end");
printf("\n 5.Insert at random");
printf("\n 6.Delete at begin");
printf("\n 7.Delete at end");
printf("\n 8.Delete at random");
printf("\n 10.exit");
printf("\n choose any option: ");
scanf("%d", &opt);
switch(opt) {
case 1: create(); break;
case 2: display(); break;
case 3: insert_begin(); break;
case 4: insert_end(); break;
case 5: insert_random(); break;
case 6: _begin(); break;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

case 7: _end(); break;


case 8: _random(); break;
case 9: printf("terminated by you\n"); exit(0); break;
default: printf("Invalid option\n"); break;
}}
return 0;
}
void create() {
struct node *ptr, *temp;
int i, n, val;
printf("Enter the number of elements\n");
scanf("%d", &n);
for(i = 0; i < n; i++) {
ptr = (struct node*)malloc(sizeof(struct node));
printf("enter data: ");
scanf("%d", &val);
ptr->data = val;
if(head == NULL) {
head = ptr;
ptr->next = head;
temp = head;
} else {
temp->next = ptr;
temp = ptr;
temp->next = head;
}}}
void display() {
if(head == NULL) {
printf("List is Empty!!!\n");
} else {
struct node *temp;
temp = head;
printf("The linked list is\n");
do {
printf("%d->", temp->data);
temp = temp->next;
} while(temp != head);
printf("%d->%d", temp->data, head->data);
}}
void insert_begin() {
int num;
struct node *ptr, *temp;
ptr = (struct node*)malloc(sizeof(struct node));

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("Enter data: ");


scanf("%d", &num);
ptr->data = num;
if(head == NULL) {
head = ptr;
ptr->next = head;
} else {
temp = head;
if(temp->next == head) {
temp->next = ptr;
ptr->next = temp;
} else {
while(temp->next != head) {
temp = temp->next;
}
ptr->next = head;
head = ptr;
temp->next = head;
}}}
void insert_end() {
int num;
struct node *ptr, *temp;
ptr = (struct node*)malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d", &num);
ptr->data = num;
if(head == NULL) {
head = ptr;
ptr->next = head;
} else {
temp = head;
while(temp->next != head) {
temp = temp->next;
}
temp->next = ptr;
ptr->next = head;
}}
void insert_random() {
int pos, i = 1, num;
struct node *ptr, *temp;
ptr = (struct node*)malloc(sizeof(struct node));

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("Enter the data: ");


scanf("%d", &num);
ptr->data = num;
printf("Enter position to insert: ");
scanf("%d", &pos);
if(head == NULL) {
head = ptr;
ptr->next = head;
} else {
temp = head;
while(i < pos - 1) {
temp = temp->next;
i++;
}
ptr->next = temp->next;
temp->next = ptr;
}}
void _begin() {
struct node *temp1, *temp;
if(head == NULL) {
printf("list is Empty\n");
} else {
temp = head;
temp1 = head;
if(temp->next == head) {
head = NULL;
printf("Deleted element is %d\n", temp->data);
free(temp);

} else {
while(temp->next != head) {
temp = temp->next;
}
head = temp1->next;
temp->next = head;
printf("Deleted element is %d\n", temp1->data);
free(temp1);
}}}
void _end() {
struct node *temp, *temp1;
if(head == NULL) {
printf("Empty list, cant be d");

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

} else {
temp = head;
if(temp->next == head) {
head = NULL;
printf("Deleted element is %d", temp->data);
free(temp);
} else {
while(temp->next != head) {
temp1 = temp;
temp = temp->next;
}
temp1->next = head;
printf("Deleted element is %d", temp->data);
free(temp);
}}}
void _random() {
struct node *temp, *temp1;
int pos, i = 1;
if (head == NULL) {
printf("Empty list, can't be d\n");
} else {
temp = head;
printf("Enter position to : ");
scanf("%d", &pos);
if (pos == 1) {
head = temp->next;
printf("Deleted element is %d\n", temp->data);
free(temp);
} else {
while (i < pos - 1 && temp->next != NULL) {
temp = temp->next;
i++;
}
if (temp->next == NULL || pos < 1) {
printf("Position out of range\n");
} else {
temp1 = temp->next;
temp->next = temp1->next;
printf("Deleted element is %d\n", temp1->data);
free(temp1);
}}}}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a program that implement Stack (its operations) using Arrays
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#define SIZE 5
void push(int);
void pop();
void display();
int stack[SIZE],top=-1;
void main()
{
int value,choice;
while(1)
{
printf("\n\n****MENU****\n");
printf("1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the value to insert");
scanf("%d",&value);
push(value);
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("\nWrong selection!!!Try again!!!");
}}}
void push(int value)
{
if(top==SIZE-1)
printf("\n Stack is Full!!!Insertion is not possible!!!");
else
{
top++;
stack[top]=value;
printf("\n Insertion sucess!!!");
}}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void pop()
{
if(top==-1)
printf("\n Stack is Empty!!Deletion is not possible!!");
else
{
printf("\nDeleted:%d",stack[top]);
top - ;
}}
void display()
{
if(top==-1)
printf("\nStack js Empty!!");
else{
int i;
printf("\n Stack elements are :\n");
for(i=top;i>=0;i - )
printf("%d\n",stack[i]);
}}

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a program that implement Circular Queue (its operations) using Arrays
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#define N 5
void enqueue();
void dequeue();
void display();
int front=-1;rear=-1;
int queue[N];
void main ()
{
int choice;
while(choice!=4)
{
printf("\nMain menu\n");
printf("\n1.Insert an element\n2.Delete an element\n.3Display the
queue\n4.exit\n");
printf("Enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\n Enter the valid choice??\n");
}}}
void enqueue()
{
int item;
printf("\n Enter the element\n");
scanf("%d",&item);
if((rear+1)%N==front)

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

{
printf("\n OVERFLOW\n");
return;
}
if(front==-1 && rear==-1)
{
front=0;
rear=0;
}else{
rear=(rear+1)%N;
}
queue[rear]=item;
printf("\n valueu inserted");
}
void dequeue()
{
int item;
if(front==-1 && rear==-1)
{
printf("\n UNDERFLOW\n");
return;
}
else
{
item=queue[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else{
front=(front+1)%N;
}
printf("\n value d");
}}
void display()
{
int i =front;
if(front==-1&&rear==-1)
{
printf("\nQueue is empty");
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

else
{
printf("Elements ina queue are:");
while(i<=rear)
{
printf("%d",queue[i]);
i=(i+1)%N;
}}}
OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write C programs to implement Stack ADT using Linked List


SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
} *top = NULL;
void push(int);
void pop();
void Display();
void main() {
int choice, value;
printf("\n:: Stack using Linked List ::\n");
while(1) {
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
Display();
break;
case 4:
exit(0);
default:
printf("\nWrong selection!!! Please try again!!!\n");
}}}
void push(int value) {
struct node *ptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr->data = value;
ptr->next = top;
top = ptr;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void pop() {
if(top == NULL)
printf("\nStack is Empty!!!\n");
else {
struct node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}}
void Display() {
if
(top==NULL)
printf("\n Stack is Empty!!!\n");
else
{
struct node*temp=top;
while(temp->next!=NULL)
{
printf("%d - >",temp->data);
temp=temp->next;
}
printf("%d - >NULL",temp->data);
}}

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM:. Write C programs to implement Circular Queue ADT using Linked List

SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *front = NULL, *rear = NULL;

void enqueue(int);
void dequeue();
void display();
int main()
{
int choice, value;
printf("\n:: Queue Implementation using Linked List ::\n");
while (1)
{
printf("\n****** MENU ******\n");
printf("1. Enqueue\n2. dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(value); // Corrected function call
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong selection!!! Please try again!!!\n");
}}
return 0;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void enqueue(int value)


{
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nMemory allocation failed. Queue is full.\n");
return;
}
ptr->data = value;
ptr->next = NULL;
if (front == NULL)
front = rear = ptr;
else
{
rear->next = ptr;
rear = ptr;
}}
void dequeue()
{
if (front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct node *temp = front;
front = front->next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}}
void display()
{
if (front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct node *temp = front;
while (temp != NULL)
{
printf("%d - ->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program to Convert the given Infix Expression to Postfix Expression
SOURCE CODE:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
char stack[50];
int top = -1;
int priority(char);
void push(char);
char pop();
int main() {
char infix[50], postfix[50], ch;
int i, j, len;
printf("Enter infix Expression : \n");
gets(infix);
len = strlen(infix);
for (i = 0, j = 0; i < len; i++) {
if (isalpha(infix[i])) {
postfix[j++] = infix[i];
} else {
if (infix[i] == '(') {
push(infix[i]);
} else if (infix[i] == ')') {
while ((ch = pop()) != '(') {
postfix[j++] = ch;
}
} else {
while (top != -1 && priority(infix[i]) <=
priority(stack[top])) {
postfix[j++] = pop();
}
push(infix[i]);
}}}
while (top != -1) {
postfix[j++] = pop();
}
postfix[j] = '
printf("\nEquivalent postfix expression is: %s\n", postfix);
return 0;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

int priority(char c) {
switch (c) {
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
case '%': return 2;
case '^': return 3;
}
return 0;
}
void push(char c) {
stack[++top] = c;
}
char pop() {
return (stack[top - ]);
}

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program to implement different hash methods


SOURCE CODE:

#include <stdio.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
{
int key,i=0,hkey;
printf("\nenter a value to insert into hash table\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
else
{
if(h[hkey] == NULL)
{
h[hkey]=key;
printf("\ninsertion successfully\n");
i++;
}
else
printf("\n Collision occurs, we cant insert....");
}
}
void search()
{
int key,index,i,flag=0,hkey;
printf("\nenter search element\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;

if(h[hkey]==key)
{
printf("value is found at index %d",hkey);
}
else
printf("\n value is not found\n");
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void display()
{
int i;
printf("\nelements in the hash table are \t|\n");
for(i=0;i< TABLE_SIZE; i++)
printf("\n at index %d value = %d ",i,h[i]);
printf ("\n");
}
main()
{
int opt,i;
while(1)
{
printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");
scanf("%d",&opt);
switch(opt)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:exit(0);
} } }

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program to implement the following collision resolving


i) Quadratic probing

SOURCE CODE:
#include <stdio.h>
#define TABLE_SIZE 5
int h[TABLE_SIZE]={NULL};
void insert()
{
int key,index,i,hkey;
printf("\nenter a value to insert into hash table\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
{
index=(hkey+i*i)%TABLE_SIZE;
if(h[index] == NULL)
{
h[index]=key;
printf("\ninsertion successfully\n");
break;
}
}
if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
}
void search()
{
int key,index,i,flag=0,hkey;
printf("\nenter search element\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE; i++)
{
index=(hkey+i*i)%TABLE_SIZE;
if(h[index]==key)
{
printf("value is found at index %d",index);
break;
}
}
if(i == TABLE_SIZE)
printf("\n value is not found\n");
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void display()
{
int i;
printf("\nelements in the hash table are \t|\n");
for(i=0;i< TABLE_SIZE; i++)
printf("\n at index %d value = %d ",i,h[i]);
printf ("\n"); }
main()
{
int opt,i;
while(1)
{
printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");
scanf("%d",&opt);
switch(opt)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:exit(0);
}
}
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

ii) Linear Probing


SOURCE CODE:
#include <stdio.h>
#define TABLE_SIZE 3
int h[TABLE_SIZE] = {NULL};
void insert();
void search();
void display();
int main() {
int opt;
while (1) {
printf("\n\n1. Insert\n2. Display\n3. Search\nEnter option: ");
scanf("%d", &opt);
switch (opt) {
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
default:
printf("Invalid option. Please try again.\n");
}}
return 0;
}
void insert()
{
int key, index, i,flag=0,hkey;
printf("\nEnter a value to insert into the hash table: ");
scanf("%d", &key);
hkey = key % TABLE_SIZE;
for (i = 0; i < TABLE_SIZE; i++)
{
index = (hkey + i) % TABLE_SIZE;
if (h[index] == NULL)
{
h[index] = key;
printf("Insertion successful.\n");
break;
}
}
Dept of CSE Page No.
CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

if (i==TABLE_SIZE)
printf("Cannot insert the element\n");
}
void search() {
int key, index, i,flag=0, hkey;
printf("\nEnter element to search: ");
scanf("%d", &key);
hkey = key % TABLE_SIZE;
for (i = 0; i < TABLE_SIZE; i++)
{
index = (hkey + i) % TABLE_SIZE;
if (h[index] == key)
{
printf("Value is found at index %d.\n", index);
break;
}
}
if(i==TABLE_SIZE)
printf("Value is not found");
}
void display()
{
int i;
printf("\nElements in the hash table are:\n");
for (i = 0; i < TABLE_SIZE; i++)
printf("Index %d value= %d\n", i, h[i]);

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program to Evaluate the given Postfix Expression.


SOURCE CODE:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int stack[50];
int top = -1;
void push(int);
int pop();
int main() {
char postfix[50], ch;
int i = 0, op1, op2;
printf("Enter the Postfix Expression:\n");
gets(postfix);
while ((ch = postfix[i++]) != '') {
if (isdigit(ch))
push(ch-48);
else
{
op2 = pop();
op1 = pop();
switch (ch)
{
case '+':
push(op1 + op2);
break;
case '-':
push(op1 - op2);
break;
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '%':
push(op1 % op2);
break;
default:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("Input invalid\n");
}}}
printf("Result: %d\n", stack[top]);
getch();
}
int pop()
{
return stack[top - ];
}
void push(int element)
{
stack[++top] = element;
}

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Implement Dictionary ADT using list data structure


SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data,key;
struct node *next;
};
struct node *head=NULL,*temp,*temp1,*temp2,*newnode;
void insert();
void display();
void 1();
void search();
int main()
{
int ch,op;
do{ printf("\n1.insert\n2. Delete \n3.display \n4. Search");
printf("\n Enter choice:");scanf("%d",&ch);
switch(ch)
{
case 1:•insert();break;
case 2:•1();break;
case 3:•display();break;
case 4:•search(); break;
default:printf("\n Invalid entry");
}
printf("\n Continue:?");
scanf("%d",&op);
}while(op);
return 0;
}
void insert()
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the key ");
scanf("%d",&newnode->key);
printf("\n Enter the value of key");
scanf("%d",&newnode->data);
if(head==NULL)
{
head=newnode;
temp=temp1=temp2=head;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

}
else if(temp->key<newnode->key)
{
temp->next=newnode;
temp=newnode;
newnode->next=NULL;
}
else
{
if(temp1->key<newnode->key)
{
while(temp1->key<newnode->key)
{
temp2=temp1;
temp1=temp1->next;
}
temp2->next=newnode;
newnode->next=temp1;
}

else
{
head=newnode;
newnode->next=temp1;
temp1=temp2=newnode;
}}}
void display(){
struct node *p;
p=head;
while(p!=NULL)
{
printf("%d\t",p->key);
p=p->next;
}}
void 1()
{
struct node *prev,*curr=head;
int k;
printf("Enter Key element to be d");
scanf("%d",&k);
while(curr!=NULL)

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

{
if(curr->key==k)
break;
prev=curr;
curr=curr->next;
}
if(curr==NULL)
printf("\n Key doesnot exit");
else
{
if(curr==head)
head=curr->next;
else
prev->next=curr->next;
}
free(curr);
}
void search()
{
struct node *curr=head;
int key1,flag=0;
printf("Enter Key element,that we want to search");
scanf("%d",&key1);
if(curr==NULL)
printf("Dictionary is Empty");
while(curr!=NULL)
{
if(curr->key==key1)
{
flag=1;
break;
}
curr=curr->next;
}
if(flag==1)
printf("Key is found");
else
printf("Key is not found");
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM:Write a C program to implement Binary search tree i) Insertion ii) deletion iii) Traversals
SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
// Create a node
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
printf("%d -> ", root->key);
// Traverse right
inorder(root->right);
}
}
// Insert a node
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);
// Traverse to the right place and insert the node
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
// Find the inorder successor
struct node *minValueNode(struct node *node) {
struct node *current = node;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

// Find the leftmost leaf


while (current && current->left != NULL)
current = current->left;

return current;
}

// Deleting a node
struct node *Node(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;

// Find the node to be d


if (key < root->key)
root->left = Node(root->left, key);
else if (key > root->key)
root->right = Node(root->right, key);

else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}

// If the node has two children


struct node *temp = minValueNode(root->right);

// Place the inorder successor in position of the node to be d


root->key = temp->key;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

// Delete the inorder successor


root->right = Node(root->right, temp->key);
}
return root;
}

// Driver code
int main() {
struct node *root = NULL;
int arr[50],i,n,ele;
printf("\nEnter how many elements u want?:");
scanf("%d",&n);
printf("\nEnter %d elements into an array:",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
root = insert(root,arr[i]);
}

printf("Inorder traversal: ");


inorder(root);

printf("\nEnter element to :");


scanf("%d",&ele);
root = Node(root, ele);
printf("\n After deleting, Inorder traversal: ");
inorder(root);
}
OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM:Write a C Program to implement binary search tree using recursion


i) Pre order ii)Post order iii)In-Order
SOURCE CODE:
#include <stdio.h>
struct tnode
{
int data;
struct tnode *right;
struct tnode *left;
};

struct tnode *CreateBST(struct tnode *, int);


void Inorder(struct tnode *);
void Preorder(struct tnode *);
void Postorder(struct tnode *);

int main()
{
struct tnode *root = NULL;
int arr[50],i,n,ele;
int choice, item;
do
{
printf("\n\nBinary Search Tree Operations\n");
printf("\n1. Creation of BST");
printf("\n2. Traverse in Inorder");
printf("\n3. Traverse in Preorder");
printf("\n4. Traverse in Postorder");
printf("\n5. Exit\n");
printf("\nEnter Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
root = NULL;
struct node *root = NULL;

printf("\nEnter how many elements u want?:");


scanf("%d",&n);

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("\nEnter %d elements into an array:",n);


for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
root = CreateBST(root,arr[i]);
}

printf("\nBST with %d nodes is ready to Use!!\n", n);


break;
case 2:
printf("\nBST Traversal in INORDER \n");
Inorder(root);
break;
case 3:
printf("\nBST Traversal in PREORDER \n");
Preorder(root);
break;
case 4:
printf("\nBST Traversal in POSTORDER \n");
Postorder(root);
break;
case 5:
printf("\n\n Terminating \n\n");
break;
default:
printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
} while(choice != 5);
return 0;
}

struct tnode *CreateBST(struct tnode *root, int item)


{
if(root == NULL)
{
root = (struct tnode *)malloc(sizeof(struct tnode));
root->left = root->right = NULL;
root->data = item;
return root;
}
else

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

{
if(item < root->data )
root->left = CreateBST(root->left,item);
else if(item > root->data )
root->right = CreateBST(root->right,item);
else
printf(" Duplicate Element !! Not Allowed !!!");

return(root);
}
}

void Inorder(struct tnode *root)


{
if( root != NULL)
{
Inorder(root->left);
printf(" %d ",root->data);
Inorder(root->right);
}
}

void Preorder(struct tnode *root)


{
if( root != NULL)
{
printf(" %d ",root->data);
Preorder(root->left);
Preorder(root->right);
}
}

void Postorder(struct tnode *root)


{
if( root != NULL)
{
Postorder(root->left);
Postorder(root->right);
printf(" %d ",root->data);
}
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM:Write a C Program to Check if a Given Binary Tree is an AVL Tree or Not


SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
#define bool int
struct node {
int data;
struct node* left;
struct node* right;
};
int height(struct node* node);
bool isBalanced(struct node* root)
{
int lh;
int rh;
if (root == NULL)
return 1;
lh = height(root->left);
rh = height(root->right);
if (abs(lh - rh) <= 1 && isBalanced(root->left)
&& isBalanced(root->right))
return 1;
return 0;
}
int max(int a, int b) { return (a >= b) ? a : b; }
int height(struct node* node)
{
if (node == NULL)
return 0;
return 1 + max(height(node->left), height(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);
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

int main()
{
struct node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
if (isBalanced(root))
printf("Tree is balanced and it is AVL Tree");
else
printf("Tree is not balanced also not an AVL Tree");
return 0;
}

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program to find height of a Binary tree


SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
int maxDepth(struct node* node)
{
if (node == NULL)
return 0;
else {
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
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);
}
int main()
{
struct node* root = newNode(1);

root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

root->left->right = newNode(5);
root->left->left->right = newNode(6);

printf("Height of tree is %d", maxDepth(root));


getchar();
return 0;
}

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program to count the number of leaf nodes in a tree.


SOURCE CODE: #include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
int getLeafCount(struct node* node)
{
if(node == NULL)
return 0;
if(node->left == NULL && node->right==NULL)
return 1;
else
return getLeafCount(node->left)+
getLeafCount(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);
}
int main()
{
struct node *root = newNode(1);
root->left• = newNode(2);
root->right• = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("Leaf count of the tree is %d", getLeafCount(root));

getchar();
return 0;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program to implement AVL tree i) Creation ii) Deletion iii) Traversal
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
int ht;
};
struct node* root = NULL;
struct node* create(int);
struct node* insert(struct node*, int);
struct node* (struct node*, int);
struct node* search(struct node*, int);
struct node* rotate_left(struct node*);
struct node* rotate_right(struct node*);
int balance_factor(struct node*);
int height(struct node*);
void inorder(struct node*);
void preorder(struct node*);
void postorder(struct node*);

int main()
{
int user_choice, data;
char user_continue = 'y
struct node* result = NULL;

while (user_continue == 'y' || user_continue == 'Y')


{
printf("\n\n - - - - AVL TREE - - - - \n");
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Search");
printf("\n4. Inorder");
printf("\n5. Preorder");
printf("\n6. Postorder");
printf("\n7. EXIT");

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("\n\nEnter Your Choice: ");


scanf("%d", &user_choice);

switch(user_choice)
{
case 1:
printf("\nEnter data: ");
scanf("%d", &data);
root = insert(root, data);
break;

case 2:
printf("\nEnter data: ");
scanf("%d", &data);
root = (root, data);
break;

case 3:
printf("\nEnter data: ");
scanf("%d", &data);
result = search(root, data);
if (result == NULL)
{
printf("\nNode not found!");
}
else
{
printf("\n Node found");
}
break;
case 4:
inorder(root);
break;

case 5:
preorder(root);
break;

case 6:
postorder(root);
break;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

case 7:
printf("\n\tProgram Terminated\n");
return 1;

default:
printf("\n\tInvalid Choice\n");
}

printf("\n\nDo you want to continue? ");


scanf(" %c", &user_continue);
}

return 0;
}
struct node* create(int data)
{
struct node* new_node = (struct node*) malloc (sizeof(struct
node));
if (new_node == NULL)
{
printf("\nMemory can't be allocated\n");
return NULL;
}
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
struct node* rotate_left(struct node* root)
{
struct node* right_child = root->right;
root->right = right_child->left;
right_child->left = root;
root->ht = height(root);
right_child->ht = height(right_child);
return right_child;
}
struct node* rotate_right(struct node* root)
{
struct node* left_child = root->left;
root->left = left_child->right;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

left_child->right = root;
root->ht = height(root);
left_child->ht = height(left_child);
return left_child;
}
int balance_factor(struct node* root)
{
int lh, rh;
if (root == NULL)
return 0;
if (root->left == NULL)
lh = 0;
else
lh = 1 + root->left->ht;
if (root->right == NULL)
rh = 0;
else
rh = 1 + root->right->ht;
return lh - rh;
}
int height(struct node* root)
{
int lh, rh;
if (root == NULL)
{
return 0;
}
if (root->left == NULL)
lh = 0;
else
lh = 1 + root->left->ht;
if (root->right == NULL)
rh = 0;
else
rh = 1 + root->right->ht;

if (lh > rh)


return (lh);
return (rh);
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

struct node* insert(struct node* root, int data)


{
if (root == NULL)
{
struct node* new_node = create(data);
if (new_node == NULL)
{
return NULL;
}
root = new_node;
}
else if (data > root->data)
{
root->right = insert(root->right, data);
if (balance_factor(root) == -2)
{
if (data > root->right->data)
{
root = rotate_left(root);
}
else
{
root->right = rotate_right(root->right);
root = rotate_left(root);
}
}
}
else
{
root->left = insert(root->left, data);
if (balance_factor(root) == 2)
{
if (data < root->left->data)
{
root = rotate_right(root);
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

}}}
root->ht = height(root);
return root;
}
struct node * (struct node *root, int x)
{
struct node * temp = NULL;

if (root == NULL)
{
return NULL;
}

if (x > root->data)
{
root->right = (root->right, x);
if (balance_factor(root) == 2)
{
if (balance_factor(root->left) >= 0)
{
root = rotate_right(root);
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);
}
}
}
else if (x < root->data)
{
root->left = (root->left, x);
if (balance_factor(root) == -2)
{
if (balance_factor(root->right) <= 0)
{
root = rotate_left(root);
}
else
{
root->right = rotate_right(root->right);
root = rotate_left(root);
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

}
}
else
{
if (root->right != NULL)
{
temp = root->right;
while (temp->left != NULL)
temp = temp->left;

root->data = temp->data;
root->right = (root->right, temp->data);
if (balance_factor(root) == 2)
{
if (balance_factor(root->left) >= 0)
{
root = rotate_right(root);
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);
}
}
}
else
{
return (root->left);
}
}
root->ht = height(root);
return (root);
}
struct node* search(struct node* root, int key)
{
if (root == NULL)
{
return NULL;
}

if(root->data == key)

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

{
return root;
}

if(key > root->data)


{
search(root->right, key);
}
else
{
search(root->left, key);
}
}
void inorder(struct node* root)
{
if (root == NULL)
{
return;
}

inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
void preorder(struct node* root)
{
if (root == NULL)
{
return;
}

printf("%d ", root->data);


preorder(root->left);
preorder(root->right);
}
void postorder(struct node* root)
{
if (root == NULL)
{
return;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
OUTPUT: }

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program for implementing DFS Graph traversal


SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void dfs(int);
int n,a[10][10];
int visited[10];
void main()
{
int i,j,v;
printf("Enter the no. of nodes in the Graph:\t");
scanf("%d",&n);
printf("Enter the adjacency matrix: \n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the starting node for Depth First search:\t");
scanf("%d",&v);
for(i=0;i<n;i++)
visited[i]=0;
dfs(v);
}
void dfs(int v)
{
int i,stack[10],top=-1,popped;
stack[++top]=v;
while(top>=0)
{
popped=stack[top - ];
if(visited[popped]==0)
{
printf("->%d",popped);
visited[popped]=1;
}
for(i=n-1;i>=0;i - )
if(a[popped][i]==1 && visited[i]==0)
stack[++top]=i;
} }

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write a C program for implementing BFS Graph traversal


SOURCE CODE: #include <stdio.h>
int n;
int v[10] = {0}; //
int adj[10][10];

void bfs(int start) {


int q[10], f = -1, r = -1, i;
q[++r] = start;
v[start] = 1;

while (r != f) {
start = q[++f];
printf("%d\t", start);

for (i = 0; i < n; i++) {


if (adj[start][i] == 1 && v[i] == 0) {
q[++r] = i;
v[i] = 1;
}
}
}
}

int main() {
int i, j, vt;
printf("Enter number of vertices: ");
scanf("%d", &n);

printf("Enter the adjacency matrix:\n");


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &adj[i][j]);
}
}

printf("Enter the initial vertex number: ");


scanf("%d", &vt);

bfs(vt);

return 0;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write C program to implement the Quick Sort


SOURCE CODE:
#include <stdio.h>
#define size 50
int arr[size];
void quicksort(int,int);
void swap(int,int);
int main()
{
int i,n;
printf("\n Enter no.of elements u want?");
scanf("%d",&n);
printf("\n Enter any %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
quicksort(0,n-1);
printf("\n After sorting the elements are:");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);

return 0;
}
void quicksort(int low, int high)
{
int pivot,lt,rt;
if(low<high)
{
pivot=arr[high];
lt=low-1;
rt=high;
while(1)
{
lt++;
while(arr[lt]<pivot)
lt++;
rt - ;
while(arr[rt]>pivot && rt!=0)
rt - ;
if(lt>=rt)
break;
else
swap(lt,rt);

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

}
swap(lt,high);
quicksort(low,lt-1);
quicksort(lt+1,high);
}
}
void swap(int l, int r)
{
int temp;
temp=arr[l];
arr[l]=arr[r];
arr[r]=temp;
}

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write C program to implement the Heap sort

SOURCE CODE:
#include <stdio.h>
void heapify(int a[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && a[left] > a[largest])
largest = left;
if (right < n && a[right] > a[largest])
largest = right;
if (largest != i)
{
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
heapify(a, n, largest);
}
}
void heapSort(int a[], int n)
{
for (int i = n/2-1; i >= 0; i - )
heapify(a, n, i);
for (int i = n - 1; i >= 0; i - )
{
int temp = a[0];
a[0] = a[i];
a[i] = temp;

heapify(a, i, 0);
}
}
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
} }

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

int main()
{
int a[] = {48, 10, 23, 43, 28, 26, 1};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM: Write C program to implement the Merge sort


SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
void merge(int a[], int , int ,int);
void mergesort(int a[], int , int );
int main()
{
int a[100],i,n;
printf("enter no of element");
scanf("%d",&n);
printf("\n\nEnter the elements to be sorted: \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("After Merge Sort :");
for(i = 0; i < n; i++)
printf(" %d", a[i]);
printf("\n");
return 0;
}
void merge(int a[], int beg,int mid,int end)
{
int i=beg,j = mid+1,index=beg,temp[100],k;
while((i<=mid)&&(j<=end))
{
if(a[i]<a[j])
{ \
temp[index]=a[i];
i++;
}
else
{ temp[index]=a[j];
j++;
}
index++;
}
if(i>mid)
{
while(j<=end)

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

{
temp[index]=a[j];
j++;
index++;
}
}
else
{
while(i<=mid)
{
temp[index]=a[i];
i++;
index++;
}
}
for(k=beg;k<index;k++)
a[k]=temp[k];
}
void mergesort(int a[],int beg,int end)
{
int mid;
if(beg<end)
{ mid=(beg+end)/2;
mergesort(a, beg, mid);
mergesort(a, mid + 1, end);
merge(a,beg,mid,end);
}
OUTPUT: }

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Write a C program to implement Knuth-Morris-Pratt pattern matching algorithm


AIM:
SOURCE CODE:
#include<stdio.h>
#include<string.h>
void prefixSuffixArray(char* pat, int M, int* lps) {
int length = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[length]) {
length++;
lps[i] = length;
i++;
} else {
if (length != 0)
length = lps[length - 1];
else {
lps[i] = 0;
i++;
}
}
}
}
void KMPAlgorithm(char* text, char* pattern) {
int M = strlen(pattern);
int N = strlen(text);
int lps[M];
prefixSuffixArray(pattern, M, lps);
int i = 0;
int j = 0;
while (i < N)
{
if (pattern[j] == text[i])
{
j++;
i++;
}
if (j == M)
{
printf("Found pattern at index %d", i - j);
j = lps[j - 1];
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

else if (i < N && pattern[j] != text[i])


{
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
int main() {
char text[50];
char pattern[50];
printf("\nEnter main text:");
gets(text);
printf("\nEnter the pattern:");
gets(pattern);
printf("The pattern is found in the text at the following index : ");
KMPAlgorithm(text, pattern);
return 0;
}
OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM:Write a C program to implement Brute Force pattern matching algorithm

SOURCE CODE:
#include <stdio.h>
#include <string.h>
int main()
{
char text[20],pat[20];
int a,b,i,j;
printf("Enter the string : ");
gets(text);
printf("Enter the pattern to find : ");
gets(pat);

a = strlen(pat);
b = strlen(text);

for (i = 0; i <= b - a; i++)


{

for (j = 0; j < a; j++)


if (text[i + j] != pat[j])
break;
if (j == a)
printf("Pattern found at position %d \n", i);

}
}

OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

AIM:Write a C program for implementing pattern matching algorthm (Boyer-Moore Algorithm)


SOURCE CODE:
# include <string.h>
# include <stdio.h>
# define NO_OF_CHARS 256
int max(int a, int b) {
return (a > b) ? a : b;
}
void badCharHeuristic(char *str, int size, int
badchar[NO_OF_CHARS]) {
int i;
for (i = 0; i < NO_OF_CHARS; i++)
badchar[i] = -1;
for (i = 0; i < size; i++)
badchar[(int) str[i]] = i;
}
void search(char *txt, char *pat) {
int m = strlen(pat);
int n = strlen(txt);
int badchar[NO_OF_CHARS];
badCharHeuristic(pat, m, badchar);
int s = 0;
while (s <= (n - m)) {
int j = m - 1;
while (j >= 0 && pat[j] == txt[s + j])
j-;
if (j < 0) {
printf("\n pattern occurs at Index = %d", s);
s += (s + m < n) ? m - badchar[txt[s + m]] : 1;
}
else
s += max(1, j - badchar[txt[s + j]]);
}}
int main() {
char txt[100],pat[100];
printf("\n Enter text:");
gets(txt);
printf("\n Enter pattern to search:");
gets(pat);
search(txt, pat);
return 0;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

OUTPUT:

Dept of CSE Page No.

You might also like