SlideShare a Scribd company logo
1 of 76
Download to read offline
KONGU ENGINEERING COLLEGE
PERUNDURAI, ERODE
20CSL31 - data StRUCtURES
Program : B.E
Branch : COMPUTER SCIENCE AND ENGINEERING
Semester : III SEMESTER
Year : IIND
YEAR
2021
KONGU ENGINEERING COLLEGE
(Autonomous)
PERUNDURAI, ERODE- 638 060
data StRUCtURES LaBORatORY
LaB MaNUaL
: COMPUTER SCIENCE AND ENGINEERING
SEMESTER
YEAR
2021 – 2022 (Odd)
KONGU ENGINEERING COLLEGE
LaBORatORY
: COMPUTER SCIENCE AND ENGINEERING
List of Experiments
Serial
No.
Name of the Experiment Page No.
1 Implementation of Singly Linked List and Its Operations 02
2 Implementation of Doubly Linked List and Its Operations 19
3 Implementation of Circular Linked List and Its Operations 34
4 Implementation of Polynomial Addition using Linked List 46
5 Infix to Postfix Conversion using Stack ADT 51
6 Postfix Expression using Array of Stack ADT 54
7 Implementation of Reversing Queue using Stack 56
8 Implementation of Binary Search Tree Traversals 59
9 Implementation of Graph Traversal Techniques 62
10 Implementation of Red Block Tree Operations 65
11 Implementation of Bubble Sort and Shell sort 69
12 Implementation of Hash Table Using Array 72
2
EXP NO : 01
1. SINGLY LINKED LIST
AIM:
To create a C program to perform various operations on Singly Linked List.
ALGORITHM:
Step1: Start the program.
Step2: Get the type of operation from the user to perform.
Step3: Do the specified operation given by the user.
Step4: Continue until the exit operation.
Step5: End the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
struct student{
int roll;
char name[25];
short age;
long long int phone;
struct student *next;
}*head;
int rno = 1,pos=0;
void printList(struct student *d,int sno){
if (d==NULL)
return;
printf("%-7d %-11d %-23s %-7hd %-10lldn",++sno,d->roll,d->name,d->age,d-
>phone);
printList(d->next,sno);
}
void printRev(struct student *d,int sno){
if (d==NULL)
return;
printRev(d->next,++sno);
printf("%-7d %-11d %-23s %-7hd %-10lldn",sno,d->roll,d->name,d->age,d-
>phone);
}
void add(short ins_ch,char name[25],short age,long long int phone){
int roll;
3
struct student *second = (struct student *) malloc(sizeof(struct student));
second->roll=rno;
rno+=1; //Rno assigned for next roll number
strcpy(second->name,name);
second->age=age;
second->phone=phone;
second->next=NULL;
if (head ==NULL)
head = second;
else{
if(ins_ch==1){
int i;
second->next=head;
head=second;
}
else if(ins_ch==2){
int i,place;
struct student *temp;
temp = head;
if (pos==2){
place=2;
second->next=temp->next;
temp->next=second;
}
else{
while(ins_ch){
printf("n Position available between 1 and %d n After
which position do you want to add? ",pos);
scanf("%d",&place);
if((place<1) || (place>=pos))
printf("ann***Requested Position not
available...***n");
else
ins_ch=0;
}
for(i=1;i<place;i++){
temp=temp->next;
}
second->next=temp->next;
temp->next=second;
}
}
else{
struct student *temp;
4
temp = head;
while(temp!=NULL && temp->next!=NULL)
temp = temp->next;
temp->next=second;
}
}
pos+=1;
printf("nDetails Added Successfully!n");
}
short search(int roll){
struct student *temp;
temp = head;
while(temp!=NULL){
if(temp->roll==roll){
printf("n*************************");
printf("nnRoll no: %d",temp->roll);
printf("nName : %s",temp->name);
printf("nAge : %hd",temp->age);
printf("nPhone : %lld",temp->phone);
printf("nn*************************n");
return 2;
}
temp=temp->next;
}
printf("nna***Student with roll number %d is not found*** n",roll);
return 5;
}
struct student* copylist(struct student *p){
if(p==NULL)
return NULL;
else{
struct student *ne=(struct student *) malloc(sizeof(struct student));
ne->roll=p->roll;
strcpy(ne->name,p->name);
ne->age=p->age;
ne->phone=p->phone;
ne->next=copylist(p->next);
return ne;
}
}
void del_process(int roll){
struct student *temp;
temp=head;
5
if(temp->roll==roll){ //First
head=head->next;
free(temp);
}
else{
struct student *ptr;
ptr=head;
while(ptr->roll!=roll){
temp=ptr;
ptr= ptr->next;
}
temp->next=ptr->next;// Middle and last
free(ptr);
}
pos-=1;
printf("nDeleted Successfully...n");
}
void swap(struct student *a, struct student *b)
{
int rno = a->roll;
a->roll = b->roll;
b->roll = rno;
char name[25];
strcpy(name,a->name);
strcpy(a->name,b->name);
strcpy(b->name,name);
short age=a->age;
a->age=b->age;
b->age=age;
long long int phone = a->phone;
a->phone=b->phone;
b->phone=phone;
}
void swapping(struct student *start){
int swapped, i;
struct student *ptr1;
struct student *lptr = NULL;
if (start == NULL)
return;
do
{
swapped = 0;
ptr1 = start;
while (ptr1->next != lptr)
6
{
if (ptr1->roll > ptr1->next->roll)
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
printf("nnSorting Successfull...n");
}
int main(){
short ch;
do{
printf("ntt MENUntt ******nn1.Addtt2.Printtt3.Searchnn4.Print
Reverset5.Copy_Listt6.Sortingnn7.Deletet8.QuitnnEnter your choice : ");
scanf("%hd",&ch);
switch(ch){
case 1:{
short i,s,ins_ch;
while(ch){
printf("nAddingn*******n1.Insert at First n2.Insert
at Middle n3.Insert at Endn4.Go Back");
printf("nnEnter your Choice : ");
scanf("%hd",&ins_ch);
if ((ins_ch ==2) && ((head==NULL)||(head-
>next==NULL)))
printf("naNot able to insert at middle...List is
empty or containing only 1 node");
else if
((ins_ch==1)||(ins_ch==2)||(ins_ch==3)||(ins_ch==4))
ch = 0;
else
printf("naInsert your choice correctly...n");
}
if(ins_ch==4)
break;
printf("nEnter how many members : ");
scanf("%hd",&s);
for(i=0;i<s;i++){
char name[25];
long long int phone;
7
short age;
fflush(stdin);
printf("nEnter name without space : ");
scanf("%[^n]",name);
printf("nEnter Age : ");
scanf("%hd",&age);
printf("nEnter Phone Number: ");
scanf("%lld",&phone);
add(ins_ch,name,age,phone);
}
break;
}
case 2:{
if (head==NULL)
printf("nList is empty...nn");
else{
printf("nAll Details n***************n");
printf("S.No Roll No Name Age
Phonen");
printf("**************************************************************
n");
printList(head,0);
}
break;
}
case 3:{
int roll;
printf("nEnter Roll number : ");
scanf("%d",&roll);
ch=search(roll);
break;
}
case 4:{
if (head==NULL)
printf("nList is empty...nn");
else{
printf("nAll Details n***************n");
printf("S.No Roll No Name Age
Phonen");
printf("**************************************************************
n");
printRev(head,0);
8
}
break;
}
case 5:{
struct student *copied=copylist(head);
printf("nPrinting Copied Listn");
if (copied==NULL)
printf("nList is empty...nn");
else{
printf("nAll Details n***************n");
printf("S.No Roll No Name Age
Phonen");
printf("**************************************************************
n");
printRev(copied,0);
}
break;
}
case 6:{
swapping(head);
break;
}
case 7:{
int roll;
printf("nEnter Roll number : ");
scanf("%d",&roll);
ch=search(roll);
if(ch==2){
printf("nPress 1 to Confirm (or) 2 to Cancel : ");
scanf("%hd",&ch);
if(ch==1)
del_process(roll);
}
break;
}
case 8:{
printf("nn********THANK YOU********");
exit(1);
break;
}
default:{
printf("na *** Sorry, Invalid Operation *** n");
break;
9
}
}
}while(1);
return 0;
}
OUTPUT:
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 1
Adding
*******
1.Insert at First
2.Insert at Middle
3.Insert at End
4.Go Back
Enter your Choice : 1
Enter how many members : 1
Enter name without space : Raja
Enter Age : 17
Enter Phone Number: 9361759453
Details Added Successfully!
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
10
7.Delete 8.Quit
Enter your choice : 2
All Details
***************
S.No Roll No Name Age Phone
**************************************************************
1 1 Raja 17 9361759453
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 1
Adding
*******
1.Insert at First
2.Insert at Middle
3.Insert at End
4.Go Back
Enter your Choice : 2
Not able to insert at middle...List is empty or containing only 1 node
Adding
*******
1.Insert at First
2.Insert at Middle
3.Insert at End
4.Go Back
Enter your Choice : 3
Enter how many members : 1
Enter name without space : Kavin
11
Enter Age : 18
Enter Phone Number: 9952864185
Details Added Successfully!
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 2
All Details
***************
S.No Roll No Name Age Phone
**************************************************************
1 1 Raja 17 9361759453
2 2 Kavin 18 9952864185
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 1
Adding
*******
1.Insert at First
2.Insert at Middle
3.Insert at End
4.Go Back
Enter your Choice : 2
12
Enter how many members : 1
Enter name without space : Neeraj
Enter Age : 17
Enter Phone Number: 9952864180
Details Added Successfully!
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 2
All Details
***************
S.No Roll No Name Age Phone
**************************************************************
1 1 Raja 17 9361759453
2 3 Neeraj 17 9952864180
3 2 Kavin 18 9952864185
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 1
Adding
*******
1.Insert at First
13
2.Insert at Middle
3.Insert at End
4.Go Back
Enter your Choice : 2
Enter how many members : 1
Enter name without space : Lohit
Enter Age : 18
Enter Phone Number: 897456231
Position available between 1 and 3
After which position do you want to add? 1
Details Added Successfully!
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 2
All Details
***************
S.No Roll No Name Age Phone
**************************************************************
1 1 Raja 17 9361759453
2 4 Lohit 18 897456231
3 3 Neeraj 17 9952864180
4 2 Kavin 18 9952864185
MENU
******
1.Add 2.Print 3.Search
14
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 3
Enter Roll number : 2
*************************
Roll no: 2
Name : Kavin
Age : 18
Phone : 9952864185
*************************
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 3
Enter Roll number : 5
***Student with roll number 5 is not found***
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 4
15
All Details
***************
S.No Roll No Name Age Phone
**************************************************************
4 2 Kavin 18 9952864185
3 3 Neeraj 17 9952864180
2 4 Lohit 18 897456231
1 1 Raja 17 9361759453
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 5
Printing Copied List
All Details
***************
S.No Roll No Name Age Phone
**************************************************************
4 2 Kavin 18 9952864185
3 3 Neeraj 17 9952864180
2 4 Lohit 18 897456231
1 1 Raja 17 9361759453
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 6
Sorting Successfull...
16
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 2
All Details
***************
S.No Roll No Name Age Phone
**************************************************************
1 1 Raja 17 9361759453
2 2 Kavin 18 9952864185
3 3 Neeraj 17 9952864180
4 4 Lohit 18 897456231
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 7
Enter Roll number : 4
*************************
Roll no: 4
Name : Lohit
Age : 18
Phone : 897456231
*************************
Press 1 to Confirm (or) 2 to Cancel : 2
17
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 4
All Details
***************
S.No Roll No Name Age Phone
**************************************************************
4 4 Lohit 18 897456231
3 3 Neeraj 17 9952864180
2 2 Kavin 18 9952864185
1 1 Raja 17 9361759453
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
nter your choice : 7
Enter Roll number : 4
*************************
Roll no: 4
Name : Lohit
Age : 18
Phone : 897456231
*************************
Press 1 to Confirm (or) 2 to Cancel : 1
18
Deleted Successfully...
MENU
******
1.Add 2.Print 3.Search
4.Print Reverse 5.Copy_List 6.Sorting
7.Delete 8.Quit
Enter your choice : 8
********THANK YOU********
RESULT:
The above code has executed successfully.
19
EXP NO : 02
2. DOUBLYLINKED LIST
AIM:
To create a C program to perform various operations on DoublyLinked List.
ALGORITHM:
Step1: Start the program.
Step2: Get the type of operation from the user to perform.
Step3: Do the specified operation given by the user.
Step4: Continue until the exit operation.
Step5: End the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
short rno=1,pos=0;
struct songs{
short sno;
char name[40];
short min;
short sec;
struct songs *pre;
struct songs *next;
}*head=NULL,*last;
void player();
void add_song();
void allsong();
void success_msg();
void play_song();
short search(short id);
void delete_song();
void space(short i){
short j=0;
for(;j<i;j++)
printf("n");
}
int main(){
player();
}
20
void player(){
short i;
system("CLS");
system("COLOR 4F");
space(17);
printf("tttMusic Playernn");
printf("ttt1.Add Song nttt2.Play Song nttt3.Delete Song
nttt4.All_Songsnttt5.Exit ntttEnter Your Choice : ");
scanf("%hd",&i);
switch(i){
case 1:
add_song();
break;
case 2:
play_song();
break;
case 3:
delete_song();
break;
case 4:
allsong();
break;
case 5:
exit(1);
break;
default:
printf("ntttWrong Inputa nntttPress Any Key To Continue...");
getch();
}
player();
}
void success_msg(){
system("CLS");
system("COLOR 4F");
space(17);
printf("tt"Song Added Successfully"nnttPress Any Key to Continue...");
getch();
pos++;
}
void allsong(){
system("CLS");
system("COLOR 4F");
space(10);
struct songs *temp=head;
21
if(temp==NULL){
printf("a No Songs Available nPress any key to continue...");
getch();
return ;
}
printf("nSong_Id Name Min Secn");
while(temp!=NULL){
printf("%-8hd %-40s %3hd %3hdn",temp->sno,temp->name,temp-
>min,temp->sec);
temp=temp->next;
}
printf("nPress Any Key To Continue...");
getch();
}
void add_song(){
short i;
char name[40];
system("CLS");
system("COLOR 4F");
space(17);
printf("tttAdd Songnn");
struct songs *n=(struct songs *) malloc(sizeof(struct songs));
n->sno=rno++;
fflush(stdin);
printf("nttEnter Song Name : ");
scanf("%[^n]",name);
strcpy(n->name,name);
printf("nttEnter Minutes : ");
scanf("%hd",&n->min);
printf("nttEnter Seconds : ");
scanf("%hd",&n->sec);
n->pre=NULL;
n->next=NULL;
if(pos==0){
head=n;
last=n;
success_msg();
return ;
}
if(pos==1){
n->pre=head;
head->next=n;
success_msg();
last=n;
22
return;
}
printf("ttt1.Insert At First nttt2.Insert at Middle nttt3.Insert at End
nttt4.Back nttt5.Exit ntttEnter Your Choice : ");
scanf("%hd",&i);
if(i==1){
n->next=head;
head->pre=n;
head=n;
success_msg();
}
else if(i==2){
struct songs *temp=head;
if(n->sno==3){
n->pre=temp;
n->next=temp->next;
temp->next->pre=n;
temp->next=n;
}
else{
int place,ins_ch=1;
while(ins_ch){
printf("n Position available between 1 and %d n After which
position do you want to add? ",pos);
scanf("%d",&place);
if((place<1) || (place>=pos))
printf("ann***Requested Position not
available...***n");
else
ins_ch=0;
}
for(i=1;i<place;i++){
temp=temp->next;
}
n->pre=temp;
n->next=temp->next;
temp->next->pre=n;
temp->next=n;
}
success_msg();
}
else if(i==3){
last->next=n;
n->pre=last;
23
success_msg();
}
else if(i==4){
free(n);
return;
}
else{
free(n);
exit(1);
}
}
void play_song(){
short i;
struct songs *temp=head;
if(head==NULL){
system("CLS");
system("COLOR 4F");
space(17);
printf("ntNo Songs Available...ntPress Any Key To Continue...");
getch();
return ;
}
short c=0,d=0;
while(1){
system("CLS");
system("COLOR 4F");
space(17);
printf("ttCurrently Playingnn");
printf("tt%snn",temp->name);
if(temp->pre!=NULL){
printf("ntt1.Previous");
c=1;
}
if(temp->next!=NULL){
printf("ntt2.Next");
d=1;
}
printf("ntt3.Stop Playing");
short ch,l=1;
do{
printf("nttEnter your Choice : ");
scanf("%hd",&ch);
if(ch==1){
if(c==0){
24
printf("anttInvalid Input");
l=1;
}
else
l=0;
}
else if(ch==2){
if(d==0){
printf("anttInvalid Input");
l=1;
}
else
l=0;
}
else if(ch==3)
l=0;
else
printf("nattInvalid Input");
}while(l);
switch(ch){
case 1:
temp=temp->pre;
break;
case 2:
temp=temp->next;
break;
case 3:
return;
}
c=0;
d=0;
}
}
short search(short id){
short i;
struct songs *temp=head;
while(temp!=NULL){
if(temp->sno==id)
return 1;
temp=temp->next;
}
short a,id;
25
system("CLS");
system("COLOR 4F");
space(17);
printf("ttEnter Song Id : ");
scanf("%hd",&id);
a=search(id);
if(a==0){
printf("nattSong Id Not FoundnttPress Any Key To Continue...");
getch();
return ;
}
struct songs *temp=head,*ptr;
while(temp!=NULL){
if(temp->sno==id)
break;
temp=temp->next;
}
if(temp->pre==NULL && temp->next==NULL){
ptr=head;
head=NULL;
free(ptr);
printf("nnttDeletion SuccessfullnttPress Any Key to Continue...");
getch();
pos--;
return;
26
}
if(temp->pre==NULL){
ptr=head;
head=ptr->next;
head->pre=NULL;
free(ptr);
}
else if(temp->next==NULL){
ptr=temp;
temp->pre->next=NULL;
last=ptr->pre;
free(ptr);
}
else{
ptr=temp;
temp->pre->next=ptr->next;
temp->next->pre=ptr->pre;
free(ptr);
}
printf("nnttDeletion SuccessfullnttPress Any Key to Continue...");
getch();
pos--;
}
OUTPUT:
Music Player
27
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 1
Add Song
Enter Song Name : Into Your Arms
Enter Minutes : 2
Enter Seconds : 26
"Song Added Successfully"
Press Any Key to Continue...
Music Player
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 1
Add Song
Enter Song Name : Birthday Song
Enter Minutes : 5
Enter Seconds : 54
"Song Added Successfully"
Press Any Key to Continue...
Music Player
28
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 1
Add Song
Enter Song Name : God Song
Enter Minutes : 4
Enter Seconds : 8
1.Insert At First
2.Insert at Middle
3.Insert at End
4.Back
5.Exit
Enter Your Choice : 2
"Song Added Successfully"
Press Any Key to Continue...
Music Player
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 1
Add Song
Enter Song Name : Vinayaga
Enter Minutes : 8
29
Enter Seconds : 2
1.Insert At First
2.Insert at Middle
3.Insert at End
4.Back
5.Exit
Enter Your Choice : 2
Position available between 1 and 3
After which position do you want to add? 2
"Song Added Successfully"
Press Any Key to Continue...
Music Player
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 4
Song_Id Name Min Sec
1 Into Your Arms 2 26
3 God Song 4 8
4 Vinayaga 8 2
2 Birthday Song 5 54
Press Any Key To Continue...
Music Player
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 2
30
Currently Playing
Into Your Arms
2.Next
3.Stop Playing
Enter your Choice : 1
Invalid Input
Enter your Choice : 2
Currently Playing
God Song
1.Previous
2.Next
3.Stop Playing
Enter your Choice : 2
Currently Playing
Vinayaga
1.Previous
2.Next
3.Stop Playing
Enter your Choice : 2
Currently Playing
Birthday Song
1.Previous
3.Stop Playing
Enter your Choice : 1
Currently Playing
31
Vinayaga
1.Previous
2.Next
3.Stop Playing
Enter your Choice : 3
Music Player
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 3
Enter Song Id : 8
Song Id Not Found
Press Any Key To Continue...
Music Player
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 3
Enter Song Id : 4
Deleted Successfully
Music Player
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 4
32
Song_Id Name Min Sec
1 Into Your Arms 2 26
3 God Song 4 8
2 Birthday Song 5 54
Press Any Key To Continue...
Music Player
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 3
Enter Song Id : 2
Deleted Successfully
Music Player
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 4
Song_Id Name Min Sec
1 Into Your Arms 2 26
3 God Song 4 8
Music Player
1.Add Song
2.Play Song
3.Delete Song
4.All_Songs
5.Exit
Enter Your Choice : 5]
--------------------------------
Process exited after 2.123 seconds with return value 1
33
Press any key to continue . . .
RESULT:
The above code has executed successfully.
34
EXP NO : 03
3. CIRCULARLINKED LIST
AIM:
To create a C program to perform various operations on CircularLinked List.
ALGORITHM:
Step1: Start the program.
Step2: Get the type of operation from the user to perform.
Step3: Do the specified operation given by the user.
Step4: Traverse the linked list to previous and next nodes.
Step5: Continue until the exit operation.
Step6: End the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int pos=0;
struct circular{
int data;
struct circular *pre;
struct circular *next;
}*head=NULL,*last;
void call();
void adddata();
void alldata();
void success_msg();
short search(int id);
void deletedata();
void traverse();
int main(){
call();
}
void call(){
short i;
printf("nnttt1.Add Data nttt2.Delete data nttt3.Print_Allnttt4.Traverse
nttt5.Exit ntttEnter Your Choice : ");
scanf("%hd",&i);
switch(i){
case 1:
35
adddata();
break;
case 2:
deletedata();
break;
case 3:
alldata();
break;
case 4:
traverse();
break;
case 5:
exit(1);
break;
default:
printf("ntttWrong Inputa nntttPress Any Key To Continue...");
getch();
}
call();
}
void traverse(){
struct circular *temp = head;
if (temp==NULL){
printf("nna List is Empty ");
return ;
}
short ch;
while(1){
printf("nntCurrent Data : %d",temp->data);
printf("n1.Previous n2.Next n3.Stop nEnter your choice : ");
scanf("%hd",&ch);
if(ch==1)
temp=temp->pre;
else if(ch==2)
temp=temp->next;
else if(ch==3)
return;
else
printf("naInvalid Input");
}
}
void success_msg(){
printf("tt"Data Added Successfully"nnttPress Any Key to Continue...");
getch();
36
pos++;
}
void alldata(){
struct circular *temp=head;
if(temp==NULL){
printf("a No Data Available nPress any key to continue...");
getch();
return ;
}
do{
printf("%dn",temp->data);
temp=temp->next;
}while(temp!=head);
printf("nPress Any Key To Continue...");
getch();
}
void adddata(){
int i;
printf("tttAdd Datann");
struct circular *n=(struct circular *) malloc(sizeof(struct circular));
printf("nttEnter Data : ");
scanf("%d",&n->data);
n->pre=NULL;
n->next=NULL;
if(pos==0){
head=n;
last=n;
head->next=head;
head->pre=head;
success_msg();
return ;
}
if(pos==1){
n->pre=head;
n->next=head;
head->pre=n;
head->next=n;
success_msg();
last=n;
return;
}
printf("ttt1.Insert At First nttt2.Insert at Middle nttt3.Insert at End
nttt4.Back nttt5.Exit ntttEnter Your Choice : ");
scanf("%hd",&i);
37
if(i==1){
last->next=n;
n->pre=last;
n->next=head;
head->pre=n;
head=n;
success_msg();
}
else if(i==2){
struct circular *temp=head;
if(pos==2){
n->pre=temp;
n->next=temp->next;
temp->next->pre=n;
temp->next=n;
}
else{
int place,ins_ch=1;
while(ins_ch){
printf("n Position available between 1 and %d n After which
position do you want to add? ",pos);
scanf("%d",&place);
if((place<1) || (place>=pos))
printf("ann***Requested Position not
available...***n");
else
ins_ch=0;
}
for(i=1;i<place;i++){
temp=temp->next;
}
n->pre=temp;
n->next=temp->next;
temp->next->pre=n;
temp->next=n;
}
success_msg();
}
else if(i==3){
last->next=n;
n->pre=last;
n->next=head;
head->pre=n;
last=last->next;
38
success_msg();
}
else if(i==4){
free(n);
return;
}
else{
free(n);
exit(1);
}
}
short search(int id){
struct circular *temp=head;
do{
if(temp->data==id)
return 1;
temp=temp->next;
}while(temp!=head);
return 0;
}
void deletedata(){
int a,id;
if(head==NULL){
printf("naEmpty...");
return ;
}
printf("ttEnter Data : ");
scanf("%d",&id);
a=search(id);
if(a==0){
printf("nattData Not FoundnttPress Any Key To Continue...");
getch();
return ;
}
struct circular *temp=head,*ptr;
do{
if(temp->data==id)
break;
temp=temp->next;
}while(temp!=head);
if(temp->pre==head && temp->next==head){
ptr=head;
head=NULL;
39
free(ptr);
printf("nnttDeletion SuccessfullnttPress Any Key to Continue...");
getch();
pos--;
return;
}
if(temp==head && temp->next!=head){
ptr=head;
head=ptr->next;
head->pre=last;
last->next=head;
free(ptr);
}
else if(temp==last && temp->pre!=head){
ptr=temp;
temp->pre->next=head;
last=ptr->pre;
head->pre=last;
free(ptr);
}
else{
ptr=temp;
temp->pre->next=ptr->next;
temp->next->pre=ptr->pre;
free(ptr);
}
printf("nnttDeletion SuccessfullnttPress Any Key to Continue...");
getch();
pos--;
}
OUTPUT:
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 1
Add Data
Enter Data : 25
40
"Data Added Successfully"
Press Any Key to Continue...
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 1
Add Data
Enter Data : 35
"Data Added Successfully"
Press Any Key to Continue...
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 1
Add Data
Enter Data : 45
1.Insert At First
2.Insert at Middle
3.Insert at End
4.Back
5.Exit
Enter Your Choice : 2
"Data Added Successfully"
Press Any Key to Continue...
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 1
41
Add Data
Enter Data : 55
1.Insert At First
2.Insert at Middle
3.Insert at End
4.Back
5.Exit
Enter Your Choice : 2
Position available between 1 and 3
After which position do you want to add? 3
***Requested Position not available...***
Position available between 1 and 3
After which position do you want to add? 2
"Data Added Successfully"
Press Any Key to Continue...
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 1
Add Data
Enter Data : 65
1.Insert At First
2.Insert at Middle
3.Insert at End
4.Back
5.Exit
Enter Your Choice : 1
"Data Added Successfully"
Press Any Key to Continue...
1.Add Data
42
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 1
Add Data
Enter Data : 75
1.Insert At First
2.Insert at Middle
3.Insert at End
4.Back
5.Exit
Enter Your Choice : 3
"Data Added Successfully"
Press Any Key to Continue...
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 3
65
25
45
55
35
75
Press Any Key To Continue...
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 2
Enter Data : 75
Deletion Successfull
43
Press Any Key to Continue...
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 3
65
25
45
55
35
Press Any Key To Continue...
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 2
Enter Data : 65
Deletion Successfull
Press Any Key to Continue...
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 3
25
45
55
35
Press Any Key To Continue...
1.Add Data
2.Delete data
3.Print_All
44
4.Traverse
5.Exit
Enter Your Choice : 2
Enter Data : 55
Deletion Successfull
Press Any Key to Continue...
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 3
25
45
35
Press Any Key To Continue...
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 4
Current Data : 25
1.Previous
2.Next
3.Stop
Enter your choice : 1
Current Data : 35
1.Previous
2.Next
3.Stop
Enter your choice : 1
Current Data : 45
45
1.Previous
2.Next
3.Stop
Enter your choice : 1
Current Data : 25
1.Previous
2.Next
3.Stop
Enter your choice : 2
Current Data : 45
1.Previous
2.Next
3.Stop
Enter your choice : 2
Current Data : 35
1.Previous
2.Next
3.Stop
Enter your choice : 5
Invalid Input
Current Data : 35
1.Previous
2.Next
3.Stop
Enter your choice : 3
1.Add Data
2.Delete data
3.Print_All
4.Traverse
5.Exit
Enter Your Choice : 5
RESULT:
The above code has executed successfully.
46
EX. NO : 04
4.POLYNOMIAL ADDITION
AIM:
To create a C program for polynomial addition using linked list.
ALGORITHM:
Step1: Start the program.
Step2: Get the two polynomial equations using linked list.
Step3: Add the two linked list values by matching the power and put the result in the
new list.
Step4: Print the result list in the Descending order of powers.
Step5: End the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct poly{
int value;
int power;
struct poly *next;
}*p1,*p2,*res;
void getdet(short ch){
struct poly *ne=(struct poly *) malloc(sizeof(struct poly));
printf("nEnter the co-efficient : ");
scanf("%d",&ne->value);
printf("nEnter the power : ");
scanf("%d",&ne->power);
ne->next=NULL;
if(ch==1){
if(p1==NULL)
p1=ne;
else{
ne->next=p1;
p1=ne;
}
}
else{
if(p2==NULL)
p2=ne;
47
else{
ne->next=p2;
p2=ne;
}
}
}
void result(){
struct poly *temp1=p1;
struct poly *temp2=p2;
while(temp1!=NULL){
short ch=0;
temp2=p2;
while(temp2!=NULL){
if(temp1->power==temp2->power){
struct poly *ne=(struct poly *) malloc(sizeof(struct poly));
ne->value=temp1->value+temp2->value;
ne->power=temp1->power;
ne->next=NULL;
if(res==NULL)
res=ne;
else{
ne->next=res;
res=ne;
}
ch=1;
}
temp2=temp2->next;
}
if(ch==0){
struct poly *ne=(struct poly *) malloc(sizeof(struct poly));
ne->value=temp1->value;
ne->power=temp1->power;
ne->next=NULL;
if(res==NULL)
res=ne;
else{
ne->next=res;
res=ne;
}
}
temp1=temp1->next;
}
temp2=p2;
while(temp2!=NULL){
48
temp1=res;
short ch=0;
while(temp1!=NULL){
if(temp2->power==temp1->power)
ch=5;
temp1=temp1->next;
}
if(ch!=5){
struct poly *ne=(struct poly *) malloc(sizeof(struct poly));
ne->value=temp2->value;
ne->power=temp2->power;
ne->next=NULL;
if(res==NULL)
res=ne;
else{
ne->next=res;
res=ne;
}
}
temp2=temp2->next;
}
}
void swap(struct poly *a, struct poly *b)
{
int power = a->power;
a->power = b->power;
b->power = power;
power=a->value;
a->value=b->value;
b->value=power;
}
void swapping(struct poly *start){
int swapped, i;
struct poly *ptr1;
struct poly *lptr = NULL;
if (start == NULL)
return;
do
{
swapped = 0;
ptr1 = start;
while (ptr1->next != lptr)
{
if (ptr1->power < ptr1->next->power)
49
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
}
void print_list(struct poly *temp){
swapping(temp);
printf("n");
while(temp!=NULL){
if(temp->next==NULL)
printf("%dx^%d ",temp->value,temp->power);
else{
printf("%dx^%d ",temp->value,temp->power);
if(temp->next->value >=0)
printf("+ ");
}
temp=temp->next;
}
}
int main(){
short n,i;
printf("nEnter how many terms in Equation 1 : ");
scanf("%hd",&n);
printf("nEnter Polynomial Equation 1 : n");
for(i=0;i<n;i++)
getdet(1);
printf("nnEnter how many terms in Equation 2 : ");
scanf("%hd",&n);
printf("nnEnter Polynomial Equation 2 : n");
for(i=0;i<n;i++)
getdet(2);
result();
printf("nPolynomial Equation 1 : n");
print_list(p1);
printf("nPolynomial Equation 2 : n");
print_list(p2);
printf("nnResult : n");
print_list(res);
50
}
OUTPUT:
Enter how many terms in Equation 1 : 3
Enter Polynomial Equation 1 :
Enter the co-efficient : 8
Enter the power : 3
Enter the co-efficient : 5
Enter the power : 2
Enter the co-efficient : 1
Enter the power : 0
Enter how many terms in Equation 2 : 2
Enter Polynomial Equation 2 :
Enter the co-efficient : 7
Enter the power : 1
Enter the co-efficient : -6
Enter the power : 0
Polynomial Equation 1 :
8x^3 + 5x^2 + 1x^0
Polynomial Equation 2 :
7x^1 -6x^0
Result :
8x^3 + 5x^2 + 7x^1 -5x^0
RESULT:
The above code has executed successfully.
51
EX. NO : 05
5.INFIX TO POSTFIX CONVERSION
AIM:
To create a C program to perform infix to postfix conversion using stack operations.
ALGORITHM:
Step1: Start the program.
Step2: Get the expression from the user.
Step3: Convert into postfix expression using stack.
Step4: Print the postfix expression accordingly.
Step5: End the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
char stack[100];
int top=-1,top1=-1,stack1[50],n1,n2,n3;
void push(char x){
top++;
stack[top]=x;
}
void pop(){
printf("%c",stack[top]);
top--;
}
int priority(char x){
if(x=='*' || x=='/')
return 3;
if(x=='+' || x=='-')
return 2;
}
void convert(char x){
if((x>=97&&x<=122)||(x>=65&&x<=90))
printf("%c",x);
else if(x=='(')
push(x);
else if(x==')'){
while(stack[top]!='(')
pop();
top--;
}
else if(x=='+' || x=='-' || x=='*' || x=='/'){
52
if(top<=-1)
push(x);
else if(stack[top]=='(')
push(x);
else if (priority(x)>priority(stack[top]))
push(x);
else{
while(1){
pop();
if((top<=-1)
||(stack[top]=='(')||(priority(x)>priority(stack[top])))
break;
}
push(x);
}
}
else{
printf("nna***Undefined Symbol in expression***");
exit(1);
}
}
void pushing(int x){
top1++;
stack1[top1]=x;
}
int popping(){
return stack1[top1--];
}
int main(){
char exp[100];
short i,j,ch;
fflush(stdin);
printf("nEnter Expression : ");
scanf("%s",exp);
j=strlen(exp);
for(i=0;i<j;i++)
convert(exp[i]);
while(top!=-1)
pop();
return 0;
}
53
OUTPUT:
Enter Expression : A+B+C*D/E*F
AB+CD*E/F*+
RESULT:
The above code has executed successfully.
54
EX. NO : 06
6.POSTFIX EVALUATION
AIM:
To create a C program to perform postfix evaluation using stack operations.
ALGORITHM:
Step1: Start the program.
Step2: Get the expression from the user.
Step3: Convert into postfix expression using stack.
Step4: Print the postfix expression accordingly.
Step5: End the program.
PROGRAM:
#include<stdio.h>
#include<string.h>
int top=-1,n1,n2,n3, stack[50],ch=2;
void push(int a){
stack[++top]=a;
}
int pop(){
if(top==-1){
printf("naInvalid Expression");
ch=3;
}
else
return stack[top--];
}
evaluate(char x){
if(isdigit(x))
push((int)(x) - 48);
else{
n1=pop();
n2=pop();
if(x=='+')
n3=n1+n2;
else if(x=='-')
n3=n2-n1;
else if(x=='*')
n3=n1*n2;
else
n3=n2/n1;
push(n3);
55
}
}
int main(){
char s[100];
printf("nEnter the postfix Expression : ");
scanf("%[^n]",s);
short i;
for(i=0;i<strlen(s);i++){
if(s[i]==' ')
continue;
else
evaluate(s[i]);
}
if(ch!=3)
printf("nResult : %d",n3);
return 0;
}
OUTPUT:
Enter the postfix Expression : 5 7 +
Result : 12
RESULT:
The above program is executed successfully.
56
EX. NO : 07
DATE :
7.REVERSE QUEUE USING STACK
AIM:
To create a C program to perform reversing queue using stacks.
ALGORITHM:
Step1: Start the program.
Step2: Get the elements from the user and store it in the queue.
Step3: Reverse the queue using stack.
Step4: Print the reversed queue.
Step5: End the program.
PROGRAM:
#include<stdio.h>
#define SIZE 25
int queue[SIZE],stack[SIZE];
short front=-1,rear=-1,i,top=-1;
void push(int a){
stack[++top]=a;
}
int pop(){
return stack[top--];
}
void enqueue_arr(int a){
if(rear==SIZE-1){
printf("naQueue is FULL");
return;
}
else if(rear==-1)
front=0;
queue[++rear]=a;
}
int dequeue_arr(){
int m;
if(front==-1||front>rear)
printf("naQueue is Emptyn");
else if(front==rear){
m=queue[front];
front=rear=-1;
}
else{
m=queue[front];
front++;
57
}
return m;
}
void display_arr(){
if(front==-1||front>rear)
printf("naQueue is Emptyn");
else
{
for(i=front;i<=rear; i++)
printf("%d t",queue[i]);
}
}
int main(){
short ch;
printf("nEnter how many terms : ");
scanf("%hd",&ch);
for(i=0;i<ch;i++){
int a;
printf("nEnter Data Value %d : ",(i+1));
scanf("%d",&a);
enqueue_arr(a);
}
printf("n *****Main Queue***** nn ");
display_arr();
for(i=0;i<ch;i++)
push(dequeue_arr());
for(i=0;i<ch;i++)
enqueue_arr(pop());
printf("n *****Reversed Queue*****nn");
display_arr();
return 0;
}
OUTPUT:
Enter how many terms : 5
Enter Data Value 1 : 12
Enter Data Value 2 : 24
Enter Data Value 3 : 36
Enter Data Value 4 : 48
58
Enter Data Value 5 : 60
*****Main Queue*****
12 24 36 48 60
*****Reversed Queue*****
60 48 36 24 12
RESULT:
The above code has executed successfully.
59
EX. NO : 08
8.BINARY SEARCH TREE TRAVERSALS
AIM:
To create a C program to perform binary search tree traversals.
ALGORITHM:
Step1: Start the program.
Step2: Get the elements from the user and create a tree.
Step3: Perform inorder,preorder and postordertraversals.
Step4: End the program.
PROGRAM:
#include <stdio.h>
#include<malloc.h>
struct Btree
{
int data;
struct Btree *left;
struct Btree *right;
};
struct Btree *create(int n, struct Btree *tl, struct Btree *tr)
{
struct Btree *t1;
t1=(struct Btree *)malloc(sizeof(struct Btree));
t1->data=n;
t1->left = tl;
t1->right = tr;
return t1;
}
struct Btree *insert(int x, struct Btree *root)
{
struct Btree *t1,*p1,*p2;
t1=create(x,NULL,NULL);
if(root == NULL)
return t1;
p1=root;
while(1)
{
if(t1->data < p1->data )
{
60
if(p1->left!=NULL)
{
p1=p1->left;
}
else
{
p1->left = t1;
break;
}
}
else
{
if(p1->right!=NULL)
{
p1=p1->right;
}
else
{
p1->right = t1;
break;
}
}
}
return root;
}
void inorder(struct Btree *T)
{
if (T==NULL) return;
inorder(T->left);
printf(" %d ",T->data);
inorder(T->right);
}
void preorder(struct Btree *T)
{
if (T==NULL) return;
printf(" %d ",T->data);
preorder(T->left);
preorder(T->right);
}
void postorder(struct Btree *T)
{
if (T==NULL) return;
61
postorder(T->left);
postorder(T->right);
printf(" %d ",T->data);
}
void main()
{
struct Btree *root = NULL;
root=insert(10,root);
printf(" n inorder ...");
inorder(root);
root=insert(20,root);
root=insert(5,root);
root=insert(7,root);
root=insert(4,root);
root=insert(15,root);
root=insert(14,root);
root=insert(22,root);
root=insert(2,root);
printf(" n preorder ...");
preorder(root);
printf(" n inorder ...");
inorder(root);
printf(" n postorder ...");
postorder(root);
printf(" n inorder ...");
inorder(root);
}
OUTPUT:
RESULT:
The above code has executed successfully.
62
EX. NO : 09
9.GRAPH TRAVERSALS
AIM:
To create a C program to perform graph traversals.
ALGORITHM:
Step1: Start the program.
Step2: Get the elements from the user in the adjacency matrix.
Step3: Perform breath first search and depth first search for the graph.
Step4: Print the result
Step5: End the program.
PROGRAM:
#include <stdio.h>
int adj [5][5] = { 0,0,1,1,0,
1,0,0,1,1,
0,1,0,0,0,
0,0,0,0,0,
0,0,1,1,0
};
int Visited[5]={0};
int n=5;
void dfs(int node)
{
int i;
Visited[node] = 1;
printf(" Visited : %d", node);
for(i=0;i<n;i++)
{
if( (adj[node][i] == 1) && (Visited[i] == 0))
dfs(i);
}
}
int main()
{
int i,n=5;
for(i=0;i<n;i++)
if( Visited[i] == 0)
63
dfs(i);
}
// Breath First Search
#include <stdio.h>
int adj [5][5] = { 0,0,1,0,0,
1,0,0,0,1,
0,1,0,0,0,
0,0,0,0,0,
0,0,1,1,0
};
int Visited[5]={0};
int Queue[10],f=-1,r=-1;
int n=5;
void bfs(int node)
{
int i,x;
if( Visited[node] == 1) return;
Visited[node] = 1;
printf(" Visited : %d", node);
Queue[++r]=node;
++f;
for(i=0;i<n;i++)
if((adj[node][i] == 1) && (Visited[i] == 0))
{
Visited[i] = 1;
printf(" Visited : %d", i);
Queue[++r]=i;
}
while( f != (r+1))
{
x=Queue[f];
f++;
bfs(x);
}
}
int main()
{
int i,n=5;
64
for(i=0;i<n;i++)
if(Visited[i] == 0)
bfs(i);
}
OUTPUT:
DFS
BFS
RESULT:
The above code has executed successfully.
65
EX. NO :10
10.RED BLACK TREE
AIM:
To create a C program to perform red black tree insertion, deletion and display.
ALGORITHM:
Step1: Start the program.
Step2: Get the elements from the user and create a red black tree.
Step3: Perform insertion deletion and display.
Step4: End the program.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node {
int d; // data
int c; // 1-red, 0-black
struct node* p; // parent
struct node* r; // right-child
struct node* l; // left child
};
struct node* root = NULL;
struct node* bst(struct node* trav,struct node* temp)
{
// If the tree is empty,
// return a new node
if (trav == NULL)
return temp;
// Otherwise recur down the tree
if (temp->d <trav->d)
{
trav->l = bst(trav->l, temp);
trav->l->p = trav;
}
else if (temp->d >trav->d)
{
trav->r = bst(trav->r, temp);
trav->r->p = trav;
}
return trav;
}
void rightrotate(struct node* temp)
{
struct node* left = temp->l;
temp->l = left->r;
if (temp->l)
temp->l->p = temp;
66
left->p = temp->p;
if (!temp->p)
root = left;
else if (temp == temp->p->l)
temp->p->l = left;
else
temp->p->r = left;
left->r = temp;
temp->p = left;
}
void leftrotate(struct node* temp)
{
struct node* right = temp->r;
temp->r = right->l;
if (temp->r)
temp->r->p = temp;
right->p = temp->p;
if (!temp->p)
root = right;
else if (temp == temp->p->l)
temp->p->l = right;
else
temp->p->r = right;
right->l = temp;
temp->p = right;
}
void fixup(struct node* root, struct node* pt)
{
struct node* parent_pt = NULL;
struct node* grand_parent_pt = NULL;
while ((pt != root) && (pt->c != 0)
&& (pt->p->c == 1))
{
parent_pt = pt->p;
grand_parent_pt = pt->p->p;
if (parent_pt == grand_parent_pt->l)
{
struct node* uncle_pt = grand_parent_pt->r;
if (uncle_pt != NULL &&uncle_pt->c == 1)
{
grand_parent_pt->c = 1;
parent_pt->c = 0;
uncle_pt->c = 0;
pt = grand_parent_pt;
}
else {
if (pt == parent_pt->r) {
leftrotate(parent_pt);
pt = parent_pt;
67
parent_pt = pt->p;
}
rightrotate(grand_parent_pt);
int t = parent_pt->c;
parent_pt->c = grand_parent_pt->c;
grand_parent_pt->c = t;
pt = parent_pt;
}
}
else {
struct node* uncle_pt = grand_parent_pt->l;
if ((uncle_pt != NULL) && (uncle_pt->c == 1))
{
grand_parent_pt->c = 1;
parent_pt->c = 0;
uncle_pt->c = 0;
pt = grand_parent_pt;
}
else {
if (pt == parent_pt->l) {
rightrotate(parent_pt);
pt = parent_pt;
parent_pt = pt->p;
}
leftrotate(grand_parent_pt);
int t = parent_pt->c;
parent_pt->c = grand_parent_pt->c;
grand_parent_pt->c = t;
pt = parent_pt;
}
}
}
root->c = 0;
}
void inorder(struct node* trav)
{
if (trav == NULL)
return;
inorder(trav->l);
printf("%d ", trav->d);
inorder(trav->r);
}
int main()
{
int n = 7;
int a[7] = { 7, 6, 5, 4, 3, 2, 1 };
68
for (int i = 0; i< n; i++) {
struct node* temp= (struct node*)malloc(sizeof(struct node));
temp->r = NULL;
temp->l = NULL;
temp->p = NULL;
temp->d = a[i];
temp->c = 1;
root = bst(root, temp);
fixup(root, temp);
}
printf("Inorder Traversal of Created Treen");
inorder(root);
return 0;
}
OUTPUT:
RESULT:
The above code has executed successfully.
69
EX. NO :11
11.BUBBLE SORT AND SHELL SORT
AIM:
To create a C program to perform bubble sort and shell sort.
ALGORITHM:
Step1: Start the program.
Step2: Get the elements from the user.
Step3: Perform sorting using bubble sort and shell sort.
Step4: Print the result
Step5: End the program.
PROGRAM:
#include<stdio.h>
void print(int a[], int n) //function to print array elements
{
int i;
for(i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
}
void bubble(int a[], int n) // function to implement bubble sort
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void main ()
{
int i, j,temp;
int a[5] = { 10, 35, 32, 13, 26};
int n = sizeof(a)/sizeof(a[0]);
70
printf("Before sorting array elements are - n");
print(a, n);
bubble(a, n);
printf("nAfter sorting array elements are - n");
print(a, n);
}
// Shell sort
#include <stdio.h>
void shellsort(int arr[], int num)
{
int i, j, k, tmp;
for (i = num / 2; i> 0; i = i / 2)
{
for (j = i; j <num; j++)
{
for(k = j - i; k >= 0; k = k - i)
{
if (arr[k+i] >= arr[k])
break;
else
{
tmp = arr[k];
arr[k] = arr[k+i];
arr[k+i] = tmp;
}
}
}
}
}
int main()
{
int arr[30];
int k, num;
printf("Enter total no. of elements : ");
scanf("%d", &num);
printf("nEnter %d numbers: ", num);
for (k = 0 ; k <num; k++)
{
scanf("%d", &arr[k]);
}
shellsort(arr, num);
printf("n Sorted array is: ");
71
for (k = 0; k <num; k++)
printf("%d ", arr[k]);
return 0;
}
OUTPUT:
Bubble Sort
Shell Sort
RESULT:
The above code has executed successfully.
72
EX. NO :12
12.Hash Table
AIM:
To create a C program to perform Hash table operations.
ALGORITHM:
Step1: Start the program.
Step2: Get the elements from the user.
Step3: Perform hash table insertion, deletion, display operations.
Step4: Print the result
Step5: End the program.
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 20
struct DataItem {
int data;
int key;
};
struct DataItem* hashArray[SIZE];
struct DataItem* dummyItem;
struct DataItem* item;
int hashCode(int key) {
return key % SIZE;
}
struct DataItem *search(int key) {
//get the hash
int hashIndex = hashCode(key);
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
++hashIndex;
//wrap around the table
73
hashIndex %= SIZE;
}
return NULL;
}
void insert(int key,int data) {
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty or deleted cell
while(hashArray[hashIndex] != NULL &&hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
struct DataItem* delete(struct DataItem* item) {
int key = item->key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];
//assign a dummy item at deleted position
hashArray[hashIndex] = dummyItem;
return temp;
}
74
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
void display() {
int i = 0;
for(i = 0; i<SIZE; i++) {
if(hashArray[i] != NULL)
printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);
else
printf(" ~~ ");
}
printf("n");
}
int main() {
dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));
dummyItem->data = -1;
dummyItem->key = -1;
insert(1, 20);
insert(2, 70);
insert(42, 80);
insert(4, 25);
insert(12, 44);
insert(14, 32);
insert(17, 11);
insert(13, 78);
insert(37, 97);
display();
item = search(37);
if(item != NULL) {
printf("Element found: %dn", item->data);
75
} else {
printf("Element not foundn");
}
delete(item);
item = search(37);
if(item != NULL) {
printf("Element found: %dn", item->data);
} else {
printf("Element not foundn");
}
}
OUTPUT:
~~ (1,20) (2,70) (42,80) (4,25) ~~ ~~ ~~ ~~ ~~ ~~ ~~
(12,44) (13,78) (14,32) ~~ ~~ (17,11) (37,97) ~~
Element found: 97
Element not found
RESULT:
The above code has executed successfully

More Related Content

Similar to DS LAB MANUAL.pdf

Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Aman Deep
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)Aman Deep
 
how do i expand upon selectionSortType so that you are no longer bou.pdf
how do i expand upon selectionSortType so that you are no longer bou.pdfhow do i expand upon selectionSortType so that you are no longer bou.pdf
how do i expand upon selectionSortType so that you are no longer bou.pdffootwearpark
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manualSyed Mustafa
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdfanujmkt
 
Sample Program file class 11.pdf
Sample Program file class 11.pdfSample Program file class 11.pdf
Sample Program file class 11.pdfYashMirge2
 
write the TODO part of the program.docx
write the TODO part of the program.docxwrite the TODO part of the program.docx
write the TODO part of the program.docxannetnash8266
 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSaraswathiRamalingam
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
C PROGRAMS - SARASWATHI RAMALINGAM
C PROGRAMS - SARASWATHI RAMALINGAMC PROGRAMS - SARASWATHI RAMALINGAM
C PROGRAMS - SARASWATHI RAMALINGAMSaraswathiRamalingam
 
C language concept with code apna college.pdf
C language concept with code apna college.pdfC language concept with code apna college.pdf
C language concept with code apna college.pdfmhande899
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docxjosies1
 

Similar to DS LAB MANUAL.pdf (20)

Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
 
how do i expand upon selectionSortType so that you are no longer bou.pdf
how do i expand upon selectionSortType so that you are no longer bou.pdfhow do i expand upon selectionSortType so that you are no longer bou.pdf
how do i expand upon selectionSortType so that you are no longer bou.pdf
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
Sample Program file class 11.pdf
Sample Program file class 11.pdfSample Program file class 11.pdf
Sample Program file class 11.pdf
 
write the TODO part of the program.docx
write the TODO part of the program.docxwrite the TODO part of the program.docx
write the TODO part of the program.docx
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
 
Computer Practical XII
Computer Practical XIIComputer Practical XII
Computer Practical XII
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
C PROGRAMS - SARASWATHI RAMALINGAM
C PROGRAMS - SARASWATHI RAMALINGAMC PROGRAMS - SARASWATHI RAMALINGAM
C PROGRAMS - SARASWATHI RAMALINGAM
 
C arrays
C arraysC arrays
C arrays
 
C language concept with code apna college.pdf
C language concept with code apna college.pdfC language concept with code apna college.pdf
C language concept with code apna college.pdf
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 

More from Selvaraj Seerangan

Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...Selvaraj Seerangan
 
END SEM _ Design Thinking _ 16 Templates.pptx
END SEM _ Design Thinking _ 16 Templates.pptxEND SEM _ Design Thinking _ 16 Templates.pptx
END SEM _ Design Thinking _ 16 Templates.pptxSelvaraj Seerangan
 
Design Thinking _ Complete Templates.pptx
Design Thinking _ Complete Templates.pptxDesign Thinking _ Complete Templates.pptx
Design Thinking _ Complete Templates.pptxSelvaraj Seerangan
 
CAT 3 _ List of Templates.pptx
CAT 3 _ List of Templates.pptxCAT 3 _ List of Templates.pptx
CAT 3 _ List of Templates.pptxSelvaraj Seerangan
 
[PPT] _ Unit 3 _ Experiment.pptx
[PPT] _ Unit 3 _ Experiment.pptx[PPT] _ Unit 3 _ Experiment.pptx
[PPT] _ Unit 3 _ Experiment.pptxSelvaraj Seerangan
 
CAT 2 _ List of Templates.pptx
CAT 2 _ List of Templates.pptxCAT 2 _ List of Templates.pptx
CAT 2 _ List of Templates.pptxSelvaraj Seerangan
 
Design Thinking - Empathize Phase
Design Thinking - Empathize PhaseDesign Thinking - Empathize Phase
Design Thinking - Empathize PhaseSelvaraj Seerangan
 
18CSL51 - Network Lab Manual.pdf
18CSL51 - Network Lab Manual.pdf18CSL51 - Network Lab Manual.pdf
18CSL51 - Network Lab Manual.pdfSelvaraj Seerangan
 
CAT 1 _ List of Templates.pptx
CAT 1 _ List of Templates.pptxCAT 1 _ List of Templates.pptx
CAT 1 _ List of Templates.pptxSelvaraj Seerangan
 
[PPT] _ UNIT 1 _ COMPLETE.pptx
[PPT] _ UNIT 1 _ COMPLETE.pptx[PPT] _ UNIT 1 _ COMPLETE.pptx
[PPT] _ UNIT 1 _ COMPLETE.pptxSelvaraj Seerangan
 
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdfSelvaraj Seerangan
 
[PPT] _ Unit 2 _ Complete PPT.pptx
[PPT] _ Unit 2 _ Complete PPT.pptx[PPT] _ Unit 2 _ Complete PPT.pptx
[PPT] _ Unit 2 _ Complete PPT.pptxSelvaraj Seerangan
 

More from Selvaraj Seerangan (20)

Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
 
Unit 5 _ Fog Computing .pdf
Unit 5 _ Fog Computing .pdfUnit 5 _ Fog Computing .pdf
Unit 5 _ Fog Computing .pdf
 
CAT III Answer Key.pdf
CAT III Answer Key.pdfCAT III Answer Key.pdf
CAT III Answer Key.pdf
 
END SEM _ Design Thinking _ 16 Templates.pptx
END SEM _ Design Thinking _ 16 Templates.pptxEND SEM _ Design Thinking _ 16 Templates.pptx
END SEM _ Design Thinking _ 16 Templates.pptx
 
Design Thinking _ Complete Templates.pptx
Design Thinking _ Complete Templates.pptxDesign Thinking _ Complete Templates.pptx
Design Thinking _ Complete Templates.pptx
 
CAT 3 _ List of Templates.pptx
CAT 3 _ List of Templates.pptxCAT 3 _ List of Templates.pptx
CAT 3 _ List of Templates.pptx
 
[PPT] _ Unit 5 _ Evolve.pptx
[PPT] _ Unit 5 _ Evolve.pptx[PPT] _ Unit 5 _ Evolve.pptx
[PPT] _ Unit 5 _ Evolve.pptx
 
[PPT] _ Unit 4 _ Engage.pptx
[PPT] _ Unit 4 _ Engage.pptx[PPT] _ Unit 4 _ Engage.pptx
[PPT] _ Unit 4 _ Engage.pptx
 
[PPT] _ Unit 3 _ Experiment.pptx
[PPT] _ Unit 3 _ Experiment.pptx[PPT] _ Unit 3 _ Experiment.pptx
[PPT] _ Unit 3 _ Experiment.pptx
 
CAT 2 _ List of Templates.pptx
CAT 2 _ List of Templates.pptxCAT 2 _ List of Templates.pptx
CAT 2 _ List of Templates.pptx
 
Design Thinking - Empathize Phase
Design Thinking - Empathize PhaseDesign Thinking - Empathize Phase
Design Thinking - Empathize Phase
 
CAT-II Answer Key.pdf
CAT-II Answer Key.pdfCAT-II Answer Key.pdf
CAT-II Answer Key.pdf
 
PSP LAB MANUAL.pdf
PSP LAB MANUAL.pdfPSP LAB MANUAL.pdf
PSP LAB MANUAL.pdf
 
18CSL51 - Network Lab Manual.pdf
18CSL51 - Network Lab Manual.pdf18CSL51 - Network Lab Manual.pdf
18CSL51 - Network Lab Manual.pdf
 
CAT 1 _ List of Templates.pptx
CAT 1 _ List of Templates.pptxCAT 1 _ List of Templates.pptx
CAT 1 _ List of Templates.pptx
 
[PPT] _ UNIT 1 _ COMPLETE.pptx
[PPT] _ UNIT 1 _ COMPLETE.pptx[PPT] _ UNIT 1 _ COMPLETE.pptx
[PPT] _ UNIT 1 _ COMPLETE.pptx
 
CAT-1 Answer Key.doc
CAT-1 Answer Key.docCAT-1 Answer Key.doc
CAT-1 Answer Key.doc
 
Unit 3 Complete.pptx
Unit 3 Complete.pptxUnit 3 Complete.pptx
Unit 3 Complete.pptx
 
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
 
[PPT] _ Unit 2 _ Complete PPT.pptx
[PPT] _ Unit 2 _ Complete PPT.pptx[PPT] _ Unit 2 _ Complete PPT.pptx
[PPT] _ Unit 2 _ Complete PPT.pptx
 

Recently uploaded

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 

Recently uploaded (20)

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 

DS LAB MANUAL.pdf

  • 1. KONGU ENGINEERING COLLEGE PERUNDURAI, ERODE 20CSL31 - data StRUCtURES Program : B.E Branch : COMPUTER SCIENCE AND ENGINEERING Semester : III SEMESTER Year : IIND YEAR 2021 KONGU ENGINEERING COLLEGE (Autonomous) PERUNDURAI, ERODE- 638 060 data StRUCtURES LaBORatORY LaB MaNUaL : COMPUTER SCIENCE AND ENGINEERING SEMESTER YEAR 2021 – 2022 (Odd) KONGU ENGINEERING COLLEGE LaBORatORY : COMPUTER SCIENCE AND ENGINEERING
  • 2. List of Experiments Serial No. Name of the Experiment Page No. 1 Implementation of Singly Linked List and Its Operations 02 2 Implementation of Doubly Linked List and Its Operations 19 3 Implementation of Circular Linked List and Its Operations 34 4 Implementation of Polynomial Addition using Linked List 46 5 Infix to Postfix Conversion using Stack ADT 51 6 Postfix Expression using Array of Stack ADT 54 7 Implementation of Reversing Queue using Stack 56 8 Implementation of Binary Search Tree Traversals 59 9 Implementation of Graph Traversal Techniques 62 10 Implementation of Red Block Tree Operations 65 11 Implementation of Bubble Sort and Shell sort 69 12 Implementation of Hash Table Using Array 72
  • 3. 2 EXP NO : 01 1. SINGLY LINKED LIST AIM: To create a C program to perform various operations on Singly Linked List. ALGORITHM: Step1: Start the program. Step2: Get the type of operation from the user to perform. Step3: Do the specified operation given by the user. Step4: Continue until the exit operation. Step5: End the program. PROGRAM: #include<stdio.h> #include<stdlib.h> #include<strings.h> struct student{ int roll; char name[25]; short age; long long int phone; struct student *next; }*head; int rno = 1,pos=0; void printList(struct student *d,int sno){ if (d==NULL) return; printf("%-7d %-11d %-23s %-7hd %-10lldn",++sno,d->roll,d->name,d->age,d- >phone); printList(d->next,sno); } void printRev(struct student *d,int sno){ if (d==NULL) return; printRev(d->next,++sno); printf("%-7d %-11d %-23s %-7hd %-10lldn",sno,d->roll,d->name,d->age,d- >phone); } void add(short ins_ch,char name[25],short age,long long int phone){ int roll;
  • 4. 3 struct student *second = (struct student *) malloc(sizeof(struct student)); second->roll=rno; rno+=1; //Rno assigned for next roll number strcpy(second->name,name); second->age=age; second->phone=phone; second->next=NULL; if (head ==NULL) head = second; else{ if(ins_ch==1){ int i; second->next=head; head=second; } else if(ins_ch==2){ int i,place; struct student *temp; temp = head; if (pos==2){ place=2; second->next=temp->next; temp->next=second; } else{ while(ins_ch){ printf("n Position available between 1 and %d n After which position do you want to add? ",pos); scanf("%d",&place); if((place<1) || (place>=pos)) printf("ann***Requested Position not available...***n"); else ins_ch=0; } for(i=1;i<place;i++){ temp=temp->next; } second->next=temp->next; temp->next=second; } } else{ struct student *temp;
  • 5. 4 temp = head; while(temp!=NULL && temp->next!=NULL) temp = temp->next; temp->next=second; } } pos+=1; printf("nDetails Added Successfully!n"); } short search(int roll){ struct student *temp; temp = head; while(temp!=NULL){ if(temp->roll==roll){ printf("n*************************"); printf("nnRoll no: %d",temp->roll); printf("nName : %s",temp->name); printf("nAge : %hd",temp->age); printf("nPhone : %lld",temp->phone); printf("nn*************************n"); return 2; } temp=temp->next; } printf("nna***Student with roll number %d is not found*** n",roll); return 5; } struct student* copylist(struct student *p){ if(p==NULL) return NULL; else{ struct student *ne=(struct student *) malloc(sizeof(struct student)); ne->roll=p->roll; strcpy(ne->name,p->name); ne->age=p->age; ne->phone=p->phone; ne->next=copylist(p->next); return ne; } } void del_process(int roll){ struct student *temp; temp=head;
  • 6. 5 if(temp->roll==roll){ //First head=head->next; free(temp); } else{ struct student *ptr; ptr=head; while(ptr->roll!=roll){ temp=ptr; ptr= ptr->next; } temp->next=ptr->next;// Middle and last free(ptr); } pos-=1; printf("nDeleted Successfully...n"); } void swap(struct student *a, struct student *b) { int rno = a->roll; a->roll = b->roll; b->roll = rno; char name[25]; strcpy(name,a->name); strcpy(a->name,b->name); strcpy(b->name,name); short age=a->age; a->age=b->age; b->age=age; long long int phone = a->phone; a->phone=b->phone; b->phone=phone; } void swapping(struct student *start){ int swapped, i; struct student *ptr1; struct student *lptr = NULL; if (start == NULL) return; do { swapped = 0; ptr1 = start; while (ptr1->next != lptr)
  • 7. 6 { if (ptr1->roll > ptr1->next->roll) { swap(ptr1, ptr1->next); swapped = 1; } ptr1 = ptr1->next; } lptr = ptr1; } while (swapped); printf("nnSorting Successfull...n"); } int main(){ short ch; do{ printf("ntt MENUntt ******nn1.Addtt2.Printtt3.Searchnn4.Print Reverset5.Copy_Listt6.Sortingnn7.Deletet8.QuitnnEnter your choice : "); scanf("%hd",&ch); switch(ch){ case 1:{ short i,s,ins_ch; while(ch){ printf("nAddingn*******n1.Insert at First n2.Insert at Middle n3.Insert at Endn4.Go Back"); printf("nnEnter your Choice : "); scanf("%hd",&ins_ch); if ((ins_ch ==2) && ((head==NULL)||(head- >next==NULL))) printf("naNot able to insert at middle...List is empty or containing only 1 node"); else if ((ins_ch==1)||(ins_ch==2)||(ins_ch==3)||(ins_ch==4)) ch = 0; else printf("naInsert your choice correctly...n"); } if(ins_ch==4) break; printf("nEnter how many members : "); scanf("%hd",&s); for(i=0;i<s;i++){ char name[25]; long long int phone;
  • 8. 7 short age; fflush(stdin); printf("nEnter name without space : "); scanf("%[^n]",name); printf("nEnter Age : "); scanf("%hd",&age); printf("nEnter Phone Number: "); scanf("%lld",&phone); add(ins_ch,name,age,phone); } break; } case 2:{ if (head==NULL) printf("nList is empty...nn"); else{ printf("nAll Details n***************n"); printf("S.No Roll No Name Age Phonen"); printf("************************************************************** n"); printList(head,0); } break; } case 3:{ int roll; printf("nEnter Roll number : "); scanf("%d",&roll); ch=search(roll); break; } case 4:{ if (head==NULL) printf("nList is empty...nn"); else{ printf("nAll Details n***************n"); printf("S.No Roll No Name Age Phonen"); printf("************************************************************** n"); printRev(head,0);
  • 9. 8 } break; } case 5:{ struct student *copied=copylist(head); printf("nPrinting Copied Listn"); if (copied==NULL) printf("nList is empty...nn"); else{ printf("nAll Details n***************n"); printf("S.No Roll No Name Age Phonen"); printf("************************************************************** n"); printRev(copied,0); } break; } case 6:{ swapping(head); break; } case 7:{ int roll; printf("nEnter Roll number : "); scanf("%d",&roll); ch=search(roll); if(ch==2){ printf("nPress 1 to Confirm (or) 2 to Cancel : "); scanf("%hd",&ch); if(ch==1) del_process(roll); } break; } case 8:{ printf("nn********THANK YOU********"); exit(1); break; } default:{ printf("na *** Sorry, Invalid Operation *** n"); break;
  • 10. 9 } } }while(1); return 0; } OUTPUT: MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 1 Adding ******* 1.Insert at First 2.Insert at Middle 3.Insert at End 4.Go Back Enter your Choice : 1 Enter how many members : 1 Enter name without space : Raja Enter Age : 17 Enter Phone Number: 9361759453 Details Added Successfully! MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting
  • 11. 10 7.Delete 8.Quit Enter your choice : 2 All Details *************** S.No Roll No Name Age Phone ************************************************************** 1 1 Raja 17 9361759453 MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 1 Adding ******* 1.Insert at First 2.Insert at Middle 3.Insert at End 4.Go Back Enter your Choice : 2 Not able to insert at middle...List is empty or containing only 1 node Adding ******* 1.Insert at First 2.Insert at Middle 3.Insert at End 4.Go Back Enter your Choice : 3 Enter how many members : 1 Enter name without space : Kavin
  • 12. 11 Enter Age : 18 Enter Phone Number: 9952864185 Details Added Successfully! MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 2 All Details *************** S.No Roll No Name Age Phone ************************************************************** 1 1 Raja 17 9361759453 2 2 Kavin 18 9952864185 MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 1 Adding ******* 1.Insert at First 2.Insert at Middle 3.Insert at End 4.Go Back Enter your Choice : 2
  • 13. 12 Enter how many members : 1 Enter name without space : Neeraj Enter Age : 17 Enter Phone Number: 9952864180 Details Added Successfully! MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 2 All Details *************** S.No Roll No Name Age Phone ************************************************************** 1 1 Raja 17 9361759453 2 3 Neeraj 17 9952864180 3 2 Kavin 18 9952864185 MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 1 Adding ******* 1.Insert at First
  • 14. 13 2.Insert at Middle 3.Insert at End 4.Go Back Enter your Choice : 2 Enter how many members : 1 Enter name without space : Lohit Enter Age : 18 Enter Phone Number: 897456231 Position available between 1 and 3 After which position do you want to add? 1 Details Added Successfully! MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 2 All Details *************** S.No Roll No Name Age Phone ************************************************************** 1 1 Raja 17 9361759453 2 4 Lohit 18 897456231 3 3 Neeraj 17 9952864180 4 2 Kavin 18 9952864185 MENU ****** 1.Add 2.Print 3.Search
  • 15. 14 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 3 Enter Roll number : 2 ************************* Roll no: 2 Name : Kavin Age : 18 Phone : 9952864185 ************************* MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 3 Enter Roll number : 5 ***Student with roll number 5 is not found*** MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 4
  • 16. 15 All Details *************** S.No Roll No Name Age Phone ************************************************************** 4 2 Kavin 18 9952864185 3 3 Neeraj 17 9952864180 2 4 Lohit 18 897456231 1 1 Raja 17 9361759453 MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 5 Printing Copied List All Details *************** S.No Roll No Name Age Phone ************************************************************** 4 2 Kavin 18 9952864185 3 3 Neeraj 17 9952864180 2 4 Lohit 18 897456231 1 1 Raja 17 9361759453 MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 6 Sorting Successfull...
  • 17. 16 MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 2 All Details *************** S.No Roll No Name Age Phone ************************************************************** 1 1 Raja 17 9361759453 2 2 Kavin 18 9952864185 3 3 Neeraj 17 9952864180 4 4 Lohit 18 897456231 MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 7 Enter Roll number : 4 ************************* Roll no: 4 Name : Lohit Age : 18 Phone : 897456231 ************************* Press 1 to Confirm (or) 2 to Cancel : 2
  • 18. 17 MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 4 All Details *************** S.No Roll No Name Age Phone ************************************************************** 4 4 Lohit 18 897456231 3 3 Neeraj 17 9952864180 2 2 Kavin 18 9952864185 1 1 Raja 17 9361759453 MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit nter your choice : 7 Enter Roll number : 4 ************************* Roll no: 4 Name : Lohit Age : 18 Phone : 897456231 ************************* Press 1 to Confirm (or) 2 to Cancel : 1
  • 19. 18 Deleted Successfully... MENU ****** 1.Add 2.Print 3.Search 4.Print Reverse 5.Copy_List 6.Sorting 7.Delete 8.Quit Enter your choice : 8 ********THANK YOU******** RESULT: The above code has executed successfully.
  • 20. 19 EXP NO : 02 2. DOUBLYLINKED LIST AIM: To create a C program to perform various operations on DoublyLinked List. ALGORITHM: Step1: Start the program. Step2: Get the type of operation from the user to perform. Step3: Do the specified operation given by the user. Step4: Continue until the exit operation. Step5: End the program. PROGRAM: #include<stdio.h> #include<stdlib.h> #include<string.h> short rno=1,pos=0; struct songs{ short sno; char name[40]; short min; short sec; struct songs *pre; struct songs *next; }*head=NULL,*last; void player(); void add_song(); void allsong(); void success_msg(); void play_song(); short search(short id); void delete_song(); void space(short i){ short j=0; for(;j<i;j++) printf("n"); } int main(){ player(); }
  • 21. 20 void player(){ short i; system("CLS"); system("COLOR 4F"); space(17); printf("tttMusic Playernn"); printf("ttt1.Add Song nttt2.Play Song nttt3.Delete Song nttt4.All_Songsnttt5.Exit ntttEnter Your Choice : "); scanf("%hd",&i); switch(i){ case 1: add_song(); break; case 2: play_song(); break; case 3: delete_song(); break; case 4: allsong(); break; case 5: exit(1); break; default: printf("ntttWrong Inputa nntttPress Any Key To Continue..."); getch(); } player(); } void success_msg(){ system("CLS"); system("COLOR 4F"); space(17); printf("tt"Song Added Successfully"nnttPress Any Key to Continue..."); getch(); pos++; } void allsong(){ system("CLS"); system("COLOR 4F"); space(10); struct songs *temp=head;
  • 22. 21 if(temp==NULL){ printf("a No Songs Available nPress any key to continue..."); getch(); return ; } printf("nSong_Id Name Min Secn"); while(temp!=NULL){ printf("%-8hd %-40s %3hd %3hdn",temp->sno,temp->name,temp- >min,temp->sec); temp=temp->next; } printf("nPress Any Key To Continue..."); getch(); } void add_song(){ short i; char name[40]; system("CLS"); system("COLOR 4F"); space(17); printf("tttAdd Songnn"); struct songs *n=(struct songs *) malloc(sizeof(struct songs)); n->sno=rno++; fflush(stdin); printf("nttEnter Song Name : "); scanf("%[^n]",name); strcpy(n->name,name); printf("nttEnter Minutes : "); scanf("%hd",&n->min); printf("nttEnter Seconds : "); scanf("%hd",&n->sec); n->pre=NULL; n->next=NULL; if(pos==0){ head=n; last=n; success_msg(); return ; } if(pos==1){ n->pre=head; head->next=n; success_msg(); last=n;
  • 23. 22 return; } printf("ttt1.Insert At First nttt2.Insert at Middle nttt3.Insert at End nttt4.Back nttt5.Exit ntttEnter Your Choice : "); scanf("%hd",&i); if(i==1){ n->next=head; head->pre=n; head=n; success_msg(); } else if(i==2){ struct songs *temp=head; if(n->sno==3){ n->pre=temp; n->next=temp->next; temp->next->pre=n; temp->next=n; } else{ int place,ins_ch=1; while(ins_ch){ printf("n Position available between 1 and %d n After which position do you want to add? ",pos); scanf("%d",&place); if((place<1) || (place>=pos)) printf("ann***Requested Position not available...***n"); else ins_ch=0; } for(i=1;i<place;i++){ temp=temp->next; } n->pre=temp; n->next=temp->next; temp->next->pre=n; temp->next=n; } success_msg(); } else if(i==3){ last->next=n; n->pre=last;
  • 24. 23 success_msg(); } else if(i==4){ free(n); return; } else{ free(n); exit(1); } } void play_song(){ short i; struct songs *temp=head; if(head==NULL){ system("CLS"); system("COLOR 4F"); space(17); printf("ntNo Songs Available...ntPress Any Key To Continue..."); getch(); return ; } short c=0,d=0; while(1){ system("CLS"); system("COLOR 4F"); space(17); printf("ttCurrently Playingnn"); printf("tt%snn",temp->name); if(temp->pre!=NULL){ printf("ntt1.Previous"); c=1; } if(temp->next!=NULL){ printf("ntt2.Next"); d=1; } printf("ntt3.Stop Playing"); short ch,l=1; do{ printf("nttEnter your Choice : "); scanf("%hd",&ch); if(ch==1){ if(c==0){
  • 25. 24 printf("anttInvalid Input"); l=1; } else l=0; } else if(ch==2){ if(d==0){ printf("anttInvalid Input"); l=1; } else l=0; } else if(ch==3) l=0; else printf("nattInvalid Input"); }while(l); switch(ch){ case 1: temp=temp->pre; break; case 2: temp=temp->next; break; case 3: return; } c=0; d=0; } } short search(short id){ short i; struct songs *temp=head; while(temp!=NULL){ if(temp->sno==id) return 1; temp=temp->next; } short a,id;
  • 26. 25 system("CLS"); system("COLOR 4F"); space(17); printf("ttEnter Song Id : "); scanf("%hd",&id); a=search(id); if(a==0){ printf("nattSong Id Not FoundnttPress Any Key To Continue..."); getch(); return ; } struct songs *temp=head,*ptr; while(temp!=NULL){ if(temp->sno==id) break; temp=temp->next; } if(temp->pre==NULL && temp->next==NULL){ ptr=head; head=NULL; free(ptr); printf("nnttDeletion SuccessfullnttPress Any Key to Continue..."); getch(); pos--; return;
  • 28. 27 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 1 Add Song Enter Song Name : Into Your Arms Enter Minutes : 2 Enter Seconds : 26 "Song Added Successfully" Press Any Key to Continue... Music Player 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 1 Add Song Enter Song Name : Birthday Song Enter Minutes : 5 Enter Seconds : 54 "Song Added Successfully" Press Any Key to Continue... Music Player
  • 29. 28 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 1 Add Song Enter Song Name : God Song Enter Minutes : 4 Enter Seconds : 8 1.Insert At First 2.Insert at Middle 3.Insert at End 4.Back 5.Exit Enter Your Choice : 2 "Song Added Successfully" Press Any Key to Continue... Music Player 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 1 Add Song Enter Song Name : Vinayaga Enter Minutes : 8
  • 30. 29 Enter Seconds : 2 1.Insert At First 2.Insert at Middle 3.Insert at End 4.Back 5.Exit Enter Your Choice : 2 Position available between 1 and 3 After which position do you want to add? 2 "Song Added Successfully" Press Any Key to Continue... Music Player 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 4 Song_Id Name Min Sec 1 Into Your Arms 2 26 3 God Song 4 8 4 Vinayaga 8 2 2 Birthday Song 5 54 Press Any Key To Continue... Music Player 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 2
  • 31. 30 Currently Playing Into Your Arms 2.Next 3.Stop Playing Enter your Choice : 1 Invalid Input Enter your Choice : 2 Currently Playing God Song 1.Previous 2.Next 3.Stop Playing Enter your Choice : 2 Currently Playing Vinayaga 1.Previous 2.Next 3.Stop Playing Enter your Choice : 2 Currently Playing Birthday Song 1.Previous 3.Stop Playing Enter your Choice : 1 Currently Playing
  • 32. 31 Vinayaga 1.Previous 2.Next 3.Stop Playing Enter your Choice : 3 Music Player 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 3 Enter Song Id : 8 Song Id Not Found Press Any Key To Continue... Music Player 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 3 Enter Song Id : 4 Deleted Successfully Music Player 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 4
  • 33. 32 Song_Id Name Min Sec 1 Into Your Arms 2 26 3 God Song 4 8 2 Birthday Song 5 54 Press Any Key To Continue... Music Player 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 3 Enter Song Id : 2 Deleted Successfully Music Player 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 4 Song_Id Name Min Sec 1 Into Your Arms 2 26 3 God Song 4 8 Music Player 1.Add Song 2.Play Song 3.Delete Song 4.All_Songs 5.Exit Enter Your Choice : 5] -------------------------------- Process exited after 2.123 seconds with return value 1
  • 34. 33 Press any key to continue . . . RESULT: The above code has executed successfully.
  • 35. 34 EXP NO : 03 3. CIRCULARLINKED LIST AIM: To create a C program to perform various operations on CircularLinked List. ALGORITHM: Step1: Start the program. Step2: Get the type of operation from the user to perform. Step3: Do the specified operation given by the user. Step4: Traverse the linked list to previous and next nodes. Step5: Continue until the exit operation. Step6: End the program. PROGRAM: #include<stdio.h> #include<stdlib.h> #include<string.h> int pos=0; struct circular{ int data; struct circular *pre; struct circular *next; }*head=NULL,*last; void call(); void adddata(); void alldata(); void success_msg(); short search(int id); void deletedata(); void traverse(); int main(){ call(); } void call(){ short i; printf("nnttt1.Add Data nttt2.Delete data nttt3.Print_Allnttt4.Traverse nttt5.Exit ntttEnter Your Choice : "); scanf("%hd",&i); switch(i){ case 1:
  • 36. 35 adddata(); break; case 2: deletedata(); break; case 3: alldata(); break; case 4: traverse(); break; case 5: exit(1); break; default: printf("ntttWrong Inputa nntttPress Any Key To Continue..."); getch(); } call(); } void traverse(){ struct circular *temp = head; if (temp==NULL){ printf("nna List is Empty "); return ; } short ch; while(1){ printf("nntCurrent Data : %d",temp->data); printf("n1.Previous n2.Next n3.Stop nEnter your choice : "); scanf("%hd",&ch); if(ch==1) temp=temp->pre; else if(ch==2) temp=temp->next; else if(ch==3) return; else printf("naInvalid Input"); } } void success_msg(){ printf("tt"Data Added Successfully"nnttPress Any Key to Continue..."); getch();
  • 37. 36 pos++; } void alldata(){ struct circular *temp=head; if(temp==NULL){ printf("a No Data Available nPress any key to continue..."); getch(); return ; } do{ printf("%dn",temp->data); temp=temp->next; }while(temp!=head); printf("nPress Any Key To Continue..."); getch(); } void adddata(){ int i; printf("tttAdd Datann"); struct circular *n=(struct circular *) malloc(sizeof(struct circular)); printf("nttEnter Data : "); scanf("%d",&n->data); n->pre=NULL; n->next=NULL; if(pos==0){ head=n; last=n; head->next=head; head->pre=head; success_msg(); return ; } if(pos==1){ n->pre=head; n->next=head; head->pre=n; head->next=n; success_msg(); last=n; return; } printf("ttt1.Insert At First nttt2.Insert at Middle nttt3.Insert at End nttt4.Back nttt5.Exit ntttEnter Your Choice : "); scanf("%hd",&i);
  • 38. 37 if(i==1){ last->next=n; n->pre=last; n->next=head; head->pre=n; head=n; success_msg(); } else if(i==2){ struct circular *temp=head; if(pos==2){ n->pre=temp; n->next=temp->next; temp->next->pre=n; temp->next=n; } else{ int place,ins_ch=1; while(ins_ch){ printf("n Position available between 1 and %d n After which position do you want to add? ",pos); scanf("%d",&place); if((place<1) || (place>=pos)) printf("ann***Requested Position not available...***n"); else ins_ch=0; } for(i=1;i<place;i++){ temp=temp->next; } n->pre=temp; n->next=temp->next; temp->next->pre=n; temp->next=n; } success_msg(); } else if(i==3){ last->next=n; n->pre=last; n->next=head; head->pre=n; last=last->next;
  • 39. 38 success_msg(); } else if(i==4){ free(n); return; } else{ free(n); exit(1); } } short search(int id){ struct circular *temp=head; do{ if(temp->data==id) return 1; temp=temp->next; }while(temp!=head); return 0; } void deletedata(){ int a,id; if(head==NULL){ printf("naEmpty..."); return ; } printf("ttEnter Data : "); scanf("%d",&id); a=search(id); if(a==0){ printf("nattData Not FoundnttPress Any Key To Continue..."); getch(); return ; } struct circular *temp=head,*ptr; do{ if(temp->data==id) break; temp=temp->next; }while(temp!=head); if(temp->pre==head && temp->next==head){ ptr=head; head=NULL;
  • 40. 39 free(ptr); printf("nnttDeletion SuccessfullnttPress Any Key to Continue..."); getch(); pos--; return; } if(temp==head && temp->next!=head){ ptr=head; head=ptr->next; head->pre=last; last->next=head; free(ptr); } else if(temp==last && temp->pre!=head){ ptr=temp; temp->pre->next=head; last=ptr->pre; head->pre=last; free(ptr); } else{ ptr=temp; temp->pre->next=ptr->next; temp->next->pre=ptr->pre; free(ptr); } printf("nnttDeletion SuccessfullnttPress Any Key to Continue..."); getch(); pos--; } OUTPUT: 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 1 Add Data Enter Data : 25
  • 41. 40 "Data Added Successfully" Press Any Key to Continue... 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 1 Add Data Enter Data : 35 "Data Added Successfully" Press Any Key to Continue... 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 1 Add Data Enter Data : 45 1.Insert At First 2.Insert at Middle 3.Insert at End 4.Back 5.Exit Enter Your Choice : 2 "Data Added Successfully" Press Any Key to Continue... 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 1
  • 42. 41 Add Data Enter Data : 55 1.Insert At First 2.Insert at Middle 3.Insert at End 4.Back 5.Exit Enter Your Choice : 2 Position available between 1 and 3 After which position do you want to add? 3 ***Requested Position not available...*** Position available between 1 and 3 After which position do you want to add? 2 "Data Added Successfully" Press Any Key to Continue... 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 1 Add Data Enter Data : 65 1.Insert At First 2.Insert at Middle 3.Insert at End 4.Back 5.Exit Enter Your Choice : 1 "Data Added Successfully" Press Any Key to Continue... 1.Add Data
  • 43. 42 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 1 Add Data Enter Data : 75 1.Insert At First 2.Insert at Middle 3.Insert at End 4.Back 5.Exit Enter Your Choice : 3 "Data Added Successfully" Press Any Key to Continue... 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 3 65 25 45 55 35 75 Press Any Key To Continue... 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 2 Enter Data : 75 Deletion Successfull
  • 44. 43 Press Any Key to Continue... 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 3 65 25 45 55 35 Press Any Key To Continue... 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 2 Enter Data : 65 Deletion Successfull Press Any Key to Continue... 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 3 25 45 55 35 Press Any Key To Continue... 1.Add Data 2.Delete data 3.Print_All
  • 45. 44 4.Traverse 5.Exit Enter Your Choice : 2 Enter Data : 55 Deletion Successfull Press Any Key to Continue... 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 3 25 45 35 Press Any Key To Continue... 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 4 Current Data : 25 1.Previous 2.Next 3.Stop Enter your choice : 1 Current Data : 35 1.Previous 2.Next 3.Stop Enter your choice : 1 Current Data : 45
  • 46. 45 1.Previous 2.Next 3.Stop Enter your choice : 1 Current Data : 25 1.Previous 2.Next 3.Stop Enter your choice : 2 Current Data : 45 1.Previous 2.Next 3.Stop Enter your choice : 2 Current Data : 35 1.Previous 2.Next 3.Stop Enter your choice : 5 Invalid Input Current Data : 35 1.Previous 2.Next 3.Stop Enter your choice : 3 1.Add Data 2.Delete data 3.Print_All 4.Traverse 5.Exit Enter Your Choice : 5 RESULT: The above code has executed successfully.
  • 47. 46 EX. NO : 04 4.POLYNOMIAL ADDITION AIM: To create a C program for polynomial addition using linked list. ALGORITHM: Step1: Start the program. Step2: Get the two polynomial equations using linked list. Step3: Add the two linked list values by matching the power and put the result in the new list. Step4: Print the result list in the Descending order of powers. Step5: End the program. PROGRAM: #include<stdio.h> #include<stdlib.h> struct poly{ int value; int power; struct poly *next; }*p1,*p2,*res; void getdet(short ch){ struct poly *ne=(struct poly *) malloc(sizeof(struct poly)); printf("nEnter the co-efficient : "); scanf("%d",&ne->value); printf("nEnter the power : "); scanf("%d",&ne->power); ne->next=NULL; if(ch==1){ if(p1==NULL) p1=ne; else{ ne->next=p1; p1=ne; } } else{ if(p2==NULL) p2=ne;
  • 48. 47 else{ ne->next=p2; p2=ne; } } } void result(){ struct poly *temp1=p1; struct poly *temp2=p2; while(temp1!=NULL){ short ch=0; temp2=p2; while(temp2!=NULL){ if(temp1->power==temp2->power){ struct poly *ne=(struct poly *) malloc(sizeof(struct poly)); ne->value=temp1->value+temp2->value; ne->power=temp1->power; ne->next=NULL; if(res==NULL) res=ne; else{ ne->next=res; res=ne; } ch=1; } temp2=temp2->next; } if(ch==0){ struct poly *ne=(struct poly *) malloc(sizeof(struct poly)); ne->value=temp1->value; ne->power=temp1->power; ne->next=NULL; if(res==NULL) res=ne; else{ ne->next=res; res=ne; } } temp1=temp1->next; } temp2=p2; while(temp2!=NULL){
  • 49. 48 temp1=res; short ch=0; while(temp1!=NULL){ if(temp2->power==temp1->power) ch=5; temp1=temp1->next; } if(ch!=5){ struct poly *ne=(struct poly *) malloc(sizeof(struct poly)); ne->value=temp2->value; ne->power=temp2->power; ne->next=NULL; if(res==NULL) res=ne; else{ ne->next=res; res=ne; } } temp2=temp2->next; } } void swap(struct poly *a, struct poly *b) { int power = a->power; a->power = b->power; b->power = power; power=a->value; a->value=b->value; b->value=power; } void swapping(struct poly *start){ int swapped, i; struct poly *ptr1; struct poly *lptr = NULL; if (start == NULL) return; do { swapped = 0; ptr1 = start; while (ptr1->next != lptr) { if (ptr1->power < ptr1->next->power)
  • 50. 49 { swap(ptr1, ptr1->next); swapped = 1; } ptr1 = ptr1->next; } lptr = ptr1; } while (swapped); } void print_list(struct poly *temp){ swapping(temp); printf("n"); while(temp!=NULL){ if(temp->next==NULL) printf("%dx^%d ",temp->value,temp->power); else{ printf("%dx^%d ",temp->value,temp->power); if(temp->next->value >=0) printf("+ "); } temp=temp->next; } } int main(){ short n,i; printf("nEnter how many terms in Equation 1 : "); scanf("%hd",&n); printf("nEnter Polynomial Equation 1 : n"); for(i=0;i<n;i++) getdet(1); printf("nnEnter how many terms in Equation 2 : "); scanf("%hd",&n); printf("nnEnter Polynomial Equation 2 : n"); for(i=0;i<n;i++) getdet(2); result(); printf("nPolynomial Equation 1 : n"); print_list(p1); printf("nPolynomial Equation 2 : n"); print_list(p2); printf("nnResult : n"); print_list(res);
  • 51. 50 } OUTPUT: Enter how many terms in Equation 1 : 3 Enter Polynomial Equation 1 : Enter the co-efficient : 8 Enter the power : 3 Enter the co-efficient : 5 Enter the power : 2 Enter the co-efficient : 1 Enter the power : 0 Enter how many terms in Equation 2 : 2 Enter Polynomial Equation 2 : Enter the co-efficient : 7 Enter the power : 1 Enter the co-efficient : -6 Enter the power : 0 Polynomial Equation 1 : 8x^3 + 5x^2 + 1x^0 Polynomial Equation 2 : 7x^1 -6x^0 Result : 8x^3 + 5x^2 + 7x^1 -5x^0 RESULT: The above code has executed successfully.
  • 52. 51 EX. NO : 05 5.INFIX TO POSTFIX CONVERSION AIM: To create a C program to perform infix to postfix conversion using stack operations. ALGORITHM: Step1: Start the program. Step2: Get the expression from the user. Step3: Convert into postfix expression using stack. Step4: Print the postfix expression accordingly. Step5: End the program. PROGRAM: #include<stdio.h> #include<stdlib.h> #include<strings.h> char stack[100]; int top=-1,top1=-1,stack1[50],n1,n2,n3; void push(char x){ top++; stack[top]=x; } void pop(){ printf("%c",stack[top]); top--; } int priority(char x){ if(x=='*' || x=='/') return 3; if(x=='+' || x=='-') return 2; } void convert(char x){ if((x>=97&&x<=122)||(x>=65&&x<=90)) printf("%c",x); else if(x=='(') push(x); else if(x==')'){ while(stack[top]!='(') pop(); top--; } else if(x=='+' || x=='-' || x=='*' || x=='/'){
  • 53. 52 if(top<=-1) push(x); else if(stack[top]=='(') push(x); else if (priority(x)>priority(stack[top])) push(x); else{ while(1){ pop(); if((top<=-1) ||(stack[top]=='(')||(priority(x)>priority(stack[top]))) break; } push(x); } } else{ printf("nna***Undefined Symbol in expression***"); exit(1); } } void pushing(int x){ top1++; stack1[top1]=x; } int popping(){ return stack1[top1--]; } int main(){ char exp[100]; short i,j,ch; fflush(stdin); printf("nEnter Expression : "); scanf("%s",exp); j=strlen(exp); for(i=0;i<j;i++) convert(exp[i]); while(top!=-1) pop(); return 0; }
  • 54. 53 OUTPUT: Enter Expression : A+B+C*D/E*F AB+CD*E/F*+ RESULT: The above code has executed successfully.
  • 55. 54 EX. NO : 06 6.POSTFIX EVALUATION AIM: To create a C program to perform postfix evaluation using stack operations. ALGORITHM: Step1: Start the program. Step2: Get the expression from the user. Step3: Convert into postfix expression using stack. Step4: Print the postfix expression accordingly. Step5: End the program. PROGRAM: #include<stdio.h> #include<string.h> int top=-1,n1,n2,n3, stack[50],ch=2; void push(int a){ stack[++top]=a; } int pop(){ if(top==-1){ printf("naInvalid Expression"); ch=3; } else return stack[top--]; } evaluate(char x){ if(isdigit(x)) push((int)(x) - 48); else{ n1=pop(); n2=pop(); if(x=='+') n3=n1+n2; else if(x=='-') n3=n2-n1; else if(x=='*') n3=n1*n2; else n3=n2/n1; push(n3);
  • 56. 55 } } int main(){ char s[100]; printf("nEnter the postfix Expression : "); scanf("%[^n]",s); short i; for(i=0;i<strlen(s);i++){ if(s[i]==' ') continue; else evaluate(s[i]); } if(ch!=3) printf("nResult : %d",n3); return 0; } OUTPUT: Enter the postfix Expression : 5 7 + Result : 12 RESULT: The above program is executed successfully.
  • 57. 56 EX. NO : 07 DATE : 7.REVERSE QUEUE USING STACK AIM: To create a C program to perform reversing queue using stacks. ALGORITHM: Step1: Start the program. Step2: Get the elements from the user and store it in the queue. Step3: Reverse the queue using stack. Step4: Print the reversed queue. Step5: End the program. PROGRAM: #include<stdio.h> #define SIZE 25 int queue[SIZE],stack[SIZE]; short front=-1,rear=-1,i,top=-1; void push(int a){ stack[++top]=a; } int pop(){ return stack[top--]; } void enqueue_arr(int a){ if(rear==SIZE-1){ printf("naQueue is FULL"); return; } else if(rear==-1) front=0; queue[++rear]=a; } int dequeue_arr(){ int m; if(front==-1||front>rear) printf("naQueue is Emptyn"); else if(front==rear){ m=queue[front]; front=rear=-1; } else{ m=queue[front]; front++;
  • 58. 57 } return m; } void display_arr(){ if(front==-1||front>rear) printf("naQueue is Emptyn"); else { for(i=front;i<=rear; i++) printf("%d t",queue[i]); } } int main(){ short ch; printf("nEnter how many terms : "); scanf("%hd",&ch); for(i=0;i<ch;i++){ int a; printf("nEnter Data Value %d : ",(i+1)); scanf("%d",&a); enqueue_arr(a); } printf("n *****Main Queue***** nn "); display_arr(); for(i=0;i<ch;i++) push(dequeue_arr()); for(i=0;i<ch;i++) enqueue_arr(pop()); printf("n *****Reversed Queue*****nn"); display_arr(); return 0; } OUTPUT: Enter how many terms : 5 Enter Data Value 1 : 12 Enter Data Value 2 : 24 Enter Data Value 3 : 36 Enter Data Value 4 : 48
  • 59. 58 Enter Data Value 5 : 60 *****Main Queue***** 12 24 36 48 60 *****Reversed Queue***** 60 48 36 24 12 RESULT: The above code has executed successfully.
  • 60. 59 EX. NO : 08 8.BINARY SEARCH TREE TRAVERSALS AIM: To create a C program to perform binary search tree traversals. ALGORITHM: Step1: Start the program. Step2: Get the elements from the user and create a tree. Step3: Perform inorder,preorder and postordertraversals. Step4: End the program. PROGRAM: #include <stdio.h> #include<malloc.h> struct Btree { int data; struct Btree *left; struct Btree *right; }; struct Btree *create(int n, struct Btree *tl, struct Btree *tr) { struct Btree *t1; t1=(struct Btree *)malloc(sizeof(struct Btree)); t1->data=n; t1->left = tl; t1->right = tr; return t1; } struct Btree *insert(int x, struct Btree *root) { struct Btree *t1,*p1,*p2; t1=create(x,NULL,NULL); if(root == NULL) return t1; p1=root; while(1) { if(t1->data < p1->data ) {
  • 61. 60 if(p1->left!=NULL) { p1=p1->left; } else { p1->left = t1; break; } } else { if(p1->right!=NULL) { p1=p1->right; } else { p1->right = t1; break; } } } return root; } void inorder(struct Btree *T) { if (T==NULL) return; inorder(T->left); printf(" %d ",T->data); inorder(T->right); } void preorder(struct Btree *T) { if (T==NULL) return; printf(" %d ",T->data); preorder(T->left); preorder(T->right); } void postorder(struct Btree *T) { if (T==NULL) return;
  • 62. 61 postorder(T->left); postorder(T->right); printf(" %d ",T->data); } void main() { struct Btree *root = NULL; root=insert(10,root); printf(" n inorder ..."); inorder(root); root=insert(20,root); root=insert(5,root); root=insert(7,root); root=insert(4,root); root=insert(15,root); root=insert(14,root); root=insert(22,root); root=insert(2,root); printf(" n preorder ..."); preorder(root); printf(" n inorder ..."); inorder(root); printf(" n postorder ..."); postorder(root); printf(" n inorder ..."); inorder(root); } OUTPUT: RESULT: The above code has executed successfully.
  • 63. 62 EX. NO : 09 9.GRAPH TRAVERSALS AIM: To create a C program to perform graph traversals. ALGORITHM: Step1: Start the program. Step2: Get the elements from the user in the adjacency matrix. Step3: Perform breath first search and depth first search for the graph. Step4: Print the result Step5: End the program. PROGRAM: #include <stdio.h> int adj [5][5] = { 0,0,1,1,0, 1,0,0,1,1, 0,1,0,0,0, 0,0,0,0,0, 0,0,1,1,0 }; int Visited[5]={0}; int n=5; void dfs(int node) { int i; Visited[node] = 1; printf(" Visited : %d", node); for(i=0;i<n;i++) { if( (adj[node][i] == 1) && (Visited[i] == 0)) dfs(i); } } int main() { int i,n=5; for(i=0;i<n;i++) if( Visited[i] == 0)
  • 64. 63 dfs(i); } // Breath First Search #include <stdio.h> int adj [5][5] = { 0,0,1,0,0, 1,0,0,0,1, 0,1,0,0,0, 0,0,0,0,0, 0,0,1,1,0 }; int Visited[5]={0}; int Queue[10],f=-1,r=-1; int n=5; void bfs(int node) { int i,x; if( Visited[node] == 1) return; Visited[node] = 1; printf(" Visited : %d", node); Queue[++r]=node; ++f; for(i=0;i<n;i++) if((adj[node][i] == 1) && (Visited[i] == 0)) { Visited[i] = 1; printf(" Visited : %d", i); Queue[++r]=i; } while( f != (r+1)) { x=Queue[f]; f++; bfs(x); } } int main() { int i,n=5;
  • 66. 65 EX. NO :10 10.RED BLACK TREE AIM: To create a C program to perform red black tree insertion, deletion and display. ALGORITHM: Step1: Start the program. Step2: Get the elements from the user and create a red black tree. Step3: Perform insertion deletion and display. Step4: End the program. PROGRAM: #include <stdio.h> #include <stdlib.h> struct node { int d; // data int c; // 1-red, 0-black struct node* p; // parent struct node* r; // right-child struct node* l; // left child }; struct node* root = NULL; struct node* bst(struct node* trav,struct node* temp) { // If the tree is empty, // return a new node if (trav == NULL) return temp; // Otherwise recur down the tree if (temp->d <trav->d) { trav->l = bst(trav->l, temp); trav->l->p = trav; } else if (temp->d >trav->d) { trav->r = bst(trav->r, temp); trav->r->p = trav; } return trav; } void rightrotate(struct node* temp) { struct node* left = temp->l; temp->l = left->r; if (temp->l) temp->l->p = temp;
  • 67. 66 left->p = temp->p; if (!temp->p) root = left; else if (temp == temp->p->l) temp->p->l = left; else temp->p->r = left; left->r = temp; temp->p = left; } void leftrotate(struct node* temp) { struct node* right = temp->r; temp->r = right->l; if (temp->r) temp->r->p = temp; right->p = temp->p; if (!temp->p) root = right; else if (temp == temp->p->l) temp->p->l = right; else temp->p->r = right; right->l = temp; temp->p = right; } void fixup(struct node* root, struct node* pt) { struct node* parent_pt = NULL; struct node* grand_parent_pt = NULL; while ((pt != root) && (pt->c != 0) && (pt->p->c == 1)) { parent_pt = pt->p; grand_parent_pt = pt->p->p; if (parent_pt == grand_parent_pt->l) { struct node* uncle_pt = grand_parent_pt->r; if (uncle_pt != NULL &&uncle_pt->c == 1) { grand_parent_pt->c = 1; parent_pt->c = 0; uncle_pt->c = 0; pt = grand_parent_pt; } else { if (pt == parent_pt->r) { leftrotate(parent_pt); pt = parent_pt;
  • 68. 67 parent_pt = pt->p; } rightrotate(grand_parent_pt); int t = parent_pt->c; parent_pt->c = grand_parent_pt->c; grand_parent_pt->c = t; pt = parent_pt; } } else { struct node* uncle_pt = grand_parent_pt->l; if ((uncle_pt != NULL) && (uncle_pt->c == 1)) { grand_parent_pt->c = 1; parent_pt->c = 0; uncle_pt->c = 0; pt = grand_parent_pt; } else { if (pt == parent_pt->l) { rightrotate(parent_pt); pt = parent_pt; parent_pt = pt->p; } leftrotate(grand_parent_pt); int t = parent_pt->c; parent_pt->c = grand_parent_pt->c; grand_parent_pt->c = t; pt = parent_pt; } } } root->c = 0; } void inorder(struct node* trav) { if (trav == NULL) return; inorder(trav->l); printf("%d ", trav->d); inorder(trav->r); } int main() { int n = 7; int a[7] = { 7, 6, 5, 4, 3, 2, 1 };
  • 69. 68 for (int i = 0; i< n; i++) { struct node* temp= (struct node*)malloc(sizeof(struct node)); temp->r = NULL; temp->l = NULL; temp->p = NULL; temp->d = a[i]; temp->c = 1; root = bst(root, temp); fixup(root, temp); } printf("Inorder Traversal of Created Treen"); inorder(root); return 0; } OUTPUT: RESULT: The above code has executed successfully.
  • 70. 69 EX. NO :11 11.BUBBLE SORT AND SHELL SORT AIM: To create a C program to perform bubble sort and shell sort. ALGORITHM: Step1: Start the program. Step2: Get the elements from the user. Step3: Perform sorting using bubble sort and shell sort. Step4: Print the result Step5: End the program. PROGRAM: #include<stdio.h> void print(int a[], int n) //function to print array elements { int i; for(i = 0; i < n; i++) { printf("%d ",a[i]); } } void bubble(int a[], int n) // function to implement bubble sort { int i, j, temp; for(i = 0; i < n; i++) { for(j = i+1; j < n; j++) { if(a[j] < a[i]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } void main () { int i, j,temp; int a[5] = { 10, 35, 32, 13, 26}; int n = sizeof(a)/sizeof(a[0]);
  • 71. 70 printf("Before sorting array elements are - n"); print(a, n); bubble(a, n); printf("nAfter sorting array elements are - n"); print(a, n); } // Shell sort #include <stdio.h> void shellsort(int arr[], int num) { int i, j, k, tmp; for (i = num / 2; i> 0; i = i / 2) { for (j = i; j <num; j++) { for(k = j - i; k >= 0; k = k - i) { if (arr[k+i] >= arr[k]) break; else { tmp = arr[k]; arr[k] = arr[k+i]; arr[k+i] = tmp; } } } } } int main() { int arr[30]; int k, num; printf("Enter total no. of elements : "); scanf("%d", &num); printf("nEnter %d numbers: ", num); for (k = 0 ; k <num; k++) { scanf("%d", &arr[k]); } shellsort(arr, num); printf("n Sorted array is: ");
  • 72. 71 for (k = 0; k <num; k++) printf("%d ", arr[k]); return 0; } OUTPUT: Bubble Sort Shell Sort RESULT: The above code has executed successfully.
  • 73. 72 EX. NO :12 12.Hash Table AIM: To create a C program to perform Hash table operations. ALGORITHM: Step1: Start the program. Step2: Get the elements from the user. Step3: Perform hash table insertion, deletion, display operations. Step4: Print the result Step5: End the program. PROGRAM: #include <stdio.h> #include <string.h> #include <stdlib.h> #define SIZE 20 struct DataItem { int data; int key; }; struct DataItem* hashArray[SIZE]; struct DataItem* dummyItem; struct DataItem* item; int hashCode(int key) { return key % SIZE; } struct DataItem *search(int key) { //get the hash int hashIndex = hashCode(key); while(hashArray[hashIndex] != NULL) { if(hashArray[hashIndex]->key == key) return hashArray[hashIndex]; ++hashIndex; //wrap around the table
  • 74. 73 hashIndex %= SIZE; } return NULL; } void insert(int key,int data) { struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem)); item->data = data; item->key = key; //get the hash int hashIndex = hashCode(key); //move in array until an empty or deleted cell while(hashArray[hashIndex] != NULL &&hashArray[hashIndex]->key != -1) { //go to next cell ++hashIndex; //wrap around the table hashIndex %= SIZE; } hashArray[hashIndex] = item; } struct DataItem* delete(struct DataItem* item) { int key = item->key; //get the hash int hashIndex = hashCode(key); //move in array until an empty while(hashArray[hashIndex] != NULL) { if(hashArray[hashIndex]->key == key) { struct DataItem* temp = hashArray[hashIndex]; //assign a dummy item at deleted position hashArray[hashIndex] = dummyItem; return temp; }
  • 75. 74 //go to next cell ++hashIndex; //wrap around the table hashIndex %= SIZE; } return NULL; } void display() { int i = 0; for(i = 0; i<SIZE; i++) { if(hashArray[i] != NULL) printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data); else printf(" ~~ "); } printf("n"); } int main() { dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem)); dummyItem->data = -1; dummyItem->key = -1; insert(1, 20); insert(2, 70); insert(42, 80); insert(4, 25); insert(12, 44); insert(14, 32); insert(17, 11); insert(13, 78); insert(37, 97); display(); item = search(37); if(item != NULL) { printf("Element found: %dn", item->data);
  • 76. 75 } else { printf("Element not foundn"); } delete(item); item = search(37); if(item != NULL) { printf("Element found: %dn", item->data); } else { printf("Element not foundn"); } } OUTPUT: ~~ (1,20) (2,70) (42,80) (4,25) ~~ ~~ ~~ ~~ ~~ ~~ ~~ (12,44) (13,78) (14,32) ~~ ~~ (17,11) (37,97) ~~ Element found: 97 Element not found RESULT: The above code has executed successfully