Вы находитесь на странице: 1из 60

Sr Name of Experiment . No 1 Write algorithm and perform programming using simple numeric computations.

2 Write algorithm and perform programming using compound statement (if, else) 3 Write algorithm and perform programming using compound statement (loop) 4 Write algorithm and perform programming for series calculation 5 Write algorithm and perform programming using function 6 Write algorithm and perform programming for sorting 7 Write algorithm and perform programming for searching 8 Write algorithm and perform programming for various matrix operations 9 Write algorithm and perform programming for linked list 10 Write algorithm and perform programming for stack 11 Write algorithm and perform programming for queue 12 Write algorithm and perform programming for tree

Page No.

Date

Grade

Signature

Experiment No. 1 Title: Write algorithm and perform programming using simple numeric computations

Experiment No. 1 Aim: Program and algorithm to calculate Simple Interset. //Program #include <stdio.h> #include <conio.h> void main() { float p, r, t, si; clrscr(); printf(Enter the value of principal = ); scanf(%f,&p); printf(Enter the rate of interest =); scanf(%f,&r); printf(Enter the time period=); scanf(%f,&t); si=p*r*t/100; printf( Simple Interest =%f,si); getch(); } Output:

o Enter the value of principal =1000 the rate of interest =3.8 the time period=2 Simple Interest =76.00

Experiment No. 1 Algorithm: Step 1: [NAME] Algorithm to calculate Simple Interest [INITIALIZATION] p is float variable for principal amount r is float variable for rate of interest t is float variable for time in years si is float variable for simple interest [INPUT] READ (p,n,r) [CALCULATE] si p*r*t/100 [OUTPUT/DIAPLAY] WRITE (si) [FINISH] HALT

Step 2:

Step 3:

Step 4:

Step 5:

Step 6:

Experiment No. 2 Title: Write algorithm and perform programming using compound statement (if..else)

Experiment No. 2 Aim: Program and algorithm to find largest of three numbers entered. //Program #include<stdio.h> #include<conio.h> Void main() { int a,b,c; clrscr(); printf(Enter a=); scanf(%d, &a); printf(Enter b=); scanf(%d, &b); printf(Enter c=); scanf(%d, &c); if(a>b && a>c) printf(\n\n Largest is a=%d, a);

if(b>a && b>c) printf(\n\n Largest is b=%d, b); if(c>a && c>b) printf(\n\n Largest is c=%d, c); getch(); }

Experiment No. 2 OUTPUT Enter a=15 Enter b=-6 Enter c=0

Largest is a=15

Experiment No .2 Algorithm: STEP 1: [NAME] Algorithm to find largest of three numbers [INITIALIZATION] a, b, c are integer variables [INPUT] READ (a, b, c) [CALCULATE] IF (a > b AND a > c) Go to Step 5 IF (b > a AND b > c) Go to Step 6 IF (c > a AND c > b) Go to Step 7 STEP 5: [OUTPUT/DISPLAY] WRITE (a) ENDIF [OUTPUT/DISPLAY] WRITE (b) ENDIF

STEP 2:

STEP 3:

STEP 4:

STEP 6:

STEP 7:

[OUTPUT/DISPLAY] WRITE (c) ENDIF [FINISH] HALT

STEP 8:

Experiment No. 3 Title: Write algorithm and perform programming using compound statement (loop)

Experiment No. 3 Aim: Program and algorithm to print table from 1 to 10. //Program #include <stdio.h> Experiment No. 3 Aim: Write algorithm and perform programming using compound statement (loop) //PROGRAM #include <conio.h> void main() { int I, j, k; clrscr(); for(i=1;i<10;i++) { for(j=1;j<10;j++) { k=i*j; printf(%d\t,k); } printf(\n); } getch(); }

10

Experiment No. 3 Algorithm: Step 1: [NAME] Algorithm to print table from 1 to 10 [INITIALIZATION] I, j, k are integer variables [OUTER LOOP] Repeat FOR i 0 to i <= 10 by i i+1 through step 6 [OUTER LOOP] Repeat FOR j 0 to j <= 10 by j j+1 through step 6 [CALCULATE] K i*j [OUTPUT/DIAPLAY] WRITE (k) ENDREPEAT ENDREPEAT [FINISH] HALT

