Anda di halaman 1dari 58

Ex.

no: 01 Date :

IMPLEMENTATION OF STACK USING ARRAY

Aim: To write a C++ program to implement STACK using array data structure. And perform the following stack operations 1. POP 2. PUSH 3. PEEP

Algorithm: STEP 1: Start STEP 2: Initialize stack, will=1,i, num STEP 3: Add element in stack PUSH(S,TOP,X) 3. a. [Check overflow condition] If(TOP>=N) then Write(Stack is full) 3. b. [Insert element] [Increment TOP] TOP <- TOP+1 S[TOP]<- X 3. c. [Finish the process] STEP 4: Delete element in stack POP(S,TOP) 4. a. [Check for underflow condition] If(TOP <- 0) then Write(Stack is empty) 4. b. [Delete element] [Decrement TOP] TOP<- TOP-1 Delete S[TOP+1] 4. c.[Finish the process] STEP 5: Stop

Output: 1. Push 2. Pop 3. Display Enter your Choice: 1 Enter the number to be pushed into the stack: 3 Number pushed is 3 Enter your Choice: 1 Enter the number to be pushed into the stack: 5 Number pushed is 5 Enter your Choice: 3 Contents of stack is 5, 3, Enter your Choice: 2 Deleted element is 5 Enter your Choice: 3 Contents of stack is 3 , Enter your Choice: 8 Invalid Choice......

Result:

Coding:
#include<iostream.h> #include<conio.h> #define max 10 int top=-1; class stack { int a[50]; public: void push(void); int pop(void); void display(void); }; void stack::push() { if(max-1<=top) { cout<<"stack is over flow\n\n"; cout<<"insertim not possible"; getch(); } else { cout<<"\n\n enter the element:"; cin>>a[++top]; } } int stack::pop() { int x=a[top]; top--; return(x); } void stack::display() { if(top!=-1) { cout<<"\n stack element are:"; cout<<"\n.............."; for(int i=top;i>=0;i--) cout<<"\n"<<a[i]; cout<<"\n\n top of the stack="<<a[top]; } else cout<<"\n stack is empts"; getch(); }

void main() { int choice; stack s1; do { clrscr(); cout<<"\n\t implementation of stack using array"; cout<<"\n\t\t 1.push"<<endl; cout<<"\n\t\t 2.pop"<<endl; cout<<"\n\t\t 3.display"<<endl; cout<<"\n\t exit"<<endl; cout<<"\n\t\t enter you choic[1-4]"; cin>>(choice); switch(choice) { case 1:s1.push(); break; case 2: if(top!=-1) { cout<<"\n popped element is"<<s1.pop(); getch(); } else { cout<<"\n stack is under fiow"; cout<<"\n delection not possible"; getch(); } break; case 3:s1.display(); break; } }while(choice<4); }

Ex. no: 2 Date:

IMPLEMENTATION OF STACK USING LINKED LIST

Aim: To demonstrate linked list implementation of stack using a C++ program.

Algorithm: Step 1: [Include all the necessary header files] Step 2: [Declare the Variables] Step 3: Read operator Step 4: IF opt 1 THEN Step 4.1: READ n Step 4.2: WHILE (n n-1) Step 4.2.1: READ d Step 4.2.2: CALL INSERT (start, d) Step 4.3: [End of while Structure] Step 5: IF opt 2 THEN Step 5.1: READ x Step 5.2: CALL del (start, x) Step 6: IF opt 3 THEN Step 6.1: READ x Step 6.2: CALL FIND Step 7: IF opt 4 THEN Step 7.1: READ x Step 7.2: CALL FINDPREVIOUS Step 8: IF opt 5 THEN Step 8.1: READ x Step 8.2: CALL FINDNEXT (start, x) Step 9: IF opt CALL len(Start) 6 THEN

Step 10: IF opt 7 THEN CALL printlist(Start) Step 10: IF opt 8 THEN CALL erase (Start)

Step 12: [End of Main] Algorithm For Find(struct node*p, int x, int *pos) Step 1: temp Step 2 :*pos p 1

Step 3: IF ( TEMP NULL) THEN RETURN NULL ELSE WHILE (TEMP! = NULL && TEMP DATA! = X) ASSIGN 1 TO *POS+1 AND TEMPS LLINK FIELD TO TEMP RETURN THE TEMP Algorithm for Previous (struct node*p, int x) Step 1: temp p

Step2: IF ( TEMP NULL) THEN RETURN NULL ELSE WHILE (TEMP LINK != NULL && TEMP ASSIGN TEMPS LINK FIELD TO TEMP RETURN THE TEMP Algorithm For Find next(struct node*p, int x)

LINK

DATA!= X)

Step 1: temp

Step2: IF (TEMP NULL) THEN RETURN NULL ELSE WHILE (TEMP LINK! = NULL && TEMP ASSIGN TEMPS LLINK FIELD TO TEMP RETURN THE TEMPS LINK FIELD

DATA! = X)

