Vous êtes sur la page 1sur 64

BASIC PROGRAM USING C++ CONCEPTS

Ex no: 1 a Date: PROGRAM: #include<iostream.h> #include<conio.h> class item { int number; float cost; public: void getdata(int a,float b) { number=a; cost=b; } void putdata(void) { cout<<"number:"<<number<<"\n"; cout<<"cost:"<<cost<<"\n"; } }; int main() { clrscr(); item x; cout<<"\n object x:"<<"\n"; x.getdata(100,299.95);

PROGRAM USING CLASS

x.putdata(); item y; cout<<"\n object y:"<<"\n"; y.getdata(200,175.50); y.putdata(); getch(); return(0); }

Output:

object x: number : 100 cost :299.95

object y: number :200 cost :175.50

Ex no: 1 b Date: PROGRAM: #include<iostream.h> #include<conio.h> class integer { int m,n; public: integer(int,int); void display(void) { cout<<"m="<<m<<"\n"; cout<<"n="<<n<<"\n"; } }; integer::integer(int x,int y) { m=x; n=y; } int main() { clrscr(); integer int1(0,100); integer int2=integer(25,75); cout<<"\n object 1"<<"\n"; int1.display(); cout<<"\n object 2"<<"\n"; int2.display();

BASIC

CONSTRUCTOR

PROGRAM

getch(); return(0); }

Output:
object 1 m=0 n=100

object 2 m=25 n=75

Ex no: 1 c Date:

BASIC

INHERITANCE

PROGRAM

PROGRAM:
#include<iostream.h> #include<conio.h> class B { int a; public: int b; void get_ab(); int get_a(void); void show_a(void); }; class D:public B { int c; public: void mul(void); void display(); }; void B::get_ab(void) { a=5; b=10; } int B::get_a() { return a; } void B::show_a() { cout<<"a="<<a<<"\n"; } void D::mul()

{ c=b*get_a(); } void D::display() { cout<<"a="<<get_a()<<"\n"; cout<<"b="<<b<<"\n"; cout<<"c="<<c<<"\n"; } int main() { clrscr(); D d; d.get_ab(); d.mul(); d.show_a(); d.display(); d.b=20; d.mul(); d.display(); getch(); return(0);}

Output: a=5 a=5 b=10 c=50 a=5 b=20 c=100