Step 2:

Step 3:

Step 4:

Step 5:

Step 6: s

Step 7:

11

Experiment No. 4 Title: Write an algorithm and perform program for series calculation.

12

Experiment No.4 Aim: Program and algorithm to print Fibonacci series //program #include<stdio.h> #include<conio.h> Void main() { Float a=1,b=0,c,I,n; Clrscr(); printf (Enter the number of terms of Fibonacci series); scanf (%f,&n); For(i=0;i<=n;i++) { c = b; b = a + b; a = c; printf (%0.0f\t, b) } getch(); }

Output: Enter the number of terms of Fibonacci series=25 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025

13

Experiment No. 4 Algorithm: Step 1:

[NAME] Algorithm to print Fibonacci series [INITIALIZATION] a 1, b 0, c, I, n are float variables [INPUT] READ (n) [LOOP] Repeat FOR i 0 to I <= n by i i+1 through step 6 [CALCULATE] c b; b a + b; a c; [OUTPUT/DIAPLAY] WRITE (b) ENDREPEAT [FINISH] HALT

Step 2:

Step 3:

Step 4:

Step 5:

Step 6:

Step 7:

14

Experiment No.5 Title: Write an algorithm and perform program using function.

15

Experiment No.5 Aim: Program and algorithm to calculate factorial of number using recursion //program #include<stdio.h> #include<conio.h> float factorial(float n); Void main() { Float fact, num; clrscr(); printf (Enter the number=); scanf (%f, &num); Fact = factorial(num); printf (factorial of %0.0f=%0.0f, num, fact); getch(); } float factorial (float n) { float temp; If(n==0) { temp = 1; return (temp) } else { temp = n*factorial(n-1);
16

Experiment No.5

return ( temp); } }

Output: Enter the number=34 Factorial of 34=295232822996533287000000000000000000000

17

Experiment No.5 Algorithm: Step 1: [NAME] Algorithm to calculate factorial of number using recursion Step 2: [INITIALIZATION] fact, num are float variables Step 3: [INPUT] READ (num) Step 4: [CALL PROCEDURE] Call factorial(num) such that fact factorial(num) Step 5: [OUTPUT/DISPLAY] WRITE(fact) Step 6: [FINISH] HALT Sub Algorithm: Step 1: [NAME] factorial(num) Num is float variable received from main procedure Step 2: [INITIALIZATION] temp is float variable Step 3: [CONDITION] If num=0 goto Step 4 ELSE goto Step 6 Step 4: [CALCULATE] Temp1 Step 5: [FINISH] RETURN Step 6: [CALL PROCEDURE] Call factorial(num) such that tempn* factorial(n-1)

18

Step 7: [FINISH] RETURN(temp)

Experiment No.6 Title: Write an algorithm and perform program using function.

19

Experiment No.6 Aim: Program and algorithm to sort array by bubble sort //program #include<stdio.h> #include<conio.h> #define size 10 void main() { int arr[size]; int I,j,temp; clrscr(); printf (Enter the elements of array:\n); for(i=0;i<size;i++) { printf (\n Enter % d element-, i+1); scanf (%d,&arr[i]); } for(i=0;i<size-1;i++) { for(j=0;j<size-1;j++) { if (arr[j]> arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1};arr[j+1] = temp; } } } printf (\n\nSorted Array:\n); for(i=0;i<size;i++)
20

Experiment No.6 { printf(%d\t,arr[i]); } getch(); }

Output: Enter the elements of array: Enter 1 element=54 Enter 2 element=69 Enter 3 element=02 Enter 4 element=87 Enter 5 element=48 Enter 6 element=156 Enter 7 element=354 Enter 8 element=01 Enter 9 element=45 Enter 10 element=645 Sorted Array: 1 2 45 48 54 69 87 156 354 645

21

