Anda di halaman 1dari 66

AKSHAYA COLLEGE OF ENGINEERING AND TECHNOLOGY, Coimbatore

NAME SECTION BRANCH

: : :

II YEAR ECE (III SEMESTER) DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB OBSERVATION

AKSHAYA COLLEGE OF ENGINEERING AND TECHNOLOGY


DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB II YEAR / III SEM - ECE

LIST OF EXPERIMENTS
Ex No NAME OF THE EXPERIMENT Page No Marks Sign

1 2 3 4

Programs for C++ Concepts(Ex 1.1 to Ex1.5) Array implementation of List Abstract Data Type (ADT) Linked list implementation of List ADT Cursor implementation of List ADT 5.1 Stack ADT - Array implementations Stack linked list implementations Implement any Stack Application using array implementation of Stack Implement any Stack Application using linked list implementation of Stack Queue ADT Array implementations Queue ADT linked list implementations

5 5.1 6.1 6 6.2 7.1 7 7.2 8 9 10

Search Tree ADT - Binary Search Tree Heap Sort Quick Sort

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex. No. 1.1 Date :

STUDENT DETAILS USING CLASSES AND OBJECT

AIM: To write a C++ program to display the student details using classes and object as array. ALGORITHM: Step 1: Create a class as student. Step 2: Declare the data members rollno, name, mark1, mark2, mark3, total and average. Step 3: Declare the member functions as getdata() and displaydata(). 3.1 getdata() method used to get the student details. 3.2 displaydata() method used to display the student details. Step 4: In the main, create an object array for the student class using the following syntax: Classname objectname[size]; Step 5: Get the number of students. Step 6: In getdata() method, get the student details using for loop. Step 7: In displaydata() method, display the student details using for loop. Step 8: Use the following syntax to call the member functions from the main Objectname.methodname();

PROGRAM: #include<iostream.h> #include<conio.h> #include<iomanip.h> class student { int rno; char name[20]; int m1,m2,m3,t; float avg; public: void getdata(); void displaydata(); };

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

void student :: getdata() { cout<<"Enter the roll no:"; cin>>rno; cout<<"Enter the name:"; cin>>name; cout<<"Enter the mark 1:"; cin>>m1; cout<<"Enter the mark 2:"; cin>>m2; cout<<"Enter the mark 3:"; cin>>m3; t=m1+m2+m3; avg=t/3; } void student :: displaydata() { cout<<setw(4)<<rno; cout<<setw(10)<<name; cout<<setw(6)<<m1<<setw(8)<<m2<<setw(8)<<m3; cout<<setw(6)<<t<<setw(7); cout<<avg<<endl; } void main() { student a[10]; clrscr(); int n; cout<<"Enter the range of the student\n"; cin>>n; for(int i=0;i<n;i++) { a[i].getdata(); } cout<<"\n\n"; cout<<"**************************************************"<<endl; cout<<"\t"<<" Student Details: "<<endl; cout<<"**************************************************"<<endl; cout<<setw(5)<<"Rollno"<<setw(8)<<"Name"<<setw(8)<<"Mark1"<<setw(8)<<"Mark 2"<<setw(8)<<"Mark3"<<setw(6)<<"Total"<<setw(6)<<"Avg"<<"\n"; for(int j=0;j<n;j++) { a[j].displaydata(); } getch(); }

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

OUTPUT : Enter the range of the student 2 Enter the roll no:1 Enter the name:mary Enter the mark 1:89 Enter the mark 2:89 Enter the mark 3:78 Enter the roll no:2 Enter the name:jim Enter the mark 1:67 Enter the mark 2:78 Enter the mark 3:89 Enter the roll no:2 Enter the name:jack Enter the mark 1:78 Enter the mark 2:89 Enter the mark 3:67 ************************************************** Student Details: ************************************************** Rollno Name Mark1 Mark2 Mark3 Total Avg 1 mary 89 89 78 256 85 2 jim 67 78 89 234 78 3 jack 78 89 67 234 78

RESULT:

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex:No:1.2 Date :

POINTER TO DATA MEMBER

AIM: To write a C++ program to find the area of rectangle using pointer to data member.

ALGORITHM: Step 1: Create a class as rectangle. Step 2: Declare the data members a, b, *x,*y. Step 3: Declare the member functions as getdata() and area(). 3.1 In the getdata() function, get the length and breadth of the rectangle and store their address to the pointer variable. 3.2 In the area() function, display the area of the rectangle. Step 4: In the main, create an object for the rectangle class using the following syntax: Classname objectname; Step 5: Call the getdata() and area() function using the following syntax: Objectname.methodname();

PROGRAM: #include<iostream.h> #include<conio.h> class rectangle { int a,b,*x,*y; public: void getdata(); void area(); }; void rectangle :: getdata() { cout<<"Enter the length of the rectangle:"; cin>>a; cout<<"Enter the breadth of the rectangle:"; cin>>b; x=&a; y=&b; } void rectangle :: area()

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

{ cout<<"The area of the rectangle is:"<<*x**y; } void main() { rectangle r; clrscr(); r.getdata(); r.area(); getch(); }

OUTPUT: Enter the length of the rectangle:12 Enter the breadth of the rectangle:15 The area of the rectangle is:180

RESULT:

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex:No:1.3 Date :

FUNCTION OVERLOADING

AIM: To write a C++ program to find the volume of cube, rectangle and cylinder using function overloading. ALGORITHM: Step 1: Create a class as shape. Step 2: Declare the data members as a, l, b, h, and r. Step 3: Declare the member function as volume with different arguments. Step 4: Volume function calculates the volume of cube, cylinder and rectangle based on the parameter passed to it. Step 5: In the main, create the object for the shape class. Step 6: Call the function using objectname.functionname();

PROGRAM: #include<iostream.h> #include<conio.h> class shape { int a,l,b,h; float r; public: void volume(int); void volume(int,int,int); void volume(float,int); }; void shape :: volume(int a) { cout<<"Volume of the cube is:"<<a*a*a; } void shape :: volume(int l,int b,int h) { cout<<"Volume of the rectangle is :"<<l*b*h; } void shape :: volume(float r,int h) { cout<<"Volume of the cylinder is:"<<0.33*3.14*r*r*h; } void main()

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

