EC6312 - lab manual



Comments



Description

DEFAULT ARGUMENTSAim: To implement function with default arguments. Algorithm: 1. 2. 3. 4. Declare the Default function in the outside of the main function. Get the values. Define the default function Print the values Program: #include<iostream.h> void printLine(char =’_’,int =70); void main() { printLine(); printLine(‘/’); printLine(‘*’,40); printLine(‘R’,55); } void printLine(char ch, int Repeatcount) { int i; cout<<endl; for(i=0;i<Repeatcount;i++) cout<<ch; } Output: -------- --------------------------/////////////////////////////////////// **************************** RRRRRRRRRRRRRRRRRRRRRRR Result: Thus the program for default arguments has been executed and verified successfully. EC6312 – OOPS and Data Structures Lab Ms.M.Abinaya, Lect/IT 1 CLASS WITH STATIC DATA MEMBER Aim: To implement static data member in class. Algorithm: 1. 2. 3. 4. Create class ITEM with static data member as count. Create a member function to increment the count. Declare the static datamember using scope resolution operator. Display the count value. Program: #include<iostream.h> class item { static int count; int num; public: void getdata(int a) { num=a; count++; cout<<”Number”<<num; } void showcount() { cout<<”count”; cout<<count<<”\n”; } }; int item::count; int main() { item a,b,c; a.showcount(); b.showcount(); c.showcount(); a.getdata(20); b.getdata(30); EC6312 – OOPS and Data Structures Lab Ms.M.Abinaya, Lect/IT 2 c.getdata(40); a.showcount(); b.showcount(); c.showcount(); } Output: count count count Number Number Number count count count 0 0 0 20 30 40 3 3 3 Result: Thus the program for static data member has been executed and verified successfully CLASS WITH STATIC MEMBER FUNCTION Aim: To implement static member function in class. Algorithm: 1. 2. 3. 4. Create class ITEM with static data member as count. Create a member function to increment the count. Declare the static data member using scope resolution operator. Display the count value. Program: #include<iostream.h> class test { int code; static int count; public: EC6312 – OOPS and Data Structures Lab Ms.M.Abinaya, Lect/IT 3 void setcode(void) { code= ++count; } void showcode(void) { cout<<”Object Number:”<<code<<”\n”; } static void showcount(void) { cout<<”Count = “<<count<<”\n”; } }; int test::count; int main() { test t1,t2; t1.setcount(); t2.setcount(); test::showcount(); test t3; t1.showcode(); t2.showcode(); t3.showcode(); return(0); } Output: count count Object Number Object Number Object Number 2 3 1 2 3 Result: Thus the program for static member function has been executed and verified successfully. EC6312 – OOPS and Data Structures Lab Ms.M.Abinaya, Lect/IT 4 T imag. void outdata(char *msg) { cout<<msg<<”(“<<real.h> template<class T> class complex { private: T real. Add real part with real of other object and store it in temp’s real.M. Declare a temporary variable temp. Display temp.Abinaya. EC6312 – OOPS and Data Structures Lab Ms. 2. cin>>real. Algorithm: 1. Assign the value for real and imaginary part.ADDITION OF TWO COMPLEX NUMBERS Aim: To write a program for object as argument and returning an object using complex number addition. } void getdata() { cout<<”real part?”. } complex operator +(complex c2). cin>>imag. 5. 3. 6. cout<<”imag part?”. Public: complex() { real=imag=0. 4. Program: #include<iostream. Lect/IT 5 . The class complex contains two member variables real and imaginary. Add imaginary part with imaginary of other object and store it in temp’s imaginary. Real part?3 Imag part?4 EC6312 – OOPS and Data Structures Lab Ms. c2. c3. c5.c6. Lect/IT 6 .getdata(). temp. } void main() { complex<int>c1.. cout<<”Enter complex number c1. return(temp). c6=c4+c5.outdata(“c6=c4+c5:”).”<<endl..”<<endl. cout<<”Enterthecomplexnumberc2”<<endl.c2. Real part?1 Imag part?2 Enter complex number c2. cout<<”Enterthecomplexnumberc5”<<endl.”<<img<<”)”<<endl.getdata(). cout<<”Addition of integercomplexobjects….cout<<”. } }. c3=c1+c2.getdata().”<<endl. cout<<”Additionof float complexobjects…. temp.outdata(“c3=c1+c2:”).. c6.getdata().Abinaya.img=imag+c2..real. cout<<”Enter complex number c4. c4. c1.imag.real=real+c2.M. } Output: Addition of integer complexobjects… Enter complex number c1. Template<class T> complex<T> complex<T>::operator +(complex<T>c2) { Complex<T>temp.c5. complex<float>c4.c3.”<<endl. 4. 2. }.M.7 C6=c4+c5:(3. int j) { a = i.4 Imag part?3. b. class myclass { int a. Perform the operation of adding two private variables in the friend function. Declare the friend function using the keyword friend. Lect/IT 7 .. void set_ab(int i.5 Imag part?2. public: friend int sum(myclass x).. Display the result. 3. void myclass::set_ab(int i.5 Enter complex number c5.h> using namespace std.Abinaya. EC6312 – OOPS and Data Structures Lab Ms. Real part?2.6) Addition of float complexobjects… Enter complex number c4. int j). FRIEND FUNCTION Aim: To write a c++ program for friend function. Create the class and declare the data member as private.6. Program: #include <iostream.C3=c1+c2:(4.2) Result: Thus the program for addition of two complex numbers has been executed and verified successfully. Algorithm: 1. Real part?1.9. it can directly access a and b. 4).b. Putwage: Assign the valve for wage variable.getwage() and getname(). int sum(myclass x) { /* Because sum() is a friend of myclass. } // Note: sum() is not a member function of any class. 3. 5. return 0. 4.Abinaya. 2. Lect/IT 8 . } int main() { myclass n. Im main() Put and display both name and wages. */ return x. Getwage: Retrieves the value of wage variable. Algorithm: 1. } Output: 7 Result: Thus the program for friend function has been executed and verified successfully. 6.a + x. n. cout << sum(n). Program: EC6312 – OOPS and Data Structures Lab Ms. Putname: Assign the valve for the character array name. putwage(). Employee class contains name and wage variable and member function a putname(). CLASS AND OBJECTS Aim: To write a c++ program for employee wages calculation using class and objects.set_ab(3.b = j. Getname: Retrieves the value of Variable name.M. getname(name). return 0. }. cout << ted.".M.#include <iostream. } int main() { employee ted.getwage() << " per year. cout << name << " makes $".h> class employee { char name[80]. // private by default public: void putname(char *n).putwage(75000). } void employee::putwage(double w) { wage = w. } void employee::getname(char *n) { strcpy(n. // now. ted. ted.putname("Ted Jones"). } EC6312 – OOPS and Data Structures Lab Ms.Abinaya. name). private again public: void putwage(double w). private: double wage. Lect/IT 9 . n). ted. // these are public void getname(char *n). // back to public double getwage(). void employee::putname(char *n) { strcpy(name. } double employee::getwage() { return wage. char name[80]. Define the base class with variables and functions. Inherits base class.M. INHERITANCE Aim: To Write a C++ program for implementing the inheritance. Access member of derived class. 6.Output: Ted Jones makes $75000 per year. Lect/IT 10 . 4. Result: Thus the program for classes and objects has been executed and verified successfully. } void show_x() { cout<<”\n\t Base class….”. Define main function Declare the variables. Define the derived class with variables and functions. EC6312 – OOPS and Data Structures Lab Ms. class derived:public base { int y.h> class base { public: int x. 2. cout<<”\n\tx=”<<x. Program: #include<iostream. } }.Abinaya. void set_x(int n) {x=n. 3. 5. Algorithm: 1. //acess member of derived class obj. Lect/IT 11 .y. cin>>x. x=10 y=20 Result: Thus the program for inheritance has been executed and verified successfully.Abinaya. int x. cin>>y. x=10 derived class…. void main() { derived obj. cout<<”\n enter the value of y”.M.show_xy().//acess member of derived class } Output: enter the value of x 10 enter the value of y 20 base class….//inherits base class obj. cout<<”\n\ty=”<<y.show_x().//inherits base class obj.public: void set_y(int n) { y=n.set_y(y).. obj. EC6312 – OOPS and Data Structures Lab Ms. cout<<”\n enter the value of x”.set_x(x). } }. } void show_xy() { cout<<”\n\n\t derived class…”. cout<<”\n\tx=”<<x. 2. } void main() { test obj.M.double b) { return(a+b). int test::sum(int a.int). Algorithm: 1. Define the functions with different arguments.b.double). int choice. 4. 5. 6. double sum(double. Call the Different task of function. float sum(float.FUNCTION OVERLOADING Aim: To Write a C++ program for implementing the function overloading. }.h> class test { public: int sum(int. EC6312 – OOPS and Data Structures Lab Ms. } float test::sum(float a . Define main function Declare the variables.float). int a. Print the output. 3. Define class. Lect/IT 12 .float b) { return(a+b).Abinaya. Program: #include<iostream.int b) { return(a+b). } double test::sum(double a. Addition of two double numbers”<<endl. cin>>x>>y.n.M. getch().float x.Addition of two integer numbers 2. break. } cout<<”\n\n result”<<result<<endl. switch(choice) { case 1: cout<<”\n enter 2 numbers”.y. break. break. case 3: cout<<”\n enter 2 number”. result=obj.sum(x.Abinaya. result=obj. } Output: Main menu 1.n). Lect/IT 13 . default: cout<<”wrong choice”. break. Addition of two integer numbers”. cin>>a>>b.y). cout<<\n enter your choice:”. cin>>m>>n. cout<<”\n\t\t main menu”. case 2: cout<<”\n enter 2 number”. double m.sum(a. cout<<”\n\t2.Addition of two float numbers 3. cout<<”\n\t1.Addition of two double numbers enter your choice 2 EC6312 – OOPS and Data Structures Lab Ms. cin>>choice. double result=0. cout<<”\n\t3. result=obj. Addition of two float numbers”.sum(m.b). Display the values. 5. int main () { int a = 100. value of b :" << b << endl. x = y. value of b :" << b << endl. int b = 200. value of a :" << a << endl. b). cout << "Before swap. EC6312 – OOPS and Data Structures Lab Ms.M. int &y).73 Result: Thus the program for function overloading has been executed and verified successfully.h> void swap(int &x. 3. swap(a. return 0. Declare the swap function.Abinaya. cout << "After swap. Program: #include <iostream. 4.13 5.enter 2 number 1. value of a :" << a << endl.6 result 6. } void swap(int &x. The function has the values with reference Call the swap function in function main Define the swap function with the reference value. int &y) { int temp. cout << "Before swap. CALL BY RREFERENCE Aim: To write a C++ program for Call by Reference Algorithm: 1. Lect/IT 14 . 2. cout << "After swap. temp = x. y = temp. h> void swap(int x.M. } Output: Before swap. Algorithm: 1. value of a :" << a << endl. cout << "After swap. 5. value of b:200 After swap. CALL BY VALUE Aim: To write a C++ program for call by value. value of a:100 Before swap.Abinaya. int main () { int a = 100. 2. 3. 4. b). value of a :" << a << endl. The function has the values with value Call the swap function in function main Define the swap function with the value.return. cout << "Before swap. swap(a. return 0. Display the values. value of b :" << b << endl. value of a:200 After swap. int y) EC6312 – OOPS and Data Structures Lab Ms. cout << "After swap. } void swap(int x. Declare the swap function. Program: #include <iostream. cout << "Before swap. value of b :" << b << endl. int y). int b = 200. value of b:100 Result: Thus the program for Call by Reference has been executed and verified successfully. Lect/IT 15 . 6. value of b:100 Result: Thus the program for Call by value has been executed and verified successfully.{ int temp. add() and mul() function are used to perform addition and multiplication of the matrices. create objects A and B for the Matrix class. Declare the class as Matrix. Declare the data member as r. } Output: Before swap. Declare the member function as Member function with default argument is used to initialize the value of the matrix. get() function is used to get the values of two matrices. default argument and friend function. In the main. c and **x.M. x = y. value of b:200 After swap. Call the add() and mul() method to perform the particular operation and finally display the result. temp = x. y = temp. FRIEND AND DEFAULT FUNCTIONS Aim: To write a C++ program to perform matrix manipulation using static variable. 5.Abinaya. 2. value of a:200 After swap. 7. EC6312 – OOPS and Data Structures Lab Ms. Lect/IT 16 . Call the get() method to get the value of matrix A and B. MATRIX MULTIPLICATION USING STATIC. 4. Algorithm: 1. 3. return. 8. value of a:100 Before swap. j<c.i++) for(int j=0.i++) x[i]=new int[c].matrix).i<r. matrix::matrix(int r1.i<r.int c1) { r=r1.h> class matrix { static int r. public: matrix(int r1=2. for(int i=0.h> #include<conio. friend matrix add(matrix.j++) { x[i][j]=0. for(i=0.j<c. friend matrix mul(matrix.Program: #include<stdio.int c1=2).c=c1. for(int i=0.Abinaya.i<r. int**x.matrix). } } void matrix::get() { cout<<"\n enter the matrix of size"<<r<<"x"<<c<<endl.h> #include<iomanip. void put().j++) cin>>x[i][j].i++) for(int j=0. } void matrix::put() { EC6312 – OOPS and Data Structures Lab Ms. x=new int*[r].M. void get().c. Lect/IT 17 . }. } int matrix::r. b.i++.Abinaya.b).for(int i=0.cout<<endl) for(int j=0.j++) c.i++) for(int j=0.b. a. return c.k<a.i<r.j<c.put(). matrix add(matrix a. cout<<"The resultant matrix (A+B):"<<endl.j++) for(int k=0.x[i][j]=a.x[k][j]).x[i][k]*(b.x[i][j]+(b. EC6312 – OOPS and Data Structures Lab Ms. b.j<a.r.j++) cout<<setw(4)<<x[i][j].x[i][j]).x[i][j]=c.i++) for(int j=0.c. for(int i=0. Lect/IT 18 .c.k++) { c.c. cout<<"The matrix A:"<<endl.M. } matrix mul(matrix a.c1.d1. matrix a. c1. for(int i=0. } return c.get().x[i][j]+a.put().i<a.matrix b) { matrix c.get(). int matrix::c. } void main() { clrscr(). a.r. cout<<"The matrix B:"<<endl.i<a.j<b. c1=add(a.matrix b) { matrix c.put(). Abinaya. Lect/IT 19 . default argument. EC6312 – OOPS and Data Structures Lab Ms.put(). d1=mul(a.M. d1.b). } 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: Thus the program for matrix multiplication using static variable .cout<<"\n\n The resultant matrix(A*B):"<<endl. getch(). friend function has been executed and verified successfully. h> #include<conio.h> class arr_list { private: struct node { int data. insert & delete. and increment the index value and add the values after head. 7.M.h> #include<string. Create a structure called Node Create an array of size 10. 9. EC6312 – OOPS and Data Structures Lab Ms.Run the program. In create function get the values and keep it as a head. 5. 11.In search function. Lect/IT 20 . } a[10].h> #include<stdlib. In delete function delete any node and add next node to the previous node link. public: int head. 4. int next. break the link and add the new inserted node. Create a constructor for initializing the variables Create a variables head. In insert function. Program: #include<iostream. In display function display all the values as a list format.Write the function definition 12. search the new node whether it is present or not. insert the new node. 6. 2. Create functions for list & create .Abinaya. 3. 10.IMPLEMENTATION OF VARIOUS LIST OPERATIONS USING ARRAYS Aim: To Implement a program for various List operations using arrays Algorithm: 1. 8. M.next. } } int arr_list::create() { int head.data. cout<<"\nenter the index for first node". } i=a[i]. cin>>i. else { cout<<a[i]. head=i. while(i!=-1) { cout<<"\nenter the data and index of the first element". cin>>a[i]. cout<<"".Abinaya.data<<"->". cin>>a[i]. void search().i<10. void insert(). int create(). void del(). arr_list::arr_list() { for(int i=0. } return head.next. Lect/IT 21 .data==-1) cout<<"". i=a[i]. EC6312 – OOPS and Data Structures Lab Ms.next. void display(int).arr_list(). }.i.data=-1. } void arr_list::display(int i) { while(i!=-1) { if(a[i].i++) { a[i]. next.data=new_data.temp.new_data. cout<<"\nenter the node to be delete".next==-1) { a[i]. } current=i.M. } } } } void arr_list::del() { int i. for(i=0. cout<<"\nenter the new data which is to be inserted".data==temp) { if(a[i].i<10.next.i<10.i++) { if(a[i].next=a[i]. cin>>temp.data++-1) { a[i+1].current.Abinaya. } void arr_list::insert() { int i. for(i=0.temp.data=-1. } } for(i=0.i++) { if(a[i]. Lect/IT 22 . cin>>new_data. cin>>temp.new_next. a[i+1]. cout<<"\nenter the data after whice you want to insert".next=i+1.i<10.} cout<<"NULL". a[i]. new_next=a[i].i++) EC6312 – OOPS and Data Structures Lab Ms.data==temp) { if(a[i+1]. for(i=0. else cout<<"\nthe node is not present". cout<<"\n5. clrscr(). a[current].deletion of element form the list".insertion of element in the list".temp.next=new_next. cin>>temp. cout<<"\t\tprogram for implementing list using array".M.{ if(a[i]. } } } void arr_list::search() { int i.searching of element from the list".data==temp) { flag=1. cout<<"\n1. } void main() { char ans. cout<<"\nenter the node to be searched".flag=0.next==current) { a[i].creation". Lect/IT 23 . cout<<"\n2. cout<<"\n6. cout<<"\nmain menu". int choice. arr_list obj.display". } } if(flag==1) cout<<"\nthe"<<temp<<"node is present is the list".i++) { if(a[i]. break.data=-1. cout<<"\n4.exit". do EC6312 – OOPS and Data Structures Lab Ms.Abinaya. cout<<"\n3.i<10. search(). break.display(obj.Abinaya. } while(ans=='Y.display(obj.head). case 3: obj. case 2: obj. break. obj. } EC6312 – OOPS and Data Structures Lab Ms. } cout<<"\ndo you wish to go to main menu?".head). obj.M. cin>>choice.insert(). cout<<"Thank You".head).{ cout<<"\nenter your choice". case 4: obj. Lect/IT 24 . getch().head=obj. switch(choice) { case 1: obj. break. break.display(obj. case 6: exit(0).create(). case 5: obj. ans=getch().del(). break.'||ans=='y'). Abinaya.display 3. Lect/IT 25 .searching of element from the list 6.exit enter your choice1 enter the index for first node3 enter the data and index of the first element2 5 enter the data and index of the first element5 4 enter the data and index of the first element4 -1 do you wish to go to main menu? enter your choice2 2->5->4->NULL do you wish to go to main menu? enter your choice3 enter the new data which is to be inserted6 enter the data after whice you want to insert5 2->5->6->4->NULL do you wish to go to main menu? enter your choice4 enter the node to be delete5 2->6->4->NULL do you wish to go to main menu? enter your choice5 enter the node to be searched6 the6node is present is the list EC6312 – OOPS and Data Structures Lab Ms.creation 2.Output: program for implementing list using array main menu 1.M.deletion of element form the list 5.insertion of element in the list 4. c. Algorithm: 1.M. EC6312 – OOPS and Data Structures Lab Ms. program was executed LINKED LIST IMPLEMENTATION USING LISTADT Aim: To implement a linked list and do all operations on it. Find the node after which the new node is to be inserted. Initialize and declare variables. 2. If choice is INSERT then a. Print the linked list after insertion. Ie LINK[PAR] = LINK [ LOC]. Enter the element to be inserted. Adjust the link fields. 4. d. Enter the element to be deleted. c.Abinaya. b. e. Start the process. d. Enter the choice. 5. Find the node containing the element (LOC) and its preceding node (PAR). Lect/IT 26 . Get a new node and set DATA[NEWNODE] = ITEM.do you wish to go to main menu? enter your choice5 enter the node to be searched1 the node is not present do you wish to go to main menu? enter your choice6 Result: Thus the Array implantation of List ADT successfully. Print the linked list after deletion. Set ITEM = DATA[LOC] and delete the node LOC. e. If choice is DELETE then a. Adjust the link fields so that PAR points to the next element. b. 3. M. delete head.h> #define TRUE 1 #define FALSE 0 class sll { private: struct node { int data. void insert_after(). void search(int key). void create(). temp=head->next. void display(). } *head. ~sll().h> #include<conio. Lect/IT 27 .Abinaya.6. while(temp!=NULL) { EC6312 – OOPS and Data Structures Lab Ms. } sll::~sll() { node*temp. void insert_head(). Stop the process. }.*temp1.h> #include<stdlib. void dele(). struct node*next. void insert_last(). sll::sll() { head=NULL. Program: #include<iostream. public: sll(). *New. flag=FALSE.n)". Lect/IT 28 . New->data=val. } cout<<"\ndo you want to enter more elements?(Y. cout<<"\nthe singly linked list is created\n". EC6312 – OOPS and Data Structures Lab Ms. do { cout<<"\nenter the data". } else { temp->next=New. getch(). } void sll::display() { node*temp.Abinaya. if(New==NULL) cout<<"unable to allocate memory\n". char ans. New->next=NULL. if(flag==TRUE) { head=New. } } void sll::create() { node*temp. temp=New.flag. ans=getch(). delete temp. temp=temp1. New=new node. }while(ans=='y'||ans=='Y'). flag=TRUE. temp=head.M. int val. temp=head. cin>>val.temp1=temp->next. } } void sll::dele() { EC6312 – OOPS and Data Structures Lab Ms.if(temp==NULL) { cout<<"\nthe list is empty\n".Abinaya. getch(). } while(temp!=NULL) { cout<<temp->data<<" ". clrscr(). Lect/IT 29 . else found=TRUE. clrscr(). while(temp!=NULL&&found==FALSE) { if(temp->data!=key) temp=temp->next.M. getch(). } void sll::search(int key) { node*temp. getch(). } found=FALSE. } getch(). temp=temp->next. if(temp==NULL) { cout<<"linked list is empty\n". temp=head. int found. return. } if(found==TRUE) { cout<<"\n the element is present in the list\n". } if(temp==NULL) cout<<"\nnode not found". delete temp. temp=temp->next.*prev. else { if(temp==head) head=temp->next. else { temp=head.M. } getch(). New->next=NULL.node*temp. Lect/IT 30 . int key. cin>>New->data.*temp. temp=head. cout<<"\nthe element is deleted\n". } } EC6312 – OOPS and Data Structures Lab Ms. temp->next=New. cout<<"\nenter the data of the node you want to delete:". else prev->next=temp->next. cin>>key. if(head==NULL) head=New. } void sll::insert_last() { node*New. prev=temp. while(temp->next!=NULL) temp=temp->next. cout<<"\nenter the element which you want to insert".Abinaya. while(temp!=NULL) { if(temp->data==key) break. temp=head. cin>>New->data. } else temp=temp->next.Abinaya. } while(temp!=NULL). temp->next=New.M. node*temp. cin>>key. New=new node. if(head==NULL) { head=New. } else { cout<<"\nenter the element after whice you want to insert the node".*temp. do { if(temp->data==key) { New->next=temp->next. cout<<"\nenter the element whice you want to insert". New=new node. if(head==NULL) head=New. EC6312 – OOPS and Data Structures Lab Ms. break. } } void sll::insert_head() { node*New. cin>>New->data. else { temp=head.void sll::insert_after() { int key.*New. Lect/IT 31 . cout<<"\nenter the element which you want to insert". cout<<"\n1. cout<<"\n3.val. clrscr().delete an element from list". s. cout<<"\n2. int choice. } } void main() { sll s.M. break. case 2: s.create". head=New.insert at end".insert an element in a list". cout<<"\n5.search". cin>>ch1. case 3: cout<<"\nenter the element you want to search".display().New->next=temp.create().display". EC6312 – OOPS and Data Structures Lab Ms. break. cout<<"\nmenu". break.display(). cin>>val.insert after". switch(choice) { case 1: s. cout<<"\n1. s.search(val).quit". do { cout<<"\nenter your choice(1-6)". cout<<"\n4. cout<<"\n3. cin>>choice. case 4: cout<<"\nthe list is:\n".Abinaya. cout<<"\nprogram to perform various operations on linked list". cout<<"\nenter your choice". Lect/IT 32 . cout<<"\n6.ch1.insert at beginning\n2. Lect/IT 33 . s. break. } }while(1). getch().Abinaya. case 6: exit(0). break.delete an element from list 6. default: cout<<"\ninvalid choice".insert_after().switch(ch1) { case 1: s.display(). break.insert an element in a list 5.insert_head(). default: cout<<"\n invalid choice". break. case 3: s. case 5: s.M. } s.insert_last().create 2.dele(). break.search 4. } Output: program to perform various operations on linked list 1.quit enter your choice(1-6)1 EC6312 – OOPS and Data Structures Lab Ms. case 2: s.display 3.display(). n) enter the data4 do you want to enter more elements?(Y.n) the singly linked list is created enter your choice(1-6)2 234 enter your choice(1-6)3 enter the element you want to search2 the element is present in the list enter your choice(1-6)3 enter the element you want to search1 enter your choice(1-6) 4 the list is: 234 menu 1.enter the data2 do you want to enter more elements?(Y.M. Lect/IT 34 .Abinaya.insert at end enter your choice1 enter the element which you want to insert1 1234 enter your choice(1-6)4 the list is: 1234 menu EC6312 – OOPS and Data Structures Lab Ms.n) enter the data3 do you want to enter more elements?(Y.insert at beginning 2.insert after 3. 1. Lect/IT 35 .Abinaya.M. EC6312 – OOPS and Data Structures Lab Ms.insert at end enter your choice2 enter the element whice you want to insert5 enter the element after whice you want to insert the node3 12354 enter your choice(1-6)4 the list is: 12354 menu 1.insert after 3.insert after 3.insert at beginning 2.insert at end enter your choice3 enter the element which you want to insert6 123546 enter your choice(1-6)5 enter the data of the node you want to delete:2 the element is deleted 13546 enter your choice(1-6)6 Result: Thus the given program Linked List implementation of the list ADT was executed successfully.insert at beginning 2. Check for the start. 7.CURSOR IMPLEMENTATION – LIST ADT Aim: To write a C++ program for cursor implementation of list ADT. Create a node with two fields data and link field. 6. void display(int). Delete the node and then link it to the next node.M. For insertion get the position in which insertion is to be done and the element to be inserted. o Create link between the created nodes and let the last node be with NULL Link o Insert the input data in the data field and press –1 to stop the same. 3.Abinaya. 2.h> #include<conio. Insert the node and change its link accordingly. Using display option list the elements of the list.h> #define MAX 20 class LIST { private: int list[MAX]. void delet(int). Program: #include<iostream. Stop the program.h> #include<stdlib. 5. public: int create(). For deletion get the position in which deletion is to be done. Get the choice of operations either insertion or deletion. Algorithm: 1. Before deletion check whether there is data in the list to be deleted. EC6312 – OOPS and Data Structures Lab Ms. }. Start the program. middle or end position of insertion. o Allocate space for the node dynamically. 4. void reverse(int). int search(int). Lect/IT 36 . int LIST::create() { int n,i; clrscr(); cout<<"\nhow many elements you want in the list:"; cin>>n; if(n>MAX) cout<<"\nerror:Number of elements exceeds the limit"; for(i=0;i<n;i++) { cout<<"\nenter the element number"<<i+1<<":"; cin>>list[i]; } cout<<"\nthe List is successfully\n"; getch(); return(n); } void LIST::display(int n) { int i; clrscr(); cout<<"\nthe List is...\n"; for(i=0;i<n;i++) cout<<"\n"<<list[i]; cout<<"\npress any key to continue...\n"; getch(); } void LIST::reverse(int n) { int i; clrscr(); cout<<"\nthe reversed list is...\n\n"; for(i=n-1;i>=0;i--) cout<<"\n"<<list[i]; cout<<"\npress any key to continue..\n"; getch(); } int LIST::search(int n) { int i,key; clrscr(); EC6312 – OOPS and Data Structures Lab Ms.M.Abinaya, Lect/IT 37 cout<<"\nentre thenumber you want to search?"; cin>>key; for(i=0;i<n;i++) { if(list[i]==key) { cout<<"\nthe given numbre is at position:"<<i<<"\n"; getch(); return i; } } cout<<"\nthe given number is not in the list\n"; getch(); return -1; } void LIST::delet(int n) { int i; i=search(n); list[i]=-1; cout<<"\nthe elements is now deleted"; cout<<"\n we put -1 to indicate empty location"; getch(); } void main() { LIST obj; int choice,len,position; char ans; do { clrscr(); cout<<"\n\tprogram to perform operations on ordered list"; cout<<"\n1.create"; cout<<"\n2.display"; cout<<"\n3.search for a number"; cout<<"\n4.reverse"; cout<<"\n5.delete"; cout<<"\n6.quit"; cout<<"\nenter your choice(1-6)"; cin>>choice; EC6312 – OOPS and Data Structures Lab Ms.M.Abinaya, Lect/IT 38 switch(choice) { case 1: len=obj.create(); break; case 2: obj.display(len); break; case 3: position=obj.search(len); break; case 4: obj.reverse(len); break; case 5: obj.delet(len); break; case 6: cout<<"\ndo you want to exit(y/n)?"; ans=getch(); if(ans=='y'||ans=='y') exit(0); else break; default: clrscr(); cout<<"\n invalid choice,tryagain"; getch(); } } while(choice!=6); } Output: program to perform operations on ordered list 1.create 2.display 3.search for a number 4.reverse 5.delete EC6312 – OOPS and Data Structures Lab Ms.M.Abinaya, Lect/IT 39 6.quit enter your choice(1-6)1 how many elements you want in the list:4 enter the element number1:1 enter the element number2:2 enter the element number3:3 enter the element number4:4 the List is successfully program to perform operations on ordered list 1.create 2.display 3.search for a number 4.reverse 5.delete 6.quit enter your choice(1-6)2 the List is... 1 2 3 4 press any key to continue... program to perform operations on ordered list 1.create 2.display 3.search for a number 4.reverse 5.delete 6.quit enter your choice(1-6)3 enter the number you want to search?2 EC6312 – OOPS and Data Structures Lab Ms.M.Abinaya, Lect/IT 40 .display 3.M.. 4 3 2 1 press any key to continue.delete 6. 1 2 -1 4 press any key to continue.quit enter your choice(1-6)5 enter thenumber you want to search?3 the given numbre is at position:2 the elements is now deleted we put -1 to indicate empty location the List is.reverse 5.Abinaya.create 2.display 3.the given number is at position:1 program to perform operations on ordered list 1. Lect/IT 41 ...search for a number 4. EC6312 – OOPS and Data Structures Lab Ms.quit enter your choice(1-6)4 the reversed list is.create 2...reverse 5. program to perform operations on ordered list 1..search for a number 4.delete 6. b. PUSH data into the stack b.h> #include<stdlib. int top.Result: Thus the Given Program Cursor implementation of list was executed successfully.M. 5. 4. POP DATA FROM STACK a. b. the link of the node is NULL.h> #include<conio. The stack represented by linked list is traversed to display its content. 2.Abinaya. ii. the link of TOP points to the new node.h> class stack { int stk[5]. If TOP is NOT NULL a. STACK ADT USING ARRAY IMPLEMENTATION Aim: To write a program for stack using array implementation. TOP points to that node. public: EC6312 – OOPS and Data Structures Lab Ms. the pervious TOP is popped from stack.. c. If TOP is NOT NULL i. the input data is the first node in stack. The operations on the stack are a. PUSH DATA INTO STACK a. c. Algorithm: 1. POP data out of stack 3. Enter the data to be inserted into stack. Program: #include<iostream. If TOP is NULL a. Define a array which stores stack elements. Lect/IT 42 . the link of TOP is the current TOP. TOP points to that node. b. If TOP is NULL the stack is empty b. pop\n3. return. } cout<<"deleted"<<stk[top--]. } }.exit". } stk[++top]=x. cout<<"\n1.display\n4.i>=0. int main() { int ch. Lect/IT 43 . return.stack() { top=-1. while(1) EC6312 – OOPS and Data Structures Lab Ms. cout<<"inserted"<<x. } for(int i=top. clrscr().i--) cout<<stk[i]<<" ". } void pop() { if(top<0) { cout<<"stack under flow". stack st. } void push(int x) { if(top>4) { cout<<"stack over flow". } void display() { if(top<0) { cout<<"stack empty".Abinaya.push\n2. return.M. M. cin>>ch. switch(ch) { case 1: cout<<"entre the element". } Output: 1. case 4: exit(0).pop().{ cout<<"\nenter a choice". Lect/IT 44 . break.pop 3.exit enter a choice1 entre the element3 inserted3 enter a choice1 entre the element2 inserted2 enter a choice1 entre the element4 inserted4 enter a choice1 EC6312 – OOPS and Data Structures Lab Ms.Abinaya. break. st.display 4. case 3: st.display(). case 2: st. cin>>ch.push(ch). } } return 0. break.push 2. entre the element5 inserted5 enter a choice3 5423 enter a choice2 deleted5 enter a choice3 423 enter a choice 4 Result: Thus the program for implementation of stack ADT using array has been executed and verified successfully. 2. ii. iii.Abinaya. the link of TOP points to the new node. Enter the data to be inserted into stack. c. TOP points to that node. ii. a. TOP points to that node. The operations on the stack are a. Lect/IT 45 . Each node in the stack contains data and link to the next node. 4.M. Define a struct for each node in the stack. Algorithm: 1. POP data out of stack 3. the input data is the first node in stack. If TOP is NULL i. STACK ADT USING LINKED LIST IMPLEMENTATION Aim: To write a program for stack ADT using linked list implementation. PUSH DATA INTO STACK a. POP DATA FROM STACK EC6312 – OOPS and Data Structures Lab Ms. PUSH data into the stack b. TOP pointer points to last node inserted in the stack. b. the link of the node is NULL. If TOP is NOT NULL i. }. } void Linked_list_Stack::push( ) { int num. ii. void print_list( ). The stack represented by linked list is traversed to display its content. void pop( ).h> class Linked_list_Stack { private: struct node { int data. node *print. EC6312 – OOPS and Data Structures Lab Ms. }. node *next. the link of TOP is the current TOP. node *bottom. void push( ). node *entry. the pervious TOP is popped from stack. If TOP is NULL the stack is empty b.a. 5. void show_working( ). node *last_entry. Linked_list_Stack::Linked_list_Stack ( ) { top=NULL. bottom=NULL. If TOP is NOT NULL i.M. public: Linked_list_Stack( ).h> #include<stdlib. Program: #include<iostream. node *second_last_entry. node *top.h> #include<conio. Lect/IT 46 .Abinaya. M. } else { entry->data=num.cout<<"\n\t Enter value to push onto Stack : ". if(top==bottom) bottom=NULL. if(print!=NULL) EC6312 – OOPS and Data Structures Lab Ms. } cout<<"\n\t *** "<<num<<" is pushed onto the Stack. else { for(last_entry=bottom. top->next=NULL. cin>>num. last_entry=last_entry->next) second_last_entry=last_entry."<<endl. entry=new node. } void Linked_list_Stack::pop( ) { if(bottom==NULL) cout<<"\n\t *** Error : Stack is empty.last_entry->next!=NULL. cout<<"\n\t *** "<<poped_element<<" is poped from the Stack.Abinaya."<<endl. entry->next=NULL. top->next=entry. int poped_element=top->data. top=entry. bottom=entry. top=entry. Lect/IT 47 . entry->next=NULL. \n"<<endl. delete top. } } void Linked_list_Stack::print_list( ) { print=bottom. if(bottom==NULL) { entry->data=num. top=second_last_entry. M.cout<<"\n\t Values pushed onto Stack are : \n"<<endl. } } void Linked_list_Stack::show_working( ) { int choice. cout<<"\n1. else cout<<"\n\t *** Nothing to show. default: cout<<"enter the valid choice".Push elements to stack"<<endl. break. switch(choice) { case 1: push(). break. case 4: exit(0). cout<<"2. case 3: print_list( ). cout<<"3. case 2: pop(). cin>>choice. while(print!=NULL) { cout<<"\t "<<print->data<<endl.Exit"<<endl. "<<endl. } int main( ) { Linked_list_Stack obj.Print the elements of stack"<<endl. clrscr( ).Pop elements to stack"<<endl. break. do { cout<<"\nEnter your Choice : ". Lect/IT 48 . EC6312 – OOPS and Data Structures Lab Ms. cout<<"\n\n********** Implementation of Linked List as a Stack **********"<<endl. } }while(1).Abinaya. print=print->next. break. cout<<"4. Enter your Choice : 1 Enter value to push onto Stack : 4 *** 4 is pushed onto the Stack.Pop elements to stack 3. Enter your Choice : 3 Values pushed onto Stack are : 2 3 Enter your Choice : 4 EC6312 – OOPS and Data Structures Lab Ms. } Output: ********** Implementation of Linked List as a Stack********** 1.Print the elements of stack 4.Exit Enter your Choice : 1 Enter value to push onto Stack : 2 *** 2 is pushed onto the Stack. return 0. Enter your Choice : 1 Enter value to push onto Stack : 3 *** 3 is pushed onto the Stack.show_working( ).Push elements to stack 2.Abinaya.M. Enter your Choice : 3 Values pushed onto Stack are : 2 3 4 Enter your Choice : 2 *** 4 is poped from the Stack. Lect/IT 49 .obj. M.h. Now create a stack application program. SOURCE FILES FOR STACK APPLICATION1 – ARRAY IMPLEMENTATIN Aim: To write a C++ program for the application of stack for checking well formed parenthesis of the expression using array implementation. 2. public: stk_class(). Crate a header file named stack. In full and empty operation check the stack whether it have value or not. int top. These operations are used from the external file stack. void push(char item). 6.h #define size 10 class stk_class { private: struct stack{ char s[size]. 7. EC6312 – OOPS and Data Structures Lab Ms.Result: Thus the Given Program for stack ADT using linked list implementation was executed successfully. 3. Chosen an application as “checking well formed of parenthesis” for this application use stack operations. 9. 8.h . In this file declare the class and all the stack operations. 5. Algorithm: 1.Abinaya. 4. Lect/IT 50 . Hence will include this file in the include file section. In push operation increment the top value and get the value In pop operation delete the value and decrement the top. }st. Program: stack. h" EC6312 – OOPS and Data Structures Lab Ms.h> #include<stdlib. return(item).s[st. } program.h> #include "d:\stack.M. st.int stempty(). else return 0. else return 0.top==size) return 1. st.top--. }.Abinaya.h> #include<conio.s[st. item=st.top]=item. } char stk_class::pop() { char item. } int stk_class::stfull() { if(st. } int stk_class::stempty() { if(st.top=-1. int stfull().top].top++. Lect/IT 51 .top==-1) return 1. } void stk_class::push(char item) { st. stk_class::stk_class() { st.cpp #include<iostream. char pop(). #define size 10 void main(void) { char item. Lect/IT 52 .Abinaya. if(bracket[i]==')') cout<<"\n the expression is invalid". stk_class obj. i=0. i++. } getch(). } while(bracket[i]==')') { item=obj. } } while(bracket[i]!='$').bracket[10]. cout<<"\n\t\t program for stack application using separate header file". i++. char ans.stempty()) cout<<"\n the expression is invalid".pop(). cout<<"\n enter the expression and put $at end ". int i. if(!obj.push(bracket[i]).M. } Output: program for stack application using separate header file enter the expression and put $at end (())()$ EC6312 – OOPS and Data Structures Lab Ms. else { do { while(bracket[i]=='(') { obj. else cout<<"\n the expression has well formed parenthesis". cin>>bracket. clrscr(). Abinaya. In push operation increment the top value and get the value In pop operation delete the value and decrement the top. Now create a stack application program.h class stk_class { private: typedef struct stack { char data. 3. 2. Lect/IT 53 . struct stack * next. In full and empty operation check the stack whether it have value or not.h .M. 7. 4.the expression has well formed parenthesis program for stack application using separate header file enter the expression and put $at end ((())$ the expression is invalid Result: Thus the program for application of stack checking well formed of parenthesis implemented using arrays was executed successfully. 5. EC6312 – OOPS and Data Structures Lab Ms. Program: stack. }node. 9. Hence will include this file in the include file section. 8. SOURCE FILES FOR STACK APPLICATION1 – LINKED LIST IMPLEMENTATIN Aim: To write a C++ program for the application of stack for checking well formed parenthesis of the expression using linked list implementation. These operations are used from the external file stack. Crate a header file named stack. Chosen an application as “checking well formed of parenthesis” for this application use stack operations. In this file declare the class and all the stack operations.h. Algorithm: 1. 6. temp=top. }. New->next=top. if(New==NULL) cout<<"\n memory cannot be allocated \n". top=top->next. } } int stk_class::sempty() { if(top==NULL) return 1. else { New->data=item. Lect/IT 54 .Abinaya.M. New=new node. delete temp. void push(char item). else return 0. void pop(). int sempty(). } EC6312 – OOPS and Data Structures Lab Ms. stk_class ::stk_class() { top=NULL. } void stk_class::pop() { node *temp. top=New. public: stk_class().node *top. } void stk_class::push(char item) { node *New. } while(bracket[i]!='$'). cout<<"\n\t\t enter the expression and put $ at the end ". } obj. char data. else { do { if(bracket[i]=='(') { obj.sempty()) { cout<<"\n the expression is invalid".cpp #include<iostream. Lect/IT 55 .M.item. } i++.push(bracket[i]). int i. int choice. stk_class obj. } else if(bracket[i]==')') { if(obj.pop(). EC6312 – OOPS and Data Structures Lab Ms.h> #include<stdlib.bracket[10]. exit(0). getch().program.Abinaya. clrscr(). if(bracket[i]==')') cout<<"\n the expression is invalid".h> #include<conio.h> #include<process.h> #include"d:\stack1. cin>>bracket. i=0.h" void main(void) { char ans. sempty()) cout<<"\n the expression has well formed parenthesis". These operations are used from the external file stack. 8. 6. If it is operand then push the value or the operator pop the value. } Output: enter the expression and put $ at the end ((()))()$ the expression has well formed parenthesis enter the expression and put $ at the end ((())$ the expression is invalid Result: Thus the program for application of stack checking well formed of parenthesis implemented using linked list was executed successfully. ‘*’. } getch(). 2. ‘-‘. In push operation increment the top value and get the value In pop operation delete the value and decrement the top. Lect/IT 56 .” for this application use stack operations. Then do the corresponding operation like ‘+’. Check the expression. SOURCE FILES FOR STACK APPLICATION2 – ARRAY IMPLEMENTATIN Aim: To write a C++ program for the application of stack for evaluation of postfix expression using array implementation. Crate a header file named stack. In this file declare the class and all the stack operations.M. 3.h. else cout<<"\n the expression is invalid". Now create a stack application program. Algorithm: 1. 5.Abinaya. 10. In full and empty operation check the stack whether it have value or not. 4.if(obj.h . 9. ‘/’. Chosen an application as “evaluating the postfix expression. 7. EC6312 – OOPS and Data Structures Lab Ms. double pop().s[st.top--. void push(double val).top==-1) cout<<"\n stack is empty\n".11.top]=val. Lect/IT 57 . public: stk_class(). stk_class::stk_class() { st.M. Program Stack2.h #define MAX 10 class stk_class { struct stack { double s[MAX].Abinaya. st. if(st. } void stk_class::push(double val) { if(st. }. return(val). int top. st.top=0. } double stk_class::pop() { double val.top++. }st.cpp #include<iostream. } program.top+1>=MAX) cout<<"\n stack is full".Hence will include this file in the include file section. st.h> EC6312 – OOPS and Data Structures Lab Ms. cout<<"the value of the expression is"<<result.h> #include<string.push(val). } EC6312 – OOPS and Data Structures Lab Ms. result=post(exp). else if(ch=='+'||ch=='-'||ch=='^'||ch=='*'||ch=='/') type="operator". while(ch!='$') { if(ch>='0'&&ch<='9') type="operand".val.*type.op1. int len. Lect/IT 58 . if(strcmp(type.op2.h> #include "d:\stack2.#include<conio. clrscr(). double post(char exp[]). int i. getch().Abinaya.h" #define size 80 void main() { char exp[size]. i=0. exp[len]='$'. obj. } double post(char exp[]) { stk_class obj. len=strlen(exp). double result."operand")==0) { val=ch-48. ch=exp[i]. cout<<"enter the postfix expression\n". double result .h> #include<math. char ch.h> #include<stdlib.M. exit(0). cin>>exp. Abinaya. case '-':result=op1-op2.M. ch=exp[i]. case '^':result=pow(op1. } obj.push(result)."operator")==0) { op2=obj. Lect/IT 59 . case '/': result=op1/op2. } result=obj.else if(strcmp(type.op2). break. return(result). break. break. break. op1=obj.pop(). case '*':result=op1*op2. EC6312 – OOPS and Data Structures Lab Ms.pop(). } Output: enter the postfix expression 19*3+5/ the value of the expression is 2 Result: Thus the program for application of stack evaluating postfix expression using array implementation has been executed and verified successfully. switch(ch) { case '+':result=op1+op2. } i++.pop(). break. 3. 10. Now create a stack application program.Abinaya. ‘-‘. char pop(). These operations are used from the external file stack.h . Chosen an application as “evaluating the postfix expression. If it is operand then push the value or the operator pop the value. In this file declare the class and all the stack operations. }. void push(char item). struct stack *next.Hence will include this file in the include file section. 5. 4. 11. ‘*’.SOURCE FILES FOR STACK APPLICATION2 – LINKED LIST IMPLEMENTATIN Aim: To write a C++ program for the application of stack for evaluation of postfix expression using array implementation. Then do the corresponding operation like ‘+’.” for this application use stack operations. In full and empty operation check the stack whether it have value or not. 2.h class stk_class { typedef struct stack { char data. 6.M. Lect/IT 60 . Check the expression. 9. In push operation increment the top value and get the value In pop operation delete the value and decrement the top.h. }node. 8. stk_class(). Crate a header file named stack. EC6312 – OOPS and Data Structures Lab Ms. public: node *top. Algorithm: 1. 7. ‘/’. Program stack3. top=New.h" #define size 80 void main() { char exp[size]. cout<<"enter the postfix expression\n". } char stk_class::pop() { char item. delete temp. node *temp. double result.Abinaya. } void stk_class::push(char item) { node *New. EC6312 – OOPS and Data Structures Lab Ms. New->data =item.h> #include"d:\stack3. int len. clrscr(). Lect/IT 61 . item=top->data.h> #include<math. New->next=top. double post(char exp[]).h> #include<conio.h> #include<stdlib.M. } program. top=top->next. temp=top.h> #include<stdlib. New=new node. return item.cpp #include<iostream.stk_class::stk_class() { top=NULL.h> #include<string. int i.push(val).pop(). } double post(char exp[]) { char ch. switch(ch) { case '+':result=op1+op2. getch().pop(). break. exp[len]='$'.val. double result . while(ch!='$') { if(ch>='0'&&ch<='9') type="operand". len=strlen(exp). ch=exp[i]."operator")==0)/*if it is operator*/ { op2=obj. case '/': result=op1/op2. break. break. cout<<"the value of the expression is"<<result. case '-':result=op1-op2. Lect/IT 62 .op1. stk_class obj. else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^') type="operator". } else if(strcmp(type.Abinaya.*type.M. if(strcmp(type."operand")==0) /* if the character is operand*/ { val=ch-48. result=post(exp). obj. EC6312 – OOPS and Data Structures Lab Ms. i=0.cin>>exp.op2. op1=obj. case '*':result=op1*op2. exit(0). Enter the data to be inserted into queue. If TOP is NOT NULL i. INSERT data into the queue b. Define a array which stores queue elements. If TOP is NULL i. the input data is the first node in queue. ii. DELETE data out of queue 3.Abinaya. } i++. OP points to that node. } Output: enter the postfix expression 19*3+5/ the value of the expression is 2 Result: Thus the program for application of stack evaluating postfix expression using linked list implementation has been executed and verified successfully. QUEUE ADT USING ARRAY IMPLEMENTATION Aim: To write a program for Queue using array implementation.push(result). ii. TOP points to that node. EC6312 – OOPS and Data Structures Lab Ms. case '^':result=pow(op1. b. INSERT DATA INTO queue a. the link of TOP points to the new node./*pop the result*/ return(result). the link of the node is NULL. } result=obj.pop(). iii. 2. } obj.M. The operations on the queue are a. ch=exp[i]. break. Algorithm: 1.op2). c.break. Lect/IT 63 . public: queue() { front=-1. The queue represented by linked list is traversed to display its content.h> #include<stdlib. If TOP is NOT NULL i. int front.h> class node { public: class node *next. head->next=NULL. } void enqueue(int x) { if(rear<0) { head=new node. int data. Lect/IT 64 . DELETE DATA FROM queue a.rear.4. temp=head.M. ii. 5. the queue is empty b. } else { node *temp. the pervious TOP is popped from queue. If TOP is NULL. class queue:public node { node *head. the link of TOP is the current TOP.*temp1. }. head->data=x.Abinaya. EC6312 – OOPS and Data Structures Lab Ms. rear++.h> #include<conio. rear=-1. Program: #include<iostream. EC6312 – OOPS and Data Structures Lab Ms. return.if(rear>=4) { cout<<"queue over flow". } while(temp!=NULL) { cout<<temp->data<< " ". temp1->data=x. if(rear<0) { cout<<"queue under flow". return. temp=temp->next. temp1->next=NULL. temp=head. temp1=new node. temp=head. return. while(temp->next!=NULL) temp=temp->next. } } void dequeue() { node *temp. } rear++. } } void display() { node *temp. temp->next=temp1.M.Abinaya. Lect/IT 65 . } if(front==rear) { front=rear=-1. if(rear<0) { cout<<" queue under flow". enqueue\n2. int ch. s1. case 4: exit(0).dequeue\n3. } }.head=NULL. break. } EC6312 – OOPS and Data Structures Lab Ms. main() { queue s1. case 3: s1. switch(ch) { case 1: cout <<"\n enter a element". case 2: s1. cin >> ch.enqueue(ch). while(1) { cout<<"\n enter your choice:". return.dequeue(). cout <<"\n1.display(). Lect/IT 66 .Abinaya.DISPLAY\n4. clrscr().EXIT". head=head->next.M. break. break. cout<<"\n\n\tQUEUE USING LINKED LIST". } front++. } } return 0. cin >> ch. Output: QUEUE USING LINKED LIST 1. 4. Algorithm: 1. 3.enqueue 2. INSERT data into the queue b. EC6312 – OOPS and Data Structures Lab Ms. Enter the data to be inserted into queue. The operations on the queue are a. INSERT DATA INTO queue a. Each node in the queue contains data and link to the next node. DELETE data out of queue 5. QUEUE ADT OPERATIONS USING LINKED LIST Aim: To write a program for Queue using Linked implementation.Abinaya.EXIT enter your choice:1 enter a element2 enter your choice:1 enter a element3 enter your choice:1 enter a element4 enter your choice:3 234 enter your choice:2 enter your choice:3 34 enter your choice:4 Result: Thus the Program for Queue using array implementation has been executed and verified successfully.DISPLAY 4.dequeue 3.M. Define a struct for each node in the queue. Front and rear pointer points to first and last node inserted in the queue. Lect/IT 67 . 2. TOP points to that node. the input data is the first node in queue.front. public: queue() { rear=-1. front=rear=-1. return. } queue1[++rear]=x. the link of TOP is the current TOP.h> #include<stdlib. Lect/IT 68 . 6. The queue represented by linked list is traversed to display its content. c. If TOP is NULL i. DELETE DATA FROM queue a. } void delet() EC6312 – OOPS and Data Structures Lab Ms. the queue is empty b. ii. the link of TOP points to the new node. ii. } void insert(int x) { if(rear>4) { cout<<"queue over flow".h> class queue { int queue1[5].h> #include<conio. Program: #include<iostream. the pervious TOP is popped from queue.Abinaya.b. If TOP is NOT NULL i. cout<<"\ninserted "<<x. the link of the node is NULL. ii. If TOP is NULL i.M. front=-1. iii. TOP points to that node. If TOP is NOT NULL i. int rear. 6. cin>>ch.insert(ch). main() { int ch.delete \n3. case 4:exit(0). qu. } for(int i=front+1. break. } EC6312 – OOPS and Data Structures Lab Ms. while(1) { cout<<"\nEnter ur choice".M.i<=rear. cout<<"\n1. cin>>ch. switch(ch) { case 1:cout<<"\nenter the element".delet(). } cout<<"\ndeleted "<<queue1[++front]. break.display \n4. case 2:qu. } void display() { if(rear==front) { cout<<" queue empty". queue qu. break. Lect/IT 69 . clrscr(). return. return.Abinaya. } }.i++) cout<<queue1[i]<<" ".display().exit". case 3:qu.{ if(front==rear) { cout<<"queue under flow".insert \n2. exit Enter ur choice1 enter the element5 inserted 5 Enter ur choice1 enter the element6 inserted 6 Enter ur choice1 enter the element9 inserted 9 Enter ur choice3 569 Enter ur choice2 deleted 5 Enter ur choice3 69 Enter ur choice4 Result : Thus the program for Queue ADT operations using Linked List has been executed and verified successfully. Lect/IT 70 .M.} return(0). } Output: 1. EC6312 – OOPS and Data Structures Lab Ms.delete 3.Abinaya.insert 2.display 4. int. display the Tree elements. void find(). 5. public: bintree() { root=NULL. }. Algorithm: 1.Display(). Insert an element is by checking the top node and the leaf node and the operation will be performed.node **).delete().M. Create a structure for a tree contains left pointer and right pointer. 3. Program: #include<iostream. void delet().*temp. void search(node**. node *root.h> #include<conio. Declare function create(). struct bst *left. } node. void del(node *. void insert(node *.search(). Lect/IT 71 . 4. } void create().int).h> class bintree { typedef struct bst { int data.*right. EC6312 – OOPS and Data Structures Lab Ms. void inorder(node *).node*).*parent.BINARY SEARCH TREE Aim: To write a program for binary search tree. void display(). 2.*New. Deleting an element contains searching the tree and deleting the item.Abinaya. void bintree::create() { New=new node.node *New) { if(New->data<root->data) { if(root->left==NULL) root->left=New. } } void bintree::inorder(node *temp) { if(temp!=NULL) EC6312 – OOPS and Data Structures Lab Ms.Abinaya.New). Lect/IT 72 . inorder(root). else { cout<<"\n the tree is: ". } if(New->data>root->data) { if(root->right==NULL)root->right=New. cout<<"\n enter the element". New->left=NULL. cin>>New->data. } void bintree::insert(node *root. else insert(root->right. if(root==NULL) root=New.M. else insert(root->left.New). New->right=NULL. } } void bintree::display() { if(root==NULL) cout<<"tree is not created". else insert(root.New). //stores the parent value if((*temp)->data>key) *temp=(*temp)->left. cout <<"\n enter the element which you want to search".Abinaya. cout<<" "<<temp->data. break.{ inorder(temp->left). } } return. } void bintree::search(node **temp. temp=root.int key. Lect/IT 73 . else { while(*temp!=NULL) { if((*temp)->data==key) { cout<<"\n the "<<(*temp)->data<<" element is present". } void bintree::delet() EC6312 – OOPS and Data Structures Lab Ms. } *parent=*temp. } } void bintree::find() { int key.key. else *temp=(*temp)->right.&parent). inorder(temp->right).M.node ** parent) { if(*temp==NULL) cout<<endl<<" tree is not created"<<endl. search(&temp. else cout<<"\n parent of node "<<temp->data<<" is"<<parent->data. cin>>key. if(temp==NULL) cout<<"\n element is not present". if(key==root->data) { bintree(). return.&parent).} void bintree::del(node *root. cout<<"\n enter the element you want to delete". EC6312 – OOPS and Data Structures Lab Ms. temp=NULL.// assigning a value NULL to root } else del(root.Abinaya. if(temp->left!=NULL&&temp->right!=NULL) { parent=temp. else { temp=root. delete temp.int key) { node *temp_succ. else parent->right=temp->left. while(temp_succ->left!=NULL) { parent=temp_succ. temp_succ=temp_succ->left. if(root==NULL) cout<<" tree is not created".key). cin>>key.key.{ int key. temp->right=NULL. temp_succ=temp_succ->left. search(&temp. } temp->data=temp_succ->data.M. cout<<" now deleted it!". } if(temp->left!=NULL&&temp->right==NULL) { if(parent->left==temp) parent->left=temp->left. Lect/IT 74 . cout<<"\n1. delete temp.create\n2.Abinaya.display".cout<<" now deleted it!".search\n3. do { cout<<"\n\n enter your choice:". cin>>choice. } if(temp->left==NULL&&temp->right==NULL) { if(parent->left==temp) parent->left=NULL. return. clrscr(). } } } void main() { int choice. bintree tr. Lect/IT 75 . cout<<"\n\t program for binary search tree". return. else parent->right=NULL. temp=NULL.delete\n4. cout<<" now deleted it!". return. } if(temp->left==NULL&&temp->right!=NULL) { if(parent->left==temp) parent->left=temp->right. cout<<" now deleted it!". switch(choice) { case 1:do { EC6312 – OOPS and Data Structures Lab Ms.M. char ans='N'. else parent->right=temp->right. create().create 2. cout<<"do you want to enter more elements: (y/n)"<<endl. } while(ans=='y'). case 3: tr.delete 4. break.display enter your choice 1 enter the element 10 do you want to enter more elements”(y/n) y enter the element 8 do you want to enter more elements”(y/n) y enter the element 7 do you want to enter more elements”(y/n) y enter the element 9 do you want to enter more elements”(y/n) EC6312 – OOPS and Data Structures Lab Ms. ans=getche(). break.tr. break. } Output: program for binary search tree 1. break. case 4: tr.find().M.delet(). }} while(choice!=5). Lect/IT 76 .search 3.display(). case 2:tr.Abinaya. M. Lect/IT 77 .search 3.delete 4.display enter your choice 3 enter the element you wish to delete 12 the 12 element is present now deleted it! EC6312 – OOPS and Data Structures Lab Ms.delete 4.y enter the element 12 do you want to enter more elements”(y/n) y enter the element 11 do you want to enter more elements”(y/n) y enter the element 13 do you want to enter more elements”(y/n) n 1.create 2.search 3.delete 4.display enter your choice 2 enter the element which you want to search 13 the 13 element is present parent of node 13 is 12 1.display enter your choice 4 the tree is: 7 8 9 10 11 12 13 1.create 2.search 3.Abinaya.create 2. create 2.display enter your choice 3 enter the element you wish to delete 11 the 11 element is present now deleted it! 1.1. Lect/IT 78 .M.create 2.search 3.display enter your choice 3 enter the element you wish to delete 7 the 7 element is present now deleted it! 1.delete 4.create 2.delete 4.search 3.delete 4.delete 4.delete 4.search 3.Abinaya.display enter your choice 4 the tree is : 7 8 9 10 11 13 1.display enter your choice 4 EC6312 – OOPS and Data Structures Lab Ms.create 2.display enter your choice 4 the tree is : 7 8 9 10 13 1.search 3.search 3.create 2. delete 4. 4.Abinaya. int n. Algorithm: 1. Program: #include<iostream.display enter your choice 5 Result: Thus the Program for Binary Search Tree has been executed and verified successfully.h> #include<conio.the tree is : 8 9 10 13 1. Display the sorted elements. Now the array is contained with sorted elements. EC6312 – OOPS and Data Structures Lab Ms. HEAP SORT Aim: To write a c program to perform heap sort. Get the size of the array from the user. 5.search 3.h> #include<stdlib.create 2. Sorting is performed when we call the heap sort function. Lect/IT 79 . 2.h> #define MAX 10 class heap { private: int arr[MAX]. public: Heap(). Get the elements to be sorted. 3.M. void insert(int num). M. while(j>0&&arr[f]<val)//creating a MAX heap { arr[j]=arr[f]. void display(). } void heap::insert(int num) { if(n<Max) { arr[n]=num. }} void heap::heapsprt() { for(int i=n-1. n++.i<MAX.i++) { int val =arr[i]. }arr[j]=val. } else cout<<”\n array is full”.i>0. for(int i=0. f=(j-1)/2. int f=(j-1)/2. }. Lect/IT 80 . j=f. EC6312 – OOPS and Data Structures Lab Ms. heap::heap() { n=0. } void heap::makeheap() { for(int i=1.i<n. void heapsort().i--) { int temp=arr[i]. int k=0.Abinaya.i++) arr[i]=0. arr[i]=arr[0]. int j=I.void makespace(). } } void heap::display() { for(int i=0. } arr[k]=temp.insert(8).”<<endl. obj. obj. cout<<”\n”. EC6312 – OOPS and Data Structures Lab Ms. obj.insert(7).display(). while(j>=0&&temp<arr[j]) { arr[k]=arr[j]. cout<<”\n the elements are …. Lect/IT 81 .insert(9). obj. else j=1. } void main() { heap obj. obj.insert(14). obj.insert(18). obj. if(j>i-1) j=-1. obj.M.insert(12). obj.int j.i<n.display(). if(j+1<=i-1&&arr[j]<arr[j+1]) j++.Abinaya.i++) cout<<”\n”<<arr[i]. j=2*k+1. k=j. if(i>2&&arr[2]>arr[1]) j=2. odj.insert(10). if(i==1) j=-1.makeheap(). cout<<”\n heapified”<<endl.. heapsort(). EC6312 – OOPS and Data Structures Lab Ms. Lect/IT 82 .obj.Abinaya. getch().M. } Output: the elements are…. 14 12 9 8 7 10 heapified 12 14 8 7 9 10 Elements sorted by heap sort 7 8 9 10 12 14 Result: Thus the program for Heap Sort has been executed and verified successfully. obj.display(). cout<<”\n elements sorted by heap sort…”<<endl. cout<<"\n * Array size = 10"<<endl. EC6312 – OOPS and Data Structures Lab Ms. Two function quicksort() and swap(). 4. then recursively sorting each partition. Get the elements from the user. void quick_sort(long []. Program: # include <iostream. 2.array_size-1). const int array_size=10.int.0. Quick sort algorithm works by partitioning the array to be sorted.Abinaya. 5. cout<<" \n\nSorted Array : \n\n". Lect/IT Sort 83 . 6. cout<<" * Data Type = long"<<endl.count_1++) { cout<<"\t Element["<<count_1<<"] = ".count_2<array_size. for(int count_2=0. 3.M.long &). cout<<" Enter the array : "<<endl<<endl. Display the sorted value.h> # include <conio.count_1<array_size. Quick sort to perform sorting. } quick_sort(array. for(int count_1=0. to be sorted. cout<<" \n\n*********************** Quick ******************"<<endl.QUICK SORT Aim: To write a program to perform quick sort. main( ) { clrscr( ). cin>>array[count_1]. Swap() is just to rearrange the values. Algorithm: 1. Get the value of how many no.int).h> void swap(long &.count_2++) { cout<<"\tElement["<<count_2<<"] = "<<array[count_2]<<endl. long array[array_size]={0}. long &element_2) { long temp=element_1. getch( ).M.} cout<<" \n\n\n******************************************************* ***". EC6312 – OOPS and Data Structures Lab Ms. quick_sort(array. do { count_2--. int count_2=last.array[count_2]). } while(count_2>=0 && array[count_2]>middle). } while(array[count_1]<middle).count_1-1).int first. int count_1=first-1. } void quick_sort(long array[]. } void swap(long &element_1. quick_sort(array. element_2=temp.first. if(count_1<count_2) swap(array[count_1]. } swap(array[count_1].int last) { if(first>=last) { } else { int middle=array[last]. return 0. while(count_1<count_2) { do { count_1++.count_1+1.array[last]). Lect/IT 84 .Abinaya.last). element_1=element_2. M. EC6312 – OOPS and Data Structures Lab Ms.} } Output: *********************** Quick Sort ****************** * Array size = 10 * Data Type = long Enter the array : Element[0] = 6 Element[1] = 4 Element[2] = 2 Element[3] = 8 Element[4] = 4 Element[5] = 1 Element[6] = 3 Element[7] = 9 Element[8] = 0 Element[9] = 7 Sorted Array : Element[0] = 0 Element[1] = 1 Element[2] = 2 Element[3] = 3 Element[4] = 4 Element[5] = 4 Element[6] = 6 Element[7] = 7 Element[8] = 8 Element[9] = 9 ********************************************************** Result: Thus the program for quick sort has been executed and verified successfully. Lect/IT 85 .Abinaya.
Copyright © 2024 DOKUMEN.SITE Inc.