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;
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;
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;
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;
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;
}
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 )
{
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;
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;
}