Ex no: 2 Date: 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; void display(int); int search(int); void deletion(int); do { clrscr();

ARRAY IMPLEMENTATION USING LIST ADT

cout<<"\n program to perform operation a list"; cout<<"\n 1.create"; cout<<"\n2.display"; cout<<\n3.search for a number"; cout<<"\n4.delete"; cout<<"\n5.quit"; cout<<"\n enter the choice"; cin>>choice; switch(choice)

{ case 1: len=create(); break; case 2: display(len); break; case 3: position=search(len); break; case 4: deletion(len); break; case 5: cout<<"do you want to exit:"; ans=getch(); if(ans='Y') exit(0); break; default: clrscr(); cout<<"\n valid choice ,try again"; getch(); } } while(choice!=5); } int create() {

int n,1,i; clrscr(); cout<<"\n how many elements you want in the list"; cin>>n; if(n>max) cout<<"\n enter the elements exceeds the limits"; for(i=0;i<n;i++) { cout<<"enter the element number"<<i+1; cin>>list[i]; } cout<<"list is successfully created"; getch(); return n; } void display(int n) { //int i; clrscr(); cout<<"the list is:"; for(int i=0;i<n;i++) cout<<list[i]<<"\t"; cout<<"\n press any key to continue"; getch(); } int search(int n) { int i,key; clrscr();

cout<<"\n enter the number do you want to search"; cin>>key; for(i=0;i<n;i++) { if(list[i]=key) { cout<<"\n the given number at position"<<i; getch(); return i; } } cout<<"\n the given number is not in the list"; getch(); } void deletion(int n) { //int i int i=search(n); list[i]=-1; cout<<"\n the element is now deleted"; cout<<"we put -1 to indicate the empty location"; getch(); }

Output: program to perform operation a list 1.create 2.display

3.search for a number 4.delete 5.quit enter the choice 1 how many elements you want in the list 4 enter the element number1 10 enter the element number2 20 enter the element number3 30 enter the element number4 40 list is successfully created program to perform operation a list 1.create 2.display 3.search for a number 4.delete 5.quit enter the choice 2 the list is 10 20 30 40 press any key to continue program to perform operation a list 1.create 2.display 3.search for a number 4.delete 5.quit enter the choice 3 enter he number do you want ot search 30 the given number at position 2 program to perform operation a list

1.create 2.display 3.search for a number 4.delete 5.quit enter the choice 4 enter the number do you want to search 40 the given number at position 3 the element is now deleted put -1 to indicate the empty location program to perform operation a list 1.create 2.display 3.search for a number 4.delete 5.quit enter the choice 5

QUEUE Ex no: 3 a Date: PROGRAM:


#include<iostream.h> #include<conio.h> #include<stdlib.h> int ch, qu[50],max=50,front=0,rear=0; void main() { void enqueue(); void dequeue(); void display(); clrscr(); cout<<"\n 1.Insertion \n "; cout<<"2.Delete \n "; cout<<"3.Display \n "; cout<<"4.exit \n "; while(1) { cout<<"\n Enter your choice : "; cin>>ch; switch (ch) { case 1: enqueue(); break; case 2:

ADT IMPLEMENTATION

QUEUE

USING ARRAY IMPLEMETNATION

dequeue(); break; case 3: display(); break; default: exit(0); }}} void enqueue() { int ele; if(rear==max) cout<<"Enqueue is full..........\n"; else { cout<<"\n Enter the data : "; cin>>ele; qu[rear++]=ele; }} void dequeue() { int i; if(rear==0) cout<<"Queue is empty........\n"; else { cout<<"\n Delete element is : "<<qu[front]<<"\n"; front++; }} void display() { int i;

if(rear==0) cout<<"Queue is empty.... \n"; else { cout<<"\n Queue element are : "; for(i=front;i<rear;i++) cout<<qu[i]<<"\t"; cout<<"\n"; } }

Output:
1.Insertion 2.Delete 3.Display 4.exit Enter your choice : 1 Enter the data : 2 Enter your choice : 1 Enter the data : 4 Enter your choice : 1 Enter the data : 6 Enter your choice : 3 Queue element are : 2 Enter your choice : 2 Delete element is : 2 Enter your choice : 3 Queue element are : 4 6 Enter your choice : 4 4 6

Ex no: 3 b Date: PROGRAM:


#include<iostream.h> #include<conio.h> #include<stdlib.h> struct node { int data; struct node*link; }; struct node*link;

QUEUE ADT LINKED LIST IMPLEMENTATION

struct node*front=NULL,*rear=NULL,*x; void main() { void enqueue(); void dequeue(); void display(); int ch; clrscr(); while(1) { cout<<"\n Option"; cout<<"\n 1.Insertion"; cout<<"\n 2.Deletion"; cout<<"\n 3.Display"; cout<<"\n 4.Exit"; cout<<"\n Enter your option : ";

cin>>ch; switch (ch) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; default: exit(0); }}} void enqueue() { if(rear==NULL){ x=(struct node*)malloc(sizeof(struct node)); cout<<"\n Enter the data: "; cin>>x->data; x->link=NULL; rear=x; front=x; } else { x=(struct node*)malloc(sizeof (struct node)); cout<<"\n Enter data: "; cin>>x->data; x->link=NULL; rear->link=x; rear=x;}}

void dequeue() { if(front==NULL) cout<<"Queue is empty"; else { x=front; cout<<"\n Dequeue element : "<<x->data; front=x->link; free(x); }} void display() { x=front; cout<<"\n Dequeue elements are: "; while(x->link!=NULL) { cout<<x->link; } cout<<x->data; }