{ shape s; int a1,l1,b1,h1,h2; float r1; clrscr(); cout<<"CUBE:"<<endl; cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl; cout<<"Enter the value of a:"; cin>>a1; s.volume(a1); cout<<endl<<endl<<"RECTANGLE:"<<endl; cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl; cout<<"Enter the value of length, breadth and height:"; cin>>l1>>b1>>h1; s.volume(l1,b1,h1); cout<<endl<<endl<<"CYLINDER"<<endl; cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl; cout<<"Enter the radius and height:"; cin>>r1>>h2; s.volume(r1,h2); getch(); } OUTPUT: CUBE: ~~~~~~~~~~~~~~~~~~~~ Enter the value of a:7 Volume of the cube is:343 RECTANGLE: ~~~~~~~~~~~~~~~~~~~~ Enter the value of length, breadth and height:6 4 5 Volume of the rectangle is :120 CYLINDER ~~~~~~~~~~~~~~~~~~~~ Enter the radius and height:5.4 3 Volume of the cylinder is:90.646779 RESULT:

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex:No:1.4 Date :

MATRIX CLASS USING DEFAULT ARGUMENT,STATIC DATA MEMBERS AND FRIEND FUNCTION

AIM: To write a C++ program to perform matrix manipulation using static variable, default argument and friend function. ALGORITHM: Step 1: Declare the class as Matrix. Step 2: Declare the data member as r, c and **x. Step 3: Declare the member function as Matrix(int r1=2,int c1=2); void get(); void put(); friend Matrix add(Matrix,Matrix); friend Matrix mul(Matrix,Matrix); Step 4: Member function with default argument is used to initialize the value of the matrix. Step 5: get() function is used to get the values of two matrices. Step 6: add() and mul() function are used to perform addition and multiplication of the matrices. Step 7: In the main, create objects A and B for the Matrix class. Step 8: Call the get() method to get the value of matrix A and B. Step 9: Call the add() and mul() method to perform the particular operation and finally display the result. PROGRAM: #include<stdio.h> #include<conio.h> #include<iomanip.h> class matrix { static int r,c; int**x; public: matrix(int r1=2,int c1=2); void get(); void put(); friend matrix add(matrix,matrix); friend matrix mul(matrix,matrix); };

10

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

matrix::matrix(int r1,int c1) { r=r1;c=c1; x=new int*[r]; for(int i=0;i<r;i++) x[i]=new int[c]; for(i=0;i<r;i++) for(int j=0;j<c;j++) { x[i][j]=0; } } void matrix::get() { cout<<"\n enter the matrix of size"<<r<<"x"<<c<<endl; for(int i=0;i<r;i++) for(int j=0;j<c;j++) cin>>x[i][j]; } void matrix::put() { for(int i=0;i<r;i++,cout<<endl) for(int j=0;j<c;j++) cout<<setw(4)<<x[i][j]; } int matrix::r; int matrix::c; matrix add(matrix a,matrix b) { matrix c; for(int i=0;i<a.r;i++) for(int j=0;j<a.c;j++) c.x[i][j]=a.x[i][j]+(b.x[i][j]); return c; } matrix mul(matrix a,matrix b) { matrix c; for(int i=0;i<a.r;i++) for(int j=0;j<b.c;j++) for(int k=0;k<a.c;k++) {

11

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

c.x[i][j]=c.x[i][j]+a.x[i][k]*(b.x[k][j]); } return c; } void main() { clrscr(); matrix a,b,c1,d1; a.get(); b.get(); cout<<"The matrix A:"<<endl; a.put(); cout<<"The matrix B:"<<endl; b.put(); c1=add(a,b); cout<<"The resultant matrix (A+B):"<<endl; c1.put(); cout<<"\n\n The resultant matrix(A*B):"<<endl; d1=mul(a,b); d1.put(); getch(); } OUTPUT: Enter the matrix of size2x2 23 23 Enter the matrix of size2x2 45 45 The matrix A: 2 3 2 3 The matrix B: 4 5 4 5 The resultant matrix(A+B): 6 8 6 8 The resultant matrix(A*B): 20 25 20 25 RESULT:

12

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex:No:1.5 Date :

COMPLEX NUMBER USING OBJECT AS ARGUMENT

AIM: To write a C++ program to add two complex numbers using object as argument. ALGORITHM: Step 1: Create a class as complex. Step 2: Declare the data members as real and img. Step 3: Declare the member functions as void getdata() void show() void sum(complex c1,complex c2) Step 4: getdata() method is used to get the values . Step 5: show() method is used to display the values. Step 6: sum() method is used to perform addition operation using object as argument. Step 7: In main, create the object for Complex class. Step 8: Call the function using objectname.functionname();

PROGRAM: #include<iostream.h> #include<conio.h> class complex { int real; int img; public: void getdata() { cout<<"Enter the real and img part"; cin>>real>>img; } void show() { cout<<"\n"<<real<<"+"<<img<<"i"<<"\n"; } void add(complex c1,complex c2); }; void complex::add(complex c1,complex c2) {

13

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

real=c1.real+c2.real; img=c1.img+c2.img; } void main() { clrscr(); complex c1,c2,c3; c1.getdata(); c2.getdata(); c3.add(c1,c2); cout<<"The complex number of c1:"; c1.show(); cout<<"The complex number of c2:"; c2.show(); cout<<"The result is:"; c3.show(); } OUTPUT: Enter the real and img part 4 5 Enter the real and img part 2 3 The complex number of c1: 4+5i The complex number of c2: 2+3i The result is: 6+8i

RESULT:

14

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex.No1.6 Date :

CONSTRUCTOR OVERLOADING

AIM: To write a C++ program to display the account number and balance using constructor overloading. ALGORITHM: Step 1: Create a class as Account. Step 2: Declare the data members as accountid and balance. Step 3: Declare the member functions as void Account() implies default constructor void Account(float) implies one argument constructor void Account(int,float) implies two argument constructor void moneytransfer(Account,float) void display() Step 4: In main, create the object for Account class. Step 5: While creating the object without argument, the default constructor is called. Step 6: While creating the object with one argument, constructor with one argument is called and value is assigned to the accoundid. Step 7: Call the display() function to display the account details. Step 8: While creating the object with two arguments, constructor with two arguments is called and value is assigned to the accoundid and balance. Step 9: Call the display() function to display the account details. Step 10: Call the money transfer() function to transfer the money and display the balance after money transfer.

PROGRAM: #include<iostream.h> #include<conio.h> class Account { int accid; float balance; public: Account(); Account(float ); Account(int ,float); void moneytransfer(Account&,float); void display();

