OOPS Practices

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

1.

CALL BY VALUE
AIM:
To write a C++ program to swap the values of two variables using call by value method.

ALGORITHM:
Sterp1: Start.
Step2: Declaring the function.
Step3: Starting the main function.
Step4: Entering two values.
Step5: Display the values before swapping.
Step6: Display the values after swapping.
Step7: Stop.

PROGRAM:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<"enter the values";
cin>>a>>b;
cout<<"The values before swapping: \n";
cout<<"a="<<a<<"b="<<b;
cout<<"\n after the swapping:"<<"\n";
swap(a,b);
getch();
}
void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
cout<<"x= "<<a<<"y= "<<b;
}

Output:
Enter the values: 3 4
The value before swapping:
a=3
B=4

The value after swapping:


a=4
B=3

RESULT:
Thus the swapping the values of two variables using call by value program in C++ was
done and output also verified.

2. CALL BY ADDRESS
AIM:
To write a C++ program to swap the values of two variables using call by address
method.
ALGORITHM:
Step1: Start.
Step2: Declare the function with arguments as pointer.
Step3: Starting the main function.
Step4: Enter the two values.
Step5: Calling the function swap.
Step6: Display the values before swapping.
Step7: Display the values after swapping.
Step8: Stop.

PROGRAM:
#include<iostream.h>
#include<conio.h>
void swap(int *a, int *b);
void main()
{
clrscr();
int a,b;
cout<<"enter the values";
cin>>a>>b;
cout<<"\n The values before swapping: ";
cout<<"a="<<a<<"b="<<b;
cout<<"\n The values after swapping:";
swap(a,b);
getch();
}
void swap(int *a, int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
cout<<"a= "<<*a<<"b= "<<*b;
}
Output:
Enter the values: 3 4
The value before swapping:
a=3
B=4

The value after swapping:


a=4
B=3
RESULT:
Thus the swapping of values of two variables using call by address program in C++ was
done and output also verified.

3. CLASSES AND OBJECTS


AIM:
To write a C++ program to read and display the data using object and classes.

ALGORITHM:
Step1: Start
Step2: Define a class person with data member and member function.
Step3: Read the data member from the user by using the object.
Step4: Display the data by function by using the object.
Step5: Stop

PROGRAM:
#include<iostream.h>
#include<conio.h>
class person
{
char name[30];
int age;
public:
void getdata(void);
void display(void);
};
void person::getdata(void)
{
cout<<”Enter the name”;
cin>>name;
cout<<”Enter the age”;
cin>>age;
}
void person::display(void)
{
cout<<”\n Name:”<<name;
cout<<”\n Age:”<<age;
}
void main()
{
clrscr();
Person p;
p.getdata();
p.display();
getch();
}
Output:
Enter the Name: balaji
Enter the Age:24
Name: Balaji
Age: 24
RESULT:
Thus the C++ program to read and display data using classes and objects was done and
output also verified.

4. FUNCTION OVERLOADING

AIM:
To write a C++ program to perform the function overloading operation.
ALGORITHM:

Step1: Start
Step2: Declare the functions area with one, two, three float argument respectively.
Step3: Start the main function.
Step4: Display the area of circle by calling area(r).
Step5: Display the area of rectangle by calling area(a,b).
Step6: Display the area of triangle by calling area(a,b,c).
Step7: Stop.

PROGRAM:

#include<iostream.h>
#include<conio.h>
double area(float);
double area(float,float);
double area(float,float,float);
void main()
{
clrscr();
int a,b;
cout<<"Area of circle="<<area(6)<<endl;
cout<<"Area of rectangle="<<area(6,10)<<endl;
cout<<"Area of triangle="<<area(2,4,14)<<endl;
getch();
}
double area(float r)
{
return(3.14*r*r);
}
double area(float a,float b)
{
return(a*b);
}
double area(float a, float b, float c)
{
float s;
s=(a+b+c)/2;
return(sqrt(s*(s-a)*(s-b)*(s-b));
}
Output:
Area of circle = 113.04
Area of rectangle = 60
Area of triangle = 53.66561
RESULT:
Thus the function overloading operation in C++ was done and output also verified.

5. CONSTRUCTOR AND DESTRUCTOR

AIM:
To write a C++ program to perform constructor and destructor
operation.
ALGORITHM:
Step1: Start.
Step2: Read the name of boys.
Step3: Declare the constructor.
Step4: Count the number of boys admitted.
Step5: Declare the destructor.
Step6: Print the number of boys left
Step7: Stop.

PROGRAM:

#include<iostream.h>
#include<conio.h>
int count=0;
class hostel
{
char name[15];
public:
hostel()
{
count++;
cout<<"\n Enter the name:";
cin>>name;
cout<<"\n Total no of boys admitted:"<<count;
}
~hostel()
{
cout<<"\n Total no of boys left: "<<count;
count--;
}
};
int main()
{
clrscr();
hostel h1,h2;
getch();
}
Output:
Enter the name balaji:
Total number of boys admitted: 1
Enter the name:raj
Total number of boys admitted:2
Total number of boys left : 2
Total number of boys left :1
RESULT:
Thus constructor and destructor program in C++ done and output also
verified.

6. UNARY OPERATOR OVERLOADING

AIM:
Write C++ program to perform the unary operator overloading
operation.

ALGORITHM:
Step1: Start.
Step2: Declare the class space.
Step3: Declare the public member function by passing arguments..
Step4: In the get data function perform the operation.
Step5: Display the result using display function.
Step6: In the main function declare the overloading concept
Step7: Stop.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class space
{
int x,y,z;
public:
void getdata(int a,int b,int c);
void display(void);
void operator-();
};
void space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
x=(x*x)+2*x;
y=(y*y*y)+10;
z=2*-x;
}
void space::display()
{
cout<<"x="<<x<<"\n";
cout<<"y="<<y<<"\n";
cout<<"z="<<z<<"\n";
}
void space::operator-()
{
x=(-x*-x)+2*-x;
y=(-y*-y*-y)+10;
z=2*-x;
}
void main()
{
clrscr();
space s;
s.getdata(1,2,3);
cout<<" s is before overloading ="<<"\n";
s.display();
-s;
cout<<"\n s after overloading is+ "<<"\n";
s.diaplay();
getch();
}

Output:
S is before overloading
x=3
y=18
z=-6
S is after overloading
x=-3
y=-18
z=6
RESULT:
Thus the unary operator overloading operation in C++ was done and
output also verified.

7. BINARY OPERATOR OVERLOADING

AIM:
Write a C++ program to perform operator overloading operation using
binary operator.
ALGORITHM:
Step1: Start.
Step2: Declare the class complex with data member x&y.
Step3: Declare the constructor with no arguments..
Step4: In the parameterized constructor initialize x with real & y with
imag.
Step5: Perform the binary overloading operation and perform addition
main function..
Step6: In the main function declare the objects and call the main
function.
Step7: Stop.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex()
{}
complex(float real,float imag)
{
x=real;
y=imag;
}
complex operator+(complex);
void display(void);
};

void complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+j"<<y<<endl;
}

void main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
cout<<"First complex number is"<<"\n";
c3=c1+c2;
c1.display();
cout<<"Second complex number is"<<"\n";
c2.display();
cout<<"Sum of complex num is "<<"\n";
c3.diaplay();
getch();
}