Output:
Option 1.Insertion 2.Deletion 3.Display 4.Exit Enter your option : 1 Enter the data: 3 Option 1.Insertion

2.Deletion 3.Display 4.Exit Enter your option : 1 Enter data: 6 Option 1.Insertion 2.Deletion 3.Display 4.Exit Enter your option : 2 Dequeue element : 3 Option 1.Insertion 2.Deletion 3.Display 4.Exit Enter your option : 3 Queue elements are: 6 Option 1.Insertion 2.Deletion 3.Display 4.Exit Enter your option : 4

4.STACK ADT IMPLEMENTATION Ex no: 4 a Date: PROGRAM: #include<iostream.h> #include<conio.h> #include<stdlib.h> #define size 5 class stack { int a[size]; int tos; public: stack(); void push(int); int pop(); int isempty(); int isfull(); void view(); void peck(); }; stack::stack() { tos=-1; } int stack::isempty() { return (tos==-1?1:0); } int stack::isfull() { return (tos==size-1?1:0); } void stack::push(int i) { if(! isfull()) { STACK ADT ARRAY IMPLEMENTATION

a[++tos]=i; } else { cout<<" stack over flow error! possible data loss!"; }} int stack::pop() { if(!isempty()) { return (a[tos --]); } else { cout<<" stack is empty ! what is pop.........!"; return 0; }} void stack :: peck() { if(isempty()) cout<<" stack is empty \n"; else cout<<a[tos]; } void stack::view() { if(isempty()) { cout<<" stack is empty"; } cout<<" The content of stack are \n"; for(int i=tos; i>=0; i--) { cout<<".......................>"<<a[i]; }}

void main() { stack s; int ch,num; clrscr(); while(1) { cout<<"\n stack operations MAIN MENU"; cout<<"\n 1.push \n 2.pop \n 3.peck \n 4.isempty \n 5.isfull \n 6.view \n"; cout<<" Enter your choice"; cin>>ch; switch(ch) { case 1: cout<<" Enter the number to push"; cin>>num; s.push(num); break; case 2: cout<<" Number poped from the stack is "<<s.pop(); break; case 3: cout<<"top most stack element is "; s.peck(); break; case 4: (s.isempty())?(cout<<" stack is empty"):(cout<<"stack is not empty"); break; case 5:

(s.isfull())?(cout<<"stack is full"):(cout<<"stack is not full"); break; case 6: s.view(); break; }} getch(); }

Output:
stack operations MAIN MENU 1.push 2.pop 3.peck 4.isempty 5.isfull 6.view Enter your choice1 Enter the number to push5 stack operations MAIN MENU 1.push 2.pop 3.peck 4.isempty 5.isfull 6.view Enter your choice1 Enter the number to push8 stack operations MAIN MENU

1.push 2.pop 3.peck 4.isempty 5.isfull 6.view Enter your choice6 The content of stack are .......................>8.......................>5 stack operations MAIN MENU 1.push 2.pop 3.peck 4.isempty 5.isfull 6.view Enter your choice 7

Ex no: Date:

4b

STACK

ADT LINKED LIST IMPLEMENTATION

PROGRAM: #include<iostream.h> #include<conio.h> #include<stdlib.h> class stack { int element; stack*next; public: stack*push(stack*,int); stack*pop(stack*); void stack_display(stack*); } *head,object; stack*stack::push(stack*head,int key) { stack *temp,*temp1; temp1=head; temp=new stack; temp->element=key; temp->next=NULL; if(head==NULL) head=temp; else { while(head->next!=NULL) head=head->next;

head->next=temp; head=temp1; } return head; } stack *stack::pop(stack *head) { stack *temp; if(head!=NULL) { temp=head; if(head->next==NULL) { cout<<"\n The poored elements from the stack is : "<<head->element<<endl; return NULL; } while(head->next->next!=NULL) head=head->next; cout<<"The poored elements from the stack is : "<<head->next->element; cout<<endl; head->next=head->next->next; head=temp; return head; } else { cout<<"\n It is impossible to pop an element from the stack"; return head; } }