15

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

}; Account::Account() { accid=1; balance=1000.0; } Account::Account(float y) { accid=2; balance=y; } Account::Account(int x,float z) { accid=x; balance=z; } void Account::display() { cout<<"\t"<<accid; cout<<"\t\t"<<balance<<"\n"; } void Account::moneytransfer(Account &ac1,float amount) { ac1.balance=ac1.balance+amount; balance=balance-amount; } void main() { clrscr(); float a; cout<<"\nEnter the amount to be transfer:"; cin>>a; Account ac1; Account ac2(25000); Account ac3(3,45000); cout<<"\n\nBefore Money Transfer:\n"; cout<<"\tAccountID\tAmount\n";

16

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

ac1.display(); ac2.display(); ac3.display(); ac3.moneytransfer(ac1,a); cout<<"\n\n After Money Transfer:\n"; cout<<"\tAccountID\tAmount\n"; ac1.display(); ac2.display(); ac3.display(); } OUTPUT: Enter the amount to be transfer:500 Before Money Transfer: AccountID Amount 1 1000 2 25000 3 45000 After Money Transfer: AccountID Amount 1 1500 2 25000 3 44500

RESULT:

17

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex no: 2 Date :

ARRAY IMPLEMENTATION OF LIST

AIM: To develop a C++ program for implementing list abstract data type using array.

ALGORITHM: STEP 1: Start the program and include the necessary header files for the program STEP 2: Declare the necessary variables. STEP 3: Create the main function and declare the necessary operations to be performed on the list. STEP 4: Define create() function such that it is called whenever the array has be created. STEP 5: Define insert() function such that it is called whenever the array elements to are to be inserted into the list STEP 6: Define the display() function such that it is called whenever the array elements has to be displayed STEP 7: Define the search() function such that it is called whenever an array element has to be searched and its position will be returned. STEP 8: Define the delete() function such that it is called whenever an array element has to be deleted from the list. STEP 9: Define the reverse() function such that it is called whenever the array elements has to be displayed in reverse form. STEP 10: Get the choice from the user and call the necessary functions wherever needed. STEP 11: Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> #include<stdlib.h> #define MAX 20 int list[MAX]; class arr_list { public: int create(); void display(int); void reverse(int); int search(int); void delet(int); };

18

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

int arr_list::create() { int i,n; clrscr(); cout<<"\n\t\t CREATE LIST"; cout<<"\n\t\t -----------"; cout<<"\n\t\t -----------"; cout<<"\n\n How many elements u want in list?:"; cin>>n; cout<<"\n"; if(n>MAX) { cout<<"\n ERROR: No of elements exceed the limit !"; } for(i=0;i<n;i++) { cout<<"\n\n Enter the element number"<<i+1<<":"; cin>>list[i]; } cout<<"\n List is succesfully created!"; cout<<"\n Press any key to continue..."; getch(); return(n); } void arr_list::display(int n) { int i; clrscr(); cout<<"\n\t\t DISPLAY LIST"; cout<<"\n\t\t -------------"; cout<<"\n\t\t -------------"; cout<<"\n\n The list is:"; cout<<"\n\n\t"; for(i=0;i<n;i++) { cout<<list[i]<<"\n\n\t"; } cout<<"\n\n Press any key to continue."; getch(); } void arr_list::reverse(int n) { int i; clrscr(); cout<<"\n\t\t REVERSE LIST"; cout<<"\n\t\t ------------";

19

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

cout<<"\n\t\t ------------"; cout<<"\n\n The reversed list is:\n\n"; cout<<"\n\t"; for(i=n-1;i>=0;i--) { cout<<list[i]<<"\n\n\t"; } cout<<"\n\n press any key to cntinue..."; getch(); } int arr_list::search(int n) { int i,key; clrscr(); cout<<"\n\t\t SEARCH LIST"; cout<<"\n\t\t ------------"; cout<<"\n\t\t ------------"; cout<<"\n\nEnter the number u want to search?:"; cin>>key; for(i=0;i<n;i++) { if(list[i]==key) { cout<<"\n\n The given number is at position"<<i; cout<<"\n\n press any key to continue..."; getch(); return i; } } cout<<"\n The given number is not in the list..."; cout<<"\n\n press any key to continue..."; getch(); } void arr_list::delet(int n) { cout<<"\n\t\t DELETE LIST "; cout<<"\n\t\t -----------"; cout<<"\n\t\t -----------"; int i; i=search(n); list[i]=-1; cout<<"\n\n The given element is now deleted..."; cout<<"\n\n WE put -1 to indicate empty location..."; getch(); }

20

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

void main() { int choice,len,position; clrscr(); arr_list a; do { cout<<"\n\n PROGRAM FOR ARRAY IMPLEMENTATION FOR ADT!!!"; cout<<"\n\n 1.Create"; cout<<"\n 2.Display"; cout<<"\n 3.Search for numbers"; cout<<"\n 4.Reverse"; cout<<"\n 5.Delete"; cout<<"\n 6.Quit"; cout<<"\n\nEnter the choice:"; cin>>choice; switch(choice) { case 1: len=a.create(); break; case 2: a.display(len); break; case 3: a.search(len); break; case 4: a.reverse(len); break; case 5: a.delet(len); break; case 6: exit(0); } }while(choice!=6); getch(); } OUTPUT: MENU: 1. CREATE 2. DISPLAY 3. SEARCH FOR NUMBERS 4. REVERSE 5. DELETE 6. QUIT ENTER THE CHOICE: 1 CREATE LIST ------------------

21

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

-----------------How many elements you want in the list? 3 Enter the element number1: 10 Enter the element number2: 20 Enter the element number3: 30 List is successfully created!!! Press any key to continue. ENTER THE CHOICE: 2 DISPLAY LIST ----------------------------------The list is 10 20 30 Press any key to continue. ENTER THE CHOICE: 3 SEARCH LIST ----------------------------------Enter the number you want to search: 20 The given number is at position 1 Press any key to continue. ENTER THE CHOICE: 4 REVERSE LIST ----------------------------------The reverse list is 30 20 10 Press any key to continue.. ENTER THE CHOICE: 5 DELETE LIST ----------------------------------Enter the number you want to search : 20 The given number is at position 1 Press any key to continue. The given element is now deleted.. We put -1 to indicate the empty location ENTER THE CHOICE: 2 DISPLAY LIST ----------------------------------The list is 10 30 ENTER THE CHOICE: 6 RESULT:

22

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex no: 3 Date :

LINKED LIST IMPLEMENTATION OF LIST ADT

AIM: To develop a C++ program for implementing list abstract data type using linked list implementation.

ALGORITHM:

STEP 1: Start the program and include the necessary header files for the program STEP 2: Declare the necessary variables. STEP 3: Create the main function and declare the necessary operations to be performed on the list. STEP 4: Define create() function such that it is called whenever the array has be created. STEP 5: Define insert() function such that it is called whenever the array elements to are to be inserted into the list. STEP 6: Define the printlist() function such that it is called whenever the array elements has to be displayed STEP 7: Define the find() function such that it is called whenever an array element has to be searched and its position will be returned. STEP 8: Define the remove() function such that it is called whenever an array element has to be deleted from the list. STEP 9: Define the reverse() function such that it is called whenever the array elements has to be displayed in reverse form. STEP 10: Define the findprev() function such that it is called whenever an array element has to be searched and its previous position will be returned. STEP 11: Get the choice from the user and call the necessary functions wherever needed. STEP 12: Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> #include<stdlib.h> template<class t> class list { struct node { t element; node*next; node(t E=0,node*n=NULL)