Output:
Implementation of stack using linked list: 1. Push 2. Pop 3. Display 4. Exit Enter your choice [1-4]:1 Enter the element: 12 Enter your choice [1-4]:1 Enter the element: 13 Enter your choice [1-4]:3 Stack elements are 13 12 Top of the stack=13 Enter your choice [1-4]:2 Popped element is 13 Enter your choice [1-4]:4

Result:

Coding:
#include<iostream.h> #include<conio.h> struct node { int data; node*link; }; class stack { node *top; public: stack() { top=NULL; } void push(int item1) { node *temp; temp=new node; if(temp==NULL) { cout<<"\n stack is full"; } else temp->data=item1; temp->link=top; top=temp; } int pop() { if(top==NULL) { cout<<"stack is empty"; return NULL; } node *temp; int item; temp=top; item=temp->data; top=top->link; delete temp; return item; } void disp(); };

void stack::disp() { node *temp1; temp1=top; cout<<"\n output data"; cout<<"\n item(s) in the stack are \n\n"; for(temp1=top;temp1;temp1=temp1->link) cout<<temp1->data<<"\n"; if(top!=NULL) cout<<"\n top of the stack is:"<<top->data; else cout<<"\n the stack is empty"; getch(); } void main () { int ch1,item,pop_it; stack s; do { clrscr(); cout<<"\n\t linked stack"; cout<<"\n\t 1. Push"; cout<<"\n\t 2. Pop"; cout<<"\n\t 3.Display"; cout<<"\n\t 4. Exit"; cout<<"\n\n\n\t enter your choice[1-4]:"; cin>>ch1; switch(ch1) { case 1:{cout<<"\n enter the item\n"; cin>>item; s.push(item); s.disp(); break; } case 2:{ pop_it=s.pop(); if(pop_it!=0) cout<<"\n popped item="<<pop_it; s.disp(); break; } case 3:{ s.disp(); break; } } }while(ch1!=4); }

Ex. no: 3 Date:

IMPLEMENTATION OF QUEUE USING ARRAYS

Aim: To write a C++ program to implement queue data structure using arrays.

Algorithm: STEP 1: Start STEP 2: [Include all header files] STEP 3: [Declare the variables] STEP 4: [If n->1 call the function Enqueue ( )] STEP 5: [If n->2 call the function Dequeue ( )] STEP 6: [If n->3 call the function Peep ( )] STEP 7: [If n->4 call the function Size ( )] STEP 8: [If n->5 call the function View ( )] STEP 9: [else Exit ( )] STEP 10: Stop

Algorithm for Enqueue( ) STEP 1: If[front= =rear] Initialize front=rear=0 STEP 2: else rear=(rear+1)% qsize Set queue[rear] =value [return] Algorithm for Dequeue( ) STEP 1: If[front = =rear] 1.1: temp=queue[front] 1.2: Initialize front=rear=-1 STEP 2:else

2.1: front=(front+1)% qsize [return]

Algorithm for Peep( ) STEP 1:If [front= =rear] STEP 1.1: temp=queue[front] [return]

Algorithm for Size( )

STEP 1:If [front= =rear] 1.1: Set f=front 1.2: Set count=1 STEP 2: If [front!=rear] 2.1: front=(front+1)%qsize 2.2: set count=count+1 [return]

Algorithm for View( )

STEP 1: If [front = =rear] Write (Queue is empty) STEP 2: else [display elements]

Output:

IMPLEMENTATION OF QUEUE USING ARRAY 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your Choice [1-4]:1 Enter the element for insertion: 12 Enter your Choice [1-4]:3 Queue Elements are 12 Front=12 Rear=12

Enter your Choice [1-4]:2 Deleted element from queue is 12

Enter your Choice [1-4]:4

Result:

Coding:
#include<iostream.h> #include<conio.h> # define size 10 class queue { int queue[size],rear, front; public: queue() { rear=front=-1; } void enq(void); void deq(void); void display(void); }; void queue::enq(void) { int item; if(rear!=size) { cout<<"\n Enter the element for instertion"; cin>>item; if(front==-1) { queue[++rear]=item; front=0; } else queue[++rear]=item; } else { cout<<"\nQUEUE IS OVERFLOW"; cout<<"\nDELETION IS NOT POSSIBLE"; } getch(); } void queue::deq(void) { if(front==-1) { cout<<"\nQUEUE IS UNDERFLOW"; cout<<"\nDELETION IS NOT POSSINLE"; getch(); return;

} cout<<"\nDeleted element from queue is"<<queue[front]; if(front==rear) front=rear=-1; else front++; getch(); } void queue::display() { if(front!=-1) { cout<<"\nQUEUE ELEMENTS ARE:"; cout<<"\n*******************"; for(int i=front;i<=rear;i++) cout<<"\t"<<queue[i]; cout<<"\n\nFRONT="<<queue[front]; cout<<"\n\tREAR="<<queue[rear]; } else cout<<"\nQUEUE IS EMPTY"; getch(); } void main() { queue q; int choice; int item; do { clrscr(); cout<<"\nIMPLEMENTATION OF QUEUE USING ARRAY"; cout<<"\n\n****************************************"; cout<<"\n\n\t\t1.ENQUEUE"; cout<<"\n\n\t\t2.DEQUEUE"; cout<<"\n\n\t\t3.DISPLAY"; cout<<"\n\n\t\t4.EXIT"; cout<<"\n\n\t\tEnter your choice[1-4]"; cin>>choice; switch(choice) { case 1:q.enq();break; case 2:q.deq();break; case 3:q.display();break; } }while(choice<4); }