Experiment No.6 Algorithm: Step 1: [NAME] Algorithm to sort array by bubble sort [INITIALISATION] SIZE is global variable with size 10 arr[SIZE] is an integer array I, j, temp are integer variables [LOOP] Repeat FOR i 0 to i SIZE by i i+1 through step 4 [INPUT] READ(arr[i]) ENDREPEAT [OUTER LOOP] Repeat FOR i 0 to i SIZE by i i+1 through step 8 [OUTER LOOP] Repeat FOR j 0 to j SIZE by j i+1 through step 8 [CONDITION] IF( arr [j] > arr[j+1]) GOTO Step 8 [SWAP] temp = arr[j] arr[j] = arr[j+1] arr[j+1] = temp ENDIF ENDREPEAT ENDREPEAT [CALCULATE] ki*j
22

Step 2:

Step 3:

Step 4:

Step 5:

Step 6:

STEP 7:

Step 8:

Step 9:

Step 10: [OUTPUT/DISPLAY] WRITE (k) ENDRPEAT ENDRPEAT Experiment No.6 Step 11: [LOOP] Repeat FOR i 0 to i SIZE by i i+1 through step 12 Step 12: [OUTPUT] WRITE (arr[i]) ENDREPEAT Step 13: [FINISH] HALT

23

Experiment No. 7 Title: Write algorithm and perform programming for searching.

24

Experiment No. 7 Aim: Program and algorithm to search number in the array using binary search. //Program #include <stdio.h> #include <conio.h> #define size 10 void main() { int arr[size]; int low, up, mid, i, j, temp, item; clrscr(); printf(Enter the element af array:\n); for(i=0; i<size; i++) { printf(\nEnter %d element=, i+1); scanf(%d, &arr[i]); } for(i=0; i<size-1; i++) { for(j=0; j<size-1; j++) { if(arr[j] > arr[j+1]) { temp= arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } printf(\n\nSorted Array;\n); for(i=0; i<size;i++)
25

{ printf(%d\t, arr[i]); scanf(%d, &item); low = 0; up = size 1; Experiment No.7 while (low <=up && item !=arr[mid]) { mid = (low + up) / 2; if(item > arr[mid]) low=mid +1; if(item < arr[mid]) low = mid + 1; if(item ++ arr[mid]) printf(\nElement %d found at %d position, item, mid+1); if(low > up) printf(\nElement %d not founr in array, item); } getch(); }

OUTPUT: Enter the element of array: Enter 1 element=54 Enter 2 element=69 Enter 3 element=02 Enter 4 element=87 Enter 5 element=48 Enter 6 element=156 Enter 7 element=354 Enter 8 element=10 Enter 9 element=45 Enter 10 element=625 Sorted Array: 2 10 45 48 54 69 87 156 354 625 Enter the number to be searched:54
26

Element 54 found at 5 position

Experiment No. 7 ALGORITHM: Step 1: Step 2: [NAME] Algorithm to search number in the array using binary search [INITIALIZATION] SIZE is a global variable with size 10 arr[SIZE} is an integer array which is sorted in ascending order I, j, are integer variables Item is an integer variables to locate minimum and maximum number in the array [LOOP] Repeat FOR i 0 to i <= SIZE by i i+1 through Step 4

Step 3:

Step 4: [INPUT] READ ( arr[i]) ENDREPEAT Step 5: [ASSIGN] Low 0 Up size -1 [LOOP] Repeat through step 15 WHILE (low <= up && item = arr[mid]) [CONDITION] IF(item > arr[mid]) GOTO Step 9

Step 6:

Step 7:

Step 8: [ASSIGN] Low mid +1 ENDIF Step 9: [CONDITION] IF (item < arr[mid]) GOTO Step 11

Step 10: [ASSIGN]


27

High mid 1 ENDIF

Experiment No. 8 Title: Write algorithm and perform programming for various matrix operations.

28

Experiment No. 8 Aim: Program and algorithm to calculate addition of matrix. //Program #include<iostream.h> #include<stdio.h> #include<conio.h> #define row 3 #define col 3 void main() { Int mat[row][col]; Int i,j,item, k=0; clrscr(); printf(Enter the element of matrix:\n); for(i=0; i<row; i++) { for(j=0;j<col;j++) { printf(Enter %*%d element:,i+1,j+1); scanf(%d,&mat[i][j]); } { printf(\n\n Enter the number to be searched:); scanf(%d,&item); for(i=0;i<row;i++) {
29