23

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

{ element=E; next=n; } }; t x; node *head; node *current; public: list() { head=new node; current=head; }; void create(); void insert(); void remove(); void find(); void printlist(); int findprev(t); }; template<class t> void list<t>::create() { int n,i; cout<<"\n Enter how many nodes to create:"; cin>>n;

for(i=1;i<=n;i++) { cout<<"\n Enter node "<<i<<":"; cin>>x; node*p=new node(x,current->next); current->next=p; current=current->next; } } template<class t> void list<t>::printlist() { node*p; p=head->next; cout<<"\n\tELEMENTS IN THE LIST\n"<<endl;

24

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

while(p!=NULL) { cout<<"-> "<<p->element<<" "; p=p->next; } } template<class t> int list<t>::findprev(t x) { node*q; for(q=head;q->next!=NULL;q=q->next) if(q->next->element==x) { current=q; return 1; } return 0; } template<class t> void list<t>::remove() { node*p; cout<<"\n ENTER ELEMENT TO BE DELETED:"; cin>>x;

if(findprev(x)) { p=current->next; current->next=p->next; delete p; } else { cout<<"\n ELEMENT NOT IN THE LIST:"; } } template<class t> void list<t>::insert() { int ch,i=1,pos; node*q,*p; cout<<"\n\t"<<"INSERT AT\n 1.FIRST\n 2.ANY WHERE\n 3.LAST"; cout<<"\n \n ENTER THE CHOICE:";

25

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

cin>>ch; cout<<"\n ENTER ELEMENT TO INSERT:"; cin>>x; switch(ch) { case 1: p=new node(x,head->next); head->next=p; break; case 2: cout<<"\n ENTER THE POSITION:"; cin>>pos; for(q=head;i<pos&&q!=NULL;q=q->next,i++); if(q) { p=new node(x,q->next); q->next=p; } else { cout<<"\n ENTER CORRECT POSITION:"; } break; . case 3: for(q=head;q->next!=NULL;q=q->next); p=new node(x,q->next); q->next=p; } } template<class t> void list<t>::find() { cout<<"\n ENTER ELEMENT TO BE SEARCHED:"; cin>>x; node*q; q=head->next; while(q!=NULL&&q->element!=x) q=q->next; if(q)

26

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

cout<<"\n ELEMENT FOUND!"; else cout<<"\n ELEMENT NOT FOUND!"; } void main() { clrscr(); list <int>l; int ch1; while(1) { cout<<"\n\nOPERATIONS OF SINGLY LINKED LIST\n"<<endl; cout<<"\n 1.CREATE\n 2.INSERT\n 3.PRINTLIST\n 4.DELETE\n 5.FIND\n 6.EXIT\n"; cout<<"\nEnter the operation to be performed:"; cin>>ch1; switch(ch1) { case 1: l.create(); break; case 2: l.insert(); break; case 3: l.printlist(); break; case 4: l.remove(); break; case 5: l.find(); break; case 6: exit(0); } } getch(); }

27

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

OUTPUT: MENU: 1. CREATE 2. DISPLAY 3. PRINTLIST 4. DELETE 5. FIND 6. EXIT ENTER THE OPERATION TO BE PERFORMED: 1 ENTER HOW MANY NODES TO CREATE? 2 ENTER THE NODE1: 10 ENTER THE NODE2: 20 ENTER THE OPERATION TO BE PERFORMED: 3 ELEMENTS IN THE LIST 10 20 ENTER THE OPERATION TO BE PERFORMED: 2 INSERT AT 1. FIRST 2. ANYWHERE 3. LAST ENTER THE CHOICE :1 ENTER THE ELEMENT TO BE INSERTED 40 ENTER THE OPERATION TO BE PERFORMED: 3 ELEMENTS IN THE LIST 40 10 20 ENTER THE OPERATION TO BE PERFORMED: 2 INSERT AT 1. FIRST 2. ANYWHERE 3. LAST ENTER THE CHOICE :2 ENTER THE ELEMENT TO BE INSERTED 50 ENTER THE OPERATION TO BE PERFORMED: 2 ELEMENTS IN THE LIST 40 50 10 20 ENTER THE OPERATION TO BE PERFORMED: 2 INSERT AT 1. FIRST 2. ANYWHERE 3. LAST ENTER THE CHOICE :3 ENTER THE ELEMENT TO BE INSERTED 30 ENTER THE OPERATION TO BE PERFORMED: 3 ELEMENTS IN THE LIST

28

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

40 50 10 20 30 ENTER THE OPERATION TO BE PERFORMED: 4 ENTER THE ELEMENT TO BE DELETED : 10 ELEMENT DELETED!!! ENTER THE OPERATION TO BE PERFORMED: 3 ELEMENTS IN THE LIST 40 50 20 30 ENTER THE OPERATION TO BE PERFORMED: 5 ENTER THE ELEMENT TO BE SEARCHED : 30 ELEMENTS FOUND !!! ENTER THE OPERATION TO BE PERFORMED : 6

RESULT:

29

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex no: 4 Date :

CURSOR IMPLEMENTATION OF LIST ADT

AIM: To develop a C++ program for implementing list abstract data type using cursor implementation.

ALGORITHM: STEP 1: Start the program and include the necessary header files for the program STEP 2: Declare the necessary variables. STEP 3: Create the main function and declare the necessary operations to be performed on the list. STEP 4: Define create() function such that it is called whenever the array has be created. STEP 5: Define the display() function such that it is called whenever the array elements has to be displayed STEP 6: Define the search() function such that it is called whenever an array element has to be searched and its position will be returned. STEP 7: Define the delete() function such that it is called whenever an array element has to be deleted from the list. STEP 8: Get the choice from the user and call the necessary functions wherever needed. STEP 9: Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> #include<stdlib.h> #define MAX 20 int List[MAX]; void main() { int choice,len,position; int create(); char ans; clrscr(); void display(int); int search(int); void delet(int); do { cout<<"\n\n\tProgram to implement cursor using list..."; cout<<"\n\n 1.CREATE"; cout<<"\n 2.DISPLAY"; cout<<"\n 3.SEARCH FOR A NUMBER"; cout<<"\n 4.DELETE";

30

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

cout<<"\n 5.QUIT"; cout<<"\n\n ENTER YOUR CHOICE(1-6):"; cin>>choice; switch(choice) { case 1: len=create(); break; case 2: display(len); break; case 3: position=search(len); break; case 4: delet(len); break; case 5: cout<<"\n Do u want to exit(y/n)???:"; ans=getche(); if(ans=='y') exit(0); else break; default: clrscr(); cout<<"\n Invalid choice....Try again"; getch(); } }while(choice!=5); }

int create() { int n,i; clrscr(); cout<<"\n How many elements you want in the list:"; cin>>n; if(n>MAX) cout<<"\n ERROR: Number of elements exceed the limit..."; for(i=0;i<n;i++) { cout<<"\n Enter the element number"<<i+1<<":"; cin>>List[i]; } cout<<"\n\n The list is successfully created...\n"; getch(); return(n); }

31

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

void display(int n) { int i; clrscr(); cout<<"\n The list is....\n\n"; for(i=0;i<n;i++) { if(List[i]!=-1) cout<<"\t"<<List[i]<<" "; } cout<<"\n\n press any key to continue...\n"; getch(); } int search(int n) { int i,key; clrscr(); cout<<"\n Enter the number u want to search?:"; cin>>key; for(i=0;i<n;i++) { if(List[i]==key) { cout<<"\n The given number is at position"<<i+1; getch(); return i; } } cout<<"\n\n The given number is not in the list...\n"; getch(); } void delet(int n) { int i; i=search(n); List[i]=-1; cout<<"\n\n The element is now deleted...\n\n"; getch(); } OUTPUT: MENU: 1. CREATE 2. DISPLAY 3. SEARCH FOR NUMBERS

32

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

4. DELETE 5. QUIT ENTER THE CHOICE: 1 How many elements you want in the list? 3 Enter the element number1: 10 Enter the element number2: 20 Enter the element number3: 30 List is successfully created!!! ENTER THE CHOICE: 2 The list is 10 20 30 ENTER THE CHOICE: 3 Enter the number you want to search: 20 The given number is at position 1 ENTER THE CHOICE: 4 Enter the number you want to search : 30 The given number is at position 2 The element is now deleted.. ENTER THE CHOICE: 2 The list is 10 20 ENTER THE CHOICE: 5

RESULT:

33

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex no: 5.1 Date :

ARRAY IMPLEMENTATION OF STACK ADT

AIM: To develop a C++ program for implementing stack abstract data type using array implementation.

ALGORITHM: STEP 1: Start the program and include the necessary header files for the program STEP 2: Declare the necessary variables and the structure for the stack. STEP 3: Create the main function and declare the necessary operations to be performed on the stack. STEP 4: Define the isfull() function such that it is used to check whether the stack is full or not. STEP 5: Define the isempty() function such that it is used to check whether the stack is empty. STEP 6: Define the display() function such that it is called whenever the elements in a stack has to be displayed STEP 7: Define the push() function such that it is called whenever an array element has to be inserted into the stack. STEP 8: Define the pop() function such that it is called whenever an array element has to be deleted from the stack. STEP 9: Get the choice from the user and call the necessary functions wherever needed. STEP 10: Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> #include<stdlib.h> #define size 5 struct stack { int s[size]; int top; }st; class arr_stack { public: int stfull(); int stempty(); void push(int); int pop(); void display(); };

34

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

int arr_stack::stfull() { if(st.top>=size-1) { return 1; } else { return 0; } } void arr_stack::push(int item) { st.top++; st.s[st.top]=item; } int arr_stack::stempty() { if (st.top==-1) { return 1; } else { return 0; } } int arr_stack::pop() { int item; item=st.s[st.top]; st.top--; return(item); } void arr_stack::display() { int i; if(stempty()) { cout<<"\nStack is empty!!!"; } else { for(i=st.top;i>=0;i--) {

35

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

cout<<"\n\n\t"; cout<<st.s[i]; } } } void main() { int item,choice; int ans; st.top=-1; arr_stack a; clrscr(); cout<<"\n PROGRAM FOR ARRAY IMPLEMENTATION OF STACK..."; do { cout<<"\n\n 1.Push"; cout<<"\n 2.Pop"; cout<<"\n 3.Display"; cout<<"\n 4.Quit"; cout<<"\n\n Enter your choice(1-4):"; cin>>choice; switch(choice) { case 1: cout<<"\n Enter the item to be pushed:"; cin>>item; if(a.stfull()) { cout<<"\n stack is full!"; } else { a.push(item); } break; case 2: if(a.stempty()) { cout<<"\n Stack is empty!underflow!!"; } else { item=a.pop(); cout<<"\n The popped plement is:"<<item; } break; case 3: a.display();

36

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

break; case 4: exit(0); } cout<<"\n\n Do you want to continue?(1-yes,2-no):"; cin>>ans; }while(ans==1); getch(); }

OUTPUT: MENU: 1. PUSH 2. POP 3. DISPLAY 4. QUIT ENTER THE CHOICE: 1 Enter the item to be pushed : 10 ENTER THE CHOICE: 1 Enter the item to be pushed : 15 ENTER THE CHOICE: 1 Enter the item to be pushed : 25 ENTER THE CHOICE: 3 The stack is 10 15 25 ENTER THE CHOICE: 2 The popped element is 25 ENTER THE CHOICE: 3 The stack is 10 15 ENTER THE CHOICE: 4

RESULT:

37

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex no: 5.2 Date :

LINKED LIST IMPLEMENTATION OF STACK ADT

AIM: To develop a C++ program for implementing stack abstract data type using linked list implementation.

ALGORITHM: STEP 1: Start the program and include the necessary header files for the program STEP 2: Declare the necessary variables and the structure for the stack. STEP 3: Create the main function and declare the necessary operations to be performed on the stack. STEP 4: Define the isfull() function such that it is used to check whether the stack is full or not. STEP 5: Define the isempty() function such that it is used to check whether the stack is empty. STEP 6: Define the display() function such that it is called whenever the elements in a stack has to be displayed STEP 7: Define the push() function such that it is called whenever an array element has to be inserted into the stack. STEP 8: Define the pop() function such that it is called whenever an array element has to be deleted from the stack. STEP 9: Get the choice from the user and call the necessary functions wherever needed. STEP 10: Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<malloc.h> struct node { int info; struct node*link; }*top=NULL; class st_list { public: void push(); void pop(); void display(); }; void st_list::push() {

38

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

struct node*tmp; int pushed_item; tmp=(struct node*)malloc(sizeof(struct node)); cout<<"\n\n Input the new value to be pushed on the stack:"; cin>>pushed_item; tmp->info=pushed_item; tmp->link=top; top=tmp; } void st_list::pop() { struct node*tmp; if(top==NULL) cout<<"\n\nStack is empty\n"; else { tmp=top; cout<<"\n\n popped item is:"<<tmp->info; top=top->link; free(tmp); } } void st_list::display() { struct node*ptr; ptr=top; if(top==NULL) cout<<"\n\n stack is empty\n"; else { cout<<"\n\n stack elements:\n"; while(ptr!=NULL) { cout<<"\n\t"<<ptr->info<<"\n"; ptr=ptr->link; } } } void main() { st_list s; int choice; clrscr();

39

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

while(1) { cout<<"\n\n PROGRAM FOR LIST IMPLEMENTATION OF STACK..."; cout<<"\n\n 1.PUSH"; cout<<"\n 2.POP"; cout<<"\n 3.DISPLAY"; cout<<"\n 4.QUIT"; cout<<"\n\n ENTER YOUR CHOICE:"; cin>>choice; switch(choice) { case 1: s.push(); break; case 2: s.pop(); break; case 3: s.display(); break; case 4: exit(0); default: cout<<"\n\n Wrong decision\n"; } } getch(); } OUTPUT: 1.PUSH 2.POP 3.DISPLAY 4.QUIT ENTER YOUR CHOICE:3 stack elements: 6 3 PROGRAM FOR LIST IMPLEMENTATION OF STACK... 1.PUSH 2.POP 3.DISPLAY 4.QUIT RESULT:

40

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex no: 6 Date :

ARRAY IMPLEMENTATION OF STACK ADT - APPLICATION

AIM: To develop a C++ program for implementing application of stack abstract data type using array implementation.

ALGORITHM: STEP 1: Start the program and include the necessary header files for the program STEP 2: Declare the necessary variables and the structure for the stack. STEP 3: Create the main function and declare the necessary operations to be performed on the stack. STEP 4: Define the infix() function such that it is used to display the expression in infix format. STEP 5: Define the prefix() function such that it is used to display the expression in prefix format. STEP 6: Define the postfix() function such that it is called whenever the elements in a stack has to be displayed in the postfix format. STEP 7: Define the push() function such that it is called whenever an array element has to be inserted into the stack. STEP 8: Define the pop() function such that it is called whenever an array element has to be deleted from the stack. STEP 9: Get the choice from the user and call the necessary functions wherever needed. STEP 10: Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> #include<alloc.h> char inf[40],post[40]; int top=0,st[20]; class infix { public: void postfix(); void push(int); char pop(); }; void infix::postfix() { int i,j=0; for(i=0;inf[i]!='\0';i++) { switch(inf[i])

41

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

{ case '+': while(st[top]>=1) post[j++]=pop(); push(1); break; case '-': while(st[top]>=1) post[j++]=pop(); push(2); break; case '*': while(st[top]>=3) post[j++]=pop(); push(3); break; case '/': while(st[top]>=3) post[j++]=pop(); push(4); break; case '^': while(st[top]>=4) post[j++]=pop(); push(5); break; case '(': push(0); break; case ')': while(st[top]!=0) post[j++]=pop(); top--; break; default: post[j++]=inf[i]; } } while(top>0) post[j++]=pop(); cout<<"\n\n postfix expression is:"<<post; } void infix::push(int ele) { top++; st[top]=ele; } char infix::pop() { int e1; char e; e1=st[top]; top--; switch(e1)

42

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

{ case 1: e='+'; break; case 2: e='-'; break; case 3: e='*'; break; case 4: e='/'; break; case 5: e='^'; break; } return(e); } void main() { clrscr(); infix i; cout<<"\n\n ENTER THE INFIX EXPRESSION:"; cin>>inf; i.postfix(); getch(); }

OUTPUT: ENTER THE INFIX EXPRESSION:a*b/c+(d*e)-f postfix expression is:ab*c/de*+f-

RESULT:

43

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex no: 7.1 Date :

ARRAY IMPLEMENTATION OF QUEUE ADT

AIM: To develop a C++ program for implementing queue abstract data type using array implementation.

ALGORITHM: STEP 1: Start the program and include the necessary header files for the program STEP 2: Declare the necessary variables and the structure for the queue. STEP 3: Create the main function and declare the necessary operations to be performed on the queue. STEP 4: Define the display() function such that it is called whenever the elements in a queue has to be displayed. STEP 5: Define the insert() function such that it is called whenever an array element has to be inserted into the queue. STEP 6: Define the delete() function such that it is called whenever an array element has to be deleted from the queue. STEP 7: Get the choice from the user and call the necessary functions wherever needed STEP 8: Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> #include<stdlib.h> #define MAX 5 int queue_arr[MAX]; int rear=-1; int front=-1; class arr_q { public: void insert(); void del(); void display(); }; void arr_q::insert() { int item; if(rear==MAX-1)

44

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

{ cout<<"\n Queue overflow\n"; } else { if(front==-1) { front=0; } cout<<"\n Enter the element for adding in queue:"; cin>>item; rear=rear+1; queue_arr[rear]=item; } } void arr_q::del() { if(front==-1|| front>rear) { cout<<"\n Queue underflow\n"; return; } else { cout<<"\n Element deleted from queue is:"<<queue_arr[front]; front=front+1; } cout<<"\n"; } void arr_q::display() { int i; if(front==-1) { cout<<"\n Queue is empty\n"; } else { cout<<"\n Queue is:\n\n"; for(i=front;i<=rear;i++) { cout<<" "<<queue_arr[i]; }

45

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

cout<<"\n"; } } void main() { int choice; arr_q q; clrscr(); while(1) { cout<<"\n PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE..."; cout<<"\n\n 1.Insert\n"; cout<<" 2.Delete\n"; cout<<" 3.Display\n"; cout<<" 4.Quit\n"; cout<<"\n Enter your choice(1-4): "; cin>>choice; switch(choice) { case 1: q.insert(); break; case 2: q.del(); break; case 3: q.display(); break; case 4: exit(0); default: cout<<"\n Wrong choice\n"; } } getch(); }

46

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

OUTPUT: PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE... 1.Insert 2.Delete 3.Display 4.Quit Enter your choice(1-4): 2 Queue underflow PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE... 1.Insert 2.Delete 3.Display 4.Quit Enter your choice(1-4): 1 Enter the element for adding in queue:23 PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE... 1.Insert 2.Delete 3.Display 4.Quit Enter your choice(1-4): 1 Enter the element for adding in queue:34 PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE... 1.Insert 2.Delete 3.Display 4.Quit Enter your choice(1-4): 3 Queue is: 23 34

PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE...

47

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