Ex. no: 4 Date:

IMPLEMENTATION OF QUEUE USING LINKED LIST

Aim: To write a C++ program to implement queue data structure using linked list.

Algorithm STEP 1: Start STEP 2: [Include all header files] STEP 3: [Declare the variables] STEP 4: [If n->1 call the function Enqueue ( )] STEP 5: [If n->2 call the function Dequeue ( )] STEP 6: [If n->3 call the function Display ()] STEP 7: [else Exit ( )] STEP 8: Stop

Algorithm for Enqueue( ) STEP 1: r next = t STEP 2: t next = NULL STEP 3: Return.

Algorithm for Dequeue( ) STEP 1: x = front STEP 2: front = front next STEP 3: delnode (x) STEP 4: Return.

Algorithm for View( ) STEP 1: If (front = NULL) Print Empty Queue Return STEP 2: Else P = Start Repeat until (p< >NULL) Print p info P = p next Return.

OUTPUT: IMPLEMENTATION OF QUEUE USING LINKEDLIST 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your Choice [1-4]:1 Enter the element for insertion: 12 Enter your Choice [1-4]:3 Queue Elements are 12 Front=12 Rear=12 Enter your Choice [1-4]:2 Deleted element from queue is 12 Enter your Choice [1-4]:4

Result:

Coding:
#include<iostream.h> #include<conio.h> #include<stdlib.h> struct node { int data; node*link; }; class queue { node *front,*rear; public: queue() { front=rear=NULL; } void addq(int item) { node *temp; temp=new node; if(temp==NULL) cout<<"\n queue is overflow"; temp->data=item; temp->link=NULL; if(front==NULL) { rear=front=temp; return; } rear->link=temp; rear=rear->link; } int delq() { if(front==NULL) { cout<<"\n queue is underflow"; return NULL; } node *temp; int item; item=front->data; temp=front; front=front->link; delete temp; return item;

} void disp(void); }; void queue::disp(void) { int rear_data; node*t1; t1=front; cout<<"\n\t output data"; if(t1!=NULL) { cout<<"\n\t queue elements are"; do { cout<<t1->data<<"\t"; if(t1-> link==NULL) rear_data=t1->data; t1=t1->link; }while(t1!=NULL); cout<<"\n\n\t front="<<front->data; cout<<"\n\n\t rear="<<rear_data; } else cout<<"\n\n queue is empty"; getch(); } void main() { clrscr(); queue q; int ch1,n,del_item; while(1) { clrscr(); cout<<"\n\t operation of quee using linked list"; cout<<"\n\t 1.enqueue"; cout<<"\n\t 2.dequeue"; cout<<"\n\t 3.display"; cout<<"\n\t 4.exit"; cout<<"\n\t enter your choice"; cin>>ch1; switch(ch1) { case 1:cout<<"\n enter the item to add"; cin>>n; q.addq(n); break; case 2:del_item=q.delq(); if(del_item!=0) cout<<"\n delection is:"<<del_item; getch();

break; case 3:q.disp(); break; default: exit(0); } } }

Ex. No: 05 Date:

CONVERT INFIX TO POSTFIX EXPRESSION USING STACK

Aim: Implement the program for conversion of infix to postfix expression using stack Algorithm: 1. Get a infix expression as input 2. Scan the characters one by one 3. If the character is an operand then push it into the result stack 4. If the character is an operator then a. If the Expression stack is empty then push the operator b. If the precedence of the operator in the expression stack> current operator i. POP the operator from the expression stack and push into the result stack c. PUSH the operator into the expression stack 5. Repeat these steps until all the characters are read 6. POP all the operators from the stack and PUSH it to the result stack. 7. Stop

OUTPUT:

INFIX TO POSTFIX CONVERSION:

ENTER THE INFIX EXPRESSION: A+ (B*C/D)

THE POSTFIX EXPRESSION IS: ABC*D/+

Result:

Coding:
#include<iostream.h> #include<conio.h> #include<ctype.h> #define max 30 class post { char str[max]; public: void input(void); int prior(char); int present(char); void process(void); void line(void); }; void post::line(void) { int i; for(i=0;i<79;i++) cout<<"*"; } void post::input() { line(); cout<<"/n/t/t INFIX TO POSTFIX CONVERSATION \n"; cin>>str; } int post::prior(char c) { int pr; switch(c) { case'#':pr=-1; break; case'+':pr=1; break; case'-':pr=1; break; case'*':pr=2; break; case'/':pr=2; break; case')':pr=0; break; case'(':pr=0; break; }

return(pr); } int post::present(char c) { int pr1; switch(c) { case'+':pr1=1; break; case'-':pr1=1; break; case'*':pr1=2; break; case'/':pr1=2; break; case'^':pr1=4; break; case')':pr1=0; break; case'(':pr1=4; break; } return(pr1); } void post::process() { int i=0,top=0; char st[max],item; st[0]='#'; cout<<"\n THE POSTFIX EXPRESSION IS:" while(str[i]!='\0') { item=str[i]; if(isalpha(item)) cout<<item; else if(item==')') { while(st[top]!='(') { cout<<st[top]; top--; } top--; } else { while(prior(st[top])>=present(item)) { cout<<st[top]; top--;

} top++; st[top]=item; } i++; } while(top>=1) { cout<<st[top]; top--; } } void main() { post s; clrscr(); s.input(); s.process(); getch(); }

Ex. No: 06 Date:

EVALUATION OF POSTFIX EXPRESSION

Aim: Evaluate a Postfix Expression using C ++ Program.

Algorithm: STEP 1: create an empty stack S; STEP 2: index 1; STEP 3: while we have not reached the end of P do STEP 4: ch P[index]; {store in ch the next character in P} STEP 5: if ch is an operand then push ch onto S; STEP 6: else {ch is an operator} operand1 pop from S; operand2 pop from S; answer operation ch performed on operand1 and operand2; push answer onto S; endif STEP 7: index index +1; endwhile STEP 8: result pop from S; STEP 9: return result

Output:
EVLUATION OF POSTFIX CONVERSION: ENTER THE POSTFIX EXPRESSION: ABC*D/+ ENTER THE VALUE FOR A: 12 ENTER THE VALUE FOR B: 13 ENTER THE VALUE FOR C: 14 ENTER THE VALUE FOR D: 11 EVALUATION: 28

Result:

Coding:
#include<iostream.h> #include<conio.h> #include<ctype.h> #include<math.h> int stk[10]; int top; class postexp { public: postexp() { top=-1; } void push(int ch) { top++; stk[top]=ch; } int pop() { int c; c=stk[top]; top--; return(c); } }; void main() { char exp[20]; int a,b; int j=0; postexp e; clrscr(); cout<<"\n\t\t evaluation of postfix expression:"; cout<<"\n\t\t input:"; cout<<"\n\t\t enter the postfix expression in character form:"; cin>>exp; while(exp[j]!='\0') { if(isalpha (exp[j])) { cout<<"\n\t\t enter the value for"<<exp[j]<<"="; cin>>a; e.push(a); j++; }

if(exp[j]=='+'|exp[j]==''|exp[j]=='*'|exp[j]=='/'|exp[j]=='%'|exp[j]=='^') { a=e.pop(); b=e.pop(); switch(exp[j++]) { case'+': e.push(b+a); break; case'-': e.push(b-a); break; case'*': e.push(b*a); break; case'/': e.push(b/a); break; case'%': e.push(b%a); break; case'^': e.push(pow(b,a)); break; } } } cout<<"\n\t output"; cout<<"\n\t evaluation:"<<stk[0]; getch(); }

Ex No: 7 Date:

POLYNOMIAL ADDITION USING LINKED LIST

Aim: To implement the addition of two polynomial using linked list. Algorithm: 1. Using the function Createpoly () read the coefficient and exponent terms of the first polynomial until exponent term is zero. 2. Using the function Createpoly() read the coefficient and exponent terms of the second polynomial until exponent term is zero 3. Using the function addpoly() add the two polynomials with the following comparisons 4. If the exponent term in the first polynomial is greater than the exponent in the second polynomial, add the node of the first polynomial with the resultant polynomial 5. If the exponent term in the first polynomial is less than the exponent in the second polynomial, add the node of the second polynomial with the resultant polynomial 6. If the exponent term in the first polynomial is equal to the exponent in the second polynomial, add both the coefficient of the first and second polynomial and the node to the resultant polynomial 7. Traverse both the polynomial according to the above comparison up to the NULL value of both the polynomials are reached. 8. Display the resultant polynomial.

Output:

ENTER THE FIRST POLYNOMIAL EXPRESSION: ENTER THE LENGTH OF EXPRESSION: 3 ENTER THE COEFFICENT: 2 ENTER THE POWER: 2 ENTER THE SECOND POLYNOMIAL EXPRESSION: ENTER THE LENGTH OF EXPRESSION: 3 ENTER THE COEFFICENT: 2 ENTER THE POWER: 2 POLYNOMIAL EXPRESSION 2X^3+2X^2