void stack::stack_display(stack*head) { if(head!=NULL){while(head->next!=NULL) { cout<<head->element<<"->"; head=head->next; } cout<<head->element; cout<<endl; } else cout<<"the stack is empty"; } void choice() { int key,ch; cout<<"\n choose the operation to be performed"; cout<<"\n 1.Push \t 2.Pop \t 3.Exit \n"; cin>>ch; head=NULL; while(1) { switch (ch) { case 1: cout<<"\n................................\n"; cout<<"\n Enter elements to be pushed \n"; cin>>key; head=object.push(head,key);

cout<<"\n The stack after push operation is : \n"; object.stack_display(head); cout<<"\n................................\n"; break; case 2: head=object.pop(head); cout<<"\n The stack after pop operation is : \n"; object.stack_display(head); break; case 3: exit(1); default: cout<<"\n Enter the correct choice\n"; break; } cout<<"\n chocce the operation to be performed "; cout<<"\n 1.push \t 2.pop \t 3.exit \n"; cin>>ch; }} void main() { clrscr(); choice(); }

Output:
choose the operation to be performed 1.Push 1 2.Pop 3.Exit

................................ Enter elements to be pushed 5 The stack after push operation is : 5 ................................ chocce the operation to be performed 1.push 1 ................................ Enter elements to be pushed 9 The stack after push operation is : 5->9 ................................ chocce the operation to be performed 1.push 2 The poored elements from the stack is : The stack after pop operation is : 5 chocce the operation to be performed 1.push 3 2.pop 3.exit 2.pop 3.exit 2.pop 3.exit

Ex no: 5 Date: PROGRAM:


#include<iostream.h> #include<conio.h> #include<stdlib.h> struct node { int data; struct node*link; };

LINKED LIST IMPLEMENTATION OF LIST ADT

struct node*head=NULL,*x,*y,*z; void main() { void create(); void insbeg(); void insrem(); void delbeg(); void delrem(); void display(); int ch; clrscr(); cout<<"\n 1.creation \n 2.Insertion at beginning \n 3.Insertion at remaining \n 4.Deletion at beginning \n 5.Deletion at remaining \n 6.Display \n 7.Exit"; while(1) { cout<<"\n Enter your choice : "; cin>>ch; switch(ch)

{ case 1: create(); break; case 2: insbeg(); break; case 3: insrem(); break; case 4: delbeg(); break; case 5: delrem(); break; case 6: display(); break; default: exit(0); } getch(); }} void create() { cout<<"\n creation\n"; int c; x=(struct node*)malloc(sizeof(struct node));

cout<<"\n Enter data : "; cin>>x->data; x->link=x; head=x; cout<<"\n press 1 to continue otherwise 0 : "; cin>>c; while(c!=0) { y=(struct node*)malloc(sizeof(struct node)); cout<<"\n Enter the data : "; cin>>y->data; x->link=y; y->link=head; x=y; cout<<"\n press 1 to continue otherwise 0 : "; cin>>c; }} void insbeg() { cout<<"\n Insertion at beginning \n"; x=head; y=(struct node*)malloc(sizeof(struct node)); cout<<"\n Enter the data : "; cin>>y->data; while(x->link!=head) { x=x->link; } x->link=y;

y->link=head; head=y; } void insrem() { cout<<"\n Insertion at remaining \n"; int c=0,pos; y=(struct node*)malloc(sizeof(struct node)); cout<<"\n Enter the data : "; cin>>y->data; cout<<"\n Enter the position to insert : "; cin>>pos; while(c<pos) { z=x; x=x->link; c++; } y->link=x; z->link=y; } void delbeg() { cout<<"\n Deletion at beginning \n "; if(head==NULL) cout<<"\n list is empty"; else { x=head;

y=head; while(x->link!=head) { x=x->link; } head=y->link; x->link=head; free(y); }} void delrem() { cout<<"\n Deletion at remaining \n"; if(head==NULL) cout<<"\n list is empty"; else { int c=1,pos; cout<<"\n Enter the position to delete : "; cin>>pos; x=head; while(c<pos) { y=x; x=x->link; c++; } y->link=x->link; free(x); }}