Output:

First complex number is 2.5+j3.5


Second complex number is 1.6+j2.7
Sum of complex number is 4.1+6.2
RESULT:
Thus the binary operator overloading operation in C++ was done and
output also verified.

8 a. SINGLE INHERITANCE

AIM:
To write a C++ program to perform single inheritance operation.

ALGORITHM:
Step1: Start.
Step2: Define a class student.
Step3: Input roll no and name.
Step4: Create a derived class student1 and inherit name and age from
student.
Step5: Calculate the total marks in student1.
Step6: Call the member functions.
Step7: Stop.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class A
{

public:
char name;
int m1,m2;
void A1()
{
cout<<"\n Enter the name:";
cin>>name;
cout<<"\n Enter the marks";
cin>>m1>>m2;
}
};
class B:public A
{
public:
int total;
void B1()
{
total=m1+m2;
cout<<"\n The total mark is "<<total;
}
};

void main()
{
clrscr();
B ob;
ob.A1();
ob.B1();
getch();
}

OUTPUT:
Enter the Name: balaji
Enter the marks: 98 97
The total mark is 195
RESULT:
Thus the single inheritance operation in C++ was done and output also
verified.

8.b.MULTIPLE INHERITANCE

AIM:
Write a C++ program to perform multiple inheritance operation.

ALGORITHM:
Step1: Start.
Step2: Declare class A and define and read the name and mark.
Step3: Declare another class B
Step4: Declare class C and inherits the class A and class B publically.
Step5: Define a function to find total mark.
Step6: Create an object of the class C.
Step7: Stop.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class A
{

public:
char name;
int m1;
void A1()
{
cout<<"\n Enter the name:";
cin>>name;
cout<<"\n Enter the marks";
cin>>m1;
}
};
class B:public A
{
public:
int m2;
void B1()
{
cout<<"\n Enter the mark:";
cin>>m2
}
};
class C:public A,public B
{
public:
int total;
void c1()
{
total=m1+m2;
cout<<"\n The total mark is "<<total;
}
};
void main()
{
clrscr();
C ob;
ob.A1();
ob.B1();
ob.C1();
getch();
}
Output:
Enter the name: balaji
Enter the mark : 98
Enter the mark : 97
The total mark is 195
RESULT:
Thus the multiple inheritance operation in C++ was done and output
also verified.
9. VIRTUAL FUNCTION

AIM:
Write a C++ program to perform the virtual function.
ALGORITHM:
Step1: Start.
Step2: Declare a base class with two function display to show.
Step3: Declare both as virtual functions.
Step4: Declare another class derived using public inheritance.
Step5: Define the function in derived class with same name of that in
base class.
Step6: In main program declare base objects b, pointer ptr and the
derived object d.
Step7: Call the both functions in both classes using pointer ptr.
Step8: Stop.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\n Display base";
}
virtual void show()
{
cout<<"\n Show base";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\n Display derived";
}
void show()
{
cout<<"\n Show derived";
}
};
void main()
{
clrscr();
base b;
derived d;
base *ptr;
cout<<"\n Pointer to base";
ptr=&b;
Ptr->display();
ptr->show();
cout<<"\n Pointer to derived";
ptr=&d;
Ptr->display();
ptr->show();
getch();
}

OUTPUT:
Pointer to base
Display base
Show base
Pointer to derived
Display derived
Show derived

RESULT:
Thus the c++ program for virtual function is written and executed
successfully.

10.SEQUENTIAL ACESS
AIM:
To write a c++ program to perform the sequential access process.
ALGORITHM:
Step1: Start.
Step2: Enter the main function.
Step3: Declare character c and string[10].
Step4: Open a file student.txt in input and output mode.
Step5: Enter the string.
Step6: Output from file is taken using seekg. It goes to position.
Step7: The string is printed as output.
Step8: Stop.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char c,string[10];
clrscr();
fstream file("student.txt", ios::in|ios::out);
cout<<"enter the string:";
cin.getline(string,10);
for(int i=0;string[i];i++)
file.put(string[i]);
file.seekg(0);
cout<<"The output string:";
while(file)
{
file.get(c);
cout<<c;
}
getch();
}

Enter the string:


HELLO
The output string:
HELLO
RESULT:
Thus the C++program for sequential access is written and executed
successfully.

11. RANDOM ACCESS

AIM:
To write a c++ program to perform random access.
ALGORITHM:
Step1: Start.
Step2: Enter the main function.
Step3: Open the file test.del in three different modes.ie binary, input
and output.
Step4: Move the input pointer to 2nd position and using seekp.
Step5: The output pointer in the 5th position using seekg.
Step6: Stop.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#define READ_SIZE 6
void main()
{
char reader[READ_SIZE+1];
clrscr();
fstream file("test.del", ios::in|ios::out);
for(int i=0;i<10;i++)
f<<i;
f.seekp(5);
f.<<"HAI";
f.seekg(5);
f.read(reader,READ_SIZE);
reader[READ_SIZE]=0;
getch();
}

OUTPUT:
01HAI56789S
RESULT:
Thus the C++ program for random access is written and executed
successfully.

12.ARRAY IMPLEMENTATION OF LIST ADT

AIM:
To write a C program to execute the concept of array implementation of list
ADT.

ALGORITHM:

1. Start the program.


2. Get the choice from the user.
3. If the choice is to add records, get the data from the user and add them to the list.
4. If the choice is to delete records, get the data to be deleted and delete it from the list.
5. If the choice is to display number of records, count the items in the list and display.
6. If the choice is to search for an item, get the item to be searched and respond yes if the item
is found, otherwise no.
7. Terminate the program

PROGRAM

#include<iostream.h>
#include<conio.h>
#include<process.h>
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20],n,d,e,f,i;
void main()
{
int c;
char g='y';
clrscr();
do
{
cout<<"\n Main Menu";
cout<<"\n 1.Create \n 2.Delete \n 3.Search \n 4.insert \n 5.Display \n 6.Exit";
cout<<"\n enter your choice\n";
cin>>c;
switch(c)
{
case 1: create(); break;
case 2: deletion(); break;
case 3: search(); break;
case 4: insert(); break;
case 5: display(); break;
case 6: exit(0); break;

default:
cout<<"The given number is not between 1-5\n";
}
cout<<"\nDo u want to continue \n";
cin>>g;
clrscr();
}
while(g=='y'|| g=='Y');
getch();
}
void create()
{

cout<<"\n Enter the number\n";


cin>>n;
for(i=0;i<n;i++)
{
cin>>b[i];
}}
void deletion()
{
cout<<"Enter the limit u want to delete \n";
cin>>d;
for(i=0;i<n;i++)
{
if(b[i]==d)
{
b[i]=0;
}}}
void search()
{
cout<<"Enter the limit \n";
cin>>e;
for(i=0;i<n;i++)
{
if(b[i]==e)
{
cout<<"Value found the position\n"<<b[i];
}}}
void insert()
{
cout<<"enter how many number u want to insert \n";
cin>>f;
for(i=0;i<f;i++)
{
cin>>b[n++];
}
}
void display()
{
cout<<"\n\n\n";
for(i=0;i<n;i++)
{
cout<<"\n\n\n"<<b[i];
}
}