for(j=0;j<col;j++) { if(item==mat[i][j])

Experiment No.8 printf(\n %d is found at %d*%d position,item,i+1,j+1); K++; } } } if(k==0) printf(\n %d is not found,item); getch(); }

OUTPUT Enter the elements of matrix: Enter 1*1 element: 1 Enter 1*2 element: 2 Enter 1*3 element: 3 Enter 2*1 element: 1 Enter 2*2 element: 2 Enter 2*3 element: 3 Enter 3*1 element: 1 Enter 3*2 element: 2 Enter 3*3 element: 3

Enter the number to be searched:3


30

3 is found at 1*3 position 3 is found at 2*3 position 3 is found at 3*3 position

Experiment no.8 Algorithm: Step 1: [NAME] Algorithm to sort array by bubble sort [INITIALIZATION] row is global variable of size 3 col is a global variable of size 3 i, j, k are integer variables item is an integer variable to be search in matrix mat[row] [col] is two dimensional array (matrix) of size row col [OUTER LOOP] Repeat FOR i 0 to i < row by i i+1 through step 6 [OUTER LOOP] Repeat FOR j 0 to j < row by j j+1 through step 6 [CONDITION] IF (item == mat[i][j]) GOTO step 6 [OUTPUT & CALCULATE] WRITE (Number found) k k+1 ENDIF ENDREPEAT [CONDITION] IF(k=0) GOTO Step 8
31

Step 2:

Step 3:

Step 4:

Step 5:

Step 6:

Step 7:

Step 8:

[OUTPUT] WRITE (Number not found) [FINISH] HALT

Step 9:

Experiment No. 9 Title: Write algorithm and perform programming for linked list.

32

Experiment No. 9 Aim:Write Algorithm and perform programming for linked list //Program: #include<iostream.h> #include<conio.h> Class linklist { private: struct node { int data; node*link; }*p; public: linklist(); void append(int num); void addatbeg(int num); void addafter(int c,int num); void del(int num); void display(); Int count(); ~linklist(); }; linklist::linklist() { p=NULL; } void linklist::append(int num)
33

{ node*q,*t; If(p==NULL) { p=new node; pdata=num; plink=NULL; } else { q=p; while(qlink!=NULL) Experiment No.9 q=qlink; t=new node; tdata=num; tlink=NULL; qlink=t; } display(); } void linklist::addatbeg(int num) { node *q; q=new node; qdata=num; qlink=p; p=q; } void linklist::del(int num) { node*q,*t; int i; for(i=0,q=p;i<c;i++) { q=qlink; if(q==NULL) {
34

cout<<endl<<there are,less than<<c<<elements; return; } } t=new node; tdata=num; tlink=qlink; qlink=t; } void linklist::del(int num) { node*q,*r; Experiment No.9 q=p; if(qdata==num) { p=qlink; delete q; return; } r=q; while(q!=NULL) { If(q data==num) { rlink=qlink; delete q; return; } r=q; q=qlink; } cout<<endl<<element<<num<<numnot found; } void linklist::display() {
35

node*q; cout<<endl; for(q=p;q!=NULL;q=qlink) cout<<qdata<<; } int linklist::count() { node*q; int c=0; for(q=p;q!=NULL;q=qlink) c++; return(c); } Experiment No. 9 { node*q; if(p==NULL) return; while(p!=NULL) { q=plink; delete p; p=q; } } void main() { linklist11; clrscr(); cout<<end;l<<number of element in link list=<<11.coun t(); 11.append(11); 11.append(22); 11.append(33); 11.append(44); 11.append(55); 11.append(66); 11.display(); 11.addatbeg(100); 11.addatbeg(200); 11.addatbeg(300); 11.addafter(3,333); 11.addafter(6,444);
36

11.display(); cout<<endl<<number of element in linked list=<<11.count(); 11.del(300); 11.del(66); 11.del(0); 11.display(); cout<<endl<<number of element in linked list=<<11.count(); getch(); } OUTPUT Number of element in linked list=0 11 1122 112233 Experiment No. 9 11223344 1122334455 112233445566 112233445566 300200100113332233444445566 Number of element in linked list=11 Element00not fount 200100113322334444455 Number of element in linked list=9

37