void display() { if(head==NULL) cout<<"\n list is empty"; else { x=head; while(x->link!=head) { cout<<x->data<<" --> "; x=x->link; } cout<<x->data; }}

Output:
1.creation 2.Insertion at beginning 3.Insertion at remaining 4.Deletion at beginning 5.Deletion at remaining 6.Display 7.Exit Enter your choice : 1 creation Enter data : 2 press 1 to continue otherwise 0 : 1 Enter the data : 6 press 1 to continue otherwise 0 : 0 Enter your choice : 6

2 --> 6 Enter your choice : 2 Insretion at beginning Enter the data : 5 Enter your choice : 3 Insertion at remaining Enter the data : 9 Enter the position to insert : 3 Enter your choice : 6 5 --> 2 --> 9 --> 6 Enter your choice : 5 Deletion at remaining Enter the position to delete : 2 Enter your choice : 6 5 --> 9 --> 6

Ex no: 6 Date: PROGRAM:


#include<iostream.h> #include<conio.h> #include<stdlib.h> struct node { int data; struct node *link; };

CURSOR IMPLEMENTATION OF THE LIST ADT

struct node*head=NULL,*x,*y,*z; void main() { void create(); void insbeg(); void insrem(); void delbeg(); void delrem(); void display(); int ch; clrscr(); cout<<"\n 1.creation\n 2.insertion at beginning\n 3.insertion at remaining\n 4.deletion at beginning\n 5.deletion at remaining\n 6.display\n 7.exit\n"; while(1) { cout<<"\n Enter your choice : "; cin>>ch; switch(ch)

{ case 1: create(); break; case 2: insbeg(); break; case 3: insrem(); break; case 4: delbeg(); break; case 5: delrem(); break; case 6: display(); break; default: exit(0); } getch(); }} void create() { cout<<"\n creation\n "; int c; x=(struct node*)malloc(sizeof(struct node));

cout<<"\n Enter the data : "; cin>>x->data; x->link=x; head=x; cout<<"\n if you wish to continue press 1 otherwise 0 : "; cin>>c; while(c!=0) { y=(struct node*)malloc(sizeof(struct node)); cout<<"\n Enter the data : "; cin>>y->data; x->link=y; y->link=head; x=y; cout<<"\n if you wish to continue press 1 otherwise 0 : "; cin>>c; }} void insbeg() { cout<<"\n insertion at beginning "; x=head; y=(struct node*)malloc(sizeof(struct node)); cout<<"\n Enter the data : "; cin>>y->data; while(x->link!=head) { x=x->link; } x->link=y;

y->link=head; head=y; } void insrem() { cout<<"\n insertion at remaining "; int c=0,pos; y=(struct node*)malloc(sizeof(struct node)); cout<<"\n Enter the data : "; cin>>y->data; cout<<"\n Enter the position to insert : "; cin>>pos; while(c<pos) { z=x; x=x->link; c++; } y->link=x; z->link=y; } void delbeg() { cout<<"\n deletion at beginning "; if(head==NULL) cout<<"\n list is empty "; else { x=head;

y=head; while(x->link!=head) { x=x->link; } head=y->link; x->link=head; free(y); }} void delrem() { cout<<"\n deletion at remaining "; if(head==NULL) cout<<"\n list is empty "; else { int c=1,pos; cout<<"\n Enter the position to delete : "; cin>>pos; x=head; while(c<pos) { y=x; x=x->link; c++; } y->link=x->link; free(x); }}

void display() {if(head==NULL) cout<<"\n list is empty "; else { x=head; while(x->link!=head) {cout<<x->data<<"-->"; x=x->link; } cout<<x->data; }}