1.Insert 2.Delete 3.Display 4.Quit Enter your choice(1-4): 2 Element deleted from queue is:23 PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE... 1.Insert 2.Delete 3.Display 4.Quit Enter your choice(1-4): 3 Queue is: 34 PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE... 1.Insert 2.Delete 3.Display 4.Quit Enter your choice(1-4): 4

RESULT:

48

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Ex no: 7.2 Date :

LINKED LIST IMPLEMENTATION OF QUEUE ADT

AIM: To develop a C++ program for implementing queue abstract data type using linked list implementation.

ALGORITHM: STEP 1: Start the program and include the necessary header files for the program STEP 2: Declare the necessary variables and the structure for the queue. STEP 3: Create the main function and declare the necessary operations to be performed on the queue. STEP 4: Define the display() function such that it is called whenever the elements in a queue has to be displayed. STEP 5: Define the insert() function such that it is called whenever element has to be inserted into the queue. STEP 6: Define the delete() function such that it is called whenever element has to be deleted from the queue. STEP 7: Get the choice from the user and call the necessary functions wherever needed STEP 8: Stop the program.

PROGRAM : #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<malloc.h> struct node { int info; struct node * link; }*front=NULL,*rear=NULL; class list_q { public: void insert(); void del(); void display(); }; void list_q::insert() { struct node*tmp;

49

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

int added_item; tmp=(struct node*)malloc(sizeof(struct node)); cout<<"\n\nInput the element for adding in queue:"; cin>>added_item; tmp->info=added_item; tmp->link=NULL; if(front==NULL) front=tmp; else rear->link=tmp; rear=tmp; } void list_q::del() { struct node*tmp; if(front==NULL) { cout<<"\n\nQUEUE UNDERFLOW\n";; } else { tmp=front; cout<<"\n\nDELETED ELEMENT IS:"<<tmp->info; front=front->link; free(tmp); } } void list_q::display() { struct node*ptr; ptr=front; if(front==NULL) { cout<<"\n\nQUEUE IS EMPTY\n"; } else { cout<<"\n\nQUEUE ELEMENTS :\n\n"; while(ptr!=NULL) { cout<<"\n\t"<<ptr->info<<"\n"; ptr=ptr->link; }

50

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

cout<<"\n"; } } void main() { clrscr(); list_q l; int choice; while(1) { cout<<"\n\n PROGRAM FOR LINKED LIST IMPLEMENTATION OF QUEUE..."; cout<<"\n\n\n 1.INSERT\n"; cout<<"\n 2.DELETE\n"; cout<<"\n 3.DISPLAY\n"; cout<<"\n 4.QUIT\n"; cout<<"\n ENTER YOUR CHOICE(1-4):"; cin>>choice; switch(choice) { case 1: l.insert(); break; case 2: l.del(); break; case 3: l.display(); break; case 4: exit(0); default: cout<<"\n\n WRONG CHOICE(1-4):"; } } getch(); }

51

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

OUTPUT: 1.INSERT 2.DELETE 3.DISPLAY 4.QUIT ENTER YOUR CHOICE(1-4):1 Input the element for adding in queue:4

PROGRAM FOR LINKED LIST IMPLEMENTATION OF QUEUE... 1.INSERT 2.DELETE 3.DISPLAY 4.QUIT ENTER YOUR CHOICE(1-4):3 QUEUE ELEMENTS : 2 4 PROGRAM FOR LINKED LIST IMPLEMENTATION OF QUEUE... 1.INSERT 2.DELETE 3.DISPLAY 4.QUIT ENTER YOUR CHOICE(1-4):2

52

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

DELETED ELEMENT IS:2

PROGRAM FOR LINKED LIST IMPLEMENTATION OF QUEUE... 1.INSERT 2.DELETE 3.DISPLAY 4.QUIT ENTER YOUR CHOICE(1-4):3

QUEUE ELEMENTS :

4 PROGRAM FOR LINKED LIST IMPLEMENTATION OF QUEUE...

1.INSERT 2.DELETE 3.DISPLAY 4.QUIT ENTER YOUR CHOICE(1-4):4

RESULT:

53

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

SEARCH TREE ADT - BINARY SEARCH TREE EX NO: 8 DATE: AIM: To write a program to implement search tree ADT using Binary Search Tree ALGORITHM: Step 1: Start the process. Step 2: Initialize and declare variables. Step 3: Construct the Tree Step 4: Data values are given which we call a key and a binary search tree Step 5: To search for the key in the given binary search tree, start with the root node and Compare the key with the data value of the root node. If they match, return the root pointer. Step 6: If the key is less than the data value of the root node, repeat the process by using the left subtree. Step 7: Otherwise, repeat the same process with the right subtree until either a match is found or the subtree under consideration becomes an empty tree. Step 8: Terminate PROGRAM: #include<stdio.h> #include<conio.h> #include<process.h> #include<alloc.h> struct tree { int data; struct tree *lchild; struct tree *rchild; }*t,*temp; int element; class bst { public: // int element; void inorder(struct tree *);

54

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

void preorder(struct tree *); void postorder(struct tree *); struct tree * create(struct tree *, int); struct tree * find(struct tree *, int); struct tree * insert(struct tree *, int); struct tree * del(struct tree *, int); struct tree * findmin(struct tree *); struct tree * findmax(struct tree *); }; struct tree * bst :: create(struct tree *t, int element) { t=(struct tree *)malloc(sizeof(struct tree)); t->data=element; t->lchild=NULL; t->rchild=NULL; return t; } struct tree * bst :: find(struct tree *t, int element) { if(t==NULL) return NULL; if(element<t->data) return(find(t->lchild,element)); else if(element>t->data) return(find(t->rchild,element)); else return t; } struct tree * bst :: findmin(struct tree *t) { if(t==NULL) return NULL; else if(t->lchild==NULL) return t; else return(findmin(t->lchild)); }

55

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

struct tree* bst :: findmax(struct tree *t) { if(t!=NULL) { while(t->rchild!=NULL) t=t->rchild; } return t; } struct tree* bst :: insert(struct tree *t,int element) { if(t==NULL) { t=(struct tree *)malloc(sizeof(struct tree)); t->data=element; t->lchild=NULL; t->rchild=NULL; return t; } else { if(element<t->data) { t->lchild=insert(t->lchild,element); } else if(element>t->data) { t->rchild=insert(t->rchild,element); } else if(element==t->data) { printf("element already present\n"); } return t; } } struct tree * bst :: del(struct tree *t, int element)

56

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