Main Menu
1.Create
2.Delete
3.Search
4.insert
5.Display
6.Exit";

enter your choice 1:


Enter the number :5
enter your choice 1:
Enter the number :6
enter your choice 1:
Enter the number :7

enter your choice :5


5
6
7
enter your choice :2

Enter the limit u want to delete: 5


Enter your choice :5
6
7
Enter your choice:6

RESULT:
Thus the C program for array implementation of list ADT is written and
executed successfully.

12. LINKED LIST IMPLEMENTATION OF LIST ADT

AIM:
To write a C program to execute the concept of linked list implementation of
list ADT.

ALGORITHM:

1. Start the program.


2. Get the choice from the user.
3. If the choice is to add records, get the data from the user and add them to the list.
4. If the choice is to delete records, get the data to be deleted and delete it from the list.
5. If the choice is to display number of records, count the items in the list and display.
6. If the choice is to search for an item, get the item to be searched and respond yes if the item
is found, otherwise no.
7. Terminate the program

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct Node
{ int data;
struct Node *Next;
};
typedef struct Node *PtrToNode;
typedef PtrToNode LIST;
typedef PtrToNode POSITION;
int IsEmpty(LIST L)
{
return L->Next==NULL;
}
LIST createlist()
{
LIST L;
L=(struct Node *)malloc(sizeof(struct Node));
if(L==NULL)
printf("fatal error");
else
{ L->data=-1;
L->Next=NULL;
}
return L;
}
void Insert(int x,POSITION P)
{
PtrToNode Temp;
Temp=(struct Node*)malloc(sizeof(struct Node));
if(Temp==NULL)
printf("fatal error");
else
{
Temp->data=x;
Temp->Next=P->Next;
P->Next=Temp;
}
}
POSITION FindPrevious(int x,LIST L)
{
POSITION P;
P=L;
while(P->Next !=NULL && P->Next->data!=x)
P=P->Next;
return P;
}
int IsLast(POSITION P)
{
return P->Next==NULL;
}
void Delete(int x,LIST L)
{
POSITION P, Tempcell;
P=FindPrevious(x,L);
if(!IsLast(P))
{
Tempcell=P->Next;
P->Next=Tempcell->Next;
free(Tempcell);
}
}
void MakeEmpty(LIST L)
{
if(L==NULL)
printf("list is not created");
else
{
while(!IsEmpty(L))
Delete(L->Next->data,L);
}
}
POSITION Find(int x,LIST L)
{
POSITION Temp;
Temp=L;
while(Temp!=NULL)
{
if(Temp->data==x)
return Temp;
Temp=Temp->Next;
}
return Temp;
}
void Display(LIST L)
{
L=L->Next;
while(L!=NULL)
{
printf("\n%d",L->data);
L=L->Next;

}
}
LIST Deletelist(LIST L)
{
MakeEmpty(L);
free(L);
L=NULL;
return L;
}
“Llist.c” file List Adt Using Linked List
#include<stdio.h>
#include<conio.h>
#include"Llist.h"
void main()
{
LIST L=NULL;
POSITION P;
char ch;
int n,x,y,z;
clrscr();
printf("\n\n1.Create \n2.Insert \n3.Delete \n4.MakeEmpty \n5.Find \n6.IsEmpty \n7.Display \
n8.Deletelist \n9.Exit\n");
A:
printf("\nEnter ur option:\t");
scanf("%d",&n);
switch(n)
{
case 1:
if(L==NULL)
{
L=createlist();
printf("\nList is created\t");
}
else
printf("\nList is already created");
break;
case 2:
if(L==NULL)
printf("\nList is not created");
else
{
printf("\nEnter the value:\t");
scanf("%d",&x);
if(L->Next==NULL)
Insert(x,L);
else
{
printf("\nWhere u want to insert:\t");
scanf("%d",&y);
P=Find(y,L);

if(P!=NULL)
Insert(x,P);
else
printf("\nElement not in the list");
}
}
break;
case 3:
if(L==NULL)
printf("\nList is not yet created");
else if(L->Next==NULL)
printf("List is empty");
else
{
printf("\nEnter the value to delete:\t");
scanf("%d",&y);
Delete(y,L);
}
break;
case 4:
if(L==NULL)
printf("\nList is not yet created");
else
{
if(L->Next==NULL)
printf("\nList is empty");
else
MakeEmpty(L);
printf("\n\n Now list is Empty");
}
break;
case 5:
if(L==NULL)
printf("\nList is not created");
else
{
if(L->Next==NULL)
printf("\nList is empty");
else
{
printf("\nEnter the value to find:\t");
scanf("%d",&z);
P=Find(z,L);
if(P==NULL)
printf("\nElement is not in the list");
else
printf("\nElement present in the list");
}
}
break;
case 6:
if(L==NULL)
printf("\nList is not created");
else if(IsEmpty(L))
printf("\nList is empty");
else
printf("\nList contains some elements");
break;
case 7:
if(L==NULL)
printf("\nList is empty");
else
Display(L);
break;
case 8:
if(L==NULL)
printf("\n List is empty");
else
{
L=Deletelist(L);
printf("\n\n List is deleted");
}
break;
case 9:
exit(0);
default:
printf("\n\n\t\t....WRONG ENTRY....");
}
goto A;
}

OUTPUT
1.Create
2.Insert
3.Delete
4.MakeEmpty
5.Find
6.IsEmpty
7.Display
8.Deletelist
9.Exit
Enter ur Option: 1
List is Created.
Enter ur Option: 2
Enter the value: 100
Enter the Option: 2
Enter the value: 200
Enter the Option: 2
Enter the value: 300
Enter the Option: 2
Enter the value: 400
Enter the Option: 2
Enter the value: 500
Enter the Option: 7
100
200
300
400
500
Enter the Option: 3
Enter the value to delete: 200
Enter the Option: 7
100
300
400
500
Enter the Option: 5
Enter the value to Find: 400
Element present in the list
Enter the Option: 6
List contains some Elements
Enter the Option: 4
Now list is empty
Enter the Option: 8
List is deleted
Enter the Option: 2
List is not created
Enter the Option: 10
*******WRONG ENTRY******
Enter the Option: 9
RESULT:
Thus the C program for Linked list implementation of list ADT is written and
executed successfully.

13. CURSOR IMPLEMENTATION OF LIST ADT

AIM:
To write a C program to execute the concept of cursor implementation of list
ADT.

ALGORITHM:

1. Start the program.