Output:
1.creation 2.insertion at beginning 3.insertion at remaining 4.deletion at beginning 5.deletion at remaining 6.display 7.exit Enter your choice : 1 Creation Enter the data : 4 if you wish to continue press 1 otherwise 0 : 1 Enter the data : 8 if you wish to continue press 1 otherwise 0 : 0 Enter your choice : 2 insertion at beginning

Enter the data : 1 Enter your choice : 6 1-->4-->8 Enter your choice : 2 insertion at beginning Enter the data : 5 Enter your choice : 3 insertion at remaining Enter the data : 7 Enter the position to insert : 3 Enter your choice :6 5-->1-->7-->4-->8 Enter your choice : 4 deletion at beginning Enter your choice : 6 1-->7-->4-->8 Enter your choice : 5 deletion at remaining enter the position to delete 2 enter your choice 6 1-->7-->8

Ex no: 7 Date: PROGRAM:


#include<iostream.h> #include<conio.h> #include<stdlib.h> typedef struct bst { int data; struct bst *left, *right; }node; void insert (node *,node *); void inorder(node*); node*search(node*,int,node**); void del(node*,int); void main() { int choice; char ans='N'; int key; node*New,*root,*temp,*parent; node*get_node(); root=NULL; clrscr();

BINARY SEARCH TREE

cout<<" Program for binary search tree"; do {cout<<"\n 1.Creation \n 2.Search \n 3.Delete \n 4.Display \n"; cout<<"\n Enter your choice \n";

cin>>choice; switch(choice) { case 1:do { New=get_node(); cout<<"\n Enter the element \n"; cin>>New->data; if(root==NULL) root=New; else insert(root,New); cout<<"\n Do you want to enter more elements \n (y/n)"; ans=getch(); } while(ans=='Y'); break; case 2: cout<<"\n Enter the element which you want to search"; cin>>key; temp=search(root,key,&parent); cout<<"\n The parent node of "<<temp->data<<"is"<<parent->data; break; case 3: cout<<"\n enter the element you want to delete "; cin>>key; del(root,key); break; case 4:

if(root==NULL) cout<<"\n Tree is not creation "; else { cout<<"\n The tree is: "; inorder(root); } break; }} while(choice!=5); } node*get_node() { node*temp; temp=(node*)malloc(sizeof (node)); temp->left=NULL; temp->right=NULL; return temp; } void insert(node*root,node*New) {if(New->data<root->data) { if(root->left==NULL) root->left=New; else insert(root->left,New); } if(New->data>root->data) { if(root->right==NULL)

root->right=New; else insert(root->right,New); }} node*search(node*root,int key,node**parent) { node*temp; temp=root; while(temp!=NULL) { if(temp->data==key) {cout<<temp->data; return temp; } *parent=temp; if(temp->data>key) temp=temp->left; else temp=temp->right; } return NULL; } void del(node *root,int key) { node*temp,*parent,*temp_succ; temp=search(root,key,&parent); if(temp->left!=NULL&&temp->right!=NULL) { parent=temp; temp_succ=temp->right; while(temp_succ->left!=NULL)

{ parent=temp_succ; temp_succ=temp_succ->left; } temp->data=temp_succ->data; parent->right=NULL; cout<<"\n Now deleted it : "; return; } if(temp->left!=NULL&&temp->right==NULL) { if(parent->left==temp) parent->left=temp->left; else parent->right=temp->left; temp=NULL; free(temp); cout<<"\n Now deleted it"; return; } if(temp->left==NULL&&temp->right!=NULL) { if(parent->left==temp) parent->left=temp->right; else parent->right=temp->right; temp=NULL; free(temp); cout<<"Now deleted it: ";

return; } if(temp->left==NULL&&temp->right==NULL) { if(parent->left==temp) parent->left=NULL; else parent->right=NULL; cout<<"\n Now deleted it "; return; }} void inorder(node*temp) {if(temp!=NULL) { inorder(temp->left); cout<<temp->data<<"\t"; inorder(temp->right); }}

Output:
Program for binary search tree 1.Creation 2.Search 3.Delete 4.Display Enter your choice 1 Enter the element 7 Do you want to enter more elements (y/n) 1.Creation 2.Search 3.Delete