{ if(t==NULL) printf("element not found\n"); else if(element<t->data) t->lchild=del(t->lchild,element); else if(element>t->data) t->rchild=del(t->rchild,element); else if(t->lchild&&t->rchild) { temp=findmin(t->rchild); t->data=temp->data; t->rchild=del(t->rchild,t->data); } else { temp=t; if(t->lchild==NULL) t=t->rchild; else if(t->rchild==NULL) t=t->lchild; free(temp); } return t; } void bst :: inorder(struct tree *t) { if(t==NULL) return; else { inorder(t->lchild); printf("\t%d",t->data); inorder(t->rchild); } } void bst :: preorder(struct tree *t) { if(t==NULL)

57

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

return; else { printf("\t%d",t->data); preorder(t->lchild); preorder(t->rchild); } } void bst :: postorder(struct tree *t) { if(t==NULL) return; else { postorder(t->lchild); postorder(t->rchild); printf("\t%d",t->data); } } void main() { int ch; bst obj; do { printf("\n\t\t\tBINARY SEARCH TREE"); printf("\n\t\t\t****** ****** ****"); printf("\nMain Menu\n"); printf("\n1.Create\n2.Insert\n3.Delete\n4.Find\n5.FindMin\n6.FindMax"); printf("\n7.Inorder\n8.Preorder\n9.Postorder\n10.Exit\n"); printf("\nEnter ur choice :"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the data:"); scanf("%d",&element); t=obj.create(t,element);

58

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

obj.inorder(t); break; case 2: printf("\nEnter the data:"); scanf("%d",&element); t=obj.insert(t,element); obj.inorder(t); break; case 3: printf("\nEnter the data:"); scanf("%d",&element); t=obj.del(t,element); obj.inorder(t); break; case 4: printf("\nEnter the data:"); scanf("%d",&element); temp=obj.find(t,element); if(temp->data==element) printf("\nElement %d is at %d",element,temp); else printf("\nElement is not found"); break; case 5: temp=obj.findmin(t); printf("\nMix element=%d",temp->data); break; case 6: temp=obj.findmax(t); printf("\nMax element=%d",temp->data); break; case 7: obj.inorder(t); break; case 8: obj.preorder(t); break; case 9: obj.postorder(t); break;

59

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

case 10: exit(0); } }while(ch<=10); } OUTPUT: BINARY SEARCH TREE ****** ****** **** Main Menu 1.Create 2.Insert 3.Delete 4.Find 5.FindMin 6.FindMax 7.Inorder 8.Preorder 9.Postorder 10.Exit Enter ur choice : 1 Enter the data:12 12 BINARY SEARCH TREE ****** ****** **** Main Menu 1.Create 2.Insert 3.Delete 4.Find 5.FindMin 6.FindMax 7.Inorder 8.Preorder 9.Postorder 10.Exit

60

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Enter ur choice :2 Enter the data:43 12 43 BINARY SEARCH TREE ****** ****** **** Main Menu 1.Create 2.Insert 3.Delete 4.Find 5.FindMin 6.FindMax 7.Inorder 8.Preorder 9.Postorder 10.Exit Enter ur choice :8 12 23 45

32

65

56

BINARY SEARCH TREE ****** ****** **** Main Menu 1.Create 2.Insert 3.Delete 4.Find 5.FindMin 6.FindMax 7.Inorder 8.Preorder 9.Postorder 10.Exit Enter ur choice :9 32 56 65 RESULT:

45

23

12

61

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

HEAP SORT EX NO: 9 DATE: AIM: To write a C++ program to implement Heap sort. ALGORITHM: Step 1: Get the size of the array from the user. Step 2: Get the elements to be sorted. Step 3: Sorting is performed when we call the heap sort function. Step 4: Now the array is contained with sorted elements. Step 5: Display the sorted elements.

PROGRAM: #include<stdio.h> #include<conio.h> #include<iostream.h> class heaps { public: void heapsort(int x[],int); }; void heaps :: heapsort(int x[],int arr) { int i,m; int lc,rc,mc,rt,temp; rt=(arr-1)/2; for(m=rt;m>=0;m--) { for(i=rt;i>=0;i--) { lc=(2*i)+1; rc=(2*i)+2; if((lc<=arr)&&(rc<=arr)) { if(x[rc]>=x[lc]) mc=rc; else

62

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

mc=lc; } else { if(rc>arr) mc=lc; else mc=rc; } if(x[i]<x[mc]) { temp=x[i]; x[i]=x[mc]; x[mc]=temp; } } } temp=x[0]; x[0]=x[arr]; x[arr]=temp; return; } void main() { int x[100],i,n; heaps obj; clrscr(); cout<<"\nEnter the size:"; cin>>n; printf("Enter %d elements:",n); for(i=0;i<n;i++) { cin>>x[i]; } for(i=n;i>1;i--) { obj.heapsort(x,i-1); } cout<<"\nSorted elements are:"; for(i=0;i<n;i++) { cout<<"-->"<<x[i]<<endl; } getch(); }

63

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

OUTPUT: Enter the size: 5 Enter 5 elements: 90 67 12 75 01 sorted elements: 1 12 67 75 90

RESULT:

64

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

QUICK SORT EX NO: 10 DATE: AIM: To write a program in C++ to implement Quick sort. ALGORITHM: Step 1: Get the value of how many no. to be sorted. Step 2: Get the elements from the user. Step 3: Two function qsort() and swap(). Quick sort to perform sorting. Step 4: Swap() is just to rearrange the values. Step 5: Quick sort algorithm works by partitioning the array to be sorted, then recursively sorting each partition. Step 6: Display the sorted value. PROGRAM: #include<stdio.h> #include<conio.h> #include<iostream.h> int i,j,n,pi,a[20]; class quicksort { public: void qsort(int a[],int ft,int lt); void swap(int a[],int i,int j); }; void quicksort :: qsort(int a[],int lb,int ub) { if(lb<ub) { pi=a[lb]; i=lb; j=ub; while(i<j) { while(a[i]<=pi && i<ub) i++; while(a[i]>=pi && j>lb) j--; if(i<j) swap(a,i,j); } swap(a,lb,j);

65

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

qsort(a,lb,j-1); qsort(a,j+1,ub); } } void quicksort :: swap(int a[],int i,int j) { int t; t=a[i]; a[i]=a[j]; a[j]=t; } void main() { int n,i,a[20]; quicksort obj; clrscr(); cout<<"\nEnter the size:"; cin>>n; cout<<"Enter the elements:"; for(i=0;i<n;i++) cin>>a[i]; obj.qsort(a,0,n-1); cout<<"\nSorted List:"; for(i=0;i<n;i++) cout<<"\n"<<a[i]; getch(); } OUTPUT: Enter the size: 5 Enter the elements: 85 32 46 04 96 Sorted list: 04 32 46 85 96 RESULT:

66

Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual T.Abirama Sundari-AP/ECE

Anda mungkin juga menyukai