Result:

Coding: #include<iostream.h> #include<conio.h> class poly { struct node { int co,ex; node*next; }*head,*temp,*temp1,*temp2,*temp3,*s; int l; public: poly() { l=0; } void cre(int); void pro(poly,poly); void show(); }; void poly :: cre(int x) { l=x; for(int i=0;i<l;i++) { head=new node; cout<<"\n enter the coefficient:"; cin>>head->co; cout<<"\n enter the exponent:"; cin>>head->ex; head->next=NULL; if(i==0) temp1=head; else temp->next=head; temp=head; } } void poly :: show() { temp2=temp1; for(int i=0;i<l;i++) { if(temp2->co!=0)

{ if(temp2->ex!=0) cout<<temp->co<<"x^"<<temp2->ex; else cout<<temp->co; s=temp2->next; if(i!=l-1) if(s->co>=0) cout<<"+"; } temp2=temp2->next; } } void poly :: pro(poly t1,poly t2) { temp2=t1.temp1; temp3=t2.temp1; while(temp2!=NULL&&temp3!=NULL) { head=new node; head->next=NULL; if(temp2->ex==temp3->ex) { head->co=temp2->co+temp3->co; head->ex=temp2->ex; temp2=temp2->next; temp3=temp3->next; } else { if(temp2->ex>temp3->ex) { head->co=temp2->co; head->ex=temp2->ex; temp2=temp2->next; } else { head->co=temp3->co; head->ex=temp3->ex; temp3=temp3->next; } } if(l==0) temp1=head; else temp->next=head; temp=head; l++; } while(temp2!=NULL)

{ head=new node; head->co=temp2->co; head->ex=temp2->ex; head->next=NULL; temp->next=head; temp=head; temp2=temp2->next; l++; } while(temp3!=NULL) { head=new node; head->co=temp3->co; head->ex=temp3->ex; head->next=NULL; temp->next=head; temp=head; temp3=temp3->next; l++; } } void main() { poly a,b,c; int ch; clrscr(); cout<<"\n enter the length of first expression="; cin>>ch; a.cre(ch); cout<<"\n enter the length of second expression="; cin>>ch; b.cre(ch); clrscr(); cout<<"\n\t\t polynomial addition using linked list"; cout<<"\n\t\t**************************************"; cout<<"\n input:\n******"; cout<<"\n 1st expression:"; a.show(); cout<<"\n 2nd expression:"; b.show(); c.pro(a,b); cout<<"\n output:\n ******"; cout<<"\n result expression:"; c.show(); getch(); }

Ex. No: 08 Date:

POLYNOMIAL ADDITION USING ARRAY

Aim: To implement the addition of two polynomial using array.

Algorithm: 1. Using the function Createpoly () read the coefficient and exponent terms of the first polynomial until exponent term is zero. 2. Using the function Createpoly() read the coefficient and exponent terms of the second polynomial until exponent term is zero 3. Using the function addpoly() add the two polynomials with the following comparisons 4. If the exponent term in the first polynomial is greater than the exponent in the second polynomial, add the node of the first polynomial with the resultant polynomial 5. If the exponent term in the first polynomial is less than the exponent in the second polynomial, add the node of the second polynomial with the resultant polynomial 6. If the exponent term in the first polynomial is equal to the exponent in the second polynomial, add both the coefficient of the first and second polynomial and the node to the resultant polynomial 7. Traverse both the polynomial according to the above comparison up to the NULL value of both the polynomials are reached. 8. Display the resultant polynomial.

Output: ENTER THE FIRST POLYNOMIAL EXPRESSION: ENTER THE LENGTH OF EXPRESSION: 3 ENTER THE COEFFICENT: 2 ENTER THE POWER: 2 ENTER THE SECOND POLYNOMIAL EXPRESSION: ENTER THE LENGTH OF EXPRESSION: 3 ENTER THE COEFFICENT: 2 ENTER THE POWER: 2 POLYNOMIAL EXPRESSION 2X^3+2X^2

Result:

Coding:
#include<iostream.h> #include<conio.h> class poly { int co[80],ex[80],l,i,j; public: void cre(); void show(); void add(poly,poly); }e1,e2,e3; void poly::cre() { cout<<"\n ENTER THE LENGTH OF EXPRESSION:"; cin>>l; for(i=0;i<1;i++) { cout<<"\n ENTER THE COEFFICIENT:"; cin>>co[i]; cout<<"\n ENTER THE EXPONENT(POW):"; cin>>ex[i]; } } void poly::show() { for(i=0;i<l;i++) { if(ex[i]==0) cout<<co[i]; else cout<<co[i]<<"x^"<<ex[i]; if(i!=l-1) if(co[i+1]>0) cout<<"+"; } } void poly::add(poly temp,poly temp1) { i=j=l=0; while(i<temp.l&&j<temp1.l) { if(temp.ex[i]==temp1.ex[j]) { co[l]=temp.co[i]+temp1.co[j]; ex[l]=temp.ex[i++];