4.Display Enter your choice 1 Enter the element 89 Do you want to enter more elements (y/n) 1.Creation 2.Search 3.Delete 4.Display Enter your choice 1 Enter the element 4 Do you want to enter more elements (y/n) 1.Creation 2.Search 3.Delete 4.Display Enter your choice 4 The tree is: 4 7 89 1.Creation 2.Search 3.Delete 4.Display Enter your choice

Ex no: 8 Date: PROGRAM:


#include<iostream.h> #include<conio.h> int a[20],n; void main() { void qsort(int,int); clrscr(); cout<<"\n enter array size"; cin>>n; cout<<"\n enter elements to be sorted"; for(int i=0;i<n;i++) cin>>a[i]; qsort(0,n-1); cout<<"the sorted element are"; for(i=0;i<n;i++) cout<<a[i]; getch(); } void qsort(int left,int right) { int i,j,pivot,temp,k; if(left<right) { i=left+1; j=right; pivot=left;

QUICK SORTS

for(;;) { while(a[pivot]>=a[i]) i=i+1; while(a[pivot]<a[j]) j=j-1; if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; for(k=0;k<n;k++) cout<<"\t"<<a[k]; cout<<"\n"; } else break; } temp=a[pivot]; a[pivot]=a[j]; a[j]=temp; for(k=0;k<n;k++) cout<<"\t"<<a[k]; cout<<"\n"; qsort(left,j-1); qsort(j+1,right); } return; }

Output:
enter array size 6 enter elementsto be sorted 23 45 36 78 23 71 23 23 23 45 45 36 36 36 45 78 78 78 23 23 23 71 71 71

the sorted element are23 36 45 78 23 71

Ex no: 9 Date: PROGRAM:


#include<iostream.h> #include<conio.h> #include<string.h>

INFIX TO POSTFIX CONVERSION

char str[50],stack1[50],stack2[50]; int i=0,j,n,top1=-1,top2=-1; int prec(char); void main() { int t=0; clrscr(); cout<<" \n Enter the expr : \n"; cin>>str; cout<<str; n=strlen(str); for(i=0;i<n;i++) { t=0; if(str[i]==')') { stack1[++top1]=stack2[top2--]; top2--; t=1; } if(str[i]=='(') {

stack2[++top2]='('; t=1; } if(t==0) { if(((str[i]>='a')&&(str[i]<='z'))||((str[i]>='A')&&(str[i]<='Z'))) stack1[++top1]=str[i]; else { if(top2==-1) stack2[++top2]=str[i]; else { if(prec(str[i])>prec(stack2[top2])) { stack2[++top2]=str[i]; } else { while(prec(str[i])<=prec(stack2[top2])) stack1[++top1]=stack2[top2--]; stack2[++top2]=str[i]; }}}}} cout<<"\n answer \n"; while(top2>=0) stack1[++top1]=stack2[top2--]; for(i=0;i<=top1;i++) cout<<stack1[i]; getch();

} int prec(char ch) { int tt; if((ch=='+')||(ch=='-')) tt=2; if((ch=='*')||(ch=='/')) tt=3; if(ch=='(') tt=1; return tt; }

Output: Enter the expr : a+b(c-d)*e a+b(c-d)*e

answer abcd-e*+

Ex no: 10 Date: PROGRAM:


#include<iostream.h> #include<conio.h> void restoreHup(int*,int); void restoreHdown(int*,int,int); void main() { int a[20],n,i,j,k; //clrscr();

HEAP SORT