2. Get the choice from the user.
3. If the choice is to add records, get the data from the user and add them to the list.
4. If the choice is to delete records, get the data to be deleted and delete it from the list.
5. If the choice is to display number of records, count the items in the list and display.
6. If the choice is to search for an item, get the item to be searched and respond yes if the item
is found, otherwise no.
7. Terminate the program

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define SPACE_SIZE 10
struct Node
{
int data;
int Next;
};
typedef int PtrToNode;
typedef PtrToNode POSITION;
typedef PtrToNode LIST;
struct Node cursor[SPACE_SIZE];
void InitializeCursor()
{
int i;
for(i=0;i<=SPACE_SIZE-1;i++)
{
cursor[i].Next=i+1;
cursor[i].data=0;
}
cursor[SPACE_SIZE-1].Next=-1;
}
POSITION CursorAlloc()
{
POSITION P;
P=cursor[0].Next;
cursor[0].Next=cursor[P].Next;
cursor[P].data=-1;
cursor[P].Next=-1;
return P;
}
void CursorFree(POSITION P)
{
cursor[P].Next=cursor[0].Next;
cursor[0].Next=P;
cursor[P].data=0;
}
void Insert(int X,POSITION P)
{
POSITION Temp;
Temp=CursorAlloc();
if(Temp==-1)
printf("\nOut of space");
else if(cursor[P].data==0)
printf("\nPosition is not in the list");
else
{

cursor[Temp].data=X;
cursor[Temp].Next=cursor[P].Next;
cursor[P].Next=Temp;
}
}
int IsLast(POSITION P)
{
return cursor[P].Next==-1;
}
int IsEmpty(LIST L)
{
return cursor[L].Next==-1;
}
POSITION Find(int X,LIST L)
{
POSITION Temp;
Temp=cursor[L].Next;
while(Temp!=-1&&cursor[Temp].data!=X)
Temp=cursor[Temp].Next;
return Temp;
}
POSITION FindPrevious(int X,LIST L)
{
POSITION Temp;
Temp=L;
while(Temp!=-1&&cursor[cursor[Temp].Next].data!=X)
Temp=cursor[Temp].Next;
return Temp;
}
void Delete(int X,LIST L)
{
POSITION P,Temp;
P=FindPrevious(X,L);
if(!IsLast(P))
{
Temp=cursor[P].Next;
cursor[P].Next=cursor[Temp].Next;
CursorFree(Temp);
}
}
void MakeEmpty(LIST L)
{
while(!IsEmpty(L))
Delete(cursor[cursor[L].Next].data,L);
}
void Display()
{
int i;
for(i=0;i<=SPACE_SIZE-1;i++)
printf("\n%d\t%d\t%d",i,cursor[i].data,cursor[i].Next);
}
“curslist.c” file Cursor Implementaon of list adt
#include<stdio.h>
#include<conio.h>
#include"curslist.h"
void main()
{
LIST L=-1;
POSITION P;
int choice,place,x;
clrscr();
printf("\n1.Create\n2.Insert\n3.Delete\n4.MakeEmpty\n5.Display\n6.Find\n7.Exit");
A:
printf("\nEnter ur choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(L==-1)
{
InitializeCursor();
L=CursorAlloc();
}
else
printf("\nList is already created");
break;
case 2:
if(L==-1)
printf("\nList is not yet initialized");
else
{
printf("\nWhere u want to insert?");
scanf("%d",&place);
printf("\nEnter the element to insert");
scanf("%d",&x);
Insert(x,place);
}
break;
case 3:
if(L==-1)
printf("\nList is not yet initialized");
else
{
printf("\nWhich element you want to delete?");
scanf("%d",&x);
Delete(x,L);
}
break;
case 4:
if(L==-1)
printf("\nList is not yet initialized");
else
MakeEmpty(L);
break;
case 5:
if(L==-1)
printf("\nList is not yet initialized");
else
Display();
break;
case 6:
if(L==-1)
printf("\nList is not yet initialized");
else
{
printf("\nWhich element you want to search?");
scanf("%d",&x);
P=Find(x,L);
printf("\nThe element is at %d",P);
}
break;
case 7:
exit(0);
default:
printf("\n *******WRONG ENTRY*******");
}
goto A;
}
OUTPUT: Cursor Implementaon of list adt
1.Create
2.Insert
3.Delete
4.MakeEmpty
5.Display
6.Find
7.Exit
Enter ur choice: 1

Enter ur choice: 5
002
1 -1 -1
203
304
405
506
607
708
809
9 0 -1
Enter ur choice: 2
Where u want to insert? 1
Enter the element to insert: 100
Enter ur choice: 5
003
1 -1 2
2 100 -1
304
405
506
607
708
809
9 0 -1
Enter ur choice: 2
Where u want to insert? 2
Enter the element to insert: 200
Enter ur choice: 2
Where u want to insert? 3
Enter the element to insert: 300
Enter ur choice: 5
003
1 -1 2
2 100 -1
3 200 4
4 300 5
506
607
708
809
9 0 -1
Enter ur choice: 6
Which element you want to search? 200
The element is at 3
Enter ur choice: 3
Which element you want to delete: 200
Enter ur choice: 5
003
1 -1 2
2 100 4
305
4 300 -1
506
607
708
809
9 0 -1
Enter ur choice: 4
Enter ur choice: 5
004
1 -1 -1
203
305
402
506
607
708
809
9 0 -1
Enter ur choice: 15
************WRONG ENTRY********
Enter ur choice: 7

RESULT:
Thus the C program for Cursor implementation of list ADT is written
and executed successfully.

14 A. CONVERT INFIX TO POSTFIX EXPRESSION(STACK APPLICATION)

AIM:-

To write a ‘C’ program to implement stack and use it to convert infix to postfix expression.

ALGORITHM:-

1. Start the program


2. Scan the Infix string from left to right.
3. Initialise an empty stack.
4. If the scannned character is an operand, add it to the Postfix string. If the scanned character
is an operator and if the stack is empty Push the character to stack.

 If the scanned character is an Operand and the stack is not empty, compare the
precedence of the character with the element on top of the stack (topStack). If
topStack has higher precedence over the scanned character Pop the stack else Push
the scanned character to stack. Repeat this step as long as stack is not empty and
topStack has precedence over the character.

Repeat this step till all the characters are scanned.

5. (After all characters are scanned, we have to add any character that the stack may have to
the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack.
Repeat this step as long as stack is not empty.
6. Return the Postfix string.
7. Terminate the program.

PROGRAM:-

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <ctype.h>

char stack[100];

int top=0;
char exp[100];

struct table

char s[2];

int isp;

int icp;

}pr[7];

int isp(char c)

int i;

for(i=0;i<=6;i++)

if(pr[i].s[0]==c)

return(pr[i].isp);

return 0;

int icp(char c)

int i;

for(i=0;i<=6;i++)

if(pr[i].s[0]==c)

return(pr[i].icp);

return 0;

void main()