j++;l++; } else if(temp.ex[i]>temp1.ex[j]) { co[l]=temp.co[i]; ex[l]=temp.ex[i]; l++;i++; } else { co[l]=temp1.co[j]; ex[l]=temp1.ex[i]; l++;j++; } } while(i<temp1.l) { co[l]=temp.co[i]; ex[l]=temp.ex[i]; i++;l++; } while(j<temp1.l) { co[l]=temp1.co[j]; ex[l]=temp1.ex[j]; l++;j++; } } void main() { clrscr(); cout<<"\n ENTER THE FIRST POLYNOMIAL EXPRESSION:\n"; e1.cre(); cout<<"\n ENTER THE SECOND POLYNOMIAL EXPRESSION:\n"; e2.cre(); clrscr(); cout<<"\n\t\t POLYNOMIAL ADDITION USING ARRAY:"; cout<<"\n INPUT;"; cout<<"\n FIRST EXPRESSION="; e1.show(); cout<<"\n SECOND EXPRESSION="; e2.show(); cout<<"\n OUTPUT:"; cout<<"\n ADDED POLYNOMIAL EXPRESSION="; e3.add(e1,e2); e3.show(); getch(); }

Ex. No: 09 Date:

DOUBLY LINKED LIST LINKED LIST IMPLEMENTATION

Aim: To write a program to implement doubly linked list using linked list.

Algorithm: Step 1: Declare header and pointer variables Step 2: Display the choices Step 3: If choice is 1 get the element to be inserted in beginning and call ins_beg function. Step 4: If choice is 2 get the element to be inserted in the end and call the ins_end function Step 5: If choice is 3 then get the element to be deleted and call deletion function. Step 6: If choice is 4 then call display function Step 7: If choice is default the exit the program Step 8: Terminate the program execution.

Output: 1. Insert in Beginning 2. Insert in the End 3. Delete 4. Display Enter your Choice: 1 Enter the element to be inserted: 2 Enter your Choice: 1 Enter the element to be inserted::3 Enter your Choice: 4 3 ->2 -> Enter your Choice: 2 Enter the element to be inserted::1 Enter your Choice: 2 Enter the element to be inserted::5 Enter your Choice: 4 3 ->2 ->1 ->5 -> Enter your Choice: 3 Enter the element to be deleted: 1 Data 1 is deleted Enter your Choice: 4 3 ->2 ->5 ->

Result:

Coding: #include<iostream.h> #include<conio.h> #include<process.h> class dou { struct node { node *back; int data; node *next; } *head,*start,*last,*temp,*temp1; int count; public: dou() { count=0; start=null; } void cre(); void ins(); void del(); int front(); void view(); }; void dou::cre() { int ch; if(count==0) { cout<<"\n ENTER THE NUMBER OF NODES"; cin>>ch; cout<<"ENTER THE VALUES:"; for(int j=0;j<ch;j++) { head=new node; cin>>head->data; head->next=null; head->next=null; if(j==0) start=head; else { head->back=last; last->next=head; } last=head;

count++; } else cout<<"\n ALREADY LIST CREATED'; } void dou::ins() { int ch; if(count==0) { cout<<"\n CREATED LIST FIRST"; getch(); return; } else { view(); cout<<"\n 1.AT.BEGIN\n2.IN MIDDLE\n3.AT END'; cin>>ch; if(ch==1) { head=new node; cout<<"\n ENTER THE VALUES:"; cin>>head->data; head->back=null; head->next=start; start=head; count++; } if(ch==2) { int p; head=new node; temp=temp1=start; cout<<"\n ENTER THE POSITION:"; cin>>p; cout<<"ENTER THE VALUES:"; cin>>head->data; for(int i=1;i<p-1;i++) temp=temp->next; temp1=temp->next; head->back=temp; temp->next=head; head->next=temp1; temp1->back=head; count++; } } if(ch==3) { head=new node;