Experiment No. 9 Algorithm: To insert node at the end. Step 1: [NAME] Algorithm to insert node at the end. Step 2: [INITIALIZATION] FIRST is a pointer variable which indicates address of first link in list. X is the information stored in new node. NODE is a global variable. NEW(POINTER), SAVE are local variables Step 3: [CREATE NEW NODE] NEW NODE Step 4: [INITIALIZE INFORMATION OF NODE] NEW(INFO) X NEW(LINK) NULL Step 5: [CHECK FOR FIRST NODE] IF FIRST=NULL THEN RETURN(NEW) END IF Step 6: [INITIALIZE THE SEARCH FOR THE LAST NODE] SAVEFIRST
38

Step 7: [SEARCH FOR THE LAST NODE] Repeat WHILE SAVE(LINK)!= NULL through Step 8 Step 8: [ASSIGNMENT] SAVESAVE(LINK) ENDREPEAT Step 9: [ASS NEW NODE AT THE NODE] SAVE(LINK)NEW Step 10: [FINISH] RETURN

39

Experiment No. 9 Algorithm: To insert node at the front. Step 1: [NAME] Algorithm to insert node at front. Step 2: [INITIALIZATION] FIRST is a pointer variable which indicates address of first link in list. X is the information stored in new node. NODE is a global variable. NEW(POINTER), SAVE are local variables Step 3: [CREATE NEW NODE] NEW NODE Step 4: [INITIALIZE INFORMATION OF NODE] NEW(INFO) X Step 5: [ASSIGN ADDRESS TO NEW NODE] NEW(LINK) FIRST Step 6: [FINISH] RETURN

40

Experiment No. 9 Algorithm: To delete node at the end. Step 1: [NAME] Algorithm to delete node at the end. Step 2: [INITIALIZATION] FIRST is a pointer variable which indicates address of first link in list. Step 3: [CHECK FOR EMPTY] IF FIRST=0 Then GOTO Step 4 Step 4: [DISPLAY AND FINISH] WRITE(EMPTY LIST) RETURN Step 5: [ACCESS THE NODE] PFIRST Step 6: [LOOP] Repeat WHILE P!=Z through Step 6 Step 7: [ASSIGNMENT] TP PLINK(P) END REPEAT Step 8: DELETE NODE] XDATA(Z) LINK(T)LINK(Z) LINK1(Z)0 Step 9: [FINISH] RETURN

41

Experiment No. 10 Title: Write algorithm and perform programming for stack.

42

Experiment No .10 AIM: WRITE A PROGRAM FOR STACK //PROGRAM: #include<iostream.h> #include<conio.h> #define max 10 class stack { private: int arr[max] int top; public: stack() { top=-1; } void push(int item) { if(top==max-1) { cout<<endl<<stack si full; return; } top++; arr[top]=item; } int pop() { if(top==-1) { cout<<endl<<stack is empty; return(0); } int data=arr[top]; top--; return data;
43

} Experiment No .10 }

void main() { clrscr(); stack s; s.push(11); s.push(12); s.push(13); s.push(14); s.push(15); s.push(16); s.push(17); s.push(18); s.push(19); s.push(20); s.push(21); s.push(22); int i=s.pop(); cout<<endl<<itempopped=<<I; i=s.pop(); cout<<endl<<itempopped=<<I; i=s.pop(); cout<<endl<<itempopped=<<I; i=s.pop(); cout<<endl<<itempopped=<<I; getch(); } /*

OUTPUT stack is full stack is full itempopped=20 itempopped=19 itempopped=18 itempopped=17


44

*/ Experiment No .10 Algorithm to PUSH element X into stack Step 2: [INITIALIZATION] S is an array of size N TOP is a global pointer variable which indicates topmost node of the stack. X is the information to be stored in new node. Step 3: [CHECK FOR OVERFLOW] IF TOP>=N GOTO Step 4 Step 4: [DISPLAY] WRITE(Overflow Stack) ENDIF Step 5: [INCREAMENT TOP] TOPTOP+1 Step 6: [INSERT NEW ELEMENT] D[TOP]X Step 7: [FINISH] RETURN