{
int i;

clrscr();

strcpy(pr[0].s,"^");

pr[0].isp=3;

pr[0].icp=4;

strcpy(pr[1].s,"*");

pr[1].isp=2;

pr[1].icp=2;

strcpy(pr[2].s,"/");

pr[2].isp=2;

pr[2].icp=2;

strcpy(pr[3].s,"+");

pr[3].isp=1;

pr[3].icp=1;

strcpy(pr[4].s,"-");

pr[4].isp=1;

pr[4].icp=1;

strcpy(pr[5].s,"(");

pr[5].isp=0;

pr[5].icp=4;
strcpy(pr[6].s,"=");

pr[6].isp=-1;

pr[6].icp=0;

clrscr();

stack[top]='=';

printf("enter the infix expression");

gets(exp);

i=0;

printf("the postfix expression is ")

while(i<strlen(exp))

if(isalpha(exp[i])==0)

if(exp[i]==')')

while(stack[top]!='(')

printf("%c",stack[top]);

top--;

top--;

else
{

while(isp(stack[top])>=icp(exp[i]))

printf("%c",stack[top]);

top--;

top++;

stack[top]=exp[i];

else

printf("%c",exp[i]);

i++;

while(top>0)

printf("%c",stack[top]);

top--;

getch();

OUTPUT:-

enter the infix expression a*(s+d/f)+c


the postfix expression is asdf/+*c+

RESULT:-

Thus the given ’C’ program to implement stack and use it to convert infix to postfix expression
implemented, executed, and verified successfully.

14. B.ARRAY IMPLEMENTATION OF STACK

AIM:
To write a C program to perform array implementation of stack.

ALGORITHM:
Step1: Start.
Step2: Declare array for stack.
Step3: Assign top value as 0.
Step4: Declare the functions for push, pop and display.
Step5: In main function, give the menu.
Step6: Declare the switch loop and give the cases accordingly.
Step7: In the push function, check whether top is equal to n.
Step8: If condition is true display stack overflow.
Step9: in the pop function, check whether top is equal to -1.
Step10: If the condition is true, display stack underflow.
Step11: If the condition is false, perform the pop function.
Step12: Perform the display function.
Step13: Stop.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define n 5
int s[n];
int top=0;
void push(int);
void pop();
void display();
main()
{
int op,y;
clrscr();
do
{
printf("\n STACK IMPLEMENTATION USING ARRAY");
printf("\n ----------------------------------");
printf("\n\n MENU\n -----------------------\n");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.DISPLAY");
printf("\n4.EXIT");
printf("%d",&op);
switch(op)
{
case 1:
printf("Enter the elements to be inserted");
scanf("%d",&y);
push(y);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
}
}
while(1);
}
void push(int y0
{
if(top>=n0
printf("\n STACK OVERFLOW!!!!");
else
{
s[top]=y;
top=top+1;
}
}
void pop()
{
if(top==-1)
printf("\n STACK UNDERFLOW!!!!");
else
top=top-1;
printf("\n The poped element is:%d, s[top]);
}
}
void display()
{
int i;
printf("The stack elements are:");
for(i=0;i<top;i++)
{
printf("\n %d",s[i]);
}
}
OUTPUT:
STACK IMPLEMENTATION USING ARRAY
------------------------------------------------------------
MENU
-----------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter Your Choice :1
Enter the element to be inserted :5
Enter Your Choice :1
Enter the element to be inserted :4
Enter Your Choice :1
Enter the element to be inserted :3
Enter Your Choice :1
Enter the element to be inserted :2
Enter Your Choice :1
Enter the element to be inserted :1
Enter Your Choice :3
The stack elements are :
5
4
3
2
1
Enter Your Choice :1
Enter the element to be inserted :6
STACK OVERFLOW!!!
Enter Your Choice :1
Enter Your Choice :2
The popped element is:1
Enter Your Choice :2
The popped element is:2
Enter Your Choice :4

RESULT:
Thus the C program for stack using array implementation is performed
successfully.

14. C.LINKED LIST IMPLEMENTATION OF STACK ADT

AIM:
To write a C program to perform linked list implementation of stack
ADT.

ALGORITHM:
Step1: Start.
Step2: Declare LIST for stack.
Step3: Assign top value as 0.
Step4: Declare the functions for push, pop and display.
Step5: In main function, give the menu.
Step6: Declare the switch loop and give the cases accordingly.
Step7: In the push function, check whether top is equal to n.
Step8: If condition is true display stack overflow.
Step9: in the pop function, check whether top is equal to -1.
Step10: If the condition is true, display stacks underflow.
Step11: If the condition is false, perform the pop function.
Step12: Perform the display function.
Step13: Stop.

#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
typedef struct node *ptrToNode;
typedef ptrToNode STACK;
typedef ptrToNode POSITION;
STACK Create(void)
{
STACK S;
S=(struct node*)malloc(sizeof(struct node));
if(S==NULL)
printf ("not created");
else
{
S->next=NULL;
printf("stack is created successfully");
}
return S;
}
void Push(int x, STACK S)
{
ptrToNode temp;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
printf("fatal error");
else
{
temp->data=x;
temp->next=S->next;
S->next=temp;
}
}
void Pop(STACK S)
{
ptrToNode first;
if(Isempty(S))
printf("no element to pop");
else
{
first=S->next;
S->next=S->next->next;
free(first);
}
}
void MakeEmpty(STACK S)
{
if(S==NULL)
printf("stack is not created");
else
{
while(!Isempty(S))
Pop(S);
}
}
STACK Dispose(STACK S)
{
MakeEmpty(S);
free(S);
S=NULL;
return S;
}
POSITION Top(int x, STACK S)
{
if(S->next->data==x)
return S->next;
else
return NULL;
}
int Isempty(STACK S)
{
return S->next==NULL;
}
void Display (STACK S)
{
S=S->next;
while(S!=NULL)
{
printf("\n%d",S->data);
S=S->next;
}
}
“Lstack.c” file source code Stack of Adt using Linked List
#include<stdio.h>
#include<conio.h>
#include"Lstack.h"
void main()
{
STACK S=NULL;
int n,i,c;
clrscr();
printf("\n\n1.createStack\n2.push\n3.pop\n4.make empty\n5.dispose\n6.display\n7.Top\n8.exit\n");
x:
{
printf("\n\nEnter the option:\t");
scanf("%d",&n);
switch(n)
{
case 1:
if(S==NULL)
S=Create();
else
printf("\n\nstack is already created");
break;
case 2:
if(S==NULL)
printf("\n\nstack is not created");
else
{
printf("enter the element:\t");
scanf("%d",&i);
Push(i,S);
}
break;
case 3:
if (S==NULL)
printf ("\n\nstack is not yet created");
else
Pop(S);
break;
case 4:
if(S==NULL)
printf("\n\nstack is not yet created");
else
MakeEmpty(S);
printf("\n the stack is empty");
break;
case 5:
if (S==NULL)
printf("\n\nnot created");
else
S=Dispose(S);
printf("\n\n The stack is Deleted");
break;
case 6:
if(S==NULL)
printf("not created");
else
printf("\n the elements present in stack are:\n");
Display(S);
break;
case 7:
if (S==NULL)
printf ("\nStack is not created");
else
{
printf("\nEnter to check the top element:\t");
scanf("%d",&c);
if(Top(c,S)==NULL)
printf("\n NO");
else
printf("\n YES Top Element is :\t%d",c);
}
break;
case 8:
exit(0);
default:
printf("\n\n........wrong entry.........");
}
goto x;
}
}

OUTPUT
Stack of Adt using Linked List
1.CreateStack
2.Push
3.Pop
4.MakeEmpty
5.Dispose
6.Display
7.Top
8.Exit
Enter the option: 1
Stack is created Successfully
Enter the option: 2
Enter the element: 100
Enter the option: 2
Enter the element: 200
Enter the option: 2
Enter the element: 300
Enter the option: 6
The Elements present in the stack are:
300
200
100
Enter the option: 7
Enter to check the top element: 300
Yes Top element is 300
Enter the option: 3
Enter the option: 6
The Elements present in the stack are:
200
100
Enter the option: 7
Enter to check the top element: 300
No
Enter the option: 4
The stack is empty
Enter the option: 5
The stack is deleted
Enter the option: 8
RESULT:
Thus the C program for Linked list implementation of Stack ADT is
successfully executed and verified.

14. D. POLYNOMIAL ADDITION (Stack Application)

AIM:-

To write a ‘C’ program to implement stack application for polynomial addition.

ALGORITHM:-

1. Start the program


2. Get the coefficients and powers for the two polynomials to be added.
3. Add the coefficients of the respective powers.
4. Display the added polynomial.
5. Terminate the program.
PROGRAM:-

#include<stdio.h>

#include<conio.h>

struct polynomial

int coff;

int pow;

struct polynomial *link;

}*ptr,*start1,*node,*start2,*start3,*ptr1,*ptr2;

typedef struct polynomial pnl;

int temp1,temp2;

void main()

void create(void);
void prnt(void);

void suml(void);

void sort(void);

clrscr();

printf("Enrter the elements of the first polynomial :");

node = (pnl *) malloc(sizeof (pnl));

start1=node;

if (start1==NULL)

printf(" Unable to create memory.");

getch();

exit();

create();

printf("Enter the elements of the second poly :");

node = (pnl *) malloc(sizeof (pnl));

start2=node;

if (start2==NULL)

printf("Unable to create memory.");

getch();

exit();

create();

clrscr();
//printing the elements of the lists

printf("The elements of the poly first are :");

ptr=start1;

prnt();

printf("The elements of the poly second are :");

ptr=start2;

prnt();

printf("The first sorted list is :");

ptr=start1;

sort();

ptr=start1;

prnt();

printf("The second sorted list is :");

ptr=start2;

sort();

ptr=start2;

prnt();

printf("The sum of the two lists are :");

suml();

ptr=start3;

prnt();

getch();

/*-----------------------------------------------------------------------------*/

void create()
{

char ch;

while(1)

printf(" Enter the coff and pow :");

scanf("%d%d",&node->coff,&node->pow);

if (node->pow==0 )

ptr=node;

node=(pnl *)malloc(sizeof(pnl));

node=NULL;

ptr->link=node;

break;

printf("Do u want enter more coff ?(y/n)");

fflush(stdin);

scanf("%c",&ch);

if (ch=='n' )

ptr=node;

node=(pnl *)malloc(sizeof(pnl));

node=NULL;

ptr->link=node;

break;

}
ptr=node;

node=(pnl *)malloc(sizeof(pnl));

ptr->link=node;

/*-------------------------------------------------------------------------*/

void prnt()

int i=1;

while(ptr!=NULL )

if(i!=1)

printf("+ ");

printf(" %dx^%d\n ",ptr->coff,ptr->pow);

ptr=ptr->link;

i++;

//printf(" %d^%d",ptr->coff,ptr->pow);

/*---------------------------------------------------------------------------*/

void sort()

for(;ptr->coff!=NULL;ptr=ptr->link)

for(ptr2=ptr->link;ptr2->coff!=NULL;ptr2=ptr2->link)

{
if(ptr->pow>ptr2->pow)

temp1=ptr->coff;

temp2=ptr->pow;

ptr->coff=ptr2->coff;

ptr->pow=ptr2->pow;

ptr2->coff=temp1;

ptr2->pow=temp2;

/*---------------------------------------------------------------------------*/

void suml()

node=(pnl *)malloc (sizeof(pnl));

start3=node;

ptr1=start1;

ptr2=start2;

while(ptr1!=NULL && ptr2!=NULL)

ptr=node;

if (ptr1->pow > ptr2->pow )

{
node->coff=ptr2->coff;

node->pow=ptr2->pow;

ptr2=ptr2->link; //update ptr list B

else if ( ptr1->pow < ptr2->pow )

node->coff=ptr1->coff;

node->pow=ptr1->pow;

ptr1=ptr1->link; //update ptr list A

else

node->coff=ptr2->coff+ptr1->coff;

node->pow=ptr2->pow;

ptr1=ptr1->link; //update ptr list A

ptr2=ptr2->link; //update ptr list B

node=(pnl *)malloc (sizeof(pnl));

ptr->link=node; //update ptr list C

}//end of while

if (ptr1==NULL) //end of list A

while(ptr2!=NULL)
{

node->coff=ptr2->coff;

node->pow=ptr2->pow;

ptr2=ptr2->link; //update ptr list B

ptr=node;

node=(pnl *)malloc (sizeof(pnl));

ptr->link=node; //update ptr list C

else if (ptr2==NULL) //end of list B

while(ptr1!=NULL)

node->coff=ptr1->coff;

node->pow=ptr1->pow;

ptr1=ptr1->link; //update ptr list B

ptr=node;

node=(pnl *)malloc (sizeof(pnl));

ptr->link=node; //update ptr list C

node=NULL;

ptr->link=node;

}
OUTPUT:-

Enrter the elements of the first polynomial : Enter the coff and pow :1 1

Do u want enter more coff ?(y/n)y

Enter the coff and pow :1 0

Enter the elements of the second poly : Enter the coff and pow :1 1

Do u want enter more coff ?(y/n)y

Enter the coff and pow :2 0

The elements of the poly first are : 1x^1 + 1x^0

The elements of the poly second are : 1x^1 + 2x^0

The first sorted list is : 1x^0 + 1x^1

The second sorted list is : 2x^0 + 1x^1

The sum of the two lists are : 3x^0 + 2x^1

RESULT:-

Thus the ‘C’ program to implement stack application for polynomial addition is successfully
implemented and output also verified.
15 STACK APPLICATION USING ARRAY IMPLEMENTATION OF STACK ADT

AIM:-

To write a ‘C’ program to implement stack and use it to convert infix to postfix expression.

ALGORITHM:-

1. Start the program


2. Scan the Infix string from left to right.
3. Initialise an empty stack.
4. If the scannned character is an operand, add it to the Postfix string. If the scanned character
is an operator and if the stack is empty Push the character to stack.

 If the scanned character is an Operand and the stack is not empty, compare the
precedence of the character with the element on top of the stack (topStack). If
topStack has higher precedence over the scanned character Pop the stack else Push
the scanned character to stack. Repeat this step as long as stack is not empty and
topStack has precedence over the character.
Repeat this step till all the characters are scanned.

5. (After all characters are scanned, we have to add any character that the stack may have to
the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack.
Repeat this step as long as stack is not empty.
6. Return the Postfix string.
7. Terminate the program.

PROGRAM:-

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <ctype.h>

char stack[100];

int top=0;

char exp[100];

struct table

char s[2];

int isp;

int icp;
}pr[7];

int isp(char c)

int i;

for(i=0;i<=6;i++)

if(pr[i].s[0]==c)

return(pr[i].isp);

return 0;

int icp(char c)

int i;

for(i=0;i<=6;i++)

if(pr[i].s[0]==c)

return(pr[i].icp);

return 0;

void main()

int i;

clrscr();

strcpy(pr[0].s,"^");

pr[0].isp=3;

pr[0].icp=4;
strcpy(pr[1].s,"*");

pr[1].isp=2;

pr[1].icp=2;

strcpy(pr[2].s,"/");

pr[2].isp=2;

pr[2].icp=2;

strcpy(pr[3].s,"+");

pr[3].isp=1;

pr[3].icp=1;

strcpy(pr[4].s,"-");

pr[4].isp=1;

pr[4].icp=1;

strcpy(pr[5].s,"(");

pr[5].isp=0;

pr[5].icp=4;

strcpy(pr[6].s,"=");

pr[6].isp=-1;

pr[6].icp=0;

clrscr();

stack[top]='=';
printf("enter the infix expression");

gets(exp);

i=0;

printf("the postfix expression is ")

while(i<strlen(exp))

if(isalpha(exp[i])==0)

if(exp[i]==')')

while(stack[top]!='(')

printf("%c",stack[top]);

top--;

top--;

else

while(isp(stack[top])>=icp(exp[i]))

printf("%c",stack[top]);

top--;

}
top++;

stack[top]=exp[i];

else

printf("%c",exp[i]);

i++;

while(top>0)

printf("%c",stack[top]);

top--;

getch();

OUTPUT:-

enter the infix expression a*(s+d/f)+c

the postfix expression is asdf/+*c+


RESULT:-

Thus the given ’C’ program to implement stack and use it to convert infix to postfix expression
implemented, executed, and verified successfully.

16.A.ARRAY IMPLEMENTATION OF QUEUE ADT

AIM:
To write a C program to implement array implementation of queue
ADT.

ALGORITHM:
Step1: Start.
Step2: Declare array for Queue.
Step3: Assign front value as 0.
Step4: Declare the functions for enqueue and dequeue.
Step5: In main function, give the menu.
Step6: Declare the switch loop and give the cases accordingly.
Step7: In the enqueue function, check whether front is equal to n.
Step8: If condition is true display queue is overflow.
Step9: in the queue function, check whether top is equal to -1.
Step10: If the condition is true, display queue underflow.
Step11: If the condition is false, perform the rear function.
Step12: Perform the display function.
Step13: Stop.
PROGRAM:

# include <stdio.h>
# define SIZE 10

int arr[ SIZE ], front = -1, rear = -1, i ;


void enqueue() ;
void dequeue() ;
void display() ;

int main()
{
int ch ;

do
{
printf( "\n[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit\n" ) ;
printf( "Enter your choice [1-4] : " ) ;
scanf( "%d", &ch ) ;

switch ( ch )
{

case 1 :
enqueue() ;
break ;

case 2 :
dequeue() ;
break ;

case 3 :
display() ;
break ;

case 4 :
break ;

default :
printf( "Invalid option\n" ) ;
}
}
while ( ch != 4 ) ;
}

void enqueue()
{
if ( rear == SIZE – 1 )
{
printf( "Queue is full (overflow)\n" ) ;
return ;
}

rear++ ;
printf( "Enter the element to ENQUEUE : " ) ;
scanf( "%d", &arr[ rear ] ) ;

if ( front == -1 )
front++ ;
}

void dequeue()
{
if ( front == -1 )
{
printf( "Queue is empty (underflow)\n" );
return ;
}

printf( "The DEQUEUE element is : %d\n", arr[ front ] ) ;

if ( front == rear )
front = rear = -1 ;
else
front++ ;
}

void display()
{
if ( front == -1 )
{
printf( "Queue is empty (underflow)\n" ) ;
return ;
}

printf( "The elements in queue are : FRONT -> " ) ;

for ( i = front ; i <= rear ; i++ )


printf( " … %d", arr[ i ] ) ;

printf( " … <- REAR\n" ) ;


}
OUTPUT:

[1].ENQUEUE
[2].DEQUEUE
[3].Display
[4].Exit

Enter your choice [1-4] : 1


Enter the element to ENQUEUE :5

Enter your choice [1-4] : 1


Enter the element to ENQUEUE :4

Enter your choice [1-4] : 1


Enter the element to ENQUEUE :3

Enter your choice [1-4] : 1


Enter the element to ENQUEUE :2

Enter your choice [1-4] : 1


Enter the element to ENQUEUE :1

Enter your choice [1-4] : 1


Queue is full (overflow)

Enter your choice [1-4] :2


The DEQUEUE element is 5
Enter your choice [1-4] :2
The DEQUEUE element is 4

Enter your choice [1-4] :3


The elements in queue are : FRONT ->
3
2
1
Enter your choice [1-4] : 4

RESULT:-

Thus the C program for array implementation of queue ADT is successfully


implemented and output also verified.

16. B.LINKED LIST IMPLEMENTATION OF QUEUE ADT

AIM:
To write a C program to implement Linked list implementation of
queue ADT.

ALGORITHM:
Step1: Start.
Step2: Declare Linked list for Queue.
Step3: Assign front value as 0.
Step4: Declare the functions for enqueue and dequeue.
Step5: In main function, give the menu.
Step6: Declare the switch loop and give the cases accordingly.
Step7: In the enqueue function, check whether front is equal to n.
Step8: If condition is true display queue is overflow.
Step9: in the queue function, check whether top is equal to -1.
Step10: If the condition is true, display queue underflow.
Step11: If the condition is false, perform the rear function.
Step12: Perform the display function.
Step13: Stop.
Program:

#include<malloc.h>
#include<stdio.h>
struct node{
int value;
struct node *next;
};

void Init(struct node *n){


n->next=NULL;
}
void Enqueue(struct node *root,int value){
struct node *j=(struct node*)malloc(sizeof(struct node));
j->value=value;
j->next=NULL;
struct node *temp ;
temp=root;
while(temp->next != NULL)
{
temp=temp->next;
}
temp->next=j;
printf(“Value Enqueued is : %d\n”,value);

}
void Dequeue(struct node *root)
{
if(root->next==NULL)
{
printf(“No Element to Dequeue\n”);
}
else
{
struct node *temp;
temp=root->next;
root->next=temp->next;
printf(“Value Dequeued is : %d\n”,temp->value);
free(temp);
}
}

void main()
{
struct node sample_queue;
Init(&sample_queue);
Enqueue(&sample_queue,10);
Enqueue(&sample_queue,50);
Enqueue(&sample_queue,570);
Enqueue(&sample_queue,5710);
Dequeue(&sample_queue);
Dequeue(&sample_queue);
Dequeue(&sample_queue);
}

Value Enqueued is :

10

50

570

5710

Value Dequeued is

10
RESULT:-

Thus the C program for Linked list implementation of queue ADT is


successfully implemented and output also verified.

17. BINARY SEARCH TREE


AIM:
To write a C program to implement for binary search tree.

ALGORITHM:
Step1: Start.
Step2: Declare the structure node with data members info, left and
right.
Step3: Declare function for create, insert, delete and search
operations.
Step4: In the main function declare a do loop.
Step5: Then given switch loop for the selection of operations.
Step6: In the create function, do the memory allocations and declare
the logic.
Step7: Next give the insert function and give define the logic
statements to perform the insert operation.
Step8: Declare separate points for s1,R and P for each function defined
in step2.
Step9: Define the delete function and declare integer variable for n and
initialize flag with zero.
Step10: Define the searching and display functions as done before and
write the logic statement.
Step11: Stop.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int INFO;
struct node *LEFT;
struct node *RIGHT;
}
*ROOT:
void create();
void insert();
void delete();
void search();
void display(struct node*);
main()
{
int op;
clrscr();
ROOT=NULL;
do
{
printf("\n 1.create \n 2.insert \n 3.delete \n 4.search \n 5.exit \n");
printf("Enter your choice:");
scanf("%d",&op);
switch(op)
{
case 1:
create();
printf("\n Binary search tree:\n");
display(ROOT);
break;
case 2:
insert();
printf("\n Binary search tree:\n");
display(ROOT);
break;
case 3:
delete();
printf("\n Binary search tree:\n");
display(ROOT);
break;
case 4:
search();
break;
case 5:
exit(0);
break;
}
}
while(1);
}
void create()
{
int N,I;
struct node*s1,*R,*P;
printf("\n Enter the total number of nodes in the tree:");
scanf("%d",&N);
printf("\n Enter the elements:");
for(i=0;i<N;i++)
{
s1=(struct node*)malloc(sizeof(struct node*));
scanf("%d",&s1->INFO);
s1->LEFT=NULL;
s1->RIGHT=NULL;
if(ROOT==NULL)
ROOT=s1;
else
{
R=ROOT;
while(R!=NULL)
{
P=R;
if(s1->INFO>R->INfo)
R=R->LEFT;
if(s1->INFO>P->INfo)
P->RIGHT=s1;
else
P->LEFT=s1;
}
}
}
void display(struct node*R)
{
if(R!=NULL)
{
printf(%d \n",R->INFO);
display(R->LEFT);
display(R->RIGHT);
}
}
void insert()
struct node*s1,*R,*P;
s1=(struct node*)malloc(sizeof(struct node*));
printf("\n Enter the element to insert:");
scanf("%d",&s1->INFO);
s1->LEFT=NULL;
s1->RIGHT=NULL;
if(ROOT==NULL)
ROOT=s1;
else
{
R=ROOT;
while(R!=NULL)
{
P=R;
if(s1->INFO>R->INFO)
R=R->RIGHT;
else
R=R->LEFT;
}
if(s1->INFO>P->INfo)
P->RIGHT=s1;
else
P->LEFT=s1;
}
}
void delete1()
{
struct node*P,*Q,*R;
int N,flag=0;
printf("\n Enter the element to delete:");
scanf(%d",&N);
P=Q=ROOT;
while(P!=NULL)
{
if(P->INFO==N)
{
printf("\n The element %d is deleted",N);
break;
}
Q=P;
if(N>P->INFO)
P=P->RIGHT;
else
P=P->LEFT;
}
if(P==NULL)
{
printf("\n Element not in the tree:");
return;
}
if(Q->LEFT==P)
flag=1;
if(P=P->LEFT!=NULL)&&(P->RIGHT!=NULL))
{
R=P;
for(P=P->RIGHT;P->LEFT;Q=P,P->LEFT)
R->INFO=P->INFO;
return;
}
if(P->LEFT==NULL)&&(P->RIGHT!=NULL))
{
if(flag==1)
Q->LEFT=P->RIGHT;
else
Q->RIGHT=Q->LEFT;
return;
}
elseif((P->LEFT!=NULL)&&(P->RIGHT==NULL))
{
if(flag==1)
Q->LEFT=P->LEFT;
else
Q->RIGHT=P->LEFT;
return;
}
else
{
if(flag==1)
Q->LEFT=NULL:
else
Q->RIGHT=NULL;
return;
}
}
void search()
{
struct node*P;
int N;
printf("\n Enter the element to search:");
scanf(%d",&N);
if(ROOT==NULL)
{
printf("\n Empty tree:");
else
{
P=ROOT;
while(P!=NULL)
{
if(N>P->INFO)
P=P->RIGHT;
elseif(N==P->INFO)
{
printf("\n Element found");
return;
}
}
printf("\n Element not found:");
}
}
OUTPUT:
1.Create
2.Insert
3.Delete
4.Search
5.Exit

Enter Your Choice :1


Enter the total no of nodes in the tree:5
Enter the Elements:
50
10
60
70
30
Binary search tree
50
10
30
70
60
Enter Your Choice: 2
Enter the element to insert: 55
Binary Search tree
50
10
30
70
60
55

RESULT:
Thus the C program for binary search tree is written and executed
successfully.
18.HEAP SORT
AIM:
To write a C program for implement heap sort.
ALGORITHM:
Step1: Start.
Step2: In the main function declare integer type variables i, j, n, pchild
and array with size 20.
Step3: Declare the float type variable for r and t.
Step4: Get the number of elements from user.
Step5: Enter the elements to the array using loop.
Step6: Check whether pchild is not equal to j and array of pchild is less
than array of pchild+1.
Step7: Then increment pchild and check whether array of I is less than
array of pchild.
Step8: Assign the value of array of I to a temporary variable, array of
pchild to array of i and temporary variable to array of pchild.
Step9: Define the swapping statements for array of i and j.
Step10: Display the sorted array using for loop.
Step11: Stop.

SOURCE CODE:
#include<stdio.h.h>
main()
{
int i,j,n,a[20],pchild;
float r,t;
printf("\n Enter the no of elements:");
scanf("%d",&n);
printf("\n Enter the elements:");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Original array:\n");
for(i=1;i<=n;i++)
{
printf("%d\n",&a[i]);
}
for(j=n;j>=2;j--)
{
for(i=j/2;i>0;i--)
{
pchild=2*I;
if(pchild!=j)&&(a[pchild]<a[pchild+1]))
pchild++;
if(a[i]<a[pchild])
{
t=a[i];
a[i]=a[pchild];
a[pchild]=t;
}
}
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("\n The sorted array is:");
for(i=1;i<=n;i++)
{
printf("%d\n",a[i];
}
getch();
}

OUTPUT:
Enter the no of Elements:
5
Enter the Elements
5
3
4
43
20
Original array is
5
3
4
43
20
The sorted array is
3
4
5
20
43

RESULT:
Thus the C program for heap sort is written and executed successfully.

19.QUICK SORT
AIM:
To write a C program for implement the quick sort.
ALGORITHM:
Step1: Start.
Step2: Declare the parameterized function with three integers as
arguments.
Step3: In the for loop read the elements to the array which is to be
sorted.
Step4: Define the logical statement for quick sort.
Step5: Display the sort element in array using for loop.
Step6: Define the quick sort function by passing the argument for array
left and right.
Step7: Declare the while loop and check the condition.
Step8: In the if loop check whether i is less than or equal to j.
Step9: Perform the concept swapping.
Step10: Increment the values for i and decrement for j.
Step11: Check step 8 in while loop and perform step4.
Step12: If the conditions are true execute step5.
Step13: Stop.

SOURCE CODE:
#include<stdio.h.h>
#include<conio.h>
#define N
int A[N]
void qsort(int[],int,int);
void main();
{
int i;
printf("\n Enter %d elements to sort:");
for(i=0;i<=N;i++)
scanf("%d",&A[i]);
qsort(A,0,N-1);
printf("\n sorted array is:\n");
for(i=0;i<=N;i++)
printf("%d\n",&A[i]);
printf("\n");
getch();
}
void qsort(int A[],int left,int right)
{
int pivot,temp,i,j;
i=left;
j=right;
pivot=A[(left+right)/2)];
do
{
while((A[i]<pivot)&&(i<right))i++;
while((pivot<A[j]&&(j>left))j--;
if(i<=j)
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
i++;
j++;
}
}
while(i<=j);
if(left<j)
qsort(A,left,j);
if(i<right)
qsort(A,right,i);
}

OUTPUT

Enter the 5 Elements to sort:


5
3
4
43
20
Sorted array is
3
4
5
20
43
RESULT:

Thus the C Program for quick sort is written and executed successfully

You might also like