cout<<"\n Enter the number of element to sort : "; cin>>n; cout<<"\n Enter the elements : "; for(i=1;i<=n;i++) { cin>>a[i]; restoreHup(a,i); } j=n; for(i=1;i<=j;i++) { int temp; temp=a[1]; a[1]=a[n]; a[n]=temp; n--; restoreHdown(a,1,n);

} n=j; cout<<"\n here it is....."; for(i=1;i<=n;i++) cout<<a[i]<<endl; } void restoreHup(int *a,int i) { int v=a[i]; while((i>1)&&(a[i/2]<v)) { a[i]=a[i/2]; i=i/2; } a[i]=v; } void restoreHdown(int *a,int i,int n) { int v=a[i]; int j=i*2; while(j<=n) { if((j<n)&&(a[j]<a[j+1])) j++; if(a[j]<a[j/2]) break; a[j/2]=a[j]; j=j*2; }

a[j/2]=v; getch(); }

Output:
Enter the number of element to sort : 5 Enter the elements : 34 56 87 23 54 12 here it is.....

12 23 54 56 87

CONTENT BEYOND SYLLABUS Ex no: 11 Date: PROGRAM: #include<iostream.h> #include<conio.h> template<class t> void insertion(t array[],int n); void main() { int array[20]; int n; clrscr(); cout<<"\n\n\n\t\tHOW MANY NUMBER ARE U GOING TO INSERT"; cin>>n; for(int i=0;i<n;i++) { cout<<"\n\t\tGive the number"; cin>>array[i]; } insertion(array,n); } void insertion(int array[],int n) { int a[30]; a[0]=array[0]; for(int i=1;i<n;i++) { int temp=array[i]; int j=i-1; while((a[j]>temp&&j>=0)) INSERTION SORT

{ a[j+1]=a[j]; j--; } a[j+1]=temp; } for(int k=0;k<n;k++) { array[k]=a[k]; } cout<<"\n\n\tPRESS ENTER TO SEE EACH NO"<<endl; for(int z=0;z<n;z++) { cout<<"\tThe sorted number is"<<array[z]<<endl; getch(); } }

Output: HOW MANY NUMBER ARE U GOING TO INSERT 4 Give the number 9 Give the number 3 Give the number 5 Give the number 2

PRESS ENTER TO SEE EACH NO The sorted number is2 The sorted number is3 The sorted number is5 The sorted number is9

Ex no: 12 Date: PROGRAM: #include<iostream.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> #define size 8

MINIMUM COST SPANNING TREE IN A GRAPH

int cost[size][size]={0},n=0; void get_mat() { int i,j,v1,v2,wt; for(i=0;i<=size;i++) for(j=0;j<=size;j++) cost[i][j]=1000; cout<<"enter no.of vertices:"; cin>>n; do { cout<<"\nenter edge and their wt(v1,v2,wt):"; cin>>v1>>v2>>wt; cost[v1][v2]=cost[v2][v1]=wt; cout<<"\ncontinue(y/n)"; fflush(stdin); } while(getch()=='y'); }

void main() { int t[size]={0},wt[size]={0},min,p,sum=0,temp,i,j,k=0,l=0,m=0; clrscr(); get_mat();

t[1]=1; cout<<"\nTree include following edges:"; for(p=1;p<=n-1;p++) { temp=1000; for(int i=1;i<=n;i++) { if(t[i]==1) { min=1000; for(j=1;j<=n;j++) { if(cost[i][j]<min&&t[j]==0) { min=cost[i][j]; k=j; }} if(min<temp) { temp=min; }}} wt[p]=cost[m][l]; sum=sum+wt[p]; l=k; m=i;

cout<<"\n\nEdge:"<<m<<" "<<l<<"wt"<<cost[m][l]<<endl; t[l]=t[m]=1; } cout<<"\n\nwt of minimum spanning tree:"<<sum; getch(); }

OUTPUT: Enter the no. of vertices:4 Enter edge and their wt(v1,v2,wt): 1 3 2

Continue(y/n) y Enter edge and their wt(v1,v2,wt): 1 2 1 Continue(y/n) y Enter edge and their wt(v1,v2,wt): 2 4 3 Continue(y/n) y Enter edge and their wt(v1,v2,wt): 3 4 7 Continue(y/n) n Tree includes following edges: Edge:12 wt:1 Edge:13 wt:2 Edge:24 wt:3 WT of minimum spanning tree:6

Vous aimerez peut-être aussi