cout<<"ENTER THE VALUES:"; cin>>head->data; head->next=null; head->back=last; last=head; count++; } void dou::del(); { int ch; if(count==0) { cout<<"\n LIST IS EMPTY\n"; return; } else { view(); cout<<"\n 1.AT BEGIN\n2.AT MIDDLE\n3.AT END"; cin>>ch; if(ch==1) { cout<<"\n DELETED VALUE"; cout<<start->data; start=start->next; start->back=null; count--; } if(ch==2) { int p; temp=start; cout<<"\n ENTER THE POSITION"; cin>>p; cout<<"\nDELETED VALUES"; for(int i=1;i<p-1;i++) temp=temp->next; temp1=temp->next; if(count==1) cout<<start->data; else cout<<temp1->data; temp1=temp1->next; temp->next=temp1; tenp1->back=temp; count--; } } if(ch==3) { cout<<"\n DELETED VALUE";

cout<<last->data; last=last->back; last->next=null; count--; if(count==0) start=null; } } void dou::view() { temp=start; if(temp==null) { cout<<"\n LIST IS EMPTY\n\t'; return; } cout<<"\n LIST ELEMENTS ARE:"; while(temp!=null) { cout<<temp->data; if(temp->next!=null) cout<<"<==>"; temp=temp->next; } getch(); } int dou::front() { int ch; clrscr(); cout<<"\n\t\t OPERATIONS IN DOUBLY LINKED LIST"; cout<<"\n 1.creation\n2.insretion\n3.delection\n4.view\n5.exit"; cout<<"\nENTER YOUR CHIOCE'; cin>>ch; return ch; } void main() { dou d; clrscr(); while(1) { switch(d.front()) { case 1: d.cre();break; case 2: d.ins();break; case 3: d.del();break; case 4: d.view();break; default;

getch(); exit(0); } } }

Ex. no.: 10 Date :

BINARY TREE TRAVERSAL

Aim: To write a C++ program to implement a stack using binary search tree.

Algorithm: 1. 2. 3. 4. 5. 6. 7. [Include all the necessary header files.] [Declare the structure with all necessary variables.] Read x; Call INORDER(). Call PREORDER(). Call POSTORDER(). Call display().

Algorithm For INSERT(P,X) 1. If (pNULL) Create P P<-datax. P->lchild PrchildNULL Else while(TEMP!=NULL) Temp2Temp1 If(temp1datax) Else Temp1Temp1rchild [End of while structure] If(temp2datax) Temp 2Temp2lchild Temp 2datax Temp2dataslchildtemp2rchild Null Else Temp 2Temp2Temp2rchildnull Temp2datax Temp 2lchildTemp 2rchildnull [Return P]

2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13 2.14

Algorithm For INORDER(p) 1. 2. 3. 4. 5. If (p! =Null) CALL INORDER (pxdhild) WRITE (Ddata) CALL INORDER (prchild) [End the function]

Algorithm for PREORDER 1. 2. 3. 4. 5. If (pl=NULL) WRITE (PData) CALL PREORDER (PlCHILD) CALL PREORDER (P Rchild) [END OF FUNTION]

Algorithm for POSTORDER 1. If (P!=NULL) 2. Call POSTORDER (Plchild) 3. Call POSTORDER (Prchild) 4. Write (Pdata) 5. [End of function] Output:
ENTER THE LEVEL OF BINARY TREE: 2 ENTER THE NODE VALUES: ABC INORDER: BAC PREORDER: ABC POSTORDER: BCA

Result:

Coding:
#include<iostream.h> #include<conio.h> #include<math.h> #include<process.h> class tree { char a[100]; int level; int maxnode; public: void gettree(); void inorder(int); void preorder(int); void postorder(int); }; void tree::gettree() { clrscr(); cout<<"\n\t binary tree traversal"; cout<<"\n"; cout<<"\n enter the level of binary tree"; cin>>level; maxnode=pow(2,level)-1; cout<<"\n enter the node values"; for(int i=1;i<=maxnode;i++) if(i>1&&a[i/2]=='#') a[i]='#'; else cin>>a[i]; for(i=maxnode+1;i<=maxnode*2+1;i++) a[i]='#'; } void tree::inorder(int i) { if(a[i]!='#') { inorder(2*i); cout<<"\t"<<a[i]; inorder(2*i+1); } } void tree::preorder(int i) { if(a[i]!='#')

{ cout<<"\t"<<a[i]; preorder(2*i); } } void tree::postorder(int i) { if(a[i]!='#') { postorder(2*i); postorder(2*i+1); cout<<"\t"<<a[i]; } } void main() { tree t; t.gettree(); cout<<"\n inorder"; t.inorder(2); cout<<"\n\t"; cout<<"\n preorder"; t.preorder(1); cout<<"\n\t"; cout<<"\n postorder"; t.postorder(1); cout<<"\n\t"; getch(); }

Ex. no.: 11 Date :

DEPTH FIRST SEARCH

Aim: To write a C++ program to implement a Depth First Search.

Algorithm: Step 1: Initialize all nodes to ready state (status = 1) Step 2: Push the starting node in stack and change its status to the waiting state (status = 2) Step 3: Repeat step 4 and 5 until stack is empty Step 4: pop the top node n of stack. Process n and change the status of n to the processed state (status = 3) Step 5: Push on to the stack, all the neighbor of n that are in ready state (status = 1), and change their status to the waiting state (status = 2) [End of the step 3 loop] Step 6: Exit

Output:

Result:

Coding:
#include<iostream.h> #include<conio.h> int i,n,j=0,b=0,k=0,x[50][50],e,f,r,v[50]; char a[50],d,l,h[50]; class depth { public: void input(); void process(int m,int q); void line(); }; void depth::input() { cout<<"\n enter the no of verture"; cin>>n; for(i=1;i<=n;i++) { cin>>a[i]; } getch(); for(i=1;i<n;i++) { for(j=1;j<=n;j++) { x[i][j]=0; } } i=0; e=n; while(e!=0) { i=i+1; cout<<"\n enter the no of register for "<<a[i]<<"="; cin>>b; k=k+1; while(b!=0) { cout<<"\n enter the value:"; cin>>d; f=n; j=1; r=1; while(f!=0) { if(d==a[r]) {

x[k][i]=1; break; } else { j=j+1; r=r+1; } f=f-1; } b=b-1; } e=e-1; } for(i=1;i<=n;i++) { v[i]=0; } getch(); clrscr(); cout<<"\n\t adjacent matrix\n\t.."<<endl; for(i=1;i<=n;i++) { for(j=1;j<n;j++) { cout<<x[i][j]<<"\t"; } cout<<endl; } cout<<"\n enter the starting vertex:"; cin>>l; cout<<"\n"; i=1; if(a[i]==l) { i=1; v[j]=1; k=1; } while(a[i]!=l) { i=i+1; if(a[i]==1) { v[i]=1; k=1; break; } } h[i]=i; cout<<a[i];

process(i,k); } void depth::process(int m,int q) { for(j=1;j<=n;j++) { if((x[m][j]==1)&&(v[j]==0)) { cout<<"t"<<a[j]; v[j]=1; q=q+1; i=i+1; h[i]=m; process (j,q); } } if((q!=n)&&(i!=0)) { m=h[i]; i+i-1; process(m,q); } } void depth::line() { int h; for(h=0;h<80;h++) { cout<<"\t"; }} void main() {clrscr(); depth d; d.line(); cout<<"\n\t\t"<<"depth first search"; d.line(); d.input(); getch(); }

Ex. no.: 12 Date :

BREADTH FIRST SEARCH

Aim: To write a C++ program to implement a Breadth First Search.

Algorithm: STEP1: color all the vertices white STEP 2: Initialize an empty queue Q STEP 3: For each neighbor v of s STEP 4: Insert v in Q; color[v] = grey STEP 5: Output s; color[s] = black STEP 6: While Q is not empty STEP 7: u =top of Q; remove u from Q STEP 8: For each neighbor v of u STEP 9: If color[v] = white STEP 10: Insert v in Q; color[v] = grey STEP 11: Output u; color[u] = black

Output:

Result:

Coding:
#include<iostream.h> #include<conio.h> int i,n,j=0,b=0,k=0,x[50][50],e,f,r,v[50]; char a[50],d,l,h[50]; class breadth { public: void input(); void process(); void line(); }; void breadth::input() { cout<<"\n enter the number of veritces:"; cin>>n; for(i=1;i<=n;i++) { cin>>a[i]; } getch(); clrscr(); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { x[i][j]=0; } } i=0; e=n; while(e!=0) { i=i+1; cout<<"\n enter the number of vertices for"<<a[i]<<"="; cin>>b; k=k+1; while(b!=0) { cout<<"\n enter the value:"; cin>>d; f=n; j=1; r=1; while(f!=0) { if(d==a[r]) {

x[k][j]=1; break; } else { j=j+1; r=r+1; } f=f-1; } b=b-1; } e=e-1; } for(i=1;i<=n;i++) { v[i]=0; } getch(); clrscr(); cout<<"\n\t\t ADJACENT MATRIX\n\t\t***********"<<endl; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cout<<x[i][j]<<"\t"; } cout<<endl; } } void breadth::process() { cout<<"\n ENTER THE STARTING VERTEX:"; cin>>l; cout<<"\n"<<l; i=1; if(a[i]==1) { v[i]=1; } while(a[i]!=1) { i=i+1; if(a[i]!=1) { v[i]=1; break; } } k=1;j=1; while(k!=n)

{ if((x[i][j]==1)&&(v[j]==0)) { cout<<"\t"<<a[i]; k=k+1; v[j]=1; if((j==n)&&(k!=n)) { i=i+1; j=1; } } else if((i==n)&&(j==n)&&(x[i][j]==1)&&(k!=n)) { i=1;j=1; } else if(j<n) { j=j+1; } else { i=i+1; j=1; } } } void breadth::line() { int h; for(h=0;h<80;h++) { cout<<"*"; } } void main() { clrscr(); breadth b; b.line(); cout<<"\n\t\t\t"<<"breadth first search"; cout<<"\n\t\t"<<"*************"<<endl; b.line(); b.input(); b.process(); getch(); }

Anda mungkin juga menyukai