Algorithm: To POP element X into stack Step 1: [NAME] Algorithm to POP element X into stack Step 2: [INITIALIZATION] S is an array of size N TOP is a global pointer variable which indicates topmost node of the stack. TEMP is integer variable Step 3: [CHECK FOR UNDERFLOW] IF TOP=0 GOTO Step 4
45

Step 4: [DISPLAY] Experiment No .10 WRITE(Empty Stack) ENDIF Step 5: [REMOVE ELEMENT] TEMPS[TOP] Step 6: [DECREAMENT TOP] TOPTOP-1 Step 7: [FINISH] RETURN

46

Experiment No. 11 Title: Write algorithm and perform programming for queue.

47

EXPERIMENT NO.11 AIM: Write algorithm and perform programming for linear queue //Program: #include<iostream;> #include<conio.h< #define max 10 Class queue { Private: Int arr[max]; Int front,rear; Public: Queue() { Front=-1; Rear=-1; } Void add(intitem) { If(rear==max-1) { Cout<<endl<<queue is full; Return; } Rear ++ arr[rear]=item; If(front==-1) Front=0; } Int delq() { Int data; If(front==-1) { Cout<<endl<<queue is emty; Return null;
48

} Data=arr[front]; If(front==rear) Experiment No .11 Front=rear=-1; Else Front++; Return data; } }; Void main() { Clrscr(); queue a; a.addq(11); a.addq(12); a.addq(13); a.addq(14); a.addq(15); a.addq(16); a.addq(17); a.addq(18); a.addq(19); a.addq(20); a.addq(21); int i=a.delq(); cout<<endl<<item deleted=<<I; i=a.delq(); cout<<endl<<item deleted=<<I; i=a.delq(); cout<<endl<<item deleted=<<I; getch(); } OUTPUT Queue is full Item deleted=11 Item deleted=12 Item deleted =13

49

Experiment No .11 Algorithm: STEP 1: [NAME] Algorithm to insert element X into Linear Queue STEP 2: [INITIALIZATION] Q is an array of N elements F ,R are global pointer variables which denotes Front end and Rear end of the queue respectively X is the information to be stored in new node [CHECK FOR OVERFLOW] IF R >= N GOTO Step 4 [DISPLAY] WRITE (Queue Overflow) [INCREMENT REAR POINTER] RR+1 [INSERT NEW ELEMENT] Q[R]X [FINISH] RETURN

STEP 3:

STEP 4:

STEP 5:

STEP 6:

STEP 7:

50

Experiment No .11 Algorithm: STEP 1: [NAME] Algorithm to delete element X into Linear Queue STEP 2: [INITIALIZATION] Q is an array of N elements F ,R are global pointer variables which denotes Front end and Rear end of the queue respectively [CHECK FOR OVERFLOW] IF F = - 1 GOTO Step 4 [DISPLAY] WRITE (Empty Queue) [REMOVE ELEMENT] TEMP Q [ F ] [RESETTING POINTERS] IF = R THEN FR-1 ELSE FF+1 [FINISH] RETURN

STEP 3:

STEP 4:

STEP 5:

STEP 6:

STEP 7:

51

Experiment No. 12 Title: Write algorithm and perform programming on trees

52

Experiment No.12 AIM:WRITE A PROGRAM FOR TREE //PROGRAM: #include <iostream.h> #include <conio.h> #define true1 #define false0 class tree { private:struct node { node *l; int data; node *r; }*p; public:tree(); void search(int n,int &found,node *&parent); void insert(int n); void traverse(); void in(node *q); void pre(node *q); void post (node *q); void post(node *q); } tree::tree() { P=null; } Void tree::search(int,int &found ,node *&parent) { node *q; found=false; parent=NULL; if(p==NULL) return; q=p;
53

while(q!=NULL) { Experiment No.12 if (q->data==n) { found=true; return; } if (q->data>n) { parent=q; q=q->1; } else { parent=q; q=q->r; } } } void tree::insert(int n) { int found; node *t,*parent; search(n,found,parent); if(found==true) cout<<endl<<such a node already exist; else { t=new node; t->data=n; t->l=NULL; t->r=NULL; if(parent==NULL) p=t; else parent->data>n?parent->1=t:parent->r=t; } } void tree::traversal() {
54

int choice; cout<<endl<<1.inorder<<endl<<2.preorder<<endl<<3.postorder<<endl<<your choice=; cin>>choice; Experiment No.12 switch(choice) { case 1:in(p); break; case2:pre(p); break; case3:post(p); break; } void tree::in(node *q) { if(q!=NULL) { in(q->1); cout<<\t<<q->data; in(q->r); } } void tree::in(node *q) { if(q!=NULL) { cout<<\t<<q->data; pre(q->1); pre(q->r); } }; void tree::pre(node *q) { if(q!=NULL) { post(q->1); post(q->r); cout<<\t<<q->data; }
55

} void main() { tree tt,ss,qq; Experiment No.12 int I, num; clrscr(); for(i=0;i<=6;i++) { cout<<endl<<enter the data for the node to be inserted:; cin>>num; tt.insert(num); } ss=tt=qq; tt.traverse(); //ss=tt; ss.traverse(); //qq=ss; qq.traverse(); getch(); } OUTPUT: enter the data for the node to be inserted: 10 enter the data for the node to be inserted: 25 enter the data for the node to be inserted: 65 enter the data for the node to be inserted: 02 enter the data for the node to be inserted: 04 enter the data for the node to be inserted: 2 such a node already exist enter the data for the node to be inserted: 05 1.inorder 2.preorder 4.postorder your choice=1 2 4 5 10 25 65 1.inorder 2.preorder 3.postorder your choice
56

Experiment No .12

Experiment No.12 Algorithm: STEP 1: [NAME] Algorithm to count number of nodes in tree [INITIALIZATION] X indicates data of new node. Global Variable: NODE (POINTER) Local Variable: NEW (POINTER) [CREATE A NEW NODE] NEW NODE [INITIALISE COUNT OF NEW NODE] INFO(NEW) X LPTR(NEW) NULL RPTR(NEW) NULL [LOCATE THE POSITION OF NEW NODE] REPEAT WHILE != NULL through Step 6 [FINISH] RETURN

STEP 2:

STEP 3:

STEP 4:

STEP 5:

STEP 6:

57

Experiment No.12 Algorithm: STEP 1: [NAME] Algorithm to arrange a tree in Inorder [INITIALIZATION] T is the pointer shown address of staring node [IS TREE EMPTY] IF T=NULL RETURN ENDIF [TRAVERSE THE LEFT SUB TREE] IF LPTR (T) != NULL THEN (T) CALL IN ORDER LPTR THEN(T) [PROCESS THE ROOT NODE] WRITE (INFO[T]) [TRAVERSE THE RIGHT SUBTREE} IF RPTR(T) != NULL THEN CALL IN ORDER RPTR (T) [FINISH] RETURN

STEP 2:

STEP 3:

STEP 4:

STEP 5:

STEP 6:

STEP 7:

58

Experiment No.12 Algorithm: STEP 1: [NAME] Algorithm to arrange a tree in Preorder [INITIALIZATION] T is the pointer shown address of staring node [IS TREE EMPTY] IF T=NULL RETURN ENDIF [PROCESS THE ROOT NODE] WRITE (INFO[T]) [TRAVERSE THE LEFT SUB TREE] IF LPTR (T) != NULL THEN (T) CALL IN ORDER LPTR THEN(T) [TRAVERSE THE RIGHT SUBTREE} IF RPTR(T) != NULL THEN CALL IN ORDER RPTR (T) [FINISH] RETURN

STEP 2:

STEP 3:

STEP 4:

STEP 5:

STEP 6:

STEP 7:

59

Experiment No .12 Algorithm: STEP 1: [NAME] Algorithm to arrange a tree in Postorder [INITIALIZATION] T is the pointer shown address of staring node [IS TREE EMPTY] IF T=NULL RETURN ENDIF [TRAVERSE THE LEFT SUB TREE] IF LPTR (T) != NULL THEN (T) CALL IN ORDER LPTR THEN(T) [TRAVERSE THE RIGHT SUBTREE} IF RPTR(T) != NULL THEN CALL IN ORDER RPTR (T) [PROCESS THE ROOT NODE] WRITE (INFO[T]) [FINISH] RETURN

STEP 2:

STEP 3:

STEP 4:

STEP 5:

STEP 6:

STEP 7:

60

Вам также может понравиться