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

Data Structures Using C

Language

Q.1 Write pseudo code and program for inputting an array of integer and to sort them using Bubble sort. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, sorts them in an orderly way using bubble sort technique and displays them on the screen. Step 1: Step 2: [Inputting the elements.] Call GET( ) [Displaying the elements in an unsorted way.] Write( Unsorted array is: ) Call DISPLAY( ) [Sorting the elements] Call BUB_SORT( ) [Displaying the elements in ascending order] Write( Sorted array is: ) Call DISPLAY( ) [Finished] Exit

Step 3: Step 4:

Step 5:

Procedure. GET( ). This procedure inputs the elements into array. Here A is the array variable, I is the loop-control variable and N depicts the number of elements in array A. Step 1: [Input the number of elements.] Write( Enter how many elements you want:) Read(N)

Step 2:

[Input the elements.] Repeat for I 1, 2, 3, ,N Read(A[I]) Step 3: [Finished] Return Procedure. BUB_SORT ( ). This procedure sort the elements in an ascending order using Bubble sort technique. Here A is the array variable, I is the loop-control variable, DUMMY is a temporary variable, N depicts the number of elements in array and J is the variable that tells the position of next element in the array to be sorted. Step 1: [Sorting of elements in array] Repeat for I N, N-1, 2 Step -1 Repeat for J 1, 2, 3, , I If ( A[J] > A[I]) Then DUMMY A[I] 2

A[I] A[J] A[J] DUMMY Step 2: [Finished] Return This procedure is displaying the elements on the screen. Here A is the array variable, I is the loop-control variable and N depicts the number of elements in array.

Procedure. DISPLAY( ).

Step 1:

[Displaying the elements on the screen.] Repeat for I 1, 2, 3, , N Write (A[I]) [Finished] Return

Step 2:

Program Coding
#include<stdio.h> #include<conio.h> int a[20], n, i; void get( ); void bub_sort( ); void display( ); void main( ) { clrscr( ); get( ); clrscr( ); printf("\n :UNSORTED ARRAY: \n"); display( ); bub_sort( ); printf("\n\n :SORTED ARRAY: \n"); display( ); getch( ); } void get( ) { printf("\n Enter how many elements do you want :- "); scanf("%d", &n); printf("\n Enter your array elements :- \n"); for(i = 0; i< n; i++) scanf("%d", &a[i]); getch( ); } void bub_sort( ) { int j, dummy; for(i= n-1; i >= 1; i--) { for(j= 0; j < i; j++) { if(a[j] > a[i]) { dummy= a[i]; a[i]= a[j]; a[j]= dummy; } } } } void display( ) { for(i= 0; i< n; i++) printf("\n %d", a[i]); getch( ); } 4

Q.2 Write pseudo code and program for inputting an array and to sort them using Insertion sort. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, sorts them in an orderly way using Insertion sort technique and displays them on the screen.

Step 1: Step 2:

[Input the elements.] Call GET( ) [Displaying the elements in an unsorted array.] Write(' Unsorted array is: ') Call DISPLAY( ) [Sorting of elements.] Call INS_SORT( ) [Displaying the elements in an ascending order.] Write(' Sorted array is: ') Call DISPLAY( ) [Finished] Exit

Step 3: Step 4:

Step5:

Procedure. GET( ). This procedure is inputs the elements into array. Here A is the array variable, I is the loop- control variable and N depicts the number of elements in array. Step1: [Input the number of elements] Write(' Enter how many elements do you want) Read(N) [Inputting the elements] Write( Enter your array elements: ) Repeat for I 1, 2, ... , N Read(A[I])

Step2:

Step 3:

[Finished] Return Procedure. INS_SORT( ).

This procedure sorts inputted and unsorted elements in a particular way using Insertion sort technique. Here A is the array variable, I is the loop-control variable, DUMMY is a temporary variable, N depicts the number of elements in the array and J is the variable that tells the position of next element in the array to be sorted

Step 1:

[Sorting of elements in array.] J2 Repeat While (J < N) DUMMY A[J] 5

I J-1 Repeat While(( I 0) && (A[I] > DUMMY)) A[I+1] A[I] I I-1 J J+1 A[I+1] DUMMY Step 2: [Finished] Return This procedure is displaying the array of elements on the screen. Here A is the array variable, I is the loop-control variable and N depicts the number of elements in array.

Procedure. DISPLAY( ).

Step1:

[Displaying the elements on the screen.] Repeat for I 1, 2, ... , N Write(A[I]) Step 2: [Finished] Return

Program Coding
#include<stdio.h> #include<conio.h> int a[20], n, i; void get( ); void ins_sort( ); void display( ); void main( ) { clrscr( ); get( ); clrscr( ); printf(" \n :UNSORTED ARRAY: \n"); display( ); ins_sort( ); printf(" \n\n :SORTED ARRAY: \n"); display( ); getch( ); } void get( ) { printf(" \n Enter how many elements do you want :- "); scanf(" %d", &n); printf(" \n Enter your array elements :- \n"); for(i = 0; i < n; i++) scanf(" %d", &a[i]); getch( ); } void ins_sort( ) { int j = 1, dummy; while(j < n) { dummy = a[j]; i = j-1; while((i >= 0) && (a[i]> dummy)) { a[i+1]= a[i]; i--; } a[i+1]= dummy; j++; } } void display( ) { for(i = 0; i < n; i++) printf(" \n %d", a[i]); getch( ); } 7

Q.3 Write pseudo code and program for inputting an array of integer and to sort them using Selection sort. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, sorts them in an orderly way using Insertion sort technique and displays them on the screen.

Step 1: Step 2:

[Input the elements.] Call GET( ) [Displaying the elements in an unsorted array.] Write(' Unsorted array is: ') Call DISPLAY( ) [Sorting of elements.] Call SEL_SORT( ) [Displaying the elements in an ascending order.] Write(' Sorted array is: ') Call DISPLAY( ) [Finished] Exit

Step 3: Step 4:

Step5:

Procedure. GET( ). This procedure is inputs the elements into array. Here A is the array variable, I is the loop- control variable and N depicts the number of elements in array. Step1: [Input the number of elements] Write(' Enter how many elements do you want) Read(N) Step2: [Inputting the elements] Write( Enter your array elements: ) Repeat for I 1, 2, ... , N Read(A[I])

Step 3:

[Finished] Return Procedure. SEL_SORT( ). This procedure sorts the elements in an ascending order using Selection sort technique. Here A is the array variable, I is the loop-control variable, DUMMY is a temporary variable, N depicts the number of elements in array and J is the variable that tells the position of next element in the array to be sorted. Step 1: [Sorting of elements in array.] Repeat for I 1, 2, 3, , N Repeat for J I+2, I+3, , N 8

If Then

(A[I] > A[J]) DUMMY A[J] A[J] A[I] A[I] DUMMY

Step 2:

[Finished] Return This procedure is displaying the array of elements on the screen. Here A is the array variable, I is the loop-control variable and N depicts the number of elements in array.

Procedure. DISPLAY( ).

Step1: [Displaying the elements on the screen.] Repeat for I 1, 2, ... , N Write(A[I]) Step 2: [Finished] Return

Program Coding
#include<stdio.h> #include<conio.h> int a[20], n, i; void get( ); void sel_sort( ); void display( ); void main( ) { clrscr( ); get( ); clrscr( ); printf(" \n :UNSORTED ARRAY: \n"); display( ); sel_sort( ); printf("\n\n :SORTED ARRAY: \n"); display( ); getch( ); } void get( ) { printf(" \n Enter how many elements do you want :- "); scanf("%d", &n); printf("\n Enter your array elements :- \n"); for(i = 0; i < n; i++) scanf("%d", &a[i]); getch( ); } void sel_sort( ) { int j, dummy; for(i = 0; i < n-1; i++) { for(j= i+1; j< n; j++) { if(a[i] > a[j]) { dummy= a[j]; a[j]= a[i]; a[i]= dummy; } } } } void display( ) { for(i= 0; i< n; i++) printf("\n %d", a[i]); getch( ); } 10

Q4. Write pseudo code and program for inputting an array of integer and sort them using Merge sort. Ans.

PSEUDO CODE
Algorithm. MAIN( ). Step 1: Step 2: This algorithm input the elements, sorts them in an orderly way using Merge sort technique and displays them on the screen.

[Input the elements.] Call GET(A, N ) [Displaying the elements in an unsorted array.] Write(' Unsorted array is: ') Call DISPLAY( ) [Sorting of elements.] Call SORT( 1, N) [Displaying the elements in an ascending order.] Write(' Sorted array is: ') Call DISPLAY( ) [Finished] Exit This procedure is inputs the elements into array. Here A is the array variable, I is the loop- control variable and N depicts the number of elements in array.

Step 3: Step 4:

Step5:

Procedure. GET( A, N).

Step1:

[Input the number of elements] Write(' Enter how many elements do you want) Read(N) [Inputting the elements] Write( Enter your array elements: ) Repeat for I 1, 2, ... , N Read(A[I])

Step2:

Step 3:

[Finished] Return Procedure. MERGE( FIRST, SEC, LAST). This procedure merges the input elements, divides into halves and unsorted elements using merge technique. Here A and C are the array variable, I is the loopcontrol variable, FIRST(1), SEC((LAST+ FIRST)/2) and LAST(N) is a temporary variable containing formal parameters, N depicts the element in the array to be sorted. Step 1: [Sorting of elements in array.] I FIRST J SEC K FIRST 1 11

Repeat While((I SEC) and (J LAST)) If (A[I] A[J]) Then K K+1 C[K] A[I] I I+1 Else K K+1 C[K] A[J] J J+1 Repeat While (I SEC) K K+1 C[K] A[J] J J+1 Repeat While (J LAST) K K+1 C[K] A[J] J J+1 Repeat for I FIRST, , LAST A[I] C[I] Step 2: [Finished] Return

Procedure. SORT(FIRST, LAST ). This procedure is sorts input and unsorted elements using Merge sort technique. Here A and C Are the array variables, I is the loop-control variable, FIRST(1) & LAST(N) is a temporary variable containing formal parameters, N depicts the number of elements in array, SIZE is a temporary variable and MID is the variable that tells the position of middle element in the array to be sorted. Step 1: [Sorting of input elements] SIZE LAST FIRST +1 If (SIZE > 1) Then MID= (FIRST+LAST)/2 Call SORT(FIRST, MID) Call SORT(MID+1, LAST) Call MERGE(FIRST, MID+1, LAST) [Finished] Return This procedure is displaying the array of elements on the screen. Here A is the array variable, I is the loop-control variable and N depicts the number of elements in array.

Step 2:

Procedure. DISPLAY( ).

Step1:

[Displaying the elements on the screen.] Repeat for I 1, 2, ... , N Write(A[I]) Step 2: [Finished] Return

Program Coding
12

#include<stdio.h> #include<conio.h> int a[20], n, i, c[20]; void get( ); void sort(int, int); void merge(int, int, int); void display( ); void main( ) { clrscr( ); get( ); clrscr( ); printf("\n :UNSORTED ARRAY: \n"); display( ); sort(0, n-1); printf(" \n\n :SORTED ARRAY: \n"); display( ); getch( ); } void get( ) { printf("\n Enter how many elements do you want :- \n"); scanf("%d", &n); printf("\n Enter your array element :- \n"); for(i= 0; i< n; i++) scanf("%d", &a[i]); getch( ); } void sort(int first, int last) { int size, mid; size= last-first+1; if(size> 1) { mid= (first+last)/2; sort(first, mid); sort(mid+1, last); merge(first, mid+1, last); } } void merge(int first, int sec, int last) { int i, j, k; i= first, j= sec, k= first-1; while((i<= sec-1) && (j<= last)) { 13

if(a[i]<= a[j]) c[++k]= a[i++]; else c[++k]= a[j++]; } while(i<= sec-1) c[++k]= a[i++]; while(j<= last) c[++k]= a[j++]; for(i= first; i<= last; i++) a[i]= c[i]; } void display( ) { for(i= 0; i< n; i++) printf("\n %d", a[i]); getch( ); }

14

Q.5 Write pseudo code and program for inputting an array and sort them using Quick sort. Ans. Algorithm. MAIN( ). Step 1: Step 2:

PSEUDO CODE
This algorithm input the elements, sorts them in an orderly way using Quick sort technique and displays them on the screen.

[Input the elements.] Call GET( ) [Displaying the elements in an unsorted array.] Write(' Unsorted array is: ') Call DISPLAY( ) [Sorting of elements.] Call SORT(1, N ) [Displaying the elements in an ascending order.] Write(' Sorted array is: ') Call DISPLAY( ) [Finished] Exit This procedure is inputs the elements into array. Here A is the array variable, I is the loop- control variable and N depicts the number of elements in array.

Step 3: Step 4:

Step 5:

Procedure. GET( A, N).

Step 1:

[Input the number of elements] Write(' Enter how many elements do you want) Read(N) [Inputting the elements] Write( Enter your array elements: ) Repeat for I 1, 2, ... , N Read(A[I]) [Finished] Return

Step 2:

Step 3:

Procedure. SORT(X, W). This procedure sorts inputted and unsorted elements in a particular way. Here A is the array variable, I is the loopcontrol variable, KEY, DUMMY and FLSG are temporary variable, X(1) and W(N) are the number variable and J is the variable that tells the position of next element in the array to be sorted. Step 1: [Sorting of elements in the array.] I X+1 JW KEY A[X] FLAG 1 15

Repeat While (FLAG) Repeat While(A[I] < KEY) I I+1 Repeat While (A[J] > Key) J J-1 If (I < J) Then DUMMY A[X] A[I] A[J] A[J] DUMMY Step 4: FLAG 1 DUMMY A[X] A[X] A[J] A[J] DUMMY Call SORT( X, J-1) Call SORT(J+1, W) Step 5: [Finished] Return

Procedure. DISPLAY( ). This procedure is displaying the array of elements on the screen. Here A is the array variable, I is the loop-control variable and N depicts the number of elements in array. Step1: [Displaying the elements on the screen.] Repeat for I 1, 2, ... , N Write(A[I]) Step 2: [Finished] Return

16

Program Coding
#include<stdio.h> #include<conio.h> int a[20], n, i; void display( ); void get( ); void sort(int, int); void main( ) { clrscr( ); get( ); clrscr( ); printf("\n :UNSORTED ARRAY: \n"); display( ); sort(0, n-1); printf("\n\n :SORTED ARRAY: \n"); display( ); getch( ); } void get( ) { printf("\n Enter the number of elements do you want :- \n"); scanf("%d", &n); if(n< 20) { printf("\n Enter the element :- \n"); for(i= 0; i< n; i++) scanf("%d", &a[i]); } else printf("\n Entered number of elements exceeds size of array... \n"); } void sort(int x, int w) { int j, key, dummy, flag= 1; if(x< w) { i= x+1; j= w; key= a[x]; while(flag) { while(a[i] < key) i++; 17

while(a[j] > key) j--; if(i < j) { dummy= a[i]; a[i]= a[j]; a[j]= dummy; } else flag= 0; } dummy= a[x]; a[x]= a[j]; a[j]= dummy; sort(x, j-1); sort(j+1, w); } } void display( ) { for(i= 0; i< n; i++) printf("\n %d", a[i]); getch( ); }

18

Q.6 Write pseudo code and program for inputting an array of integer and to search a particular element by using Linear search. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, and searches for a particular element using linear search technique and displays the result of search on the screen.

Step 1: Step 2: Step 3: Step 4:

[Input the elements in the array.] Call GET( ) [Searching the element] Call LIN_SEARCH( ) [Displaying the result.] Call DISPLAY( ) [Finished] Exit

Procedure. GET( ). This procedure inputs the elements into array. Here A is the array variable, I is the loop-control variable and X contain the element which is to be searched from the array. Step 1: [Input the number elements] Write( Enter how many elements you want: ) Read(N) [Input the elements] Write( Enter your array elements:) Repeat for I 1, 2, 3, , N Read(A[I]) [Input the search element] Write( Enter your element: ) Read(X)

Step 2:

Step 3:

Step 4: [Finished] Function. LIN_SEARCH( ). This function searches a specific element using linear search technique. Here A is the array element. I is the loop-control variable, N depicts the number of elements in array and X contains the element that is to be searched in the array. Step 1: [Searching of the element in the array.] Repeat for I 1, 2, 3, , N If (A[I] = X) Then Return(I) Step 2: [Finished] Return(0) This procedure is displaying the elements on the screen. Here 19

Procedure. DISPLAY( ).

POS is the variable containing the position of element. Step 1: [Displaying the result on the screen] If (POS = 0) Then Write( Search unsuccessful) Else Write( Given element found at position:- POS+1) [Finished] Return

Step 2:

20

Program Coding
#include<stdio.h> #include<conio.h> int a[20], n, i, pos, x; void get( ); void display( ); int lin_search( ); void main( ) { clrscr( ); get( ); pos= lin_search( ); display( ); getch( ); } int lin_search( ) { for(i= 0; i< n; i++) { if(a[i] == x) return(i); } return(-1); } void get( ) { printf("\n Enter how many elements do you want :- \n"); scanf("%d", &n); printf("\n Enter your elements in array :- \n"); for(i= 0; i< n; i++) scanf("%d", &a[i]); printf("\n Enter the element you want to search:- \n"); scanf("%d", &x); getch( ); } void display( ) { if(pos== -1) printf("\n Given element i.e. %d not found in array...",x); else printf("\n Given element found in array at the position = %d",pos+1); getch( ); }

21

Q.7 Write pseudo code and coding for inputting an array of integer and to search a particular element by using Linear search recursively. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, and searches for a particular element using linear search technique recursively. U is the variable contain the position of the element found in the array and displays the result of search on the screen.

Step 1: Step 2: Step 3: Step 4:

[Input the elements in the array.] Call GET( ) [Searching the element] U = Call LIN_SEARCHR( I ) [Displaying the result.] Call DISPLAY( ) [Finished] Exit

Procedure. GET( ). This procedure inputs the elements into array. Here A is the array variable, I is the loop-control variable and X contain the element which is to be searched from the array. Step 1: [Input the number elements] Write( Enter how many elements you want: ) Read(N) [Input the elements] Write( Enter your array elements:) Repeat for I 1, 2, 3, , N Read(A[I]) [Input the search element] Write( Enter your element: ) Read(X)

Step 2:

Step 3:

Step 4:

[Finished] Return Function. LIN_SEARCH( I ). This function searches a specific element using linear search technique. Here A is the array element. I is the loop-control variable, N depicts the number of elements in array and X contains the element that is to be searched in the array. Step 1: [Searching of the element in the array.] If (I = N+1) Then Return(0) Else If (A[I] = X) Return (I) 22

Step 2: Step 3:

[Calling the recursive function.] Return (Call LIN_SEARCHR(I+1)) [Finished] Return This procedure is displaying the elements on the screen. Here U is the variable containing the position of element.

Procedure. DISPLAY( ). Step 1:

[Displaying the result on the screen] If (U = 0) Then Write( Search unsuccessful) Else Write( Given element X found at position:- U+1) [Finished] Return

Step 2:

23

Program Coding
#include<stdio.h> #include<conio.h> int a[20], n, i, u, x; void get( ); void display( ); int lin_searchr( int); void main( ) { clrscr( ); get( ); u= lin_searchr(0); display( ); getch( ); } int lin_searchr(int i) { if(i == n) return(-1); else if(a[i] ==x) return(i); else return(lin_searchr(i+1)); } void get( ) { printf("\n Enter how many elements U want in array :- "); scanf("%d", &n); printf("\n Enter your elements in array :- \n"); for(i= 0; i< n; i++) scanf("\n %d", &a[i]); printf("\n Enter the element you want to search :- \n"); scanf("%d", &x); } void display( ) { if(u== -1) printf("\n Given element not found in array ..."); else printf("\n Given element found at position %d",u+1); getch( ); }

24

Q. 8 Write pseudo code and program for inputting an array of integer and to search a particular element by using Binary Search Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, and searches for a particular element using Binary search technique and displays the result of search on the screen. Step 1: Step 2: Step 3: Step 4: [Input the elements in the array.] Call GET( ) [Searching the element] Call BIN_SEARCH( ) [Displaying the result.] Call DISPLAY( ) [Finished] Exit

Procedure. GET( ). This procedure inputs the elements into array. Here A is the array variable, I is the loop-control variable and X contain the element which is to be searched from the array. Step 1: [Input the number elements] Write( Enter how many elements you want: ) Read(N) [Input the elements] Write( Enter your array elements:) Repeat for I 1, 2, 3, , N Read(A[I])

Step 2:

Step 3:

[Input the search element] Write( Enter your element: ) Read(X) Step 4: [Finished] Return Procedure. BIN_SEARCH( ). This procedure searches a specific element using binary search technique. Here A is the array element, I is the loopcontrol variable, N depicts the number of elements in the array, FIRST(1) and LAST(N) are the variable containing formal parameters and X contains the element which is to be searched from the array. Step 1: [Searching of the element in the array.] FIRST 1 LAST N While(FIRST 1) Then MID= (FIRST + LAST)/2 If (A[MID] = X) Then Return (MID) 25

FLAG 2 Else If Else LAST= MID -1 Step 2: [Finished] Return(0) This procedure is displaying the elements on the screen. Here U is the variable containing the position of element. (A[MID] < X) FIRST = MID+1

Procedure. DISPLAY( ). Step 1:

[Displaying the result on the screen] If (U= 0) Then Write( Search unsuccessful) Else Write( Given element X found at position:- U+1) [Finished] Return

Step 2:

26

Program Coding
#include<stdio.h> #include<conio.h> int a[20], n, x, u, i; void get( ); void display( ); int bin_search( ); void main( ) { clrscr( ); get( ); u=bin_search( ); display( ); getch( ); } int bin_search( ) { int mid, first, last; first= 0, last= n-1; while(first <= last) { mid = (first+last)/2; if(a[mid] == x) return(mid); else if(a[mid]<x) first= mid+1; else last= mid-1; } return(-1); } void get( ) { printf("\n Enter how many elements do you want in array :- "); scanf("%d", &n); printf("\n Enter your elements in array:- "); for(i= 0; i< n; i++) scanf("%d \n", &a[i]); printf("\n Enter number which you want to search :- "); scanf("%d", &x); } void display( ) { 27

if(u== -1) printf("\n Given element i.e. %d not found in array ..."); else printf("\n Given element i.e. %d found at the position :- %d",x,u+1); getch( ); }

28

Q 9. Write pseudo code and program for inputting an array of integer and to search a particular element by using Binary search recursively. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, and searches for a particular element using Binary search technique recursively and displays the result of search on the screen.

Step 1: Step 2: Step 3: Step 4:

[Input the elements in the array.] Call GET( ) [Searching the element] Call BIN_SEARCH( 1, N) [Displaying the result.] Call DISPLAY( ) [Finished] Exit

Procedure. GET( ). This procedure inputs the elements into array. Here A is the array variable, I is the loop-control variable and X contain the element which is to be searched from the array. Step 1: [Input the number elements] Write( Enter how many elements you want: ) Read(N) [Input the elements] Write( Enter your array elements:) Repeat for I 1, 2, 3, , N Read(A[I])

Step 2:

Step 3:

[Input the search element] Write( Enter your element: ) Read(X) Step 4: [Finished] Return Function. BIN_SEARCHR( FIRST, LAST). This function searches a specific element using binary search recursively. Here A is the array element, I is the loop-control variable, N depicts the number of elements in the array, FIRST(1) and LAST(N) are the variables containing formal parameters and X contain the element which is to be search from the array. Step 1: [Searching of the element from the array.] If ( FIRST LAST)/2 Then MID = (FIRST + LAST)/2 If (A[MID] = X) 29

Then Else If Then Step 2:

RETURN( MID)

(A[MID] < X) Return( Call BIN_SEARCHR(MID+1,LAST)

[Finished] Return (Call BIN_SEARCHR(FIRST, MID-1)) This procedure is displaying the elements on the screen. Here U is the variable containing the position of element.

Procedure. DISPLAY( ). Step 1:

[Displaying the result on the screen] If (U = 0) Then Write( Search unsuccessful) Else Write( Given element X found at position:- U+1) [Finished] Return

Step 2:

30

Program Coding
#include<stdio.h> #include<conio.h> int a[20], n, x, u, i; void get( ); void display( ); int bin_searchr(int, int); void main( ) { clrscr( ); get( ); u= bin_searchr(0, n-1); display( ); getch( ); } int bin_searchr(int first, int last) { int mid; if(first<= last) { mid= (first+last)/2; if(a[mid]== x) return(mid); else if(a[mid]< x) return(bin_searchr(mid+1, last)); else return(bin_searchr(first, mid-1)); } else return(-1); } void get( ) { printf("\n Enter how many elements do you want in array :- "); scanf("%d", &n); printf("\n Enter your elements in array :- "); for(i= 0; i< n; i++) scanf("%d \n", &a[i]); printf("\n Enter the element you want to search :- "); scanf("%d", &x);

} void display( ) { if(u == -1) printf("\n Given number is not found in array ..."); else printf("\n Given number is found at the position :- %d",u+1); getch( ); } 31

Q.10 Write pseudo code and program for implementing different operation on simple array. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm show menu and Calling different procedures for operation on simple array. CHOICE and ANS are integer and character variables respectively for storing selected option. Step 1: [Menu for operation.] N 1 Repeat While (True) Write( Operations on simple array) Write( 1- Input) Write( 2- Insertion) Write(3- Deletion) Write(4- Output) Write(5- Exit) Write( Enter your choice) Read(CHOICE) [Calling the functions] Select Case(CHOICE) Case 1: Call INPUT( ) Case 2: If Else Write( Array full, Insertion not possible) Case 3: If Else Write( Array empty, Deletion not possible) Case 4: Call OUTPUT( ) Case 5: Write( You have chosen exit) Write(You want to exit? If yes press y) Read(ANS) If ( ANS = y or ANS = Y) Then EXIT Default: Write(Invalid Condition) Step 3: [Finished] Exit ( N 1) Call DEL( ) (N 10) Call INSERT( )

Step 2:

Procedure. INPUT( ). This procedure input the elements into the array, N depicts the 32

number of elements in the array, I is the loop- control variable. A represents array. Step 1: [Input elements into array] Write( Enter number of elements:) Read(N) Write( Enter number of elements:) Repeat for I 1, 2, 3, .. , N Read(A[I]) [Finished] Return

Step 2:

Procedure. OUTPUT( ). This procedure displays the elements in the array on the screen. A is the array element. I is the loop-control variable. N depicts the number of elements in array A. Step 1: [Displaying the result on the screen.] Write(Elements in the array are) Repeat for I 1, 2, 3, , N Read(A[I]) [Finished] Return

Step 2:

33

Procedure. INSERT( ). This procedure inserts an element or given information into given position, POS contains the position where the element is to be inserted. A represents the array. N depicts the number of elements in the array A. Step 1: [Entering and inserting the element] Write( Enter the position where element is to be inserted:) Read(POS) If ((POS < 1) or (POS> N+1)) Then Write(Specified position is out of range) Else Repeat for I N, N-1, , POS, STEP-1 A[I+1] A[I] Write(Enter the element to be inserted) Read(A[POS-1]) N N+1 [Finished] Return

Step 2:

Procedure. DELETION( ). This procedure deletes an element from the given position. POS contains the position where the element is to be inserted. A represents the array. N depicts the number of elements in array A. Step 1: [Entering and deleting element] Write(Enter the position where element is to be deleted:) Read(POS) If ((POS>10) or (POS(N+1))) Then Write(Specified position is out of range) Else Write(Element to be deleted: , A[POS-1]) Repeat for I POS, POS-1, , N-1 A[I] A[I+1] N N-1 Step 2: [Finished] Return

34

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> void input( ); void output( ); void deletion( ); void insert( ); int a[10], i, n; void main( ) { clrscr( ); int ch; char ans; while(1) { clrscr( ); printf("\n \t Operations on array"); printf("\n\t------------------------"); printf("\n\t 1- Input); printf( \n\t 2- Insertion); printf( \n\t 3- Deletion ); printf(\n\t 4- Output ); printf(\n\t 5- Exit "); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d", &ch); switch(ch) { case 1: input( ); break; case 2: if(n != 10) insert( ); else { printf("\n Array full, insertion not possible. "); getch( ); } break; case 3: if(n != 0) deletion( ); else 35

{ printf("\n Array empty, deletion not possible. "); getch( ); } break; case 4: output( ); break; case 5: printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n Invalid choice ....."); getch( ); break; } } } void input( ) { printf("\n Enter the number of elements:- "); scanf("%d", &n); printf("\n Enter elements :- \n"); for(i= 0; i< n; i++) scanf("%d", &a[i]); } void output( ) { printf("\n Element in array are :-"); for(i= 0; i< n; i++) printf("\n %d", a[i]); getch( ); } void insert( ) { int pos; printf("\n Enter the position where element is to be inserted :- "); scanf("%d", &pos); 36

if((pos < 1) || (pos > (n+1))) printf("\n Specified position is out of range... "); else { for(i= n-1; i>= pos-1; i--) a[i+1] = a[i]; printf("\n Enter the element to be inserted :- "); scanf("%d", &a[pos-1]); n++; } getch( ); } void deletion( ) { int pos; printf("\n Enter the position where element is to be deleted :- "); scanf("%d", &pos); if((pos < 0) || (pos > n)) printf("\n Specified position is out of range... "); else { for(i= pos; i<= n-1; i--) a[i-1] = a[i]; printf("\n Enter the element to be deleted :- "); scanf("%d", &a[pos-1]); n--; } getch( ); }

37

Q.11 Write the pseudo code and program for implementing stack using array Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operation on the stack and Calling of all functions. TOP points to the element on the top of stack, DATA contains the element to be pushed or popped. CHOICE and ANS are integer and character variables for storing chosen

option. Step 1: [Displaying the menu.] TOP 0 Repeat While(True) Write( Implementing stack using array) Write(1- Push an element from the stack) Write(2- Pop an element from the stack) Write(3- Display the stack) Write(4- Exit from output window) Write(Enter your choice) Read(CHOICE) [Calling the functions] Select Case(CHOICE) Case 1: Write(Enter the element pushed into the stack) Read(DATA) Call PUSH(DATA) Case 2: DATA Call POP( ) If (DATA -99) Then Write(Element popped is :DATA) Case 3: Call DISPLAY( ) Case 4: Write(You want to exit? If yes then press y) Read(ANS) If (ANS = y or ANS = Y) Then EXIT Default: Write(Invalid choice) Step 3: [Finished] Return Procedure. PUSH(X). This procedure input or push an element passed on as parameter into the stack. Here A is the array variable, TOP points to the element on the top of the stack and X contain the element which user have entered and want to perform operation on it. Step 1: [Input or pushing the element] If (TOP = 20) Then Write(Stack overflow) Else TOP TOP+1 A[TOP] X 38

Step 2:

Step 2:

[Finished] Return

Function. POP( ).

This procedure pop or delete the input elements from the top of the stack. Here A represents the array variable, TOP points to the element on the top of the stack and DUMMY is an integer variable to store element temporarily. Step 1: [Sorting of elements in the array.] If (TOP = 0) Then Write(Stack underflow) Return(-99) Else DUMMY A[TOP] TOP TOP+1 Step 2: [Finished] Return(DUMMY)

Procedure. DISPLAY( ). This procedure display the elements on the screen. Here A is the array variable, I is the loop-control variable and TOP points to the element on the stack. Step 1: [Displaying the elements on the screen.] If (TOP = 0) Then Write(Stack is empty) Else Write(Elements of stack are:) Repeat for I TOP, , 1 Write (A[I]) [Finished] Return

Step 2:

39

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> void push(int); int pop( ); void display( ); int a[20], top; void main( ) { clrscr( ); int ch, data; char ans; while(1) { clrscr( ); printf("\n \t Implementing stack using array"); printf("\n\t--------------------------------------"); printf("\n\t 1- Push an element into stack "); printf("\n\t 2- Pop an element from stack "); printf("\n\t 3- Display the stack "); printf("\n\t 4- Exit from output window "); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d", &ch); switch(ch) { case 1: printf("\n Enter element to be pushed into the stack"); fflush(stdin); scanf("%d", &data); push(data); break; case 2: data= pop( ); if(data != -99) printf("\n Element popped is :%d", data); fflush(stdin); getch( ); break; case 3: display( ); break; case 4: printf("\n You have chosen to exit"); 40

printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n Invalid choice ....."); getch( ); break; } } }

void push(int x) { if(top == 19) printf("\n\n Stack overflow"); else { top++; a[top] = x; } } int pop( ) { int dummy; if(top == -1) { printf("\n\n Stack underflow"); getch( ); return(-99); } else { dummy= a[top]; top--; return(dummy); } } void display( ) { 41

int i; if(top == -1) printf("\n\n Stack is empty"); else { printf("\n\n Elements in stack :- "); for(i= top; i >= 0; i--) printf("\n %d", a[i]); } getch( ); }

42

Q.12 Write pseudo code and program for implementing two stack within one array. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operation on the stack and Calling of all functions. TOP1 and TOP2 points to the element on the top of the two stacks, DATA contains the element to be pushed or popped. CHOICE, CH and ANS are integer and character variables for storing chosen option.

Step 1:

[Displaying the menu.] TOP1 0 TOP2 10 Repeat While(True) Write( Implementing stack using array) Write(1- Push an element from the stack) Write(2- Pop an element from the stack) Write(3- Display the stack) Write(4- Exit from output window) Write(Enter your choice) Read(CHOICE) [Calling different functions] Select Case(CHOICE) Case 1: Write( Enter where the element is to be pushed) Write( For Stack1-press 1 & for Stack2-press 2:) Read(CH) Write(Enter element to be pushed into stack:) Read(DATA) If (CH = 1) Then Call PUSH1(DATA) Else (CH = 2) Call PUSH2(DATA) Else Write(Invalid choice) Case 2: Write(Enter from where element to be deleted) Write(For Stack 1-press 1 & Stack 2-press 2:) Read(CH) If (CH = 1) Then DATA Call POP1( ) Else (CH = 2) DATA Call POP2( ) Else Write(Invalid choice) If Then (DATA -99) Write( Element popped is:, DATA) 43

Step 2:

If

If

If

Case 3: Write(Enter which stack is to be viewed) Write( For Stack 1-press 1 & for Stack 2-press 2) Read (CH) If (CH = 1) Then Call DISPLAY1( ) Else (CH = 2) Call CISPLAY2( ) Else Write(Invalid Choice) Case 4: Write(You have chosen to exit) Write( Sure you want to exit? If press y:) Read(ANS) If (ANS = y or ANS = Y) Then EXIT

Default: Write( Invalid choice) Step 3: [Finished] Return This procedure input or push an element passed on as parameter into the stack. Here A is the array variable, TOP1 points to the element on the top of the stack and X contain the element which user have entered and want to perform operation on it.

Procedure. PUSH1(X).

Step 1:

[Input or pushing the element] If (TOP1 = 10) Then Write(Stack overflow) Else TOP1 TOP1+1 A[TOP1] X [Finished] Return This procedure input or push an element passed on as parameter into the stack. Here A is the array variable, TOP2 points to the element on the top of the stack and X contain the element which user have entered and want to perform operation on it.

Step 2:

Procedure. PUSH2(X).

Step 1:

[Input or pushing the element] If (TOP2 = 20) Then Write(Stack overflow) Else TOP2 TOP2+1 A[TOP2] X [Finished] 44

Step 2:

Return Function. POP1( ). This procedure pop or delete the input elements from the top of the stack. Here A represents the array variable, TOP1 points to the element on the top of the stack and DUMMY is an integer variable to store element temporarily.

Step 1:

[Sorting of elements in the array.] If (TOP1 = 0) Then Write(Stack underflow) Return(-99) Else DUMMY A[TOP1] TOP1 TOP1+1

Step 2:

[Finished] Return(DUMMY) This procedure pop or delete the input elements from the top of the stack. Here A represents the array variable, TOP2 points to the element on the top of the stack and DUMMY is an integer variable to store element temporarily.

Function. POP2( ).

Step 1:

[Sorting of elements in the array.] If (TOP2 = 10) Then Write(Stack underflow) Return(-99) Else DUMMY A[TOP2] TOP2 TOP2+1

Step 2:

[Finished] Return(DUMMY)

Procedure. DISPLAY1( ). This procedure display the elements on the screen. Here A is the array variable, I is the loop-control variable and TOP1 points to the element on the stack. Step 1: [Displaying the elements on the screen.] If (TOP1 = 0) Then Write(Stack is empty) Else Write(Elements of stack are:) Repeat for I TOP1, , 1 Write (A[I]) 45

Step 2:

[Finished] Return

Procedure. DISPLAY2( ). This procedure display the elements on the screen. Here A is the array variable, I is the loop-control variable and TOP2 points to the element on the stack. Step 1: [Displaying the elements on the screen.] If (TOP2 = 10) Then Write(Stack is empty) Else Write(Elements of stack are:) Repeat for I TOP1, , 20 Write (A[I]) Step 2: [Finished] Return

46

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> void push1(int); int pop1( ); void display1( ); void push2(int); int pop2( ); void display2( ); int a[20], top1, top2; void main( ) { clrscr( ); int choice, ch, data; char ans; top1= -1; top2= 9; while(1) { clrscr( ); printf("\n \t Implementing stack using one array"); printf("\n\t------------------------------------------------); printf("\n\t 1- Push an element into stack); printf("\n\t 2- Pop an element from stack); printf("\n\t 3- Display the stack "); printf("\n\t 4- Exit from output window "); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d", &choice); switch(choice) { case 1: printf("\n Enter where element is to be pushed "); printf("\nFor Stack1-press 1 & Stack2-press 2 :"); fflush(stdin); scanf("%d", &ch); printf("\n\n Enter element to be pushed into stack"); scanf("%d", &data); if(ch== 1) push1(data); else if(ch== 2) push2(data); else 47

fflush(stdin);

printf("\n Invalid choice ........"); break; case 2: printf("\nEnter from where element to be deleted "); printf("\n For Stack1-press 1 & Stack2-press 2 :"); fflush(stdin); scanf("%d", &ch); if(ch==1) data= pop1( ); else if(ch==2) data=pop2( ); else printf("\n Invalid choice ......"); if(data!= -99) printf("\n Element popped is :%d", data); fflush(stdin); printf("\n\n Press any key to continue "); fflush(stdin); getch( ); break; case 3: printf("\n Enter which stack is to be viewed "); printf("\n For Stack1-press 1 & Stack2-press 2 :"); fflush(stdin); scanf("%d", &ch); if(ch==1) display1( ); else if(ch==2) display2( ); else printf("\n Invalid choice ........"); printf("\n\n Press any key to continue "); fflush(stdin); getch( ); break; case 4: printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); 48

else break; default: printf("\n Invalid choice ....."); getch( ); break; } } } void push1(int x) { if(top1== 9) printf("\n\n Stack1 overflow"); else { top1++; a[top1]=x; } } void push2(int x) { if(top2== 19) printf("\n\n Stack2 overflow"); else { top2++; a[top2]=x; } } int pop1( ) { int dummy; if(top1== -1) { printf("\n\n Stack1 underflow"); getch( ); return(-99); } else { dummy= a[top1]; top1--; return(dummy); } } 49

int pop2( ) { int dummy; if(top2== 9) { printf("\n\n Stack2 underflow"); getch( ); return(-99); } else { dummy=a[top2]; top2--; return(dummy); } } void display1( ) { int i; if(top1== -1) printf("\n\n Stack1 is empty"); else { printf("\n\n Elements in stack1 are :- "); for(i= top1; i>= 0; i--) printf("\n %d", a[i]); } getch( ); } void display2( ) { int i; if(top2== 9) printf("\n\n Stack2 is empty"); else { printf("\n\n Elements in stack2 are :- "); for(i= top2; i>= 10; i--) printf("\n %d", a[i]); } getch( ); }

50

Q. 13 Write pseudo code and program for Implementing simple queue using array. Ans.

PSEUDO CODE
Algorithm. MANI( ). This algorithm is used to show the menu operation on queue and Calling of all functions. FRONT points to the element on the top of the queue, DATA contains the element to be inserted or deleted, REAR points to the element in the back. CHOICE and ANS are variables for storing chosen option.

Step 1:

[Displaying the menu.] FRONT 0 REAR 0 Repeat While(True) Write( Implementing Simple queue using array) Write(1- Insert an element from the queue) Write(2- Delete an element from the queue) Write(3- Display the queue) Write(4- Exit from output window) Write(Enter your choice) Read(CHOICE) [Calling the functions.] Select Case(CHOICE) Case 1: Write( Enter element to inserted into the stack) Read(DATA) Call INSERT(DATA) Case 2: DATA Call DEL( ) If (DATA -99) Then Write(Element deleted is:, DATA)

Step 2:

Case 3: Call DISPLAY( ) Case 4: Write (You want to exit? If yes press y) Read(ANS) If (ANS = y or ANS = Y) Then EXIT Default: Write( Invalid choice) Step 3: [Finished] Return Procedure. INSERT( X). This procedure input or pushing an element passed as parameter into the queue. Here A is the element in the last and X is the variable containing the element. Step 1: [Input or insertion of element] If (REAR = 20) Then Write( Simple queue is full) Else REAR REAR +1 A[REAR] X If (FRONT = 0) 51

Then FRONT FRONT +1 Step 2: [Finished] Return This function delete the input elements from the queue. Here A represents the array variable, FRONT points to the element on the top of the queue, DUMMY is an integer variable to store element temporarily and REAR points to the elements in the last.

Function. DEL( ).

Step 1:

[Deletion of elements from the queue] If (FRONT = 0) Then Write( Simple Queue is empty) Return (-99) Else DUMMY A[FRONT] If (FRONT = REAR) Then FRONT FRONT+1

Step 2:

[Finished] Return(DUMMY)

Procedure. DISPLAY( ). This procedure display the elements on the screen. Here A is the array variable, I is the loop-control variable and FRONT points to the element in the last. Step 1: [Displaying the elements on the screen.] If (FRONT = 0) Then Write( Queue is empty) Else Write(Elements of queue are:) Repeat for I FRONT, , REAR Write (A[I]) [Finished] Return

Step 2:

52

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> void insert(int); int del( ); void display( ); int a[20], rear, front; void main( ) { clrscr( ); int ch, data; char ans; front= -1; rear= -1; while(1) { clrscr( ); printf("\n \t Implementing simple queue using array"); printf("\n\t------------------------------------------------"); printf("\n\t 1- Insert an element into queue "); printf("\n\t 2- Delete an element from the queue "); printf("\n\t 3- Display the queue "); printf("\n\t 4- Exit from output window "); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d", &ch); switch(ch) { case 1: printf("\n Enter the element to be inserted into the simple queue: ); fflush(stdin); scanf("%d",&data); insert(data); break; case 2: data=del( ); if(data != -99) printf("\n Element deleted is :- %d", data); printf("\n Press any key to continue "); fflush(stdin); getch( ); break; case 3: 53

display( ); printf("\n Press any key to continue "); fflush(stdin); getch( ); break; case 4: printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans=='y' || ans=='Y') exit(0); else break; default: printf("\n Invalid choice ....."); getch( ); break; } } } void insert(int x) { if(rear == 19) printf("\n Simple queue is full"); else { rear++; a[rear] = x; if(front == -1) front++; } } int del( ) { int dummy; if(front == -1) { printf("\n Simple queue is empty"); getch( ); return(-99); } else { dummy = a[front]; if(front == rear) 54

front= rear= -1; else front++; return(dummy); } } void display( ) { int i; if(front == -1) printf("\n Queue is empty"); else { printf("\n Elements in queue :- \n"); for(i= front; i<= rear; i++) printf("\n %d", a[i]); } getch( ); }

55

Q.14 Write pseudo code and program for Implementing Circular queue using array Ans.

PSEUDO CODE
Algorithm. MANI( ). This algorithm is used to show the menu operation on queue and Calling of all functions. FRONT points to the element on the top of the queue, DATA contains the element to be inserted or deleted, REAR points to the element in the back. CHOICE and ANS are variables for storing chosen option.

Step 1:

[Displaying the menu.] FRONT 0 REAR 0 Repeat While(True) Write( Implementing Circular queue using array) Write(1- Insert an element from the queue) Write(2- Delete an element from the queue) Write(3- Display the queue) Write(4- Exit from output window) Write(Enter your choice) Read(CHOICE) [Calling the functions.] Select Case(CHOICE) Case 1: Write( Enter element to inserted into the stack) Read(DATA) Call INSERT(DATA) Case 2: DATA Call DEL( ) If (DATA -99) Then Write(Element deleted is:, DATA) Case 3: Call DISPLAY( ) Case 4: Write (You want to exit? If yes press y) Read(ANS) If (ANS = y or ANS = Y) Then EXIT Default: Write( Invalid choice)

Step 2:

Step 3:

[Finished] Return

Procedure. INSERT( X). This procedure input or pushing an element passed as parameter into the queue. Here A is the element in the last and X is the variable containing the element. Step 1: [Input or insertion of element.] If (((REAR = 20)AND(FRONT =0))or(REAR= FRONT-1)) 56

Then Write(Circular queue is full) Else If (REAR = 0) Then FRONT FRONT +1 REAR REAR+1 Else If (REAR = 20) REAR 1 Else REAR REAR+1 A[REAR] X Step 2: [Finished] Return

Function. DEL( ). This function deletes inputted elements from the queue. Here A represents the array variable, FRONT points to the element on the top of the queue, DUMMY is an integer variable to store temporarily and REAR points to the elements in the last. Step 1: [Deletion of elements from the queue] If (FRONT = 0) Then Write( Simple Queue is empty) Return (-99) Else DUMMY A[FRONT] If (FRONT = 20) Then FRONT 1 Else If(FRONT = REAR) FRONT = REAR= 0 Else FRONT FRONT +1 Step 2: [Finished] Return(DUMMY)

Procedure. DISPLAY( ). This procedure display the elements on the screen. Here A is the array variable, I is the loop-control variable and FRONT points to the element in the last. Step 1: [Displaying the elements on the screen.] If (FRONT = 0) Then Write( Queue is empty) Else Write(Elements of queue are:) If (FRONT REAR) Then Repeat for I FRONT, , REAR 57

Write(A[I]) Else Repeat for I FRONT, , 20 Write(A[I]) Repeat for I 1, , REAR Write(A[I]) Step 2: [Finished] Return

58

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> void insert(int); int del( ); void display( ); int a[20], rear, front; void main( ) { clrscr( ); int ch,data; char ans; front= -1; rear= -1; while(1) { clrscr( ); printf("\n \t Implementing circular queue using array"); printf("\n\t------------------------------------------------"); printf("\n\t 1- Insert an element into queue "); printf("\n\t 2- Delete an element from the queue "); printf("\n\t 3- Display the queue "); printf("\n\t 4- Exit from output window "); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d", &ch); switch(ch) { case 1: printf("\n Enter element inserted into queue"); fflush(stdin); scanf("%d", &data); insert(data); break; case 2: data=del( ); if(data != -99) printf("\n Element deleted is :- %d", data); printf("\n Press any key to continue "); fflush(stdin); getch( ); break; case 3: display( ); 59

printf("\n Press any key to continue "); fflush(stdin); getch( ); break; case 4: printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n Invalid choice ....."); getch( ); break; } } } void insert(int x) { if(((rear == 19) && (front == 0)) || (rear == front-1)) printf("\n Circular queue is full"); else if(rear== -1) { front++; rear++; } else if(rear==19) rear= 0; else rear++; a[rear]=x; } int del( ) { int dummy; if(front== -1) { printf("\n Circular queue is empty"); getch( ); return(-99); } else 60

{ dummy=a[front]; if(front==19) front=0; else if(front==rear) front=rear= -1; else front++; return(dummy); } } void display( ) { int i; if(front==-1) printf("\n Circular queue is empty"); else if { printf("\n Elements in queue :- \n"); if(front<= rear) for(i= front; i<= rear; i++) printf("\n %d", a[i]); } else { for(i= front; i<= 19; i++) printf("%d \n", a[i]); for(i= 0; i<= rear; i++) printf("%d \n", a[i]); } getch( ); }

61

Q.15 Write pseudo code and program for Implementing two Priority Queue using two different array. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operations on priority queue and Calling of all functions. FRONT1 and FRONT2 points to the elements to the top of the queue, DATA contains the element to be inserted or deleted, REAR1 and REAR2 points to the element in the back. CHOICE and ANS are variable for storing chosen option. Step 1: [Display of menu] FRONT1 0 REAR1 0 FRONT2 0 REAR2 0 Repeat While(True) Write(Implementing two priority queue using two arrays) Write(1- Insert an element into queue) Write(2- Delete an element from the queue) Write(3- Display the queue) Write(4- Exit from output window) Write(Enter your choice) Read(CHOICE) [Calling the functions.] Select Case(CHOICE) Case 1: Write(Enter element to inserted into the queue) Read(DATA) Call INSERT(DATA) Case 2: Call DEL( ) Case 3: Call DISPLAY( ) Case 4: Write(You want to exit? If yes press y) Read(ANS) If (ANS = y or ANS = Y) Then EXIT Default: Write( Invalid choice) [Finished] Return

Step 2:

Step 3:

Procedure. INSERT(X). This procedure input or push an element passed an as parameter into the queue. Here A and B are the array variables, FRONT1 and FRINT2 points to the element on the top of the queue, REAR1 and REAR2 points to the element in the last and X contain the element which is to be inserted. ANS is to store selected choice. Step 1: [Selection of Queue] Write( Enter your priority. Press 1 or 2) Read(ANS) 62

Step 2:

[Inputs or insertion of element] If (ANS = 1) Then If (REAR1 = 10) Then Write(Priority Queue 1 is full) Else REAR1 REAR1 +1 A[REAR1] X If (FRONT1 = 0) Then FRONT1 FRONT +1 Else If (ANS =2) If (REAR2 = 10) Then Write(Priority Queue 2 is full) Else REAR2 REAR2+1 B[REAR2] X If (FRONT2 = 0) Then FRONT2 FRONT2 +1 Else Write(Invalid choice)

Step 3:

[Finished] Return

Procedure. DEL( ). This procedure deletes input elements from the queue. Here A and B are the array variables, FRONT1 and FRONT2 points to the element on the top of the queue, DUMMY is an integer variable to store the value of the array temporarily and REAR1 and REAR2 points to the elements in the last. ANS is used to store selected choice. Step 1: [Delete element from the queue.] If (FRONT1 0) Then DUMMY A[FRONT1] If (FRONT1 = REAR1) FRONT1 = REAR1 = 0 Else FRONT1 FRONT1+1 Write(Element deleted from priority queue1 :DUMMY) Else Write(Priority Queue 1 is empty) If (FRONT2 0) Then DUMMY B[FRONT2] If (FRONT2 = REAR2) Then FRONT2 = REAR2 = 0 Else FRONT2 FRONT2 + 1 Write(Element deleted from priority queue 2 :DUMMY) Else Write(Priority Queue 2 is empty) 63

Step 3:

[Finished] Return

Procedure. DISPLAY( ). This procedure display elements of queue on the screen. Here A and B are the array variables, I is the loop-control variable and FRONT1 and FRONT2 points to the elements on the top of the queue and REAR1 and REAR2 points to the element in the last. Step 1: [Display the elements on the screen.] If (FRONT1 0) Then Write(Elements of Priority queue 1 are:) Repeat for I FRONT1, , REAR1 Write(A[I]) Else Write(Priority queue 1 is empty) If (FRONT2 0) Then Write(Elements of Priority queue 2 are:) Repeat for I FRONT2, ,REAR2 Write (B[I]) Else Write(Priority queue2 is empty) Step 2: [Finished] Return

64

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> void insert(int); void del( ); void display( ); int a[10], b[10], i, rear1, front1, front2, rear2; void main( ) { clrscr( ); int ch, data; char ans; front1 = rear1 = -1; front2 = rear2 = -1; while(1) { clrscr( ); printf("\n\t Implementing two Priority queue using 2 arrays"); printf("\n\t--------------------------------------------------"); printf("\n\t 1- Insert an element into queue "); printf("\n\t 2- Delete an element from the queue "); printf("\n\t 3- Display the queue "); printf("\n\t 4- Exit from output window "); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d", &ch); switch(ch) { case 1: printf("\n Enter element inserted into queue"); fflush(stdin); scanf("%d", &data); insert(data); break; case 2: del( ); break; case 3: display( ); printf("\n Press any key to continue "); fflush(stdin); getch( ); break; case 4: 65

printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n Invalid choice ....."); getch( ); break; } } }

void insert(int x) { int ans; printf("\n Enter your priority . Press 1 or 2"); scanf("%d", &ans); if(ans == 1) { if(rear1 == 9) printf("\n Priority Queue number 1 is full!"); else { rear1++; a[rear1] = x; if(front1 == -1) front1++; } } else if(ans == 2) { if(rear2 == 9) printf("\n Priority queue 2 is full"); else { rear2++; b[rear2] = x; if(front2 == -1) front2++; } } else 66

printf("\n Invalid choice"); getch( ); }

void del( ) { int dummy, ans; if(front1 != -1) { dummy = a[front1]; if(front1 = rear1) front1= rear1= -1; else front1++; printf("\n Element deleted from queue 1 is %d", dummy); } else { printf("\n Priority queue 1 is empty"); if(front2 -1) { dummy= b[front2]; if(front2 == rear2) front2= rear2= -1; else front2++; printf("\n Element deleted from queue2 is: dummy); } else printf("\n Priority queue2 is empty"); } } void display( ) { if(front1 != -1) { printf("\n Elements in queue number 1 are:"); for(i= front1; i<= rear1; i++) printf("\n %d", a[i]); } else printf("\n Priority queue 1 is empty"); if(front2 != -1) { printf("\n Elements in queue number 2 are:"); 67

for(i= front2; i<= rear2; i++) printf("\n %d", b[i]); } else printf("\n Priority queue 2 is empty"); getch( ); }

68

Q.16 Write pseudo code and program for Implementing two stack using two different array. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operation on the stack and Calling of all functions. Here A and B are two arrays.TOP1 and TOP2 points to the element on the top of the two stacks, DATA contains the element to be pushed or popped. CHOICE, CH and ANS are integer and character variables for storing chosen option. Step 1: [Displaying the menu.] TOP1 0 TOP2 0 Repeat While(True) Write( Implementing stack using array) Write(1- Push an element from the stack) Write(2- Pop an element from the stack) Write(3- Display the stack) Write(4- Exit from output window) Write(Enter your choice) Read(CHOICE) [Calling different functions] Select Case(CHOICE) Case 1: Write( Enter where the element is to be pushed) Write( For Stack1-press 1 & for Stack2-press 2:) Read(CH) Write(Enter element to be pushed into stack:) Read(DATA) If (CH = 1) Then Call PUSH1(DATA) Else If (CH = 2) Call PUSH2(DATA) Else Write(Invalid choice) Case 2: Write(Enter from where element to be deleted) Write(For Stack 1-press 1 & Stack 2-press 2:) Read(CH) If (CH = 1) Then DATA Call POP1( ) Else (CH = 2) DATA Call POP2( ) Else Write(Invalid choice) If (DATA -99) Then Write( Element popped is:, DATA) 69

Step 2:

If

Case 3:

Write(Enter which stack is to be viewed) Write( For Stack 1-press 1 & for Stack 2-press 2) Read (CH) If (CH = 1) Then Call DISPLAY1( ) Else Call CISPLAY2( ) Else Write(Invalid Choice)

If

(CH = 2)

Case 4:

Write(You have chosen to exit) Write( You want to exit? If press y:) Read(ANS) If (ANS=y or ANS=Y) Then EXIT

Default: Write( Invalid choice) Step 3: [Finished] Return

Procedure. PUSH1(X). This procedure input or push an element passed on as parameter into the stack. Here A is the array variable, TOP1 points to the element on the top of the stack and X contain the element which user have entered and want to perform operation on it. Step 1: [Input or pushing the element] If (TOP1 = 20) Then Write(Stack overflow) Else TOP1 TOP1+1 A[TOP1] X Step 2: [Finished] Return This procedure input or push an element passed on as parameter into the stack. Here B is the array variable, TOP2 points to the element on the top of the stack and X contain the element which user have entered and want to perform operation on it.

Procedure. PUSH2(X).

Step 1:

[Input or pushing the element] If (TOP2 = 20) Then Write(Stack overflow) Else TOP2 TOP2+1 B[TOP2] X [Finished] Return 70

Step 2:

Function. POP1( ).

This procedure pop or delete the input elements from the top of the stack. Here A represents the array variable, TOP1 points to the element on the top of the stack and DUMMY is an integer variable to store element temporarily.

Step 1:

[Sorting of elements in the array.] If (TOP1 = 0) Then Write(Stack underflow) Return(-99) Else DUMMY A[TOP1] TOP1 TOP1+1

Step 2:

[Finished] Return(DUMMY) This procedure pop or delete the input elements from the top of the stack. Here B represents the array variable, TOP2 points to the element on the top of the stack and DUMMY is an integer variable to store element temporarily.

Function. POP2( ).

Step 1:

[Sorting of elements in the array.] If (TOP2 = 0) Then Write(Stack underflow) Return(-99) Else DUMMY B[TOP2] TOP2 TOP2 +1

Step 2:

[Finished] Return(DUMMY)

Procedure. DISPLAY1( ). This procedure display the elements on the screen. Here A is the array variable, I is the loop-control variable and TOP1 points to the element on the stack. Step 1: [Displaying the elements on the screen.] If (TOP1 = 0) Then Write(Stack is empty) Else Write(Elements of stack are:) Repeat for I TOP1, , 1 Write (A[I]) Step 2: [Finished] Return Procedure. DISPLAY2( ). This procedure display the elements on the screen. Here B is 71

the array variable, I is the loop-control variable and TOP2 points to the element on the stack. Step 1: [Displaying the elements on the screen.] If (TOP2 = 0) Then Write(Stack is empty) Else Write(Elements of stack are:) Repeat for I TOP1, , 20 Write (B[I]) Step 2: [Finished] Return

72

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> void push1(int); int pop1( ); void display1( ); void push2(int); int pop2( ); void display2( ); int a[20], top1, top2, b[20]; void main( ) { clrscr( ); int choice, ch, data; char ans; top1= -1; top2= -1; while(1) { clrscr( ); printf("\n\t Implementing stack using 2 different array); printf("\n\t-------------------------------------------------"); printf("\n\t 1- Push an element into stack "); printf("\n\t 2- Pop an element from stack "); printf("\n\t 3- Display the stack "); printf("\n\t 4- Exit from output window "); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d", &choice); switch(choice) { case 1: printf("\n Enter where element to pushed "); printf("\n For Stack1-press 1 &Stack2- press2 :"); fflush(stdin); scanf("%d", &ch); printf("\n\n Enter element pushed into stack"); fflush(stdin); scanf("%d", &data); if(ch == 1) push1(data); else if(ch == 2) push2(data); else printf("\n Invalid choice ........"); break; 73

case 2: printf("\n Enter where element to be deleted "); printf("\n For Stack1-press 1 &Stack2- press 2 :"); fflush(stdin); scanf("%d", &ch); if(ch == 1) data= pop1( ); else if(ch == 2) data= pop2( ); else printf("\n Invalid choice ......"); if(data != -99) printf("\n Element popped is :%d", data); fflush(stdin); printf("\n\n Press any key to continue "); fflush(stdin); getch( ); break; case 3: printf("\n Enter which stack is to be viewed "); printf("\n For Stack1-press 1 & Stack2-press 2 :"); fflush(stdin); scanf("%d", &ch); if(ch == 1) display1( ); else if(ch == 2) display2( ); else printf("\n Invalid choice ........"); printf("\n\n Press any key to continue "); fflush(stdin); getch( ); break; case 4: printf("\n You have chosen to exit"); printf("\n You want to exit ? If yes pres y); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); else 74

break; default: printf("\n Invalid choice ....."); getch( ); break; } } } void push1(int x) { if(top1 == 19) printf("\n\n Stack1 overflow"); else { top1++; a[top1] = x; } } void push2(int x) { if(top2 == 19) printf("\n\n Stack2 overflow"); else { top2++; b[top2] = x; } } int pop1( ) { int dummy; if(top1 == -1) { printf("\n\n Stack1 underflow"); getch( ); return(-99); } else { dummy= a[top1]; top1--; return(dummy); } } int pop2( ) 75

{ int dummy; if(top2 == -1) { printf("\n\n Stack2 underflow"); getch( ); return(-99); } else { dummy = b[top2]; top2--; return(dummy); } } void display1( ) { int i; if(top1 == -1) printf("\n\n Stack1 is empty"); else { printf("\n\n Elements in stack1 are :- "); for(i= top1; i>= 0; i--) printf("\n %d", a[i]); } getch( ); } void display2( ) { int i; if(top2 == -1) printf("\n\n Stack2 is empty"); else { printf("\n\n Elements in stack2 are :- "); for(i= top2; i>= 0; i--) printf("\n %d", b[i]); } getch( ); }

76

Q.17 Write the pseudo code and program for Implementing two priority queues within one array. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operations on priority queue and Calling of all functions. FRONT1 and FRONT2 points to the elements to the top of the queue, DATA contains the element to be inserted or deleted, REAR1 and REAR2 points to the element in the back. CHOICE and ANS are variable for storing chosen option. Step 1: [Display of menu] FRONT1 0 REAR1 0 FRONT2 10 REAR2 10 Repeat While(True) Write(Implementing two priority queue using two arrays) Write(1- Insert an element into queue) Write(2- Delete an element from the queue) Write(3- Display the queue) Write(4- Exit from output window) Write(Enter your choice) Read(CHOICE) [Calling the functions.] Select Case(CHOICE) Case 1: Write(Enter element to inserted into the queue) Read(DATA) Call INSERT(DATA) Case 2: Call DEL( ) Case 3: Call DISPLAY( ) Case 4: Write(You want to exit? If yes press y) Read(ANS) If (ANS = y or ANS = Y) Then EXIT Default: Write( Invalid choice) Step 3: [Finished] Return

Step 2:

Procedure. INSERT(X). This procedure input or push an element passed an as parameter into the queue. Here A is the array variables, FRONT1 and FRONT2 points to the element on the top of the queue, REAR1 and REAR2 points to the element in the last and X contain the element which is to be inserted. ANS is to store selected choice. Step 1: [Selection of Queue] Write( Enter your priority. Press 1 or 2) 77

Step 2:

Read(ANS) [Inputs or insertion of element] If (ANS = 1) Then If Then Else

(REAR1 = 10) Write(Priority Queue 1 is full) REAR1 REAR1 +1 A[REAR1] X If (FRONT1 = 0) Then FRONT1 FRONT +1

Else If

(ANS = 2) If (REAR2 = 20) Then Write(Priority Queue 2 is full) Else REAR2 REAR2+1 A[REAR2] X If (FRONT2 = 10) then FRONT2 FRONT2 +1

Else Write(Invalid choice) [Finished] Return Procedure. DEL( ). This procedure deletes input elements from the queue. Here A is the array variables, FRONT1 and FRONT2 points to the element on the top of the queue, DUMMY is an integer variable to store the value of the array temporarily and REAR1 and REAR2 points to the elements in the last. ANS is used to store selected choice. Step 3: Step 1: [Delete element from the queue.] If (FRONT1 0) Then DUMMY A[FRONT1] If (FRONT1 = REAR1) FRONT1 = REAR1 = 0 Else FRONT FRONT +1 Write(Element deleted from priority queue 1 is, DUMMY) Else Write( Priority Queue 1 is empty) Else Write(Priority Queue 1 is empty) If (FRONT2 10) Then DUMMY B[FRONT2] If (FRONT2 = REAR2) Then FRONT2 = REAR2 = 10 Else FRONT2 FRONT2+1 Write(Element deleted from priority queue 2 is ,DUMMY) Else Write(Priority Queue 2 is empty) 78

Step 3:

[Finished] Return Procedure. DISPLAY( ). This procedure display elements of queue on the screen. Here A and B are the array variables, I is the loop-control variable and FRONT1 and FRONT2 points to the elements on the top of the queue and REAR1 and REAR2 points to the element in the last. Step 1: [Display the elements on the screen.] If (FRONT1 0) Then Write(Elements of Priority queue 1 are:) Repeat for I FRONT1, , REAR1 Write(A[I]) Else Write(Priority queue 1 is empty) If (FRONT2 10) Then Write(Elements of Priority queue 2 are:) Repeat for I FRONT2, ,REAR2 Write (A[I]) Else Write(Priority queue2 is empty) Step 2: [Finished] Return

79

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> void insert(int); void del( ); void display( ); int a[20], i, rear1, front1, front2, rear2; void main( ) { clrscr( ); int ch, data; char ans; front1= rear1= -1; front2= rear2= 9; while(1) { clrscr( ); printf("\n\t Implementing two priority queue using 2 arrays"); printf("\n\t--------------------------------------------------"); printf("\n\t 1- Insert an element into queue "); printf("\n\t 2- Delete an element from the queue "); printf("\n\t 3- Display the queue "); printf("\n\t 4- Exit from output window "); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d", &ch); switch(ch) { case 1: printf("\n Enter element inserted into queue"); fflush(stdin); scanf("%d", &data); insert(data); break; case 2: del( ); break; case 3: display( ); printf("\n Press any key to continue "); fflush(stdin); getch( ); break; 80

case 4: printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n Invalid choice ....."); getch( ); break; } } } void insert(int x) { int ans; printf("\n Enter your priority . Press 1 or 2"); scanf("%d", &ans); if(ans == 1) { if(rear1==9) printf("\n Priority Queue number 1 is full!"); else { rear1++; a[rear1] = x; if (front1 == -1) front1++; } } else if(ans == 2) { if(rear2 == 19) printf("\n Priority queue 2 is full"); else { rear2++; a[rear2] = x; if(front2 == 9) front2++; } } else printf("\n Invalid choice"); 81

getch( ); }

void del( ) { int dummy, ans; if(front1 != -1) { dummy = a[front1]; if(front1 = rear1) front1= rear1= -1; else front1++; printf("\n Element to be deleted from queue 1 is %d",dummy); } else { printf("\n Priority queue 1 is empty"); if(front2 != 9) { dummy= a[front2]; if(front2 == rear2) front2 = rear2 = 9; else front2++; printf("\n Element deleted from queue 2 is %d", dummy); } else printf("\n Priority queue2 is empty"); } } void display( ) { if(front1 != -1) { printf("\n Elements in queue number 1 are:"); for(i= front1; i <= rear1; i++) printf("\n %d", a[i]); } else printf("\n Priority queue 1 is empty"); if(front2 != 9) { printf("\n Elements in queue number 2 are:"); 82

for(i= front2; i<= rear2; i++) printf("\n %d", a[i]); } else printf("\n Priority queue 2 is empty"); getch( ); }

83

Q. 18 Write the pseudo code and program for Implementing stack and simple queue within one array. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operations on the stack and simple queue and Calling of all functions. TOP points to the element on the top of the stack, FRONT points to element on the top of the queue, REAR points to the element present in the last of the queue, DATA contains the element to be inserted or deleted or pushed or popped. CHOICE and ANS are the integer and character variables for storing chosen option. CH1 and CH2 are integer variable and used for chosen options. Step 1: [Displaying the menu.] TOP 0 FRONT 10 REAR 10 Repeat While(True) Write(Implementing stack & simple queue within an array.) Write(1- Operations on stack) Write(2- Operations on simple queue) Write(3- Exit from window) Write( Enter your choice) [Calling the functions.] Select Case(CHOICE) Case 1: Write( Implementing stack using array) Write(1- Push an element from the stack) Write(2- Pop an element from the stack) Write(3- Display the stack) Write(4- Exit from output window) Write(Enter your choice) Read(CH1) Select Case(CH1) Case 1: Write(Enter element pushed into stack) Read(DATA) Call PUSH(DATA) Case 2: DATA Call POP( ) If (DATA -99) Then Write(Element popped is :DATA) Case 3: Call DISPLAY( ) Case 4: Write(You want to exit? If yes then press y) Read(ANS) If (ANS = y or ANS = Y) Then EXIT 84

Step 2:

Default: Write(Invalid choice) Case 2: Write( Implementing Simple queue using array) Write(1- Insert an element from the queue) Write(2- Delete an element from the queue) Write(3- Display the queue) Write(4- Exit from output window) Write(Enter your choice) Read(CH2) Select Case(CH2) Case 1: Write( Enter element inserted into stack) Read(DATA) Call INSERT(DATA) Case 2: DATA Call DEL( ) If (DATA -99) Then Write(Element deleted is:, DATA) Case 3: Call DISPLAY( ) Case 4: Write (You want to exit? If yes press y) Read(ANS) If (ANS = y or ANS = Y) Then EXIT Default: Write( Invalid choice) Case 3: Write (You want to exit? If yes press y) Read(ANS) If (ANS = y or ANS = Y) Then EXIT Default: Write(Invalid Choice) Step 3: [Finished] Exit

Procedure. PUSH(X). This procedure input or push an element passed on as parameter into the stack. Here A is the array variable, TOP points to the element on the top of the stack and X contain the element which user have entered and want to perform operation on it. Step 1: [Input or pushing the element] If (TOP = 10) Then Write(Stack overflow) Else TOP TOP+1 A[TOP] X 85

Step 2:

[Finished] Return This procedure pop or delete the input elements from the top of the stack. Here A represents the array variable, TOP points to the element on the top of the stack and DUMMY is an integer variable to store element temporarily.

Function. POP( ).

Step 1:

[Sorting of elements in the array.] If (TOP = 0) Then Write(Stack underflow) Return(-99) Else DUMMY A[TOP] TOP TOP+1

Step 2:

[Finished] Return(DUMMY) the

Procedure. DISPLAY1( ). This procedure display the elements on the screen. Here A is array variable, I is the loop-control variable and TOP points to the element on the stack. Step 1: [Displaying the elements on the screen.] If (TOP = 0) Then Write(Stack is empty) Else Write(Elements of stack are:) Repeat for I TOP, , 1 Write (A[I]) [Finished] Return

Step 2:

Procedure. INSERT( X). This procedure input or pushing an element passed as parameter into the queue. Here A is the element in the last and X is the variable containing the element. Step 1: [Input or insertion of element] If (REAR = 20) Then Write( Simple queue is full) Else REAR REAR +1 A[REAR] X If (FRONT = 10) Then FRONT FRONT +1 Step 2: [Finished] 86

Return Function. DEL( ). This function delete the input elements from the queue. Here A represents the array variable, FRONT points to the element on the top of the queue, DUMMY is an integer variable to store element temporarily and REAR points to the elements in the last. Step 1: [Deletion of elements from the queue] If (FRONT = 10) Then Write( Simple Queue is empty) Return (-99) Else DUMMY A[FRONT] If Then Step 2: (FRONT = REAR) FRONT FRONT+1

[Finished] Return(DUMMY)

Procedure. DISPLAY2( ). This procedure display the elements on the screen. Here A is the array variable, I is the loop-control variable and FRONT points to the element in the last. Step 1: [Displaying the elements on the screen.] If (FRONT = 10) Then Write( Queue is empty) Else Write(Elements of queue are:) Repeat for I FRONT, , REAR Write (A[I]) [Finished] Return

Step 2:

87

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> int a[20], top, rear, front; void push(int); int pop( ); void display1( ); void insert(int); void display2( ); int del( ); void main( ) { clrscr( ); int choice, data, ch1, ch2; char ans; rear= front = 9; top= -1; while(1) { clrscr( ); printf("\n\t Implementing Stack & Simple queue within array. \n"); printf(" ---------------------------------------------------------- \n"); printf("\n\t 1- Operations on Stack"); printf("\n\t 2- Operations on Simple queue"); printf("\n\t 3- Exit from the window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d", &choice); switch(choice) { case 1: printf("\n \t Implementing stack using array"); printf("\n\t------------------------------------------"); printf("\n\t 1- Push an element into stack "); printf("\n\t 2- Pop an element from stack "); printf("\n\t 3- Display the stack "); printf("\n\t 4- Exit from output window "); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d", &ch1); switch(ch1) { case 1: printf("\n Enter element be pushed into stack"); fflush(stdin); scanf("%d", &data); push(data); 88

break; case 2: data= pop( ); if(data != -99) printf("\n Element popped is :%d", data); printf("\n Press any key to continue. \n"); getch( ); break; case 3: display1( ); break; case 4: printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n Invalid condition. \n"); printf("\n Press any key to continue. \n"); getch( ); break; } case 2: clrscr( ); printf("\n\t Implementing simple queue using array"); printf("\n\t--------------------------------------------"); printf("\n\t 1- Insert an element into queue "); printf("\n\t 2- Delete element from the queue "); printf("\n\t 3- Display the queue "); printf("\n\t 4- Exit from output window "); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d",&ch2); switch(ch2) { case 1: printf("\n Enter element be inserted into queue"); scanf("%d", &data); insert(data); break; 89

case 2: data = del( ); if(data != -99) printf("\n Element deleted is :- %d", data); printf("\n Press any key to continue "); getch( ); break; case 3: display2( ); printf("\n Press any key to continue "); fflush(stdin); getch( ); break; case 4: printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n Invalid choice ..."); printf("\n Press any key to continue. \n"); getch( ); break; } case 3: printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans ==' Y') exit(0); else break; default: printf("\n Invalid choice ..."); printf("\n Press any key to continue ... \n"); getch( ); break; } 90

} } void push(int x) { if(top == 9) printf("\n\n Stack overflow"); else { top++; a[top] = x; } } int pop( ) { int dummy; if(top == -1) { printf("\n\n Stack underflow"); getch( ); return(-99); } else { dummy = a[top]; top--; return(dummy); } } void display1( ) { int i; if(top == -1) printf("\n\n Stack is empty"); else { printf("\n\n Elements in stack :- "); for(i= top; i>= 0; i--) printf("\n %d", a[i]); } getch( ); } void insert(int x) { if(rear == 19) printf("\n Simple queue is full"); 91

else { rear++; a[rear] = x; if(front == 9) front++; } } int del( ) { int dummy; if(front == 9) { printf("\n Simple queue is empty"); getch( ); return(-99); } else { dummy= a[front]; if(front == rear) front = rear = 9; else front++; return(dummy); } } void display2( ) { int i; if(front == 9) printf("\n Queue is empty"); else { printf("\n Elements in queue :- \n"); for(i= front; i<= rear; i++) printf("\n %d", a[i]); } getch( ); }

92

Q. 19 Write the pseudo code and program for Implementing Input restricted dequeue using array. Ans.

PSEUDO CODE
Algorithm MAIN( ). This algorithm is used to show the menu operations on input restricted dequeue and Calling of all functions. FRONT points to the element on the top of queue, DATA contains the element to be inserted or deleted, REAR points to the element in the back. CHOICE and ANS are variables for storing chosen option. Step 1: [ Displaying the menu] FRONT 0 REAR 0 Repeat While(True) Write( Implementing Input Restricted dequeue using array) Write( 1- Insert an element into queue) Write( 2- Delete an element from the queue) Write( 3- Display the queue) Write( 4- Exit from output window) Write( Enter your choice:- ) Read(CHOICE) [Calling the functions.] Select Case(CHOICE) Case 1: Write( Enter the element to be inserted into queue) Read(DATA) Call INSERT(DATA) Case 2: DATA Call DEL( ) If (DATA -99) Then Write( Element deleted is: DATA) Case 3: Call DISPLAY( ) Case 4: Write( Are you sure you want to exit? If yes press y) Read( ANS) If (ANS =y or ANS =Y) Then EXIT(0) Default: Write( Invalid choice) Step 3: [Finished] Exit This procedure is inputting or inserting an element passed on as parameter into the queue. Here A is the array variable, FRONT points to the element on the top of the queue, REAR points to the element in the last of the queue and X is the variable containing the element.

Step 2:

Procedure .INSERT(X).

Step 1:

[Inputting or insertion of element] If (REAR = 20) 93

Then Write( Input restricted dequeue is full. ) Else REAR REAR + 1 A[REAR] X If (FRONT= 0) Then FRONT FRONT + 1 Step 2: [Finished] Return

Function. DEL ( ). This procedure deletes inputted from the input restricted dequeue. Here A is the array variable, FRONT points to the element on the top of the input restricted dequeue, DUMMY is an integer variable to store element temporarily, REAR points to the element in the last and ANS stores chosen option. Step 1: [ Deletion of element from the queue] If (FRONT = 0) Then Write( Input restricted dequeue is empty ) Return(-99) Else Write( Enter from which end element is to be deleted) Write( F- Front and R- Rear) Read(ANS) ANS TOUPPER(ANS) If Then Else If Then Else If Then Else Write( Invalid Choice) Step 2: [Finished] Return(DUMMY) (ANS = R) DUMMY A[REAR] REAR REAR-1 (ANS= F) DUMMY A[FRONT] FRONT FRONT + 1 (FRONT = REAR) DUMMY A[FRONT] FRONT REAR 0

Procedure. DISPLAY ( ). This procedure is displaying the elements of input restricted dequeue on the screen. Here A is the array variable, I is the loop-control variable and FRONT points to the element on the top of the input restricted dequeue and REAR points to the element in the last. 94

Step 1:

[Displaying the elements on the screen.] If (FRONT= 0) Then Write( Input restricted dequeue is empty.) Else Write( Elements of input restricted dequeue are: ) Repeat for I FRONT, , REAR Write(A[I]) [Finished] Return

Step 2:

95

Program Coding
#include<stdio.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> void insert(int); int del( ); void display( ); int a[20],rear,front; void main( ) { int ch,data; char ans; front= rear= -1; while(1) { clrscr(); printf("\n Implementing Input Resrtricted Dequeue using array... "); printf("\n------------------------------------------------------- "); printf("\n 1- Insert an element into queue"); printf("\n 2- Delete an element from the queue"); printf("\n 3- Display elements of the queue"); printf("\n 4- Exit from output window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d", &ch); switch(ch) { case 1: printf("\n Enter element to be inserted into queue :- "); fflush(stdin); scanf("%d",&data); insert(data); break; case 2: data=del( ); if(data != -99) printf("\n Element deleted is : %d",data); printf("\n\n Press any key to continue..."); fflush(stdin); getch(); break; case 3: display( ); printf("\n Press any key to continue..."); fflush(stdin); 96

getch( ); break; case 4: printf("\n You have chosen to exit"); printf("\n R you sure you want to exit? If yes press y."); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n Invalid choice... "); break; } } } void insert(int x) { if(rear == 19) printf("\n Input restricted dequeue is full."); else { rear++; a[rear]= x; if(front== -1) front++; } } int del( ) { int dummy= -99; char ans; if(front== -1) { printf("\n\n Input restricted dequeue is empty."); getch( ); return(dummy); } else { printf("\n\n Enter from which end element is to be deleted :- "); printf("\n F- Front \n R- Rear"); fflush(stdin); scanf("%c",&ans); ans= toupper(ans); if(front == rear) { dummy= a[front]; 97

front= rear= -1; } else { if(ans=='F') { dummy= a[front]; front++; } else if(ans == 'R') { dummy= a[rear]; rear--; } else printf("\n Invalid choice"); } } return(dummy); } void display( ) { int i; if(front == -1) printf("\n Queue is empty"); else { printf("\n Elements in queue :- \n"); for(i= front; i<= rear i++) printf("\n %d", a[i]); } getch( ); }

98

Q. 20 Write pseudo-code and program for Implementing Output restricted Dequeue using array. Ans.

PSEUDO CODE
Algorithm MAIN( ). This algorithm is used to show the menu operations on output restricted dequeue and Calling of all functions. FRONT points to the element on the top of queue, DATA contains the element to be inserted or deleted, REAR points to the element in the back. CHOICE and ANS are variables for storing chosen option. Step 1: [ Displaying the menu] FRONT 0 REAR 0 Repeat While(True) Write( Implementing Output Restricted dequeue using array) Write( 1- Insert an element into queue) Write( 2- Delete an element from the queue) Write( 3- Display the queue) Write( 4- Exit from output window) Write( Enter your choice:- ) Read(CHOICE) [Calling the functions.] Select Case(CHOICE) Case 1: Write( Enter the element to be inserted into queue) Read(DATA) Call INSERT(DATA) Case 2: DATA Call DEL( ) If (DATA -99) Then Write( Element deleted is: DATA) Case 3: Call DISPLAY( ) Case 4: Write( Are you sure you want to exit? If yes press y) Read( ANS) If (ANS =y or ANS =Y) Then EXIT(0) Default: Write( Invalid choice) Step 3: [Finished] Exit

Step 2:

Procedure. INSERT(X). This procedure is inputting or pushing an element passed on as parameter into the queue. Here A is the array variable, FRONT points to the element on the top of the output restricted dequeue, REAR points to the elememt in the last, X is the variable containing the element and ANS is for storing the chosen option. Step 1: [Inputting or insertion of element] 99

If (REAR = 20) Then Write( Output restricted dequeue is full) Else Write( Enter from which end element is to be inserted:) Write( F- FRONT and R- REAR) Read(ANS) ANS TOUPER(ANS) If Then (ANS = F) If (FRONT= 0) Then FRONT FRONT + 1 REAR REAR +1 A[FRONT] X Else If (FRONT = 1) Write(Queue is full from front end ) Else FRONT FRONT 1 A[FRONT] X If Then (ANS= R) (REAR = 0) FRONT FRONT + 1 REAR REAR + 1 A[REAR] X Else If (REAR = 20) Write( Queue is full from rear end ) Else REAR REAR +1 A[REAR] X Write( Invalid choice) Step 2: [Finished] Return

Else If Then

Else

Function. DEL( ). This procedure deletes inputted elements from the output restricted dequeue. Here A is the array variable, FRONT points to the element on the top of the queue, DUMMY is an integer variable to store element temporarily and REAR points to the element in the last. Step 1: [Deletion of element.] If (FRONT = 0) Then Write( Output restricted dequeue is empty) Return(-99) Else DUMMY A[FRONT] If (FRONT = REAR) Then FRONT REAR 0 Else FRONT FRONT +1 Step 2: [Finished] 100

Return(DUMMY) Procedure. DISPLAY ( ). This procedure is displaying the elements of output restricted dequeue on the screen. Here A is the array variable, I is the loop-control variable and FRONT points to the element on the top of the output restricted dequeue and REAR points to the element in the last. Step 1: [Displaying the elements on the screen.] If (FRONT= 0) Then Write( Input restricted dequeue is empty.) Else Write( Elements of input restricted dequeue are: ) Repeat for I FRONT, , REAR Write(A[I]) Step 2: [Finished] Return

101

Program Coding
#include<stdio.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> void insert(int); int del( ); void display( ); int a[20],rear,front; void main( ) { int ch,data; char ans; front= rear= -1; while(1) { clrscr( ); printf("\n Implementing Output Resrtricted Dequeue using array "); printf("\n------------------------------------------------------- "); printf("\n 1- Insert an element into queue"); printf("\n 2- Delete an element from the queue"); printf("\n 3- Display elements of the queue"); printf("\n 4- Exit from output window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter element to be inserted into queue :- "); fflush(stdin); scanf("%d",&data); insert(data); break; case 2: data= del( ); if(data != -99) printf("\n Element deleted is : %d", data); printf("\n\n Press any key to continue..."); fflush(stdin); getch( ); break; case 3: display( ); printf("\n Press any key to continue..."); fflush(stdin); 102

getch( ); break; case 4: printf("\n You have chosen to exit"); printf("\n R you sure you want to exit? If yes press y."); fflush(stdin); scanf("%c",&ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n Invalid choice... "); break; } } } void insert(int x) { char ans; printf("\n\n Enter the end from which the data is to be inserted :- "); printf("\n F- Front \n R- Rear \n"); fflush(stdin); scanf("%c", &ans); ans= toupper(ans); if(ans == 'F') { if(front == -1) { front++; rear++; a[front]=x; } else if(front== 0) printf("\n Output restricted dequeue is full from front end... "); else { front--; a[front]=x; } } else if(ans == 'R') { if(rear== -1) { front++; rear++; a[rear]=x; } 103

else if(rear== 19) printf("\n Output restricted dequeue is full from rear end ... "); else { rear++; a[rear]= x; } } else printf("\n Invalid choice"); getch( ); } int del( ) { int dummy; if(front== -1) { printf("\n Output restricted dequeue is empty. "); getch( ); return(-99); } else { dummy= a[front]; if(front== rear) front= rear= -1; else front++; return(dummy); } } void display( ) { int i; if(front== -1) printf("\n Queue is empty"); else { printf("\n Elements in queue :- \n"); for(i= front; i<= rear;I ++) printf("\n %d", a[i]); } getch( ); }

104

Q. 21. Write pseudo code and program for Implementing different operations on matrices. Ans.

PSEUDO CODE
Algorithm MAIN( ) This algorithm consist of menu for Calling different procedure for doing different operations on matrix. CHOICE is an integer variable for storing selected option. Step 1: [Menu for performing operations] Repeat While ( True) Write( Different operations on matrix) Write( 1- Identity matrix) Write( 2- Upper triangular matrix) Write( 3- Lower triangular matrix) Write( 4- Diagonal matrix) Write( 5- Tridiagonal matrix) Write( 6- Unit upper triangular matrix) Write( 7- Unit lower triangular matrix) Write( 8- Exit) Write( Enter your choice:- ) Read(CHOICE) [ Calling of functions for each operation] Select Case( CHOICE) Case 1: Call IDENTITY( ) Case 2: Call UP_TRI( ) Case 3: Call LOW_TRI( ) Case 4: Call DIAGONAL( ) Case 5: Call TRI_DIAG( ) Case 6: Call UT_UP_TRI( ) Case 7: Call UT_LOW_TRI( ) Case 8: EXIT(0) Default: Write( Invlalid Choice) [Finished] Exit

Step 2:

Step 2:

Procedure. INPUT ( ). This procedure inputs entered elements into array. I is the loop-control variable. A represents the array. J tells the position of next element in array. Step 1: [Entering elements] Write( Enter elements:) Repeat for I 1, 2, 3 Repeat for J 1, 2, 3 Read(A[I][J]) Step 2: [Finished] Return Procedure. DISPLAY ( ). This procedure displays elements in array on screen. I is the loop-control variable. A represents the array. J tells the position of next element in array. 105

Step 1:

[Displaying elements of the array] Repeat for I 1, 2, 3 Repeat for J 1, 2, 3 Write( A[I][J]) [Finished] Return

Step 2:

Procedure. IDENTITY( ). This procedure displays elements in array on screen. I is the loop-control variable. A represents the array. J tells the position of next element in array. Step 1 : [Displaying the elements of array] Call INPUT( ) Call DISPLAY( ) Repeat for I 1, 2, 3 Repeat for J 1, 2, 3 If (I = J) Then A[I][J] = 1 Else A[I][J] = 0 Call DISPLAY( ) Step 2: [Finished] Return Procedure. DIAGONAL( ). This procedure displays elements in array on screen. I is the loop-control variable. A represents the array. J tells the position of next element in array. Step 1: [Displaying the elements of the array] Call INPUT( ) Call DISPLAY( ) Repeat for I 1, 2, 3 Repeat for J 1, 2, 3 If (( I > J) or (I < j)) Then A[I][J] = 0 Call DISPLAY( ) [Finished] Return

Step 2:

Procedure. TRI_DIAG ( ). This procedure displays elements in array on screen. I is the loop-control variable. A represents the array. J tells the position of next element in array. Step 1: [Displaying the elements of the array] Call INPUT( ) Call DISPLAY( ) Repeat for I 1, 2, 3 Repeat for J 1, 2, 3 If ((I J+2) or (I+2 J)) Then A[I][J] = 0 Call DISPLAY( ) 106

Step 2:

[Finished] Return

Procedure. LOW_TRI( ). This procedure displays elements in array on screen. I is the loop-control variable. A represents the array. J tells the position of next element in array. Step 1: [Displaying the elements of the array] Call INPUT( ) Call DISPLAY( ) Repeat for I 1, 2, 3 Repeat for J 1, 2, 3 If (I < J) Then A[I][J] = 0 Call DISPLAY( ) Step 2: [Finished] Return Procedure. UT_UP_TRI ( ). This procedure displays elements in array on screen. I is the loop-control variable. A represents the array. J tells the position of next element in array. Step 1: [Displaying the elements of the array] Call INPUT( ) Call DISPLAY( ) Repeat for I 1, 2, 3 Repeat for J 1, 2, 3 If (I > J) Then A[I][J] = 0 Else If Call DISPLAY ( ) Step 2: [Finished] Return (I = J) A[I][J] = 1

Procedure. UT_LOW_TRI( ). This procedure displays elements in array on screen. I is the loop-control variable. A represents the array. J tells the position of next element in array. Step 1: [Displaying the elements of the array] Call INPUT( ) Call DISPLAY( ) Repeat for I 1, 2, 3 Repeat for J 1, 2, 3 If (I < J) Then A[I][J] = 0 107

Else If Call DISPLAY ( ) Step 2: [Finished] Return (I = J) A[I][J] = 1

108

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> void input( ); void display( ); void identity( ); void up_tri( ); void lw_tri( ); void diagonal( ); void tri_diag( ); void ut_up_tri( ); void ut_lw_tri( ); int a[3][3],i,j; void main( ) { int ch; while(1) { clrscr( ); printf("\n Different operations on matrix... "); printf("\n------------------------------------"); printf("\n 1- Identity matrix"); printf("\n 2- Upper triangular matrix"); printf("\n 3- Lower triangular matrix"); printf("\n 4- Diagonal matrix"); printf("\n 5- Tridiagonal matrix"); printf("\n 6- Unit upper triangular matrix"); printf("\n 7- Unit lower triangular matrix"); printf("\n 8- Exit from output window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d",&ch); switch(ch) { case 1: identity( ); break; case 2: up_tri( ); break; case 3: lw_tri( ); break; case 4: 109

diagonal( ); break; case 5: tri_diag( ); break; case 6: ut_up_tri( ); break; case 7: ut_lw_tr(); break; case 8: exit(0); break; default: printf("\n Invalid choice... "); break; } getch( ); } } void input( ) { printf("\n Enter elements : \n"); for(i= 0; i< 3; i++) { for(j= 0; j< 3; j++) { scanf("%d\n", &a[i][j]); } } getch(); } void display( ) { printf("\n Elements are: \n"); for(i= 0; i< 3; i++) { for(j= 0; j< 3; j++) printf(" %d ", a[i][j]); printf("\n\n"); } getch( ); } void identity( ) 110

{ input( ); clrscr( ); display( ); for(i= 0; i< 3; i++) { for(j= 0; j< 3; j++) { if(i == j) a[i][j]= 1; else a[i][j]= 0; } } display( ); } void diagonal( ) { input( ); clrscr( ); display( ); for(i= 0; i< 3; i++) { for(j= 0; j< 3; j++) { if((i> j) || (i< j)) a[i][j]= 0; } } display( ); } void tri_diag( ) { input( ); clrscr( ); display( ); for(i= 0; i< 3; i++) { for(j= 0; j< 3; j++) { if((i>= j+2) || (i+2<= j)) a[i][j]= 0; } } display( ); } void up_tri( ) 111

{ input( ); clrscr( ); display( ); for(i= 0; i< 3; i++) { for(j= 0; j< 3; j++) { if(i> j) a[i][j]= 0; } } display( ); } void lw_tri( ) { input( ); clrscr( ); display( ); for(i= 0; i< 3; i++) { for(j= 0; j< 3; j++) { if(i< j) a[i][j]= 0; } } display( ); } void ut_up_tri( ) { input( ); clrscr( ); display( ); for(i= 0; i< 3; i++) { for(j= 0; j< 3; j++) { if(i> j) a[i][j]= 0; else if(i== j) a[i][j]= 1; } } display( ); } void ut_lw_tri( ) 112

{ input( ); clrscr( ); display( ); for(i= 0; i< 3; i++) { for(j= 0; j< 3; j++) { if(i< j) a[i][j]= 0; else if(i==j) a[i][j]=1; } } display( ); }

113

Q. 22 Write pseudo code and program for implementing grounded simply singly link list. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show menu of various operations to be performed on the linked list. ANS is a character variable and CHOICE is an integer variable to hold selected option input. Structure of node is as follows INFO LINK GSSLL Step 1: [Displaying the menu ] Repeat While( True) Write( Implementing grounded simply linked list) Write( 1- Creation of linked list) Write( 2- Insertion of element in the beginning ) Write( 3- Insertion of element in between) Write( 4- Insertion of element in the last) Write( 5- Deletion of element from the beginning Write( 6- Deletion of element from between) Write( 7- Deletion of element from last) Write( 8- Searching of element in the list) Write( 9- Concatenation of two different linked list) Write( 10- Display of elements in the list) Write( 11- Exit from output window) Write( Enter your choice: ) Read(CHOICE) [Calling of functions for each operation] Select Case( CHOICE) Case 1: Call CREATE( ) Case 2: Call INS_BEG( ) Case 3: Call INS_BET( ) Case 4: Call INS_LAST( ) Case 5: Call DEL_BEG( ) Case 6: Call DEL_BET( ) Case 7: Call DEL_LAST( ) Case 8: Call SEARCH( ) Case 9: Call CONCATE( ) Case 10: Call DISPLAY( ) Case 11: Write( You have chosen to exit) Write( Are you sure you want to exit) Write( If yes press y ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then EXIT(0) [Finished] Exit

Step 2:

Step 3:

114

Procedure. CREATE( ).

This procedure creates grounded simply singly linked list with initially given information. This procedure is recursive in nature and Calls itself from within to add next information to the list. PTR and PTR1 are two pointers. PTR is used for traversing through the list and PTR1 is used to confirm addition of newly allocated nodes. ANS is a character variable used for checking the choice. HEAD is the pointer to the first node with initial value of NULL. Structure of node is as follows INFO GSSLL LINK

Step 1:

[Allocating memory for new node] PTR1 <= GSSLL If (PTR1 = NULL) Then Write( Memory is full ) Step 2: [Entering the information] LINK(PTR1) NULL Write( Enter information: ) Read(INFO(PTR1)) Step 3: [Inserting the node information linked list] If (HEAD = NULL) Then HEAD PTR1 Else PTR HEAD Repeat While(LINK(PTR) NULL) PTR LINK(PTR) LINK(PTR) PTR1 Step 4: [Checking for additional data] Write( Do you have more information? ) Write( If yes press y: ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then Call CREATE( ) Step 5: [Finished] Return Procedure. INS_BEG( ). This procedure inserts given information in the beginning of list by linking the new node with the previously created node. PTR1 is a pointer used to confirm variable used for checking the choice. HEAD is the pointer to the first node. Structure of node is as follows INFO GSSLL Step 1: [Allocating memory for new node] PTR1 <= GSSLL If (PTR1 = NULL) Then Write( Memory is full ) Return 115 LINK

Step 2:

[Entering the information] Write( Enter information: ) Read(INFO(PTR1)) LINK(PTR1) NULL Step 3: [Adding the new node to the list] LINK(PTR1) HAED HEAD PTR1 Step 4: [Finished] Return Procedure. INS_BET( ). This procedure inserts given information in between of list. PTR, PTR1and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next node. COUNT is an integer variable used for keeping track of current position. POS is the integer variable used for storing the entered position for insertion of node. HEAD is the pointer to the first node. Structure of node is as follows INFO GSSLL Step 1: [Allocating memory for new node] COUNT 1 PTR1 <= GSSLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the position and information] Write( Enter the position where new node is to be inserted: ) Read(POS) Write( Enter information: ) Read(INFO(PTR1)) PTR HEAD [Inserting the new node] Repeat While((PTR NULL) and (COUNT < POS-1)) PTR LINK(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR2 LINK(PTR) LINK(PTR) PTR1 LINK(PTR1) PTR2 Else Write( The given position is out of range. ) Step 4: [Finished] Return LINK

Step 2:

Step 3:

116

Procedure. INS_LAST( ). This procedure insert given information in last of list . PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. HEAD is the pointer to the first node. Structure of node is as follows INFO GSSLL Step 1: [Allocating memory for new node] PTR1 <= GSSLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the information] Write( Enter information: ) Read( INFO(PTR1)) LINK(PTR1) PTR1 [Inserting the new node] PTR HEAD Repeat While(LINK(PTR) NULL) PTR LINK(PTR) LINK(PTR) PTR1 [Finished] Return LINK

Step 2:

Step 3:

Step 4:

Procedure. DEL_BEG( ). This procedure delete the element from the beginning of list. PTR is a pointer used to transverse through the list. HEAD is the pointer to the first node. Structure of node is as follows INFO GSSLL Step 1: [Checking memory] If (HEAD = NULL) Then Write( Link list is empty) [Deleting the node] PTR HEAD HEAD LINK(PTR) Write( Element that was deleted is: INFO(PTR)) Restore(PTR) [Finished] Return LINK

Step 2:

Step 3:

Procedure. DEL_BET( ). This procedure delete the element from between of list. PTR, PTR1 and PTR2 are pointers. PTR is used to transverse through 117

the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next nodes. COUNT is an integer variable used for keeping track of current position. POS is the integer variable used for storing the entered position for deletion of node. HEAD is the pointer which points to the first node. Structure of node is as follows INFO GSSLL Step 1: [Checking memory] COUNT 1 If (HEAD = NULL) Then Write( Link list is empty ) [Entering position for deletion] Write( Enter the position from where node is to be deleted:) Read(POS) PTR HEAD [Deleting the node] Repeat While((PTR1 NULL) and (COUNT < POS 1)) PTR LINK(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR1 LINK(PTR) PTR2 LINK(PTR1) LINK(PTR) PTR2 Write( Element deleted was: INFO(PTR1)) Restore(PTR1) Else Write( Position given is out of range ) [Finished] Return LINK

Step 2:

Step 3:

Step 4:

Procedure. DEL_LAST( ). This procedure delete the element from the last of list. PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. HEAD is the pointer to the first node. Structure of node is as follows INFO GSSLL Step 1: [Checking memory] If (HEAD = NULL) Then Write( Link list is empty ) [Deleting the node] PTR HEAD Repeat While(LINK(LINK(PTR)) NULL) PTR LINK(PTR) 118 LINK

Step 2:

PTR1 LINK(PTR) LINK(PTR) NULL Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) Step 3: [Finished] Return

Procedure. DISPLAY( ). This procedure display the element of the list on the screen. OTR is a pointer used to transverse through the list. COUNT is an integer variable used for keeping track of current position. HEAD is the pointer to the first node. Structure of node is as follows INFO GSSLL Step 1: [Checking memory] COUNT 1 PTR HEAD If (PTR = NULL) Then Write( Linked list is empty) [Displaying elements on the screen] Write( Elements in the list are:) Repeat While(PTR NULL) Write( Node: COUNT, Element: INFO(PTR)) COUNT COUNT + 1 PTR LINK(PTR) [Finished] Return This procedure search the element from given the list. PTR is pointer used to transverse through the list. POS is an integer variable used for telling the position of the element in the list. SER is an integer variable used for storing the element that is to be searched. HEAD is the pointer to the first node. Structure of node is as follows INFO GSSLL Step 1: [Entering the element for search] POS 1 FLAG 0 Write(Enter the element to be searched: ) Read(SER) [Search for element] PTR HEAD 119 LINK LINK

Step 2:

Step 3:

Procedure. SEARCH( ).

Step 2:

Repeat While(PTR NULL) If (INFO(PTR) = SER) Then FLAG 1 POS POS + 1 PTR LINK(PTR) Step 3: [Displaying the result] If (FLAG = 1) Then Write( Element found at position, POS) Else Write( Element not found. ) [Finished] Return

Step 4:

Procedure. CONCATE( ). This procedure join the two link list. PTR and PTR1 are pointer used to transverse through the list. HEAD is the pointer to the first node of the first linked list. HEAD1 is the pointer to the first node of the second linked list. Structure of node is as follows INFO GSSLL Step 1: [Concatenation of two different linked list] If (HEAD = NULL) Then Write( Please create your first linked list) Call CREATE( ) HEAD1 HEAD HEAD NULL Write( Please create your second linked list) Call CREATE( ) PTR HEAD1 Repeat While(LINK(PTR) NULL) PTR LINK(PTR) LINK(PTR) HEAD HEAD HEAD1 Call DISPLAY( ) [Finished] Return LINK

Step 2:

120

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct gssll { int info; struct gssll*link; }*head=NULL; void create( ); void ins_beg( ); void ins_bet( ); void ins_last( ); void del_beg( ); void del_bet( ); void del_last( ); void search( ); void concate( ); void display( ); void main( ) { int ch; char ans; while(1) { clrscr( ); printf("\n\n Implementing Grounded Simply Singly Linked List"); printf("\n----------------------------------------------- "); printf("\n\n\t 1- Creation of linked list"); printf("\n\t 2- Insertion of element in the beginning"); printf("\n\t 3- Insertion of element in between"); printf("\n\t 4- Insertion of element at the last"); printf("\n\t 5- Deletion of element from beginning"); printf("\n\t 6- Deletion of element from between"); printf("\n\t 7- Deletion of element from the last"); printf("\n\t 8- Searching of element in the list"); printf("\n\t 9- Concatenation of two different linked lists"); printf("\n\t 10- Display the elements in the list"); printf("\n\t 11- Exit from output window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d",&ch); switch(ch) { case 1: create( ); break; 121

case 2: ins_beg( ); break; case 3: ins_bet( ); break; case 4: ins_last( ); break; case 5: del_beg( ); break; case 6: del_bet( ); break; case 7: del_last( ); break; case 8: search( ); break; case 9: concate( ); break; case 10: display( ); break; case 11: printf("\n You have chosen to exit"); printf("\n Are you sure you want exit?"); printf("\n If yes press y."); fflush(stdin); scanf("%c",&ans); if(ans=='y'||ans=='Y') exit(0); else break; default: printf("\n\n Invalid choice..."); printf("\n Press any key to continue"); fflush(stdin); getch( ); break; } } } void create( ) { char ans; struct gssll *ptr, *ptr1; ptr1=(struct gssll*)malloc(sizeof(struct gssll)); if(!ptr1) printf("\n Memory is full. "); else { ptr1->link = NULL; printf("\n Enter information :- "); scanf("%d",&ptr1->info); if(head == NULL) head = ptr1; else { 122

ptr=head; while(ptr->link != NULL) ptr= ptr->link; ptr->link= ptr1; } printf("\n Do you have more information? "); printf("\n If yes press 'Y' :"); fflush(stdin); scanf("%c",&ans); if(ans== 'Y' || ans== 'y') create( ); } } void ins_beg( ) { struct gssll *ptr1; ptr1=(struct gssll*)malloc(sizeof(struct gssll)); if(!ptr1) printf("\n Memory is full."); else { ptr1->link= NULL; printf("\n Enter the information for new node :"); scanf("%d",&ptr1->info); ptr1->link = head; head=ptr1; } } void ins_bet( ) { int pos,count=1; struct gssll *ptr, *ptr1, *ptr2; ptr1=(struct gssll*)malloc(sizeof(struct gssll)); if(!ptr1) printf("\n Memory is full."); else { ptr1->link = NULL; printf("\n Enter the position where new node is to be inserted : "); scanf("%d",&pos); printf("\n Enter the information for new node : "); scanf("%d",&ptr1->info); ptr=head; while((ptr != head) && (count< pos-1)) { ptr= ptr->link; count++; } if(count== pos-1) { ptr2= ptr->link; 123

ptr->link = ptr1; ptr1->link = ptr2; } else { printf("\n The given position is out of range"); printf("\n Press any key "); fflush(stdin); getch( ); } } } void ins_last( ) { struct gssll *ptr, *ptr1; ptr1=(struct gssll*)malloc(sizeof(struct gssll)); if(!ptr1) printf("\n Memory is full."); else { ptr1->link = NULL; printf("\n Enter the information : "); scanf("%d",&ptr1->info); ptr = head; while(ptr->link != NULL) ptr = ptr->link; ptr->link = ptr1; } } void del_beg( ) { struct gssll *ptr; if(head == NULL) printf("\n Link list is empty."); else { ptr= head; head= ptr->link; printf("\n Element that was deleted is : %d",ptr->info); printf("\n Press any key"); fflush(stdin); getch( ); free(ptr); } } void del_bet( ) { struct gssll *ptr, *ptr1, *ptr2; int pos,count=1; if(head == NULL) 124

printf("\n Linked list is empty."); else { printf("\n Enter the position from where node is to be deleted : "); scanf("%d",&pos); ptr= head; while((ptr != NULL) && (count < pos-1)) { ptr= ptr->link; count++; } if(count== pos-1) { ptr1= ptr->link; ptr2= ptr1->link; ptr->link = ptr2; printf("\n Element deleted was : %d",ptr1->info); printf("\n Press any key"); fflush(stdin); getch( ); free(ptr1); } else { printf("\n Position given is out of range."); printf("\n Press any key."); fflush(stdin); getch( ); } } } void del_last( ) { struct gssll *ptr, *ptr1; if(head == NULL) printf("\n Linked list is empty."); else { ptr= head; while(ptr->link->link != NULL) ptr= ptr->link; ptr1= ptr->link; ptr->link = NULL; printf("\n Element deleted was : %d",ptr1->info); printf("\n Press any key."); fflush(stdin); getch( ); free(ptr1); } } void display( ) { 125

int count=1; struct gssll *ptr; ptr= head; if(ptr == NULL) printf("\n Linked list is empty."); else { printf("\n Elements in list are : \n"); while(ptr != NULL) { printf("\n\n Node : %d Element : %d",count,ptr->info); count++; ptr= ptr->link; } } printf("\n Press any key."); fflush(stdin); getch( ); } void search( ) { int ser=0,pos=1,flag=0; struct gssll *ptr; printf("\n Enter the element to be searched : "); scanf("%d",&ser); ptr = head; while(ptr != NULL) { if(ptr->info == ser) { flag=1; break; } pos++; ptr=ptr->link; } if(flag== 1) printf("\n Element found at %d position ",pos); else printf("\n Element not found. \n\n SEARCH UNSUCCESSFUL !!!"); getch( ); } void concate( ) { struct gssll *ptr, *head1; if(head == NULL) { printf("\n Please create your first linked list."); create( ); } head1= head; head= NULL; 126

printf("\n Please create your second link listed list."); create( ); ptr= head1; while(ptr->link != NULL) ptr= ptr->link; ptr->link =head; head= head1; display( ); }

127

Q23. Write pseudo code and program for implementing header simply singly link list. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show menu of various operations to be performed on the linked list. ANS is a character variable and CHOICE is an integer variable to hold selected option input. Structure of node is as follows INFO LINK HSSLL Step 1: [Displaying the menu ] Repeat While( True) Write( Implementing header simply linked list) Write( 1- Creation of linked list) Write( 2- Insertion of element in the beginning ) Write( 3- Insertion of element in between) Write( 4- Insertion of element in the last) Write( 5- Deletion of element from the beginning Write( 6- Deletion of element from between) Write( 7- Deletion of element from last) Write( 8- Searching of element in the list) Write( 9- Concatenation of two different linked list) Write( 10- Display of elements in the list) Write( 11- Exit from output window) Write( Enter your choice: ) Read(CHOICE) [Calling of functions for each operation] Select Case( CHOICE) Case 1: Call CREATE( ) Case 2: Call INS_BEG( ) Case 3: Call INS_BET( ) Case 4: Call INS_LAST( ) Case 5: Call DEL_BEG( ) Case 6: Call DEL_BET( ) Case 7: Call DEL_LAST( ) Case 8: Call SEARCH( ) Case 9: Call CONCATE( ) Case 10: Call DISPLAY( ) Case 11: Write( You have chosen to exit) Write( Are you sure you want to exit) Write( If yes press y ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then EXIT(0) [Finished] Exit

Step 2:

Step 3:

128

Procedure. CREATE( ).

This procedure creates grounded simply singly linked list with initially given information. This procedure is recursive in nature and Calls itself from within to add next information to the list. PTR and PTR1 are two pointers. PTR is used for traversing through the list and PTR1 is used to confirm addition of newly allocated nodes. ANS is a character variable used for checking the choice. HEAD is the pointer to the first node with initial value of NULL. Structure of node is as follows INFO HSSLL LINK

Step 1:

[Allocating memory for new node] PTR1 <= HSSLL If (PTR1 = NULL) Then Write( Memory is full ) [Entering the information] LINK(PTR1) NULL Write( Enter information: ) Read(INFO(PTR1)) [Inserting the node information linked list] If (HEAD.LINK = NULL) Then HEAD.LINK PTR1 Else PTR HEAD.LINK Repeat While(LINK(PTR) NULL) PTR LINK(PTR) LINK(PTR) PTR1

Step 2:

Step 3:

Step 4:

[Checking for additional data] Write( Do you have more information? ) Write( If yes press y: ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then Call CREATE( ) [Finished] Return

Step 5:

Procedure. INS_BEG( ). This procedure inserts given information in the beginning of list by linking the new node with the previously created node. PTR1 is a pointer used to confirm variable used for checking the choice. HEAD is the pointer to the first node. Structure of node is as follows INFO HSSLL 129 LINK

Step 1:

[Allocating memory for new node] PTR1 <= HSSLL If (PTR1 = NULL) Then Write( Memory is full ) Return [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) LINK(PTR1) NULL [Adding the new node to the list] LINK(PTR1) HAED HEAD.LINK PTR1 HEAD.INFO HEAD.INFO + 1 [Finished] Return This procedure inserts given information in between of list. PTR, PTR1and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next node. COUNT is an integer variable used for keeping track of current position. POS is the integer variable used for storing the entered position for insertion of node. HEAD is the pointer to the first node. Structure of node is as follows INFO HSSLL LINK

Step 2:

Step 3:

Step 4:

Procedure. INS_BET( ).

Step 1:

[Allocating memory for new node] COUNT 1 PTR1 <= HSSLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the position and information] Write( Enter the position where new node is to be inserted: ) Read(POS) Write( Enter information: ) Read(INFO(PTR1)) PTR HEAD.LINK [Inserting the new node] Repeat While((PTR NULL) and (COUNT < POS-1)) PTR LINK(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR2 LINK(PTR) LINK(PTR) PTR1 LINK(PTR1) PTR2 130

Step 2:

Step 3:

HEAD.INFO HEAD.INFO + 1 Else Write( The given position is out of range. ) Step 4: [Finished] Return

Procedure. INS_LAST( ). This procedure insert given information in last of list . PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. HEAD is the pointer to the first node. Structure of node is as follows INFO HSSLL Step 1: [Allocating memory for new node] PTR1 <= HSSLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the information] Write( Enter information: ) Read( INFO(PTR1)) LINK(PTR1) PTR1 [Inserting the new node] PTR HEAD.LINK Repeat While(LINK(PTR) NULL) PTR LINK(PTR) LINK(PTR) PTR1 HEAD.INFO HEAD.INFO + 1 [Finished] Return This procedure delete the element from the beginning of list. PTR is a pointer used to transverse through the list. HEAD is the pointer to the first node. Structure of node is as follows INFO HSSLL Step 1: [Checking memory] If (HEAD.LINK = NULL) Then Write( Link list is empty) [Deleting the node] 131 LINK LINK

Step 2:

Step 3:

Step 4:

Procedure. DEL_BEG( ).

Step 2:

PTR HEAD.LINK HEAD LINK(PTR) Write( Element that was deleted is: INFO(PTR)) Restore(PTR) HEAD.INFO HEAD.INFO - 1 Step 3: [Finished] Return

Procedure. DEL_BET( ). This procedure delete the element from between of list. PTR, PTR1 and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next nodes. COUNT is an integer variable used for keeping track of current position. POS is the integer variable used for storing the entered position for deletion of node. HEAD is the pointer which points to the first node. Structure of node is as follows INFO HSSLL Step 1: [Checking memory] COUNT 1 If (HEAD.LINK = NULL) Then Write( Link list is empty ) [Entering position for deletion] Write( Enter the position from where node is to be deleted:) Read(POS) PTR HEAD.LINK [Deleting the node] Repeat While((PTR1 NULL) and (COUNT < POS 1)) PTR LINK(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR1 LINK(PTR) PTR2 LINK(PTR1) LINK(PTR) PTR2 Write( Element deleted was: INFO(PTR1)) Restore(PTR1) HEAD.INFO HEAD.INFO 1 Else Step 4: Write( Position given is out of range ) [Finished] Return LINK

Step 2:

Step 3:

Procedure. DEL_LAST( ). This procedure delete the element from the last of list. PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. HEAD is the pointer to the first node. Structure of node is as follows 132

INFO HSSLL Step 1:

LINK

[Checking memory] If (HEAD.LINK = NULL) Then Write( Link list is empty ) [Deleting the node] PTR HEAD.LINK Repeat While(LINK(LINK(PTR)) NULL) PTR LINK(PTR) PTR1 LINK(PTR) LINK(PTR) NULL Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) HEAD.INFO HEAD.INFO -1 [Finished] Return

Step 2:

Step 3:

Procedure. DISPLAY( ). This procedure display the element of the list on the screen. PTR is a pointer used to transverse through the list. COUNT is an integer variable used for keeping track of current position. HEAD is the pointer to the first node. Structure of node is as follows INFO HSSLL Step 1: [Checking memory] COUNT 1 PTR HEAD.LINK If (PTR = NULL) Then Write( Linked list is empty) [Displaying elements on the screen] Write( Elements in the list are:) Repeat While(PTR NULL) Write( Node: COUNT, Element: INFO(PTR)) COUNT COUNT + 1 PTR LINK(PTR) [Finished] Return This procedure search the element from given the list. PTR is pointer used to transverse through the list. POS is an integer variable used for telling the position of the element in the list. SER is an integer variable used for storing the element that is to be searched. HEAD is the pointer to the first node. Structure of node is as follows 133 LINK

Step 2:

Step 3:

Procedure. SEARCH( ).

INFO HSSLL Step 1:

LINK

[Entering the element for search] POS 1 FLAG 0 Write(Enter the element to be searched: ) Read(SER) [Search for element] PTR HEAD.LINK Repeat While(PTR NULL) If (INFO(PTR) = SER) Then FLAG 1 POS POS + 1 PTR LINK(PTR) [Displaying the result] If (FLAG = 1) Then Write( Element found at position, POS) Else Write( Element not found. ) [Finished] Return

Step 2:

Step 3:

Step 4:

Procedure. CONCATE( ). This procedure join the two link list. PTR and PTR1 are pointer used to transverse through the list. HEAD is the pointer to the first node of the first linked list. HEAD1 is the pointer to the first node of the second linked list. Structure of node is as follows INFO Step 1: LINK

HSSLL [Concatenation of two different linked list] If (HEAD.LINK = NULL) Then Write( Please create your first linked list) Call CREATE( ) HEAD1.LINK HEAD.LINK HEAD NULL Write( Please create your second linked list) Call CREATE( ) PTR HEAD1.LINK Repeat While(LINK(PTR) NULL) PTR LINK(PTR) LINK(PTR) HEAD HEAD.LINK HEAD1.LINK Call DISPLAY( ) Step 2: [Finished] Return

134

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct hssll { int info; struct hssll*link; }head; void create( ); void ins_beg( ); void ins_bet( ); void ins_last( ); void del_beg( ); void del_bet( ); void del_last( ); void search( ); void concate( ); void display( ); void main( ) { int ch; char ans; head.info= 0; head.link= NULL; while(1) { clrscr( ); printf("\n\n Implementing Header Simply Linked List"); printf("\n----------------------------------------------- "); printf("\n\n\t 1- Creation of linked list"); printf("\n\t 2- Insertion of element in the beginning"); printf("\n\t 3- Insertion of element in between"); printf("\n\t 4- Insertion of element at the last"); printf("\n\t 5- Deletion of element from beginning"); printf("\n\t 6- Deletion of element from between"); printf("\n\t 7- Deletion of element from the last"); printf("\n\t 8- Searching of element in the list"); printf("\n\t 9- Concatenation of two different linked lists"); printf("\n\t 10- Display the elements in the list"); printf("\n\t 11- Exit from output window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d",&ch); switch(ch) { case 1: create( ); 135

break; case 2: ins_beg( ); break; case 3: ins_bet( ); break; case 4: ins_last( ); break; case 5: del_beg( ); break; case 6: del_bet( ); break; case 7: del_last( ); break; case 8: search( ); break; case 9: concate( ); break; case 10: display( ); break; case 11: printf("\n You have chosen to exit"); printf("\n Are you sure you want exit?"); printf("\n If yes press y."); fflush(stdin); scanf("%c",&ans); if(ans== 'y' || ans== 'Y') exit(0); else break; default: printf("\n\n Invalid choice..."); printf("\n Press any key to continue"); fflush(stdin); getch( ); break; } } } void create( ) { char ans; struct hssll *ptr, *ptr1; ptr1= (struct hssll*)malloc(sizeof(struct hssll)); if(!ptr1) printf("\n Memory is full. "); else { ptr1->link =NULL; printf("\n Enter information :- "); scanf("%d",&ptr1->info); if(head.link == NULL) head.link = ptr1; else { ptr = head.link; 136

while(ptr->link != NULL) ptr= ptr->link; ptr->link = ptr1; } head.info++; printf("\n Do you have more information? "); printf("\n If yes press 'Y' :"); fflush(stdin); scanf("%c",&ans); if(ans== 'Y' || ans== 'y') create( ); } } void ins_beg( ) { struct hssll *ptr1; ptr1=(struct hssll*)malloc(sizeof(struct hssll)); if(!ptr1) printf("\n Memory is full."); else { ptr1->link =NULL; printf("\n Enter the information for new node :"); scanf("%d",&ptr1->info); ptr1->link = head.link; head.link= ptr1; head.info++; } } void ins_bet( ) { int pos,count=1; struct hssll *ptr, *ptr1, *ptr2; ptr1=(struct hssll*)malloc(sizeof(struct hssll)); if(!ptr1) printf("\n Memory is full."); else { ptr1->link =NULL; printf("\n Enter the position where new node is to be inserted : "); scanf("%d",&pos); if(pos >= head.info) printf("\n Position given is out of range."); else { printf("\n Enter the information for new node : "); scanf("%d",&ptr1->info); ptr= head.link; while(count < pos-1) { ptr= ptr->link; 137

count++; } if(count == pos-1) { ptr2 = ptr->link; ptr->link =ptr1; ptr1->link =ptr2; head.info++; } } } } void ins_last( ) { struct hssll *ptr, *ptr1; ptr1=(struct hssll*)malloc(sizeof(struct hssll)); if(!ptr1) printf("\n Memory is full."); else { ptr1->link = NULL; printf("\n Enter the information : "); scanf("%d",&ptr1->info); ptr= head.link; while(ptr->link != NULL) ptr= ptr->link; ptr->link =ptr1; head.info++; } } void del_beg( ) { struct hssll *ptr; if(head.link == NULL) printf("\n Link list is empty."); else { ptr= head.link; head.link= ptr->link; printf("\n Element that was deleted is : %d",ptr->info); printf("\n Press any key"); fflush(stdin); getch( ); free(ptr); head.info--; } } void del_bet( ) { struct hssll *ptr, *ptr1, *ptr2; int pos,count= 1; if(head.link == NULL) 138

printf("\n Linked list is empty."); else { printf("\n Enter the position from where node is to be deleted : "); scanf("%d",&pos); if(pos >= head.info) printf("\n Position given is out of range."); else { ptr = head.link; while((ptr != NULL) && (count < pos-1)) { ptr= ptr->link; count++; } if(count == pos-1) { ptr1 = ptr->link; ptr2 = ptr1->link; ptr->link = ptr2; printf("\n Element deleted was : %d",ptr1->info); printf("\n Press any key"); fflush(stdin); getch( ); free(ptr1); head.info--; } } } } void del_last( ) { struct hssll *ptr, *ptr1; if(head.link == NULL) printf("\n Linked list is empty."); else { ptr= head.link; while(ptr->link->link != NULL) ptr = ptr->link; ptr1 = ptr->link; ptr->link = NULL; printf("\n Element deleted was : %d",ptr1->info); printf("\n Press any key."); fflush(stdin); getch( ); free(ptr1); head.info--; } } void display( ) { int count =1; 139

struct hssll *ptr; ptr= head.link; if(ptr == NULL) printf("\n Linked list is empty."); else { printf("\n Elements in list are : \n"); while(ptr != NULL) { printf("\n\n Node : %d Element : %d", count, ptr->info); count++; ptr= ptr->link; } } printf("\n Press any key."); fflush(stdin); getch( ); } void search( ) { int ser=0, pos=1, flag=0; struct hssll *ptr; printf("\n Enter the element to be searched : "); scanf("%d",&ser); ptr= head.link; while(ptr != NULL) { if(ptr->info == ser) { flag= 1; break; } pos++; ptr= ptr->link; } if(flag== 1) printf("\n Element found at %d position ",pos); else printf("\n Element not found. \n\n SEARCH UNSUCCESSFUL !!!"); getch( ); } void concate( ) { struct hssll *ptr,head1; if(head.link == NULL) { printf("\n Please create your first linked list."); create( ); } head1.link= head.link; head.link= NULL; printf("\n Please create your second link listed list."); create( ); 140

ptr= head1.link; while(ptr->link != NULL) ptr= ptr->link; ptr->link= head.link; head.link= head1.link; display( ); }

141

Q. 24. Ans.

Write pseudo code and program for implementing grounded circular singly linked list.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show menu of various operations to be performed on the linked list. ANS is a character variable and CHOICE is an integer variable to hold selected option input. Structure of node is as follows INFO GCSLL Step 1: [Displaying the menu ] Repeat While( True) Write( Implementing grounded circular singly linked list) Write( 1- Creation of linked list) Write( 2- Insertion of element in the beginning ) Write( 3- Insertion of element in between) Write( 4- Insertion of element in the last) Write( 5- Deletion of element from the beginning Write( 6- Deletion of element from between) Write( 7- Deletion of element from last) Write( 8- Searching of element in the list) Write( 9- Concatenation of two different linked list) Write( 10- Display of elements in the list) Write( 11- Exit from output window) Write( Enter your choice: ) Read(CHOICE) [Calling of functions for each operation] Select Case( CHOICE) Case 1: Call CREATE( ) Case 2: Call INS_BEG( ) Case 3: Call INS_BET( ) Case 4: Call INS_LAST( ) Case 5: Call DEL_BEG( ) Case 6: Call DEL_BET( ) Case 7: Call DEL_LAST( ) Case 8: Call SEARCH( ) Case 9: Call CONCATE( ) Case 10: Call DISPLAY( ) Case 11: Write( You have chosen to exit) Write( Are you sure you want to exit) Write( If yes press y ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then EXIT(0) [Finished] Exit 142 LINK

Step 2:

Step 3:

Procedure. CREATE( ).

This procedure creates grounded circular singly linked list with initially given information. This procedure is recursive in nature and Calls itself from within to add next information to the list. PTR and PTR1 are pointers. PTR is used for traversing through the list and PTR1 is used to confirm addition of newly allocated nodes. ANS is a character variable used for checking the choice. HEAD is the pointer to the first node with initial value of NULL. Structure of node is as follows INFO GCSLL LINK

Step 1:

[Allocating memory for new node] PTR1 <= GCSLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) [Inserting the node information linked list] If (HEAD = NULL) Then HEAD PTR1 LINK(PTR1) HEAD Else PTR HEAD Repeat While(LINK(PTR) HEAD) PTR LINK(PTR) LINK(PTR} PTR1 LINK(PTR1) HEAD [Checking for additional data] Write( Do you have more information?) Write( If yes press y: ) READ(ANS) If ((ANS = y ) or (ANS = Y)) Then Call CREATE( ) [Finished] Return

Step 2:

Step 3:

Step 4:

Step 5:

Procedure. INS_BEG( ). This procedure inserts given information in the beginning of list by linking the new node with the used to transverse through the list. PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly nodes. ANS is a character variable used for checking the choice. HEAD is the pointer to the first node. Structure of node is as follows INFO LINK 143

GCSLL Step 1: [Allocating memory for new node] PTR1 <= GCSLL If (PTR1 = NULL) Then Write( Memory is full ) Return [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) LINK(PTR1) HEAD [Adding the new node to the list] PTR HEAD Repeat While(LINK(PTR) HEAD) PTR LINK(PTR) LINK(PTR) PTR1 HEAD PTR1 [Finished] Return

Step 2:

Step 3:

Step 4:

Procedure. INS_BET( ).

This procedure inserts given information in between of list. PTR, PTR1and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to node next to be inserted node. COUNT is an integer variable used for keeping track of current position. POS is the integer variable used for storing the entered position for insertion of node. HEAD is the pointer to the first node. Structure of node is as follows INFO GCSLL Step 1: [Allocating memory for new node] COUNT 1 PTR1 <= GCSLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the position and information] Write( Enter the position where new node is to be inserted: ) Read(POS) Write( Enter information: ) Read(INFO(PTR1)) PTR HEAD [Inserting the new node] Repeat While((PTR NULL) and (COUNT < POS-1)) PTR LINK(PTR) 144 LINK

Step 2:

Step 3:

COUNT COUNT + 1 If (COUNT = POS -1) Then PTR2 LINK(PTR) LINK(PTR) PTR1 LINK(PTR1) PTR2 Else Write( The given position is out of range. ) Step 4: [Finished] Return

Procedure. INS_LAST( ). This procedure insert given information in last of list . PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. HEAD is the pointer to the first node. Structure of node is as follows INFO GCSLL Step 1: [Allocating memory for new node] PTR1 <= GCSLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the information] Write( Enter information: ) Read( INFO(PTR1)) [Inserting the new node] PTR HEAD Repeat While(LINK(PTR) NULL) PTR LINK(PTR) LINK(PTR) PTR1 LINK(PTR1) HEAD [Finished] Return LINK

Step 2:

Step 3:

Step 4:

Procedure. DEL_BEG( ). This procedure delete the element from the beginning of list. PTR is a pointer used to transverse through the list. HEAD is the pointer to the first node. Structure of node is as follows INFO GCSLL Step 1: [Checking memory] If (HEAD = NULL) Then Write( Link list is empty) 145 LINK

Step 2:

[Deleting the node] PTR HEAD PTR1 HEAD Write( Element that was deleted is: INFO(PTR)) Repeat While(LINK(PTR) HEAD) PTR LINK(PTR) HEAD LINK(PTR1) Restore(PTR1) [Finished] Return

Step 3:

Procedure. DEL_BET( ). This procedure delete the element from between of list. PTR, PTR1 and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to node next to the node to be deleted. COUNT is an integer variable used for keeping track of current position. POS is the integer variable used for storing the entered position for deletion of node. HEAD is the pointer which points to the first node. Structure of node is as follows INFO GCSLL Step 1: [Checking memory] COUNT 1 If (HEAD = NULL) Then Write( Link list is empty ) [Entering position for deletion] Write( Enter the position from where node is to be deleted:) Read(POS) PTR HEAD [Deleting the node] Repeat While((PTR1 NULL) and (COUNT < POS 1)) PTR LINK(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR1 LINK(PTR) PTR2 LINK(PTR1) LINK(PTR) PTR2 Write( Element deleted was: INFO(PTR1)) Restore(PTR1) Else Write( Position given is out of range ) Step 4: [Finished] Return 146 LINK

Step 2:

Step 3:

Procedure. DEL_LAST( ). This procedure delete the element from the last of list. PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm deletion of newly allocated nodes. HEAD is the pointer to the first node. Structure of node is as follows INFO GCSLL Step 1: [Checking memory] If (HEAD = NULL) Then Write( Link list is empty ) [Deleting the node] PTR HEAD If (LINK(PTR) = HEAD) Then HEAD NULL Write( Node to be deleted is INFO(PTR)) Restore(PTR) Else Repeat While(LINK(LINK(PTR)) NULL) PTR LINK(PTR) PTR1 LINK(PTR) LINK(PTR) NULL Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) [Finished] Return LINK

Step 2:

Step 3:

Procedure. DISPLAY( ). This procedure display the element of the list on the screen. PTR is a pointer used to transverse through the list. COUNT is an integer variable used for keeping track of current position. HEAD is the pointer to the first node. Structure of node is as follows INFO GCSLL Step 1: [Checking memory] COUNT 1 PTR HEAD If (PTR = NULL) Then Write( Linked list is empty) [Displaying elements on the screen] Write( Elements in the list are:) Repeat While(LINK(PTR) HEAD) Write( Node: COUNT, Element: INFO(PTR)) COUNT COUNT + 1 PTR LINK(PTR) 147 LINK

Step 2:

Step 3:

[Finished] Return This procedure search the element from given the list. PTR is pointer used to transverse through the list. POS is an integer variable used for telling the position of the element in the list. SER is an integer variable used for storing the element that is to be searched. HEAD is the pointer to the first node. Structure of node is as follows INFO GCSLL LINK

Procedure. SEARCH( ).

Step 1:

[Entering the element for search] POS 1 FLAG 0 Write(Enter the element to be searched: ) Read(SER) [Search for element] PTR HEAD Repeat While(PTR HEAD) If (INFO(PTR) = SER) Then FLAG 1 POS POS + 1 PTR LINK(PTR) [Displaying the result] If (FLAG = 1) Then Write( Element found at position, POS) Else Write( Element not found. ) [Finished] Return

Step 2:

Step 3:

Step 4:

Procedure. CONCATE( ). This procedure join the two link list. PTR and PTR1 are pointer used to transverse through the list. HEAD is the pointer to the first node of the first linked list. HEAD1 is the pointer to the first node of the second linked list. Structure of node is as follows INFO GCSLL Step 1: [Concatenation of two different linked list] If (HEAD = NULL) Then Write( Please create your first linked list) Call CREATE( ) HEAD1 HEAD. HEAD NULL Write( Please create your second linked list) 148 LINK

Call CREATE( ) PTR HEAD1. Repeat While(LINK(PTR) HEAD1) PTR LINK(PTR) LINK(PTR) HEAD PTR1 HEAD Repeat While(LINK(PTR) HEAD) PTR1 LINK(PTR1) LINK(PTR1) HEAD1 HEAD HEAD1 Call DISPLAY( ) Step 2: [Finished] Return

149

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct gcsll { int info; struct gcsll*link; }; struct gcsll*head=NULL; struct gcsll*link=NULL; void create( ); void ins_beg( ); void ins_bet( ); void ins_last( ); void del_beg( ); void del_bet( ); void del_last( ); void search( ); void concate( ); void display( ); void main( ) { int ch; char ans; while(1) { clrscr( ); printf("\n\t Implementing grounded circular singly linked list"); printf("\n\t----------------------------------------------------"); printf("\n\n\t 1- Creation of limked list"); printf("\n\t 2- Insertion of element in the beginning"); printf("\n\t 3- Insertion of element in the between"); printf("\n\t 4- Insertion of element at the last"); printf("\n\t 5- Deletion of element from the beginning"); printf("\n\t 6- Deletion of element from between"); printf("\n\t 7- Deletion of element from the last"); printf("\n\t 8- Searching of element in the list"); printf("\n\t 9- Concatenation of two different linked lists"); printf("\n\t 10- Display of elements in the list"); printf("\n\t 11- Exit from output window"); printf("\n\n Enter your choice:- "); scanf("%d",&ch); switch(ch) { case 1: create( ); 150

break; case 2: ins_beg( ); break; case 3: ins_bet( ); break; case 4: ins_last( ); break; case 5: del_beg( ); break; case 6: del_bet( ); break; case 7: del_last( ); break; case 8: search( ); break; case 9: concate( ); break; case 10: display( ); break; case 11: printf("\n You have chosen to exit"); printf("\nR you sure you want to exit?If yes press y."); fflush(stdin); scanf("%c",&ans); if(ans == 'y' || ans ==' Y') exit(0); else break; default: printf("\n\n Invalid choice..."); printf("\n Press any key to continue"); fflush(stdin); getch( ); break; } } } void create( ) { char ans; struct gcsll *ptr,*ptr1; ptr1=(struct gcsll*)malloc(sizeof(struct gcsll)); if(!ptr1) 151

printf("\n Memory is full"); else { printf("\n Enter information:"); scanf("%d",&ptr1->info); if(head == NULL) { head= ptr1; ptr1->link =head; } else { ptr= head; while(ptr->link != head) ptr= ptr->link; ptr->link = ptr1; } ptr1->link =head; printf("\n Do you have more information?"); printf("\n If yes press 'Y':"); fflush(stdin); scanf("%c",&ans); if(ans == 'y' || ans == 'Y') create( ); } } void ins_beg( ) { struct gcsll*ptr1,*ptr; ptr1=(struct gcsll*)malloc(sizeof(struct gcsll)); if(!ptr1) printf("\n Memory is full... "); else { printf("\n Enter the information for new node: "); scanf("%d",&ptr1->info); ptr1->link = head; ptr=head; while(ptr->link != head) ptr= ptr->link; ptr->link = ptr1; head= ptr1; } } void ins_bet( ) { int pos,count=1; struct gcsll*ptr,*ptr1,*ptr2; ptr1=(struct gcsll*)malloc(sizeof(struct gcsll)); 152

if(!ptr1) printf("\n Memory is full"); else { printf("\n Enter the position where new node is to be inserted:"); scanf("%d",&pos); printf("\n Enter the information for new node:"); scanf("%d",&ptr1->info); ptr= head; while((ptr->link != head) && (coun t< pos-1)) { ptr= ptr->link; count++; } if(count== pos-1) { ptr2= ptr->link; ptr->link = ptr1; ptr1->link = ptr2; } else { printf("\n The given position is out of range"); printf("\n Press any key"); fflush(stdin); getch( ); } } } void ins_last( ) { struct gcsll*ptr,*ptr1; ptr1=(struct gcsll*)malloc(sizeof(struct gcsll)); if(!ptr1) printf("\n Memeory is full"); else { printf("\n Enter the information:"); scanf("%d",&ptr1->info); ptr= head; while(ptr->link != head) ptr= ptr->link; ptr->link = ptr1; ptr1->link = head; } } void del_beg( ) { struct gcsll*ptr,*ptr1; if(head== NULL) printf("\n Link list is empty"); 153

else { ptr= head; ptr1= head; printf("\n Element that was deleted is: %d",ptr->info); printf("\n Press any key"); fflush(stdin); getch( ); while(ptr->link != head) ptr= ptr->link; head= ptr1->link; ptr->link = ptr1->link; free(ptr1); } } void del_bet( ) { struct gcsll*ptr,*ptr1,*ptr2; int pos,count=1; if(head== NULL) printf("\n Linked list is emepty"); else { printf("\n Enter the position from where node is to be deleted:"); scanf("%d",&pos); ptr=head; while((ptr->link != head) && (count < pos-1)) { ptr= ptr->link; count++; } if(count== pos-1) { ptr1= ptr->link; ptr2= ptr1->link; ptr->link = ptr2; printf("\n Element deleted was :%d",ptr1->info); printf("\n Press any key"); fflush(stdin); getch( ); free(ptr1); } else { printf("\n Position given is out of range"); printf("\n Press any key"); fflush(stdin); getch( ); } } } 154

void del_last( ) { struct gcsll *ptr,*ptr1; if(head== NULL) printf("\n Linked list is empty"); else { ptr= head; if(ptr->link== head) { head= NULL; printf("\n Node to be deleted is : %d",ptr->info); fflush(stdin); getch( ); free(ptr); } else { while(ptr->link->link != head) ptr= ptr->link; ptr1= ptr->link; ptr->link = head; printf("\n Element deleted was :%d",ptr1->info); fflush(stdin); getch( ); free(ptr1); } } } void display( ) { int count=1; struct gcsll *ptr; ptr= head; if(ptr== NULL) printf("\n Linked list is empty"); else { printf("\n\n Elements in the list are: \n"); while(ptr->link != head) { printf("\n\n Node: %d Element : %d",count,ptr->info); count++; ptr= ptr->link; } printf("\n\n Node :%d Element :%d",count,ptr->info); } printf("\n Press any key to continue"); fflush(stdin); getch( ); } 155

void search( ) { int ser,pos=1,flag=0; struct gcsll *ptr; printf("\n Enter the element to be searched: "); scanf("%d",&ser); ptr= head; while(ptr->link != head) { if(ptr->info == ser) { flag= 1; break; } pos++; ptr= ptr->link; } if(flag == 1) printf("\n Element found at %d position ",pos); else printf("\n Element not found. \n\n SEARCH UNSUCCESSFUL !!! "); getch( ); } void concate( ) { struct gcsll *ptr,*ptr1,*head1; if(head == NULL) { printf("\n Please create your first linked list"); create( ); } head1=head; head=NULL; printf("\n Please create your second linked list"); create( ); ptr=head1; while(ptr->link != head1) ptr= ptr->link; ptr->link= head; ptr1= head; while(ptr1->link != head) ptr1=ptr1->link; ptr1->link=head1; head=head1; display( ); getch( ); }

156

Q. 25. Ans.

Write pseudo code and progeam for implementing Header circular singly linked list.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show menu of various operations to be performed on the linked list. ANS is a character variable and CHOICE is an integer variable to hold selected option input. Structure of node is as follows INFO HCSLL Step 1: [Displaying the menu ] Repeat While( True) Write( Implementing header circular singly linked list) Write( 1- Creation of linked list) Write( 2- Insertion of element in the beginning ) Write( 3- Insertion of element in between) Write( 4- Insertion of element in the last) Write( 5- Deletion of element from the beginning Write( 6- Deletion of element from between) Write( 7- Deletion of element from last) Write( 8- Searching of element in the list) Write( 9- Concatenation of two different linked list) Write( 10- Display of elements in the list) Write( 11- Exit from output window) Write( Enter your choice: ) Read(CHOICE) [Calling of functions for each operation] Select Case( CHOICE) Case 1: Call CREATE( ) Case 2: Call INS_BEG( ) Case 3: Call INS_BET( ) Case 4: Call INS_LAST( ) Case 5: Call DEL_BEG( ) Case 6: Call DEL_BET( ) Case 7: Call DEL_LAST( ) Case 8: Call SEARCH( ) Case 9: Call CONCATE( ) Case 10: Call DISPLAY( ) Case 11: Write( You have chosen to exit) Write( Are you sure you want to exit) Write( If yes press y ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then EXIT(0) [Finished] Exit 157 LINK

Step 2:

Step 3:

Procedure. CREATE( ).

This procedure creates grounded circular singly linked list with initially given information. This procedure is recursive in nature and Calls itself from within to add next information to the list. PTR and PTR1 are pointers. PTR is used for traversing through the list and PTR1 is used to confirm addition of newly allocated nodes. ANS is a character variable used for checking the choice. HEAD.LINK is the pointer to the first node with initial value of NULL. Structure of node is as follows INFO HCSLL LINK

Step 1:

[Allocating memory for new node] PTR1 <= HCSLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) [Inserting the node information linked list] If (HEAD.LINK = NULL) Then HEAD PTR1 LINK(PTR1) HEAD.LINK Else PTR HEAD.LINK Repeat While(LINK(PTR) HEAD) PTR LINK(PTR) LINK(PTR) PTR1 LINK(PTR1) HEAD.LINK HEAD.INFO HEAD.INFO + 1 [Checking for additional data] Write( Do you have more information?) Write( If yes press y: ) READ(ANS) If ((ANS = y ) or (ANS = Y)) Then Call CREATE( ) [Finished] Return

Step 2:

Step 3:

Step 4:

Step 5:

Procedure. INS_BEG( ). This procedure inserts given information in the beginning of list by linking the new node with the used to transverse through the list. PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly nodes. ANS is a character variable used for checking the choice. HEAD.LINK is the pointer to the first node. Structure of node is as follows 158

INFO HCSLL Step 1: [Allocating memory for new node] PTR1 <= HCSLL If (PTR1 = NULL) Then Write( Memory is full ) Return [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) LINK(PTR1) HEAD.LINK

LINK

Step 2:

Step 3:

[Adding the new node to the list] PTR HEAD Repeat While(LINK(PTR) HEAD.LINK) PTR LINK(PTR) LINK(PTR) PTR1 HEAD.LINK PTR1 HEAD.INKO HEAD.INFO + 1 [Finished] Return This procedure inserts given information in between of list. PTR, PTR1and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to node next to be inserted node. COUNT is an integer variable used for keeping track of current position. POS is the integer variable used for storing the entered position for insertion of node. HEAD.LINK is the pointer to the first node. Structure of node is as follows INFO HCSLL LINK

Step 4:

Procedure. INS_BET( ).

Step 1:

[Allocating memory for new node] COUNT 1 PTR1 <= HCSLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the position and information] Write( Enter the position where new node is to be inserted: ) Read(POS) Write( Enter information: ) Read(INFO(PTR1)) PTR HEAD.LINK [Inserting the new node] 159

Step 2:

Step 3:

Repeat While((PTR NULL) and (COUNT < POS-1)) PTR LINK(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR2 LINK(PTR) LINK(PTR) PTR1 LINK(PTR1) PTR2 HEAD.INFO HEAD.INFO + 1 Else Write( The given position is out of range. ) Step 4: [Finished] Return

Procedure. INS_LAST( ). This procedure insert given information in last of list . PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. HEAD.LINK is the pointer to the first node. Structure of node is as follows INFO HCSLL Step 1: [Allocating memory for new node] PTR1 <= HCSLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the information] Write( Enter information: ) Read( INFO(PTR1)) [Inserting the new node] PTR HEAD.LINK Repeat While(LINK(PTR) NULL) PTR LINK(PTR) LINK(PTR) PTR1 LINK(PTR1) HEAD.LINK HEAD.INFO HEAD.INFO + 1 [Finished] Return LINK

Step 2:

Step 3:

Step 4:

Procedure. DEL_BEG( ). This procedure delete the element from the beginning of list. PTR is a pointer used to transverse through the list. HEAD.LINK is the pointer to the first node. Structure of node is as follows INFO HCSLL 160 LINK

Step 1:

[Checking memory] If (HEAD.LINK = NULL) Then Write( Link list is empty) [Deleting the node] PTR HEAD.LINK PTR1 HEAD.LINK Write( Element that was deleted is: INFO(PTR)) Repeat While(LINK(PTR) HEAD.LINK) PTR LINK(PTR) HEAD.LINK LINK(PTR1) LINK(PTR) LINK(PTR1) Restore(PTR1) HEAD.INFO HEAD.INFO - 1 [Finished] Return

Step 2:

Step 3:

Procedure. DEL_BET( ). This procedure delete the element from between of list. PTR, PTR1 and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to node next to the node to be deleted. COUNT is an integer variable used for keeping track of current position. POS is the integer variable used for storing the entered position for deletion of node. HEAD.LINK is the pointer which points to the first node. Structure of node is as follows INFO HCSLL Step 1: [Checking memory] COUNT 1 If (HEAD.LINK = NULL) Then Write( Link list is empty ) [Entering position for deletion] Write( Enter the position from where node is to be deleted:) Read(POS) PTR HEAD [Deleting the node] Repeat While((PTR1 HEAD.LINK) and (COUNT < POS 1)) PTR LINK(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR1 LINK(PTR) PTR2 LINK(PTR1) LINK(PTR) PTR2 Write( Element deleted was: INFO(PTR1)) Restore(PTR1) HEAD.LINK HEAD.LINK - 1 Else 161 LINK

Step 2:

Step 3:

Write( Position given is out of range ) Step 4: [Finished] Return

Procedure. DEL_LAST( ). This procedure delete the element from the last of list. PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm deletion of newly allocated nodes. HEAD.LINK is the pointer to the first node. Structure of node is as follows INFO HCSLL Step 1: [Checking memory] If (HEAD.LINK = NULL) Then Write( Link list is empty ) [Deleting the node] PTR HEAD.LINK If (LINK(PTR) = HEAD) Then HEAD NULL Write( Node to be deleted is INFO(PTR)) Restore(PTR) HEAD.LINK HEAD.LINK - 1 Else Repeat While(LINK(LINK(PTR)) HEAD.LINK) PTR LINK(PTR) PTR1 LINK(PTR) LINK(PTR) HEAD.LINK Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) HEAD.INFO HEAD.INFO - 1 [Finished] Return LINK

Step 2:

Step 3:

Procedure. DISPLAY( ). This procedure display the element of the list on the screen. PTR is a pointer used to transverse through the list. COUNT is an integer variable used for keeping track of current position. HEAD.LINK is the pointer to the first node. Structure of node is as follows INFO HCSLL Step 1: [Checking memory] COUNT 1 PTR HEAD If (PTR = NULL) Then Write( Linked list is empty) 162 LINK

Step 2:

[Displaying elements on the screen] Write( Elements in the list are:) Repeat While(LINK(PTR) HEAD.LONK) Write( Node: COUNT, Element: INFO(PTR)) COUNT COUNT + 1 PTR LINK(PTR) [Finished] Return

Step 3:

Procedure. SEARCH( ). This procedure search the element from given the list. PTR is pointer used to transverse through the list. POS is an integer variable used for telling the position of the element in the list. SER is an integer variable used for storing the element that is to be searched. HEAD.LINK is the pointer to the first node. Structure of node is as follows INFO HCSLL Step 1: [Entering the element for search] POS 1 FLAG 0 Write(Enter the element to be searched: ) Read(SER) [Search for element] PTR HEAD.LINK Repeat While(PTR HEAD.LINK) If (INFO(PTR) = SER) Then FLAG 1 POS POS + 1 PTR LINK(PTR) [Displaying the result] If (FLAG = 1) Then Write( Element found at position, POS) Else Write( Element not found. ) [Finished] Return LINK

Step 2:

Step 3:

Step 4:

Procedure. CONCATE( ). This procedure join the two link list. PTR and PTR1 are pointer used to transverse through the list. HEAD is the pointer to the first node of the first linked list. HEAD1 is the pointer to the first node of the second linked list. Structure of node is as follows INFO HCSLL Step 1: [Concatenation of two different linked list] 163 LINK

If (HEAD.LINK = NULL) Then Write( Please create your first linked list) Call CREATE( ) HEAD1.LINK HEAD.LINK. HEAD NULL Write( Please create your second linked list) Call CREATE( ) PTR HEAD1.LINK. Repeat While(LINK(PTR) HEAD1.LINK) PTR LINK(PTR) LINK(PTR) HEAD.LINK PTR1 HEAD.LINK Repeat While(LINK(PTR1) HEAD.LINK) PTR1 LINK(PTR1) LINK(PTR1) HEAD1.LINK HEAD.LINK HEAD1.LINK Call DISPLAY( ) Step 2: [Finished] Return

164

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct hcsll { int info; struct hcsll*link; }; struct hcsll head; void create( ); void ins_beg( ); void ins_bet( ); void ins_last( ); void del_beg( ); void del_bet( ); void del_last( ); void search( ); void concate( ); void display( ); void main( ) { int ch; char ans; head.info= 0; head.link= NULL; while(1) { clrscr( ); printf("\n\t Implementing Header Circular Singly Linked List"); printf("\n\t----------------------------------------------------"); printf("\n\n\t 1- Creation of limked list"); printf("\n\t 2- Insertion of element in the beginning"); printf("\n\t 3- Insertion of element in the between"); printf("\n\t 4- Insertion of element at the last"); printf("\n\t 5- Deletion of element from the beginning"); printf("\n\t 6- Deletion of element from between"); printf("\n\t 7- Deletion of element from the last"); printf("\n\t 8- Searching of element in the list"); printf("\n\t 9- Concatenation of two different linked lists"); printf("\n\t 10- Display of elements in the list"); printf("\n\t 11- Exit from output window"); printf("\n\n Enter your choice:- "); scanf("%d",&ch); switch(ch) { 165

case 1: create( ); break; case 2: ins_beg( ); break; case 3: ins_bet( ); break; case 4: ins_last( ); break; case 5: del_beg( ); break; case 6: del_bet( ); break; case 7: del_last( ); break; case 8: search( ); break; case 9: concate( ); break; case 10: display(); break; case 11: printf("\n You have chosen to exit"); printf("\nR you sure you want to exit?If yes press y."); fflush(stdin); scanf("%c",&ans); if(ans=='y'||ans=='Y') exit(0); else break; default: printf("\n\n Invalid choice..."); printf("\n Press any key to continue"); fflush(stdin); getch( ); break; } } } void create( ) { char ans; struct hcsll *ptr,*ptr1; ptr1=(struct hcsll*)malloc(sizeof(struct hcsll)); if(!ptr1) printf("\n Memory is full"); else { printf("\n Enter information:"); scanf("%d",&ptr1->info); if(head.link == NULL) { head.link= ptr1; ptr1->link = head.link; } 166

else { ptr= head.link; while(ptr->link != head.link) ptr= ptr->link; ptr->link=ptr1; } ptr1->link = head.link; head.info++; printf("\n Do you have more information?"); printf("\n If yes press 'Y':"); fflush(stdin); scanf("%c",&ans); if(ans == 'y' || ans == 'Y') create( ); }

} void ins_beg( ) { struct hcsll*ptr1,*ptr; ptr1=(struct hcsll*)malloc(sizeof(struct hcsll)); if(!ptr1) printf("\n Memory is full... "); else { printf("\n Enter the information for new node: "); scanf("%d",&ptr1->info); ptr1->link = head.link; ptr= head.link; while(ptr->link != head.link) ptr= ptr->link; ptr->link= ptr1; head.link= ptr1; head.info++; } } void ins_bet( ) { int pos,count=1; struct hcsll*ptr,*ptr1,*ptr2; ptr1=(struct hcsll*)malloc(sizeof(struct hcsll)); if(!ptr1) printf("\n Memory is full"); else { printf("\n Enter the position where new node is to be inserted:"); scanf("%d",&pos); printf("\n Enter the information for new node:"); scanf("%d",&ptr1->info); ptr= head.link; while((ptr->link != head.link) && (count < pos-1)) { ptr= ptr->link; count++; 167

} if(count== pos-1) { ptr2= ptr->link; ptr->link = ptr1; ptr1->link = ptr2; head.info++; } else { printf("\n The given position is out of range"); printf("\n Press any key"); fflush(stdin); getch( ); } } } void ins_last( ) { struct hcsll*ptr,*ptr1; ptr1=(struct hcsll*)malloc(sizeof(struct hcsll)); if(!ptr1) printf("\n Memeory is full"); else { printf("\n Enter the information:"); scanf("%d",&ptr1->info); ptr= head.link; while(ptr->link != head.link) ptr= ptr->link; ptr->link = ptr1; ptr1->link = head.link; head.info++; } } void del_beg( ) { struct hcsll*ptr,*ptr1; if(head.link == NULL) printf("\n Link list is empty"); else { ptr= head.link; ptr1= head.link; printf("\n Element that was deleted is: %d",ptr->info); printf("\n Press any key"); fflush(stdin); getch( ); while(ptr->link != head.link) ptr= ptr->link; head.link= ptr1->link; 168

ptr->link = ptr1->link; free(ptr1); head.info--; } } void del_bet( ) { struct hcsll*ptr,*ptr1,*ptr2; int pos,count= 1; if(head.link == NULL) printf("\n Linked list is emepty"); else { printf("\n Enter the position from where node is to be deleted:"); scanf("%d",&pos); ptr=head.link; while((ptr->link != head.link) && (count < pos-1)) { ptr= ptr->link; count++; } if(count== pos-1) { ptr1= ptr->link; ptr2= ptr1->link; ptr->link = ptr2; printf("\n Element deleted was :%d",ptr1->info); printf("\n Press any key"); fflush(stdin); getch( ); free(ptr1); head.info--; } else { printf("\n Position given is out of range"); printf("\n Press any key"); fflush(stdin); getch( ); } } } void del_last( ) { struct hcsll *ptr,*ptr1; if(head.link == NULL) printf("\n Linked list is empty"); else { ptr=head.link; if(ptr->link == head.link) 169

{ head.link= NULL; printf("\n Node to be deleted is : %d",ptr->info); fflush(stdin); getch( ); free(ptr); head.info--; } else { while(ptr->link->link!=head.link) ptr= ptr->link; ptr1= ptr->link; ptr->link= head.link; printf("\n Element deleted was :%d",ptr1->info); fflush(stdin); getch( ); free(ptr1); } } } void display( ) { int count=1; struct hcsll *ptr; ptr= head.link; if(ptr == NULL) printf("\n Linked list is empty"); else { printf("\n\n Elements in the list are: \n"); while(ptr->link != head.link) { printf("\n\n Node: %d Element : %d",count,ptr->info); count++; ptr= ptr->link; } printf("\n\n Node :%d Element :%d",count,ptr->info); } printf("\n Press any key to continue"); fflush(stdin); getch( ); } void search( ) { int ser,pos=1,flag=0; struct hcsll *ptr; printf("\n Enter the element to be searched: "); scanf("%d",&ser); ptr=head.link; while(ptr->link != head.link) 170

{ if(ptr->info == ser) { flag= 1; break; } pos++; ptr= ptr->link; } if(flag== 1) printf("\n Element found at %d position ",pos); else printf("\n Element not found. \n\n SEARCH UNSUCCESSFUL !!! "); getch( ); } void concate( ) { struct hcsll *ptr,*ptr1,head1; if(head.link== NULL) { printf("\n Please create your first linked list"); create( ); } head1.link= head.link; head.link= NULL; printf("\n Please create your second linked list"); create( ); ptr= head1.link; while(ptr->link != head1.link) ptr= ptr->link; ptr->link= head.link; ptr1= head.link; while(ptr1->link != head.link) ptr1= ptr1->link; ptr1->link= head1.link; head.link= head1.link; display( ); getch( ); }

171

Q.26 Write pseudo code and program for implementing grounded simply doubly linked list Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show menu of various operations to be performed on the linked list. ANS is a character variable and CHOICE is an integer variable to hold selected option input. Structure of node is as follows LEFT GSDLL Step 1: INFO RIGHT

[Displaying the menu ] Repeat While( True) Write( Implementing grounded simply doubly linked list) Write( 1- Creation of linked list) Write( 2- Insertion of element in the beginning ) Write( 3- Insertion of element in between) Write( 4- Insertion of element in the last) Write( 5- Deletion of element from the beginning Write( 6- Deletion of element from between) Write( 7- Deletion of element from last) Write( 8- Searching of element in the list) Write( 9- Concatenation of two different linked list) Write( 10- Display of elements in the list) Write( 11- Exit from output window) Write( Enter your choice: ) Read(CHOICE) [Calling of functions for each operation] Select Case( CHOICE) Case 1: Call CREATE( ) Case 2: Call INS_BEG( ) Case 3: Call INS_BET( ) Case 4: Call INS_LAST( ) Case 5: Call DEL_BEG( ) Case 6: Call DEL_BET( ) Case 7: Call DEL_LAST( ) Case 8: Call SEARCH( ) Case 9: Call CONCATE( ) Case 10: Call DISPLAY( ) Case 11: Write( You have chosen to exit) Write( Are you sure you want to exit) Write( If yes press y ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then EXIT(0) [Finished] Exit 172

Step 2:

Step 3:

Procedure. CREATE( ). This procedure creates grounded simply doubly linked list with initially given information. This procedure is recursive in nature and Calls itself from within to add next information to the list. PTR and PTR1 are two pointers. PTR is used for traversing through the list and PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes ANS is a character variable used for checking the choice. HEAD is the pointer to the first node with initial value of NULL. Structure of node is as follows LEFT GSDLL Step 1: INFO RIGHT

[Allocating memory for new node] PTR1 <= GSDLL If (PTR1 = NULL) Then Write( Memory is full ) [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) LEFT(PTR1) RIGHT(PTR1) NULL [Inserting the node information linked list] If (HEAD = NULL) Then HEAD PTR1 Else PTR HEAD Repeat While(RIGHT(PTR) NULL) PTR RIGHT(PTR) RIGHT(PTR) PTR1 LEFT(PTR1) PTR

Step 2:

Step 3:

Step 4:

[Checking for additional data] Write( Do you have more information? ) Write( If yes press y: ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then Call CREATE( ) [Finished] Return

Step 5:

Procedure. INS_BEG( ). This procedure inserts given information in the beginning of list by linking the new node with the previously created node. PTR is used to traverse through the list.PTR1 is a pointer used to confirm variable used for checking the choice. LEFT and RIGHT are pointer used to confirm addition of newly allocated nodes. ANS is a character variable used for checking the choice. HEAD is the pointer to the first node. Structure of node is as follows LEFT INFO RIGHT GSDLL Step 1: [Allocating memory for new node] 173

PTR1 <= GSDLL If (PTR1 = NULL) Then Write( Memory is full ) Return Step 2: [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) LEFT(PTR1) RIGHT(PTR1) NULL Step 3: [Adding the new node to the list] PTR HEAD RIGHT(PTR1) HEAD LEFT(PTR) PTR1 HEAD PTR1 Step 4: [Finished] Return

Procedure. INS_BET( ). This procedure inserts given information in between of list. PTR, PTR1and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next node. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. POS is the integer variable used for storing the entered position for insertion of node. HEAD is the pointer to the first node. Structure of node is as follows LEFT GSDLL Step 1: INFO RIGHT

[Allocating memory for new node] COUNT 1 PTR1 <= GSDLL If (PTR1 = NULL) Then Write( Memory is full) Return Step 2: [Entering the position and information] Write( Enter the position where new node is to be inserted: ) Read(POS) Write( Enter information: ) Read(INFO(PTR1)) PTR HEAD LEFT(PTR1) RIGHT(PTR1) NULL Step 3: [Inserting the new node] Repeat While((RIGHT(PTR) NULL) and (COUNT < POS-1)) PTR RIGHT(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR2 RIGHT(PTR) RIGHT(PTR) PTR1 RIGHT(PTR1) PTR2 LEFT(PTR2) PTR1 LEFT(PTR1) PTR 174

Else Write( The given position is out of range. ) Step 4: [Finished] Return Procedure. INS_LAST( ). This procedure inserts given information in last of list . PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. HEAD is the pointer to the first node. Structure of node is as follows LEFT GSDLL Step 1: INFO RIGHT

[Allocating memory for new node] PTR1 <= GSDLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the information] Write( Enter information: ) Read( INFO(PTR1)) LEFT(PTR1) RIGHT(PTR1) NULL [Inserting the new node] PTR HEAD Repeat While(RIGHT(PTR) NULL) PTR RIGHT(PTR) RIGHT(PTR) PTR1 LEFT(PTR1) PTR [Finished] Return

Step 2:

Step 3:

Step 4:

Procedure. DEL_BEG( ). This procedure delete the element from the beginning of list. PTR is a pointer used to transverse through the list. PTR1 is used to check deletion of node. LEFT and RIGHT are pointes used to store addresses of neighbouring nodes. HEAD is the pointer to the first node. Structure of node is as follows LEFT GSDLL Step 1: INFO RIGHT

[Checking memory] If (HEAD = NULL) Then Write( Link list is empty) [Deleting the node] PTR1 HEAD Write( Element that was deleted is: INFO(PTR)) PTR RIGHT(PTR1) 175

Step 2:

HEAD PTR LEFT(PTR) NULL Restore(PTR) Step 3: [Finished] Return

Procedure. DEL_BET( ). This procedure delete the element from between of list. PTR, PTR1 and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next nodes. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighbouring node. POS is the integer variable used for storing the entered position for deletion of node. HEAD is the pointer which points to the first node. Structure of node is as follows LEFT GSDLL Step 1: INFO RIGHT

[Checking memory] PTR HEAD COUNT 1 If (HEAD = NULL) Then Write( Link list is empty ) [Entering position for deletion] Write( Enter the position from where node is to be deleted:) Read(POS) [Deleting the node] Repeat While((RIGHT(PTR) NULL) and (COUNT < POS 1)) PTR RIGHT(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR1 RIGHT(PTR) PTR2 RIGHT(PTR1) RIGHT(PTR) PTR2 LEFT(PTR2) PTR Write( Element deleted was: INFO(PTR1)) Restore(PTR1) Else Write( Position given is out of range ) [Finished] Return

Step 2:

Step 3:

Step 4:

Procedure. DEL_LAST( ). This procedure delete the element from the last of list. PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of beighbouring nodes. HEAD is the pointer to the first node. Structure of node is as follows 176

LEFT INFO RIGHT GSDLL Step 1: [Checking memory] If (HEAD = NULL) Then Write( Link list is empty ) Step 2: [Deleting the node] PTR1 HEAD If (RIGHT(PTR1) = NULL) Then HEAD NULL Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) Else PTR HEAD Repeat While(RIGHT(RIGHT(PTR)) NULL) PTR RIGHT(PTR) PTR1 RIGHT(PTR) RIGHT(PTR) NULL Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) Step 3: [Finished] Return Procedure. DISPLAY( ). This procedure display the element of the list on the screen. PTR is a pointer used to transverse through the list. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. HEAD is the pointer to the first node. Structure of node is as follows LEFT GSDLL Step 1: INFO RIGHT

[Checking memory] COUNT 1 PTR NULL PTR HEAD If (PTR = NULL) Then Write( Linked list is empty) [Displaying elements on the screen] Write( Elements in the list are:) Repeat While(RIGHT(PTR) NULL) Write( Node: COUNT, Element: INFO(PTR)) COUNT COUNT + 1 PTR RIGHT(PTR) [Finished] Return

Step 2:

Step 3:

Procedure. SEARCH( ). This procedure search the element from given the list. PTR is pointer used to transverse through the list. POS is an integer variable used for telling the position of the element in the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. SER is an integer 177

variable used for storing the element that is to be searched. HEAD is the pointer to the first node. Structure of node is as follows LEFT INFO RIGHT GSDLL [Entering the element for search] POS 1 FLAG 0 Write(Enter the element to be searched: ) Read(SER) [Search for element] PTR HEAD Repeat While(PTR NULL) If (INFO(PTR) = SER) Then FLAG 1 POS POS + 1 PTR RIGHT(PTR) [Displaying the result] If (FLAG = 1) Then Write( Element found at position, POS) Else Write( Element not found. ) [Finished] Return

Step 1:

Step 2:

Step 3:

Step 4:

Procedure. CONCATE( ). This procedure join the two link list. PTR and PTR1 are pointer used to transverse through the list. LEFT and RIGHT rae pointers used to store addresses of neighbouring nodes. HEAD is the pointer to the first node of the first linked list. HEAD1 is the pointer to the first node of the second linked list. Structure of node is as follows LEFT INFO RIGHT GSDLL Step 1: [Concatenation of two different linked list] If (HEAD = NULL) Then Write( Please create your first linked list) Call CREATE( ) HEAD1 HEAD HEAD NULL PTR1 HEAD Write( Please create your second linked list) Call CREATE( ) PTR HEAD1 Repeat While(RIGHT(PTR) NULL) PTR RIGHT(PTR) RIGHT(PTR) PTR1 LEFT(PTR1) PTR1 HEAD HEAD1 Call DISPLAY( ) Step 2: [Finished] Return

178

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct gsdll { int info; struct gsdll*left,*right; }*head=NULL; void create( ); void ins_beg( ); void ins_bet( ); void ins_last( ); void del_beg( ); void del_bet( ); void del_last( ); void search( ); void concate( ); void display( ); void main( ) { int ch; char ans; while(1) { clrscr( ); printf("\n\n Implementing Grounded Doubly Linked List"); printf("\n----------------------------------------------- "); printf("\n\n\t 1- Creation of linked list"); printf("\n\t 2- Insertion of element in the begining"); printf("\n\t 3- Insertion of element in between"); printf("\n\t 4- Insertion of element at the last"); printf("\n\t 5- Deletion of element from begining"); printf("\n\t 6- Deletion of element from between"); printf("\n\t 7- Deletion of element from the last"); printf("\n\t 8- Searching of element in the list"); printf("\n\t 9- Concatenation of two different linked lists"); printf("\n\t 10- Display the elements in the list"); printf("\n\t 11- Exit from output window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d",&ch); switch(ch) { case 1: create( ); break; 179

case 2: ins_beg( ); break; case 3: ins_bet( ); break; case 4: ins_last( ); break; case 5: del_beg(); break; case 6: del_bet( ); break; case 7: del_last( ); break; case 8: search( ); break; case 9: concate( ); break; case 10: display( ); break; case 11: printf("\n You have chosen to exit"); printf("\n Are you sure you want exit?"); printf("\n If yes press y."); fflush(stdin); scanf("%c",&ans); if(ans== 'y' || ans== 'Y') exit(0); else break; default: printf("\n\n Invalid choice..."); printf("\n Press any key to continue"); fflush(stdin); getch( ); break; } } } void create( ) { char ans; struct gsdll *ptr,*ptr1; ptr1=(struct gsdll*)malloc(sizeof(struct gsdll)); if(!ptr1) printf("\n Memory is full."); else { printf("\n Enter information:- "); scanf("%d",&ptr1->info); ptr1->left= ptr1->right= NULL; if(head == NULL) head= ptr1; else { ptr= head; 180

while(ptr->right != NULL) ptr = ptr->right; ptr->right = ptr1; ptr1->left = ptr; } printf("\n Do you have more information?"); printf("\n If yes, press 'y' :"); fflush(stdin); scanf("%c",&ans); if(ans == 'y' || ans == 'Y') create( ); } } void ins_beg( ) { struct gsdll *ptr=NULL,*ptr1; ptr1= (struct gsdll*)malloc(sizeof(struct gsdll)); if(!ptr1) printf("\n Memory is full !"); else { printf("\n Enter the information for new node :"); scanf("%d",&ptr1->info); ptr1->left = ptr1->right = NULL; ptr= head; ptr1->right= ptr; ptr->left= ptr1; head= ptr1; } getch( ); } void ins_bet( ) { int pos,count=1; struct gsdll *ptr,*ptr1,*ptr2; ptr1=(struct gsdll*)malloc(sizeof(struct gsdll)); if(!ptr1) printf("\n Memory is full !"); else { printf("\n Enter the position where new node is to be inserted :"); scanf("%d",&pos); printf("\n Enter the information for the new node :"); scanf("%d",&ptr1->info); ptr1->left = ptr1->right = NULL; ptr=head; while((ptr->right != NULL)&&(count < pos-1)) { ptr= ptr->right; count++; } if(count == pos-1) 181

{ ptr2= ptr->right; ptr->right= ptr1; ptr1->right= ptr2; ptr2->left= ptr1; ptr1->left= ptr; } else { printf("\n\n Position given is out of range !!!"); printf("\n Press any key"); fflush(stdin); getch( ); } } getch( ); } void ins_last( ) { struct gsdll *ptr,*ptr1; ptr1=(struct gsdll*)malloc(sizeof(struct gsdll)); if(!ptr1) printf("\n Memory is full !"); else { printf("\n Enter the information :"); scanf("%d",&ptr1->info); ptr1->left= ptr1->right= NULL; ptr=head; while(ptr->right != NULL) ptr= ptr->right; ptr->right= ptr1; ptr1->left= ptr; } getch( ); } void del_beg( ) { struct gsdll *ptr,*ptr1; if(head== NULL) printf("\n Link list is empty."); else { ptr1=head; printf("\n Element that was deleted is : %d",ptr1->info); fflush(stdin); getch( ); ptr= ptr1->right; head= ptr; ptr->left = NULL; free(ptr1); 182

} getch( ); } void del_bet( ) { struct gsdll *ptr,*ptr1,*ptr2; int pos,count=1; if(head== NULL) printf("\n Link list is empty."); else { printf("\n Enter the position from where node is to be deleted :"); scanf("%d",&pos); while((ptr->right != NULL) && (count < pos-1)) { ptr= ptr->right; count++; } if(count== pos-1) { ptr1= ptr->right; ptr2= ptr1->right; ptr->right= ptr2; ptr2->left= ptr; printf("\n Element deleted was :%d",ptr1->info); fflush(stdin); getch( ); free(ptr1); } else { printf("\n\n Position given is out of range !!!"); printf("\n Press any key"); fflush(stdin); getch( ); } } getch( ); } void del_last( ) { struct gsdll *ptr,*ptr1; if(head== NULL) printf("\n Link list is empty."); else { ptr= head; if(ptr->right == NULL) { head= NULL; printf("\n Node to be deleted is : %d",ptr->info); 183

fflush(stdin); getch( ); free(ptr); } else { ptr= head; while(ptr->right->right != NULL) ptr= ptr->right; ptr1= ptr->right; ptr->right= NULL; printf("\n Element deleted was : %d",ptr1->info); fflush(stdin); getch( ); free(ptr1); } } getch( ); } void display( ) { int count= 1; struct gsdll *ptr= NULL; ptr=head; if(ptr== NULL) printf("\n Linked list is empty"); else { printf("\n\n Elements in the list are :\n"); while(ptr->right!=NULL) { printf("\n\n Node : %d Element : %d",count,ptr->info); count++; ptr=ptr->right; } } printf("\n Press any key to continue"); fflush(stdin); getch( ); } void search( ) { int ser,pos= 1,flag=0; struct gsdll *ptr; printf("\n Enter the element to be searched :"); scanf("%d",&ser); ptr=head; while(ptr->right != NULL) { if(ptr->info == ser) { 184

flag= 1; break; } pos++; ptr= ptr->right; } if(flag== 1) printf("\n Element found at %d position",pos); else printf("\n Element not found"); getch( ); } void concate( ) { struct gsdll *ptr,*ptr1,*head1; if(head== NULL) { printf("\n Please create your first linked list."); create( ); } head1= head; head= NULL; printf("\n Please create your second linked list"); create( ); ptr= head1; ptr1= head; while(ptr->right != NULL) ptr= ptr->right; ptr->right = ptr1; ptr1->left= ptr; head= head1; display( ); getch( ); }

185

Q. 27 Write pseudo code and program for implementing header simply doubly linked list. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show menu of various operations to be performed on the linked list. ANS is a character variable and CHOICE is an integer variable to hold selected option input. Structure of node is as follows LEFT HSDLL Step 1: INFO RIGHT

[Displaying the menu ] Repeat While( True) Write( Implementing header simply doubly linked list) Write( 1- Creation of linked list) Write( 2- Insertion of element in the beginning ) Write( 3- Insertion of element in between) Write( 4- Insertion of element in the last) Write( 5- Deletion of element from the beginning Write( 6- Deletion of element from between) Write( 7- Deletion of element from last) Write( 8- Searching of element in the list) Write( 9- Concatenation of two different linked list) Write( 10- Display of elements in the list) Write( 11- Exit from output window) Write( Enter your choice: ) Read(CHOICE)

Step 2:

[Calling of functions for each operation] Select Case( CHOICE) Case 1: Call CREATE( ) Case 2: Call INS_BEG( ) Case 3: Call INS_BET( ) Case 4: Call INS_LAST( ) Case 5: Call DEL_BEG( ) Case 6: Call DEL_BET( ) Case 7: Call DEL_LAST( ) Case 8: Call SEARCH( ) Case 9: Call CONCATE( ) Case 10: Call DISPLAY( ) Case 11: Write( You have chosen to exit) Write( Are you sure you want to exit) Write( If yes press y ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then EXIT(0) Step 3: [Finished] Exit Procedure. CREATE( ). This procedure creates grounded simply doubly linked list 186

with initially given information. This procedure is recursive in nature and Calls itself from within to add next information to the list. PTR and PTR1 are two pointers. PTR is used for traversing through the list and PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes ANS is a character variable used for checking the choice. HEAD.RIGHT is the pointer to the first node with initial value of NULL. Structure of node is as follows LEFT HSDLL Step 1: INFO RIGHT

[Allocating memory for new node] PTR1 <= HSDLL If (PTR1 = NULL) Then Write( Memory is full ) [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) LEFT(PTR1) RIGHT(PTR1) NULL [Inserting the node information linked list] If (HEAD.RIGHT = NULL) Then HEAD.RIGHT PTR1 Else PTR HEAD.RIGHT Repeat While(RIGHT(PTR) NULL) PTR RIGHT(PTR) RIGHT(PTR) PTR1 LEFT(PTR1) PTR HEAD.INFO HEAD.INFO + 1

Step 2:

Step 3:

Step 4:

[Checking for additional data] Write( Do you have more information? ) Write( If yes press y: ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then Call CREATE( ) [Finished] Return

Step 5:

Procedure. INS_BEG( ). This procedure inserts given information in the beginning of list by linking the new node with the previously created node. PTR is used to traverse through the list.PTR1 is a pointer used to confirm variable used for checking the choice.LEFT and RIGHT are pointer used to confirm addition of newly allocated nodes. ANS is a character variable used for checking the choice. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows LEFT HSDLL INFO RIGHT 187

Step 1:

[Allocating memory for new node] PTR1 <= HSDLL If (PTR1 = NULL) Then Write( Memory is full ) Return [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) LEFT(PTR1) RIGHT(PTR1) NULL [Adding the new node to the list] PTR HEAD.RIGHT RIGHT(PTR1) PTR LEFT(PTR) PTR1 HEAD.RIGHT PTR1 HEAD.INFO HEAD.INFO + 1 [Finished] Return

Step 2:

Step 3:

Step 4:

Procedure. INS_BET( ). This procedure inserts given information in between of list. PTR, PTR1and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next node. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. POS is the integer variable used for storing the entered position for insertion of node. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows LEFT HSDLL Step 1: INFO RIGHT

[Allocating memory for new node] COUNT 1 PTR1 <= HSDLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the position and information] Write( Enter the position where new node is to be inserted: ) Read(POS) Write( Enter information: ) Read(INFO(PTR1)) PTR HEAD.RIGHT LEFT(PTR1) RIGHT(PTR1) NULL [Inserting the new node] Repeat While((RIGHT(PTR) NULL) and (COUNT < POS-1)) PTR RIGHT(PTR) COUNT COUNT + 1 188

Step 2:

Step 3:

If (COUNT = POS -1) Then PTR2 RIGHT(PTR) RIGHT(PTR) PTR1 RIGHT(PTR1) PTR2 LEFT(PTR2) PTR1 LEFT(PTR1) PTR HEAD.INFO HEAD .INFO +1 Else Write( The given position is out of range. ) Step 4: [Finished] Return

Procedure. INS_LAST( ). This procedure inserts given information in last of list . PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. HEAD is the pointer to the first node. Structure of node is as follows LEFT HSDLL Step 1: INFO RIGHT

[Allocating memory for new node] PTR1 <= HSDLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the information] Write( Enter information: ) Read( INFO(PTR1)) LEFT(PTR1) RIGHT(PTR1) NULL [Inserting the new node] PTR HEAD.RIGHT Repeat While(RIGHT(PTR) NULL) PTR RIGHT(PTR) RIGHT(PTR) PTR1 LEFT(PTR1) PTR HEAD.INFO HEAD.INFO + 1 [Finished] Return

Step 2:

Step 3:

Step 4:

189

Procedure. DEL_BEG( ). This procedure delete the element from the beginning of list. PTR is a pointer used to transverse through the list. PTR1 is used to check deletion of node. LEFT and RIGHT are pointes used to store addresses of neighbouring nodes. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows LEFT HSDLL Step 1: INFO RIGHT

[Checking memory] If (HEAD.RIGHT = NULL) Then Write( Link list is empty) [Deleting the node] PTR1 HEAD.RIGHT Write( Element that was deleted is: INFO(PTR)) PTR RIGHT(PTR1) HEAD.RIGHT PTR LEFT(PTR) NULL Restore(PTR) [Finished] Return

Step 2:

Step 3:

Procedure. DEL_BET( ). This procedure delete the element from between of list. PTR, PTR1 and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next nodes. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighbouring node. POS is the integer variable used for storing the entered position for deletion of node. HEAD.RIGHT is the pointer which points to the first node. Structure of node is as follows LEFT HSDLL Step 1: INFO RIGHT

[Checking memory] PTR HEAD.RIGHT COUNT 1 If (HEAD.RIGHT = NULL) Then Write( Link list is empty ) [Entering position for deletion] Write( Enter the position from where node is to be deleted:) Read(POS) [Deleting the node] Repeat While((RIGHT(PTR) NULL) and (COUNT < POS 1)) PTR RIGHT(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR1 RIGHT(PTR) 190

Step 2:

Step 3:

PTR2 RIGHT(PTR1) RIGHT(PTR) PTR2 LEFT(PTR2) PTR Write( Element deleted was: INFO(PTR1)) Restore(PTR1) HEAD.INFO HEAD.INFO - 1 Else Write( Position given is out of range ) Step 4: [Finished] Return

Procedure. DEL_LAST( ). This procedure delete the element from the last of list. PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of beighbouring nodes. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows LEFT HSDLL Step 1: INFO RIGHT

[Checking memory] If (HEAD.RIGHT = NULL) Then Write( Link list is empty ) [Deleting the node] PTR1 HEAD If (RIGHT(PTR1) = NULL) Then HEAD NULL Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) HEAD.INFO HEAD.INFO - 1 Else PTR HEAD.RIGHT Repeat While(RIGHT(RIGHT(PTR)) NULL) PTR RIGHT(PTR) PTR1 RIGHT(PTR) RIGHT(PTR) NULL Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) HEAD.INFO HEAD.INFO - 1 [Finished] Return

Step 2:

Step 3:

Procedure. DISPLAY( ). This procedure display the element of the list on the screen. PTR is a pointer used to transverse through the list. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows LEFT INFO RIGHT 191

HSDLL Step 1: [Checking memory] COUNT 1 PTR NULL PTR HEAD.RIGHT If (PTR = NULL) Then Write( Linked list is empty)

Step 2:

[Displaying elements on the screen] Write( Elements in the list are:) Repeat While(RIGHT(PTR) NULL) Write( Node: COUNT, Element: INFO(PTR)) COUNT COUNT + 1 PTR RIGHT(PTR) Step 3: [Finished] Return Procedure. SEARCH( ). This procedure search the element from given the list. PTR is pointer used to transverse through the list. POS is an integer variable used for telling the position of the element in the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. SER is an integer variable used for storing the element that is to be searched. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows LEFT HSDLL Step 1: INFO RIGHT

[Entering the element for search] POS 1 FLAG 0 Write(Enter the element to be searched: ) Read(SER) [Search for element] PTR HEAD.RIGHT Repeat While(PTR NULL) If (INFO(PTR) = SER) Then FLAG 1 POS POS + 1 PTR RIGHT(PTR) [Displaying the result] If (FLAG = 1) Then Write( Element found at position, POS) Else Write( Element not found. ) [Finished] Return

Step 2:

Step 3:

Step 4:

Procedure. CONCATE( ). This procedure join the two link list. PTR and PTR1 are pointer used to transverse through the list. LEFT and RIGHT rae pointers 192

used to store addresses of neighbouring nodes. HEAD.RIGHT is the pointer to the first node of the first linked list. HEAD1.RIGHT is the pointer to the first node of the second linked list. Structure of node is as follows LEFT HSDLL Step 1: INFO RIGHT

[Concatenation of two different linked list] If (HEAD.RIGHT = NULL) Then Write( Please create your first linked list) Call CREATE( ) HEAD1.RIGHT HEAD.RIGHT HEAD.RIGHT NULL PTR1 HEAD.RIGHT Write( Please create your second linked list) Call CREATE( ) PTR HEAD1.RIGHT Repeat While(RIGHT(PTR) NULL) PTR RIGHT(PTR) RIGHT(PTR) PTR1 LEFT(PTR1) PTR1 HEAD.RIGHT HEAD1.RIGHT Call DISPLAY( ) [Finished] Return

Step 2:

193

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct hsdll { int info; struct hsdll*left,*right; }; struct hsdll head; void create( ); void ins_beg( ); void ins_bet( ); void ins_last( ); void del_beg( ); void del_bet( ); void del_last( ); void search( ); void concate( ); void display( ); void main( ) { int ch; char ans; head.left=head.right=NULL; head.info=0; while(1) { clrscr( ); printf("\n\n Implementing Header Singly Doubly Linked List"); printf("\n----------------------------------------------- "); printf("\n\n\t 1- Creation of linked list"); printf("\n\t 2- Insertion of element in the begining"); printf("\n\t 3- Insertion of element in between"); printf("\n\t 4- Insertion of element at the last"); printf("\n\t 5- Deletion of element from begining"); printf("\n\t 6- Deletion of element from between"); printf("\n\t 7- Deletion of element from the last"); printf("\n\t 8- Searching of element in the list"); printf("\n\t 9- Concatenation of two different linked lists"); printf("\n\t 10- Display the elements in the list"); printf("\n\t 11- Exit from output window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d",&ch); switch(ch) { 194

case 1: create( ); break; case 2: ins_beg( ); break; case 3: ins_bet( ); break; case 4: ins_last( ); break; case 5: del_beg( ); break; case 6: del_bet( ); break; case 7: del_last( ); break; case 8: search( ); break; case 9: concate( ); break; case 10: display( ); break; case 11: printf("\n You have chosen to exit"); printf("\n Are you sure you want exit?"); printf("\n If yes press y."); fflush(stdin); scanf("%c",&ans); if(ans=='y'||ans=='Y') exit(0); else break; default: printf("\n\n Invalid choice..."); printf("\n Press any key to continue"); fflush(stdin); getch( ); break; } } } void create( ) { char ans; struct hsdll *ptr,*ptr1; ptr1=(struct hsdll*)malloc(sizeof(struct hsdll)); if(!ptr1) printf("\n Memory is full."); else { printf("\n Enter information:- "); scanf("%d",&ptr1->info); ptr1->left = ptr1->right = NULL; if(head.right == NULL) head.right= ptr1; 195

else { ptr= head.right; while(ptr->right != NULL) ptr= ptr->right; ptr->right = ptr1; ptr1->left = ptr; head.info++; } printf("\n Do you have more information?"); printf("\n If yes, press 'y' :"); fflush(stdin); scanf("%c",&ans); if(ans == 'y' || ans == 'Y') create( ); } } void ins_beg( ) { struct hsdll *ptr=NULL,*ptr1; ptr1=(struct hsdll*)malloc(sizeof(struct hsdll)); if(!ptr1) printf("\n Memory is full !"); else { printf("\n Enter the information for new node :"); scanf("%d",&ptr1->info); ptr1->left = ptr1->right = NULL; ptr= head.right; ptr1->right= ptr; ptr->left= ptr1; head.right= ptr1; head.info++; } getch( ); } void ins_bet( ) { int pos,count=1; struct hsdll *ptr,*ptr1,*ptr2; ptr1=(struct hsdll*)malloc(sizeof(struct hsdll)); if(!ptr1) printf("\n Memory is full !"); else { printf("\n Enter the position where new node is to be inserted :"); scanf("%d",&pos); printf("\n Enter the information for the new node :"); scanf("%d",&ptr1->info); ptr1->left= ptr1->right= NULL; ptr= head.right; 196

while((ptr->right != NULL) && (count < pos-1)) { ptr=ptr->right; count++; } if(count== pos-1) { ptr2= ptr->right; ptr->right= ptr1; ptr1->right= ptr2; ptr2->left= ptr1; ptr1->left= ptr; head.info++; } else { printf("\n\n Position given is out of range !!!"); printf("\n Press any key"); fflush(stdin); getch( ); } } getch( ); } void ins_last( ) { struct hsdll *ptr,*ptr1; ptr1=(struct hsdll*)malloc(sizeof(struct hsdll)); if(!ptr1) printf("\n Memory is full !"); else { printf("\n Enter the information :"); scanf("%d",&ptr1->info); ptr1->left = ptr1->right = NULL; ptr= head.right; while(ptr->right != NULL) ptr= ptr->right; ptr->right= ptr1; ptr1->left= ptr; head.info++; } getch( ); } void del_beg( ) { struct hsdll *ptr,*ptr1; if(head.right== NULL) printf("\n Link list is empty."); else { 197

ptr1=head.right; printf("\n Element that was deleted is : %d",ptr1->info); fflush(stdin); getch( ); ptr= ptr1->right; head.right=ptr; ptr->left =NULL; free(ptr1); head.info--; } getch( ); } void del_bet( ) { struct hsdll *ptr,*ptr1,*ptr2; int pos,count= 1; if(head.right== NULL) printf("\n Link list is empty."); else { printf("\n Enter the position from where node is to be deleted :"); scanf("%d",&pos); while((ptr->right != NULL) && (count < pos-1)) { ptr= ptr->right; count++; } if(count== pos-1) { ptr1= ptr->right; ptr2= ptr1->right; ptr->right= ptr2; ptr2->left= ptr; printf("\n Element deleted was :%d",ptr1->info); fflush(stdin); getch( ); free(ptr1); head.info--; } else { printf("\n\n Position given is out of range !!!"); printf("\n Press any key"); fflush(stdin); getch( ); } } getch( ); } void del_last( ) { 198

struct hsdll *ptr,*ptr1; if(head.right == NULL) printf("\n Link list is empty."); else { ptr= head.right; if(ptr->right == NULL) { head.right=NULL; printf("\n Node to be deleted is : %d",ptr->info); fflush(stdin); getch( ); free(ptr); head.info--; } else { ptr= head.right; while(ptr->right->right != NULL) ptr= ptr->right; ptr1= ptr->right; ptr->right= NULL; printf("\n Element deleted was : %d",ptr1->info); fflush(stdin); getch( ); free(ptr1); head.info--; } } getch( ); } void display( ) { int count= 1; struct hsdll *ptr= NULL; ptr= head.right; if(ptr== NULL) printf("\n Linked list is empty"); else { printf("\n\n Elements in the list are :\n"); while(ptr->right != NULL) { printf("\n\n Node : %d Element : %d",count,ptr->info); count++; ptr= ptr->right; } } printf("\n Press any key to continue"); fflush(stdin); getch( ); } 199

void search( ) { int ser,pos=1,flag=0; struct hsdll *ptr; printf("\n Enter the element to be searched :"); scanf("%d",&ser); ptr= head.right; while(ptr->right != NULL) { if(ptr->info == ser) { flag=1; break; } pos++; ptr= ptr->right; } if(flag== 1) printf("\n Element found at %d position",pos); else printf("\n Element not found"); getch( ); } void concate( ) { struct hsdll *ptr,*ptr1,head1; if(head.right== NULL) { printf("\n Please create your first linked list."); create( ); } head1.right= head.right; head.right= NULL; printf("\n Please create your second linked list"); create( ); ptr= head1.right; ptr1= head.right; while(ptr->right != NULL) ptr= ptr->right; ptr->right= ptr1; ptr1->left= ptr; head.right=head1.right; display( ); getch( ); }

200

Q. 28 Write pseudo code and program for implementing grounded circular doubly linked list. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show menu of various operations to be performed on the linked list. ANS is a character variable and CHOICE is an integer variable to hold selected option input. Structure of node is as follows LEFT GCDLL Step 1: INFO RIGHT

[Displaying the menu ] Repeat While( True) Write( Implementing grounded circular doubly linked list) Write( 1- Creation of linked list) Write( 2- Insertion of element in the beginning ) Write( 3- Insertion of element in between) Write( 4- Insertion of element in the last) Write( 5- Deletion of element from the beginning Write( 6- Deletion of element from between) Write( 7- Deletion of element from last) Write( 8- Searching of element in the list) Write( 9- Concatenation of two different linked list) Write( 10- Display of elements in the list) Write( 11- Exit from output window) Write( Enter your choice: ) Read(CHOICE) [Calling of functions for each operation] Select Case( CHOICE) Case 1: Call CREATE( ) Case 2: Call INS_BEG( ) Case 3: Call INS_BET( ) Case 4: Call INS_LAST( ) Case 5: Call DEL_BEG( ) Case 6: Call DEL_BET( ) Case 7: Call DEL_LAST( ) Case 8: Call SEARCH( ) Case 9: Call CONCATE( ) Case 10: Call DISPLAY( ) Case 11: Write( You have chosen to exit) Write( Are you sure you want to exit) Write( If yes press y ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then EXIT(0) [Finished] Exit 201

Step 2:

Step 3:

Procedure. CREATE( ). This procedure creates grounded simply doubly linked list with initially given information. This procedure is recursive in nature and Calls itself from within to add next information to the list. PTR and PTR1 are two pointers. PTR is used for traversing through the list and PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes ANS is a character variable used for checking the choice. HEAD is the pointer to the first node with initial value of NULL. Structure of node is as follows LEFT GCDLL Step 1: INFO RIGHT

[Allocating memory for new node] PTR1 <= GCDLL If (PTR1 = NULL) Then Write( Memory is full ) [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) [Inserting the node information linked list] If (HEAD = NULL) Then HEAD PTR1 Else PTR HEAD Repeat While(RIGHT(PTR) NULL) PTR RIGHT(PTR) RIGHT(PTR) PTR1 RIGHT(PTR) PTR1 LEFT(PTR1) PTR RIGHT(PTR1) HEAD

Step 2:

Step 3:

Step 4:

[Checking for additional data] Write( Do you have more information? ) Write( If yes press y: ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then Call CREATE( ) [Finished] Return

Step 5:

Procedure. INS_BEG( ). This procedure inserts given information in the beginning of list by linking the new node with the previously created node. PTR is used to traverse through the list.PTR1 is a pointer used to confirm variable used for checking the choice.LEFT and RIGHT are pointer used to confirm addition of newly allocated nodes. ANS is a character variable used for checking the choice. HEAD is the pointer to the first node. Structure of node is as follows 202

LEFT GCDLL Step 1:

INFO

RIGHT

[Allocating memory for new node] PTR1 <= GCDLL If (PTR1 = NULL) Then Write( Memory is full ) Return [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) If (HEAD = NULL) Then HEAD PTR1 LEFT(PTR1) RIGHT(PTR1) NULL [Adding the new node to the list] PTR HEAD RIGHT(PTR1) HEAD LEFT(PTR) PTR1 PTR2 LEFT(PTR) RIGHT(PTR2) PTR1 LEFT(PTR1) PTR2 HEAD PTR1 [Finished] Return

Step 2:

Step 3:

Step 4:

Procedure. INS_BET( ). This procedure inserts given information in between of list. PTR, PTR1and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next node. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. POS is the integer variable used for storing the entered position for insertion of node. HEAD is the pointer to the first node. Structure of node is as follows LEFT GCDLL Step 1: INFO RIGHT

[Allocating memory for new node] COUNT 1 PTR1 <= GCDLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the position and information] Write( Enter the position where new node is to be inserted: ) Read(POS) Write( Enter information: ) Read(INFO(PTR1)) 203

Step 2:

PTR HEAD Step 3: [Inserting the new node] Repeat While((RIGHT(PTR) NULL) and (COUNT < POS-1)) PTR RIGHT(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR2 RIGHT(PTR) RIGHT(PTR) PTR1 RIGHT(PTR1) PTR2 LEFT(PTR2) PTR1 LEFT(PTR1) PTR Else Write( The given position is out of range. ) Step 4: [Finished] Return

Procedure. INS_LAST( ). This procedure inserts given information in last of list . PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. HEAD is the pointer to the first node. Structure of node is as follows LEFT GCDLL Step 1: INFO RIGHT

[Allocating memory for new node] PTR1 <= GCDLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the information] Write( Enter information: ) Read( INFO(PTR1)) If (HEAD= NULL) Then PTR1 HEAD LEFT(PTR1) RIGHT(PTR1) NULL [Inserting the new node] PTR HEAD Repeat While(RIGHT(PTR) NULL) PTR RIGHT(PTR) PTR2 HEAD LEFT(PTR2) PTR1 RIGHT(PTR1) PTR2 RIGHT(PTR) PTR1 LEFT(PTR1) PTR [Finished] 204

Step 2:

Step 3:

Step 4:

Return Procedure. DEL_BEG( ). This procedure delete the element from the beginning of list. PTR is a pointer used to transverse through the list. PTR1 is used to check deletion of node. LEFT and RIGHT are pointes used to store addresses of neighbouring nodes. HEAD is the pointer to the first node. Structure of node is as follows LEFT GCDLL Step 1: INFO RIGHT

[Checking memory] If (HEAD = NULL) Then Write( Link list is empty) [Deleting the node] PTR1 HEAD Write( Element that was deleted is: INFO(PTR)) PTR RIGHT(PTR1) HEAD PTR RIGHT(PTR2) PTR LEFT(PTR) PTR2 Restore(PTR1) [Finished] Return

Step 2:

Step 3:

Procedure. DEL_BET( ). This procedure delete the element from between of list. PTR, PTR1 and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next nodes. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighbouring node. POS is the integer variable used for storing the entered position for deletion of node. HEAD is the pointer which points to the first node. Structure of node is as follows LEFT GCDLL Step 1: INFO RIGHT

[Checking memory] COUNT 1 If (HEAD = NULL) Then Write( Link list is empty ) [Entering position for deletion] Write( Enter the position from where node is to be deleted:) Read(POS) PTR HEAD [Deleting the node] Repeat While((RIGHT(PTR) NULL) and (COUNT < POS 1)) 205

Step 2:

Step 3:

PTR RIGHT(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR1 RIGHT(PTR) PTR2 RIGHT(PTR1) RIGHT(PTR) PTR2 LEFT(PTR2) PTR Write( Element deleted was: INFO(PTR1)) Restore(PTR1) Else Write( Position given is out of range ) Step 4: [Finished] Return

Procedure. DEL_LAST( ). This procedure delete the element from the last of list. PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of beighbouring nodes. HEAD is the pointer to the first node. Structure of node is as follows LEFT INFO RIGHT GCDLL Step 1: [Checking memory] If (HEAD = NULL) Then Write( Link list is empty ) Step 2: [Deleting the node] PTR1 HEAD If (RIGHT(PTR1) = NULL) Then HEAD NULL Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) Else Repeat While(RIGHT(RIGHT(PTR)) NULL) PTR2 LEFT(PTR) LEFT(PTR) PTR2 PTR1 LEFT(PTR) RIGHT(PTR2) PTR Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) [Finished] Return

Step 3:

Procedure. DISPLAY( ). This procedure display the element of the list on the screen. PTR is a pointer used to transverse through the list. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. HEAD is the pointer to the first node. Structure of node is as follows LEFT GCDLL INFO RIGHT

206

Step 1:

[Checking memory] COUNT 1 PTR NULL PTR HEAD If (PTR = NULL) Then Write( Linked list is empty) [Displaying elements on the screen] Write( Elements in the list are:) Repeat While(RIGHT(PTR) NULL) Write( Node: COUNT, Element: INFO(PTR)) COUNT COUNT + 1 PTR RIGHT(PTR) Write( Node: COUNT, Element: INFO(PTR)) [Finished] Return

Step 2:

Step 3:

Procedure. SEARCH( ). This procedure search the element from given the list. PTR is pointer used to transverse through the list. POS is an integer variable used for telling the position of the element in the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. SER is an integer variable used for storing the element that is to be searched. HEAD is the pointer to the first node. Structure of node is as follows LEFT GCDLL Step 1: INFO RIGHT

[Entering the element for search] POS 1 FLAG 0 Write(Enter the element to be searched: ) Read(SER) Step 2: [Search for element] PTR HEAD Repeat While(PTR NULL) If (INFO(PTR) = SER) Then FLAG 1 POS POS + 1 PTR RIGHT(PTR) Step 3: [Displaying the result] If (FLAG = 1) Then Write( Element found at position, POS) Else Write( Element not found. ) [Finished] Return

Step 4:

Procedure. CONCATE( ). This procedure join the two link list. PTR and PTR1 are pointer used to transverse through the list. LEFT and RIGHT rae pointers used to store addresses of neighbouring nodes. HEAD is the pointer to the first node of the first linked list. HEAD1 is the pointer to the first node of 207

the second linked list. Structure of node is as follows LEFT INFO RIGHT GCDLL Step 1: [Concatenation of two different linked list] If (HEAD = NULL) Then Write( Please create your first linked list) Call CREATE( ) HEAD1 HEAD HEAD NULL PTR1 HEAD Write( Please create your second linked list) Call CREATE( ) PTR HEAD1 Repeat While(RIGHT(PTR) NULL) PTR RIGHT(PTR) RIGHT(PTR) PTR1 LEFT(PTR1) PTR PTR2 LEFT(PTR1) RIGHT(PTR2) HEAD1 LEFT(HEAD1) PTR2 HEAD HEAD1 Call DISPLAY( ) Step 2: [Finished] Return

208

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct gcdll { int info; struct gcdll*left,*right; }* head=NULL; void create( ); void ins_beg( ); void ins_bet( ); void ins_last( ); void del_beg( ); void del_bet( ); void del_last( ); void search( ); void concate( ); void display( ); void main( ) { int ch; char ans; while(1) { clrscr( ); printf("\n\n Implementing Grounded Circular Doubly Linked List"); printf("\n----------------------------------------------- "); printf("\n\n\t 1- Creation of linked list"); printf("\n\t 2- Insertion of element in the begining"); printf("\n\t 3- Insertion of element in between"); printf("\n\t 4- Insertion of element at the last"); printf("\n\t 5- Deletion of element from begining"); printf("\n\t 6- Deletion of element from between"); printf("\n\t 7- Deletion of element from the last"); printf("\n\t 8- Searching of element in the list"); printf("\n\t 9- Concatenation of two different linked lists"); printf("\n\t 10- Display the elements in the list"); printf("\n\t 11- Exit from output window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d",&ch); switch(ch) { case 1: create( ); break; 209

case 2: ins_beg( ); break; case 3: ins_bet( ); break; case 4: ins_last( ); break; case 5: del_beg( ); break; case 6: del_bet( ); break; case 7: del_last( ); break; case 8: search( ); break; case 9: concate( ); break; case 10: display( ); break; case 11: printf("\n You have chosen to exit"); printf("\n Are you sure you want exit?"); printf("\n If yes press y."); fflush(stdin); scanf("%c",&ans); if(ans=='y'||ans=='Y') exit(0); else break; default: printf("\n\n Invalid choice..."); printf("\n Press any key to continue"); fflush(stdin); getch( ); break; } } } void create( ) { char ans; struct gcdll *ptr, *ptr1; ptr1=(struct gcdll*)malloc(sizeof(struct gcdll)); if(!ptr1) printf("\n Memory is full !"); else { printf("\n Enter information:- "); scanf("%d",&ptr1->info); if(head== NULL) { head= ptr1; ptr1->left= ptr1->right= head; } 210

else { ptr=head; ptr->left = ptr1; while(ptr->right != head) ptr= ptr->right; ptr->right = ptr1; ptr1->right = head; ptr1->left = ptr; } printf("\n Do you have more information? "); printf("\n If yes press 'y':"); fflush(stdin); scanf("%c",&ans); if(ans == 'y' || ans == 'Y') create( ); } } void ins_beg( ) { struct gcdll *ptr=NULL,*ptr1,*ptr2; ptr1=(struct gcdll*)malloc(sizeof(struct gcdll)); if(!ptr1) printf("\n Memory is full."); else { printf("\n Enter the information for new node:- "); scanf("%d",&ptr1->info); ptr= head; ptr2= ptr->left; ptr1->right = ptr; ptr2->right = ptr1; ptr1->left = ptr2; ptr->left = ptr1; head= ptr1; } getch( ); } void ins_bet( ) { int pos,count=1; struct gcdll *ptr,*ptr1,*ptr2; ptr1=(struct gcdll*)malloc(sizeof(struct gcdll)); if(!ptr1) printf("\n Memory is full. "); else { printf("\n Enter the position where new node is to be inserted :"); scanf("%d",&pos); printf("\n Enter the information for new node :"); scanf("%d",&ptr1->info); 211

ptr=head; while((ptr->right != NULL) && (count < pos-1)) { ptr= ptr->right; count++; } if(count == pos-1) { ptr2= ptr->right; ptr->right= ptr1; ptr1->right= ptr2; ptr2->left= ptr1; ptr1->left= ptr; } else { printf("\n\n Position given is out of range !!!"); fflush(stdin); getch( ); } } getch( ); } void ins_last( ) { struct gcdll *ptr, *ptr1, *ptr2; ptr1=(struct gcdll*)malloc(sizeof(struct gcdll)); if(!ptr1) printf("\n Memory is full."); else { printf("\n Enter the information :"); scanf("%d",&ptr1->info); if(head== NULL) { head= ptr1; ptr1->left= ptr1->right= head; } else { ptr=head; while(ptr->right != head) ptr= ptr->right; ptr2= head; ptr2->left= ptr1; ptr1->right= ptr2; ptr->right= ptr1; ptr1->left= ptr; } } getch( ); } 212

void del_beg( ) { struct gcdll *ptr,*ptr1,*ptr2; if(head== NULL) printf("\n Linked list is empty"); else { ptr=head; ptr= ptr1->right; ptr2= ptr1->left; if(ptr== ptr2) head= NULL; else { ptr->left= ptr2; ptr2->right= ptr; head= ptr; } printf("\n Element that was deleted is : %d",ptr1->info); printf("\n Press any key"); fflush(stdin); getch( ); free(ptr1); } } void del_bet( ) { int pos,count= 1; struct gcdll *ptr,*ptr1,*ptr2; if(head== NULL) printf("\n LINK LIST IS EMPTY"); else { ptr= head; printf("\nEnter the position from where element is to be deleted"); scanf("%d",&pos); while((ptr->right != head) && (count < pos-1)) { ptr= ptr->right; count++; } if(count == pos-1) { ptr1= ptr->right; ptr2= ptr1->right; ptr->right= ptr2; ptr2->left= ptr; printf("\n Element deleted was : %d",ptr1->info); printf("\n Press any key."); fflush(stdin); getch( ); 213

free(ptr1); } else printf("\n Position given is out of range "); printf("\n Press any key."); fflush(stdin); getch( ); } } void del_last( ) { struct gcdll *ptr,*ptr1,*ptr2; if(head==NULL) printf("\n LINK LIST IS EMPTY"); else { ptr=head; if(ptr->right== head) { head= NULL; printf("\n Node to be deleted is %d",ptr->info); printf("\n Press any key"); fflush(stdin); getch( ); free(ptr); } else { while(ptr->right->right != head) ptr1= ptr1->right; ptr= ptr->right; ptr1= ptr->left; ptr2= ptr1->left; ptr2->right= ptr; ptr->left= ptr2; printf("\n Element deleted was : %d",ptr1->info); fflush(stdin); getch( ); free(ptr1); } } getch( ); } void display( ) { int count= 1; struct gcdll *ptr= NULL; ptr= head; if(ptr== NULL) printf("\n LINK LIST IS EMPTY"); else 214

{ printf("\n Elements in the list are :-\n"); while(ptr->right != head) { printf("\n Node:%d Element:%d",count,ptr->info); count++; ptr= ptr->right; } printf("\n Node:%d Element:%d",count,ptr->info); } printf("\n Press any key"); fflush(stdin); getch( ); } void search( ) { int ser,count= 1,flag= 0; struct gcdll *ptr; printf("\nEnter the element to be search"); scanf("%d",&ser); ptr= head; if(ptr== NULL) { printf("\n List is empty."); return; } do { if(ptr->info == ser) { flag=1; break; } count++; ptr= ptr->right; } while(ptr->right != head); if(flag== 1) printf("\n Element found at %d position",count); else printf("\n Element not found."); getch( ); } void concate( ) { struct gcdll *ptr,*ptr1,*ptr2,*head1; if(head==NULL) { printf("\n Please create your First link list"); create( ); 215

} head1=head; head=NULL; printf("\n Please create your second link list"); create( ); ptr=head1; while(ptr->right != head1) ptr= ptr->right; ptr1= head; ptr2= ptr1->left; ptr1->left = ptr; ptr->right= ptr1; ptr2->right= head1; head1->left= ptr2; head= head1; display( ); getch( ); }

216

Q. 29 Write pseudo code and program for implementing header circular doubly linked list. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show menu of various operations to be performed on the linked list. ANS is a character variable and CHOICE is an integer variable to hold selected option input. Structure of node is as follows LEFT HCDLL Step 1: INFO RIGHT

[Displaying the menu ] Repeat While( True) Write( Implementing header circular doubly linked list) Write( 1- Creation of linked list) Write( 2- Insertion of element in the beginning ) Write( 3- Insertion of element in between) Write( 4- Insertion of element in the last) Write( 5- Deletion of element from the beginning Write( 6- Deletion of element from between) Write( 7- Deletion of element from last) Write( 8- Searching of element in the list) Write( 9- Concatenation of two different linked list) Write( 10- Display of elements in the list) Write( 11- Exit from output window) Write( Enter your choice: ) Read(CHOICE) [Calling of functions for each operation] Select Case( CHOICE) Case 1: Call CREATE( ) Case 2: Call INS_BEG( ) Case 3: Call INS_BET( ) Case 4: Call INS_LAST( ) Case 5: Call DEL_BEG( ) Case 6: Call DEL_BET( ) Case 7: Call DEL_LAST( ) Case 8: Call SEARCH( ) Case 9: Call CONCATE( ) Case 10: Call DISPLAY( ) Case 11: Write( You have chosen to exit) Write( Are you sure you want to exit) Write( If yes press y ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then EXIT(0) [Finished] Exit

Step 2:

Step 3:

217

Procedure. CREATE( ). This procedure creates grounded simply doubly linked list with initially given information. This procedure is recursive in nature and Calls itself from within to add next information to the list. PTR and PTR1 are two pointers. PTR is used for traversing through the list and PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes ANS is a character variable used for checking the choice. HEAD.RIGHT is the pointer to the first node with initial value of NULL. Structure of node is as follows LEFT HCDLL Step 1: INFO RIGHT

[Allocating memory for new node] PTR1 <= HCDLL If (PTR1 = NULL) Then Write( Memory is full ) [Entering the information] Write( Enter information: ) Read(INFO(PTR1)) [Inserting the node information linked list] If (HEAD.RIGHT = NULL) Then HEAD.RIGHT PTR1 LEFT(PTR1) RIGHT(PTR1) HEAD.RIGHt Else PTR HEAD.RIGHT Repeat While(RIGHT(PTR) NULL) PTR RIGHT(PTR) RIGHT(PTR) PTR1 RIGHT(PTR) PTR1 LEFT(PTR1) PTR RIGHT(PTR1) HEAD.RIGHT

Step 2:

Step 3:

Step 4:

[Checking for additional data] Write( Do you have more information? ) Write( If yes press y: ) Read(ANS) If ((ANS = y) or (ANS = Y)) Then Call CREATE( ) [Finished] Return

Step 5:

Procedure. INS_BEG( ). This procedure inserts given information in the beginning of list by linking the new node with the previously created node. PTR is used to traverse through the list.PTR1 is a pointer used to confirm variable used for checking the choice.LEFT and RIGHT are pointer used to confirm addition of newly allocated nodes. ANS is a character variable used for checking the choice. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows 218

LEFT HCDLL Step 1:

INFO

RIGHT

[Allocating memory for new node] PTR NULL PTR1 <= HCDLL If (PTR1 = NULL) Then Write( Memory is full ) Return

Step 2:

[Entering the information] Write( Enter information: ) Read(INFO(PTR1)) If (HEAD.RIGHT = NULL) Then HEAD.RIGHT PTR1 LEFT(PTR1) RIGHT(PTR1) NULL Step 3: [Adding the new node to the list] PTR HEAD.RIGHT RIGHT(PTR1) PTR LEFT(PTR) PTR1 PTR2 LEFT(PTR) RIGHT(PTR2) PTR1 LEFT(PTR1) PTR2 HEAD.RIGHT PTR1 Step 4: [Finished] Return

Procedure. INS_BET( ). This procedure inserts given information in between of list. PTR, PTR1and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next node. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. POS is the integer variable used for storing the entered position for insertion of node. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows LEFT HCDLL Step 1: INFO RIGHT

[Allocating memory for new node] COUNT 1 PTR1 <= HCDLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the position and information] Write( Enter the position where new node is to be inserted: ) Read(POS) Write( Enter information: ) 219

Step 2:

Read(INFO(PTR1)) PTR HEAD.RIGHT Step 3: [Inserting the new node] Repeat While((RIGHT(PTR) HEAD.RIGHT) and (COUNT < POS-1)) PTR RIGHT(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR2 RIGHT(PTR) RIGHT(PTR) PTR1 RIGHT(PTR1) PTR2 LEFT(PTR2) PTR1 LEFT(PTR1) PTR Else Write( The given position is out of range. ) Step 4: [Finished] Return

Procedure. INS_LAST( ). This procedure inserts given information in last of list . PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows LEFT HCDLL Step 1: INFO RIGHT

[Allocating memory for new node] PTR1 <= GCDLL If (PTR1 = NULL) Then Write( Memory is full) Return [Entering the information] Write( Enter information: ) Read( INFO(PTR1)) If (HEAD.RIGHT= NULL) Then PTR1 HEAD.RIGHT LEFT(PTR1) RIGHT(PTR1) HEAD.RIGHT [Inserting the new node] PTR HEAD.RIGHT Repeat While(RIGHT(PTR) HEAD.RIGHT) PTR RIGHT(PTR) PTR2 HEAD.RIGHT LEFT(PTR2) PTR1 RIGHT(PTR1) PTR2 RIGHT(PTR) PTR1 LEFT(PTR1) PTR 220

Step 2:

Step 3:

Step 4:

[Finished] Return

Procedure. DEL_BEG( ). This procedure delete the element from the beginning of list. PTR is a pointer used to transverse through the list. PTR1 is used to check deletion of node. LEFT and RIGHT are pointes used to store addresses of neighbouring nodes. HEAD is the pointer to the first node. Structure of node is as follows LEFT HCDLL Step 1: INFO RIGHT

[Checking memory] If (HEAD.RIGHT = NULL) Then Write( Link list is empty) [Deleting the node] PTR1 HEAD.RIGHT PTR RIGHT(PTR1) PTR 2 LEFT(PTR1) If (PTR = PTR2) Then HEAD.RIGHT NULL Else RIGHT(PTR2) PTR LEFT(PTR) PTR2 HEAD.RIGHT PTR Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) [Finished] Return

Step 2:

Step 3:

Procedure. DEL_BET( ). This procedure delete the element from between of list. PTR, PTR1 and PTR2 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. PTR2 points to next nodes. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighbouring node. POS is the integer variable used for storing the entered position for deletion of node. HEAD.RIGHT is the pointer which points to the first node. Structure of node is as follows LEFT INFO RIGHT HCDLL Step 1: [Checking memory] COUNT 1 If (HEAD.RIGHT = NULL) Then Write( Link list is empty ) Step 2: [Entering position for deletion] Write( Enter the position from where node is to be deleted:) Read(POS) PTR HEAD.RIGHT 221

Step 3:

Step 4:

[Deleting the node] Repeat While((RIGHT(PTR) HEAD.RIGHT) and (COUNT < POS 1)) PTR RIGHT(PTR) COUNT COUNT + 1 If (COUNT = POS -1) Then PTR1 RIGHT(PTR) PTR2 RIGHT(PTR1) RIGHT(PTR) PTR2 LEFT(PTR2) PTR Write( Element deleted was: INFO(PTR1)) Restore(PTR1) Else Write( Position given is out of range ) [Finished] Return

Procedure. DEL_LAST( ). This procedure delete the element from the last of list. PTR and PTR1 are pointers. PTR is used to transverse through the list. PTR1 is used to confirm addition of newly allocated nodes. LEFT and RIGHT are pointers used to store addresses of beighbouring nodes. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows LEFT INFO RIGHT HCDLL Step 1: [Checking memory] If (HEAD.RIGHT = NULL) Then Write( Link list is empty ) [Deleting the node] PTR1 HEAD.RIGHT If (RIGHT(PTR1) = HEAD.RIGHT) Then HEAD.RIGHT NULL Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) Else Repeat While(RIGHT(RIGHT(PTR)) HEAD.RIGHT) PTR2 LEFT(PTR) PTR2 RIGHT(PTR2) PTR1 RIGHT(PTR) If (PTR1 = PTR2) Then HEAD.RIGHT NULL Else RIGHT(PTR) PTR2 LEFT(PTR2) PTR Write( Element that was deleted is: INFO(PTR1)) Restore(PTR1) Step 3: [Finished] Return

Step 2:

222

Procedure. DISPLAY( ). This procedure display the element of the list on the screen. PTR is a pointer used to transverse through the list. COUNT is an integer variable used for keeping track of current position. LEFT and RIGHT are pointers used to store addresses of neighboring nodes. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows LEFT INFO RIGHT HCDLL Step 1: [Checking memory] COUNT 1 PTR NULL PTR HEAD.RIGHT If (PTR = NULL) Then Write( Linked list is empty) Step 2: [Displaying elements on the screen] Write( Elements in the list are:) Repeat While(RIGHT(PTR) HEAD.RIGHT) Write( Node: COUNT, Element: INFO(PTR)) COUNT COUNT + 1 PTR RIGHT(PTR) Write( Node: COUNT, Element: INFO(PTR)) [Finished] Return

Step 3:

Procedure. SEARCH( ). This procedure search the element from given the list. PTR is pointer used to transverse through the list. POS is an integer variable used for telling the position of the element in the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. SER is an integer variable used for storing the element that is to be searched. HEAD.RIGHT is the pointer to the first node. Structure of node is as follows LEFT HCDLL Step 1: INFO RIGHT

[Entering the element for search] POS 1 FLAG 0 Write(Enter the element to be searched: ) Read(SER)

Step 2:

[Search for element] PTR HEAD.RIGHT Repeat While(PTR NULL) If (INFO(PTR) = SER) Then FLAG 1 POS POS + 1 PTR RIGHT(PTR) Step 3: [Displaying the result] If (FLAG = 1) Then Write( Element found at position, POS) Else 223

Write( Element not found. ) Step 4: [Finished] Return

Procedure. CONCATE( ). This procedure join the two link list. PTR and PTR1 are pointer used to transverse through the list. LEFT and RIGHT rae pointers used to store addresses of neighbouring nodes. HEAD.RIGHT is the pointer to the first node of the first linked list. HEAD1.RIGHT is the pointer to the first node of the second linked list. Structure of node is as follows LEFT HCDLL Step 1: INFO RIGHT

[Concatenation of two different linked list] If (HEAD.RIGHT = NULL) Then Write( Please create your first linked list) Call CREATE( ) HEAD1.RIGHT HEAD.RIGHT HEAD.RIGHT NULL PTR1 HEAD.RIGHT Write( Please create your second linked list) Call CREATE( ) PTR HEAD1.RIGHT Repeat While(RIGHT(PTR) HEAD.RIGHT) PTR RIGHT(PTR) RIGHT(PTR) PTR1 LEFT(PTR1) PTR PTR2 V LEFT(PTR1) RIGHT(PTR2) HEAD1.RIGHT LEFT(HEAD1.RIGHT) PTR2 HEAD.RIGHT HEAD1.RIGHT Call DISPLAY( ) [Finished] Return

Step 2:

224

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct hcdll { int info; struct hcdll*left,*right; }; struct hcdll head; void create( ); void ins_beg( ); void ins_bet( ); void ins_last( ); void del_beg( ); void del_bet( ); void del_last( ); void search( ); void concate( ); void display( ); void main( ) { int ch; char ans; head.right=head.right=NULL; head.info=NULL; while(1) { clrscr( ); printf("\n\n Implementing Header Circular Doubly Linked List"); printf("\n----------------------------------------------- "); printf("\n\n\t 1- Creation of linked list"); printf("\n\t 2- Insertion of element in the begining"); printf("\n\t 3- Insertion of element in between"); printf("\n\t 4- Insertion of element at the last"); printf("\n\t 5- Deletion of element from begining"); printf("\n\t 6- Deletion of element from between"); printf("\n\t 7- Deletion of element from the last"); printf("\n\t 8- Searching of element in the list"); printf("\n\t 9- Concatenation of two different linked lists"); printf("\n\t 10- Display the elements in the list"); printf("\n\t 11- Exit from output window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d",&ch); switch(ch) 225

{ case 1: create( ); break; case 2: ins_beg( ); break; case 3: ins_bet( ); break; case 4: ins_last( ); break; case 5: del_beg( ); break; case 6: del_bet( ); break; case 7: del_last( ); break; case 8: search( ); break; case 9: concate(); break; case 10: display( ); break; case 11: printf("\n You have chosen to exit"); printf("\n Are you sure you want exit?"); printf("\n If yes press y."); fflush(stdin); scanf("%c",&ans); if(ans=='y'||ans=='Y') exit(0); else break; default: printf("\n\n Invalid choice..."); printf("\n Press any key to continue"); fflush(stdin); getch( ); break; } } } void create( ) { char ans; struct hcdll *ptr, *ptr1; ptr1=(struct hcdll*)malloc(sizeof(struct hcdll)); if(!ptr1) printf("\n Memory is full !"); else { printf("\n Enter information:- "); scanf("%d",&ptr1->info); if(head.right == NULL) { 226

head.right = ptr1; ptr1->left= ptr1->right= head.right; } else { ptr= head.right; ptr->left= ptr1; while(ptr->right != head.right) ptr= ptr->right; ptr->right= ptr1; ptr1->right= head.right; ptr1->left= ptr; } printf("\n Do you have more information? "); printf("\n If yes press 'y':"); fflush(stdin); scanf("%c",&ans); if(ans=='y' || ans=='Y') create( ); } } void ins_beg( ) { struct hcdll *ptr=NULL,*ptr1,*ptr2; ptr1=(struct hcdll*)malloc(sizeof(struct hcdll)); if(!ptr1) printf("\n Memory is full."); else { printf("\n Enter the information for new node:- "); scanf("%d",&ptr1->info); ptr= head.right; ptr2= ptr->left; ptr1->right= ptr; ptr2->right= ptr1; ptr1->left= ptr2; ptr->left= ptr1; head.right=ptr1; } getch ( ); } void ins_bet( ) { int pos,count=1; struct hcdll *ptr,*ptr1,*ptr2; ptr1=(struct hcdll*)malloc(sizeof(struct hcdll)); if(!ptr1) printf("\n Memory is full. "); else { printf("\n Enter the position where new node is to be inserted :"); 227

scanf("%d",&pos); printf("\n Enter the information for new node :"); scanf("%d",&ptr1->info); ptr=head.right; while((ptr->right != NULL) && (count < pos-1)) { ptr= ptr->right; count++; } if(count== pos-1) { ptr2= ptr->right; ptr->right= ptr1; ptr1->right= ptr2; ptr2->left= ptr1; ptr1->left= ptr; } else { printf("\n\n Position given is out of range !!!"); fflush(stdin); getch( ); } } getch( ); } void ins_last( ) { struct hcdll *ptr, *ptr1, *ptr2; ptr1=(struct hcdll*)malloc(sizeof(struct hcdll)); if(!ptr1) printf("\n Memory is full."); else { printf("\n Enter the information :"); scanf("%d",&ptr1->info); if(head.right== NULL) { head.right= ptr1; ptr1->left= ptr1->right= head.right; } else { ptr= head.right; while(ptr->right != head.right) ptr= ptr->right; ptr2= head.right; ptr2->left= ptr1; ptr1->right= ptr2; ptr->right=ptr1; ptr1->left= ptr; } 228

} getch( ); } void del_beg( ) { struct hcdll *ptr,*ptr1,*ptr2; if(head.right== NULL) printf("\n Linked list is empty"); else { ptr= head.right; ptr= ptr1->right; ptr2= ptr1->left; if(ptr== ptr2) head.right= NULL; else { ptr->left= ptr2; ptr2->right= ptr; head.right= ptr; } printf("\n Element that was deleted is : %d",ptr1->info); printf("\n Press any key"); fflush(stdin); getch( ); free(ptr1); } } void del_bet( ) { int pos,count=1; struct hcdll *ptr,*ptr1,*ptr2; if(head.right== NULL) printf("\n LINK LIST IS EMPTY"); else { ptr=head.right; printf("\nEnter the position from where element is to be deleted"); scanf("%d",&pos); while((ptr->right != head.right) && (count < pos-1)) { ptr= ptr->right; count++; } if(count== pos-1) { ptr1= ptr->right; ptr2= ptr1->right; ptr->right= ptr2; ptr2->left= ptr; printf("\n Element deleted was : %d",ptr1->info); 229

printf("\n Press any key."); fflush(stdin); getch( ); free(ptr1); } else printf("\n Position given is out of range "); printf("\n Press any key."); fflush(stdin); getch( ); } } void del_last( ) { struct hcdll *ptr,*ptr1,*ptr2; if(head.right== NULL) printf("\n LINK LIST IS EMPTY"); else { ptr=head.right; if(ptr->right== head.right) { head.right=NULL; printf("\n Node to be deleted is %d",ptr->info); printf("\n Press any key"); fflush(stdin); getch( ); free(ptr); } else { while(ptr->right->right!=head.right) ptr1= ptr1->right; ptr= ptr->right; ptr1= ptr->left; ptr2= ptr1->left; ptr2->right= ptr; ptr->left= ptr2; printf("\n Element deleted was : %d",ptr1->info); fflush(stdin); getch( ); free(ptr1); } } getch( ); } void display( ) { int count=1; struct hcdll *ptr=NULL; ptr=head.right; 230

if(ptr== NULL) printf("\n LINK LIST IS EMPTY"); else { printf("\n Elements in the list are :-\n"); while(ptr->right != head.right) { printf("\n Node:%d Element:%d",count,ptr->info); count++; ptr= ptr->right; } printf("\n Node:%d Element:%d",count,ptr->info); } printf("\n Press any key"); fflush(stdin); getch( ); } void search( ) { int ser,count=1,flag=0; struct hcdll *ptr; printf("\nEnter the element to be search"); scanf("%d",&ser); ptr=head.right; if(ptr== NULL) { printf("\n List is empty."); return; } do { if(ptr->info== ser) { flag=1; break; } count++; ptr= ptr->right; } while(ptr->right != head.right); if(flag== 1) printf("\n Element found at %d position",count); else printf("\n Element not found."); getch( ); } void concate( ) { struct hcdll *ptr,*ptr1,*ptr2,head1; if(head.right== NULL) 231

{ printf("\n Please create your First link list"); create( ); } head1.right= head.right; head.right= NULL; printf("\n Please create your second link list"); create( ); ptr= head1.right; while(ptr->right != head1.right) ptr= ptr->right; ptr1= head.right; ptr2= ptr1->left; ptr1->left= ptr; ptr->right= ptr1; ptr2->right= head1.right; head1.right->left= ptr2; head.right= head1.right; display( ); getch( ); }

232

Q.30. Ans.

Write pseudo code and program for implementing stacks using linked list.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operations on the stack and Calling of all functions. TOP is a pointer that points to the element on the top of stack and is initialized to NULL, DATA INFO STACK Step 1: [Displaying the menu] TOP NULL Repeat While(True) Write( Implementing stacks using linked llist) Write( 1- Push an element into stack) Write( 2- Pop an element from the stack) Write(3- Display the stack) Write(4- Exit from output window) Write( Enter your choice: ) Read(CHOICE) Step 2: [Calling the functions] Select Case( CHOICE) Case 1: Write( Enter the element to be pushed into the stack) Read(DATA) Call PUSH(DATA) Case 2: DATA Call POP( ) If (DATA -99) Then Write( Element popped is: DATA) Case 3: Call DISPLAY( ) Case 4: Write( Sure you want to exit? If yes press y) Read(ANS) If (ANS = y or ANS = Y) Then EXIT(0) Default: Write( Invalid choice) Step 3: [Finished] Exit LINK

Procedure. PUSH(X). This procedure is inputting or pushing an element passed on as parameter into the stack. Here TOP is a pointer that points to the element on the top of the stack. X is the variable containing the element. PTR1 checks the allocation of addition of new node. INFO STACK Step 1: [Checking memory for allocation] PTR1 <= STACK 233 LINK

If (PTR1 = NULL) Then Write(Memory is full) Step 2: [Inputting or pushing the element] INFO(PTR1) X LINK(PTR1) NULL If (TOP = NULL) Then TOP PTR1 Else LINK(PTR1) TOP TOP PTR1 Write( Element was pushed into stack: ) Step 3: [Finished] Return Procedure. POP( ). This procedure pops or deletes inputted elements from the top of the stack. Here TOP points to the stack and DUMMY is an integer variable to store element temporarily. PTR is used to traverse through the list. Structure of node is as follows INFO STACK [Sorting of elements in the array.] If (TOP = NULL) Then Write(Stack is empty) Else PTR TOP TOP LINK(PTR) DUMMY INFO(PTR) Restore(PTR) Step 2: [Finished] Return(DUMMY) Step 1: Procedure. DISPLAY( ). This procedure is displaying the elements of stack on the screen . Here PTR is used to traverse through the list and TOP is a pointer that points to the element on the top of the stack. Structure of node is as follows INFO Step 1: LINK LINK

STACK [Displaying the elements on the screen.] If (TOP = NULL) Then Write( Stack is empty) Else PTR TOP Write( Elements in stack are: ) Repeat While(PTR NULL) Write(INFO(PTR)) PTR LINK(PTR0

Step 2:

[Finished] 234

Return

235

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct stack { int info; struct stack*link; }*top=NULL; void push(int); int pop( ); void display( ); void main( ) { int ch,data; char ans; while(1) { clrscr( ); printf("\n Implementing Stacks using linfed lists."); printf("\n----------------------------------------- "); printf("\n\n\t 1- Push an element into stack."); printf("\n\t 2- Pop an element from the stack."); printf("\n\t 3- Display elements of the stack."); printf("\n\t 4- Exit from output window."); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter element to be pushed into stack :- "); fflush(stdin); scanf("%d",&data); push(data); break; case 2: data= pop( ); if(data != -99) printf("\n Element popped is : %d",data); printf("\n\n Press any key to continue..."); fflush(stdin); getch( ); break; 236

case 3: display( ); printf("\n Press any key to continue."); fflush(stdin); getch( ); break; case 4: printf("\n You have chossen to exit. "); printf("\n Sure you want to exit ? If yes press y"); fflush(stdin); scanf("%c",&ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n Invalid condition..."); getch( ); break; } } } void push(int x) { struct stack*ptr1; ptr1=(struct stack*)malloc(sizeof(struct stack)); if(!ptr1) printf("\n\n Stack's memory is full 1"); else { ptr1->info = x; ptr1->link =NULL; if(top == NULL) top = ptr1; else { ptr1->link =top; top= ptr1; } printf("\n Element was pushed into the stack."); } getch( ); } int pop( ) { struct stack*ptr; int dummy=-99; if(top == NULL) printf("\n\n Stack is empty! \n There is no element to be popped."); else 237

{ ptr= top; top= ptr->link; dummy= ptr->info; free(ptr); } return(dummy); } void display( ) { struct stack*ptr; if(top == NULL) printf("\n\n Stack is empty."); else { ptr = top; printf("\n\n Elements in the stack :- \n"); while(ptr != NULL) { printf("\n %d",ptr->info); ptr= ptr->link; } } getch( ); }

238

Q. 31 Write pseudo code and program for implementing queue using linked list. Ans.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operations on the queue and Calling of all functions. FROMT is a pointer that points to the element in the front of queue. Both are initialized to NULL. DATA contains the element to be pushed or popped. CHOICE and ANS are variables for storing chosen option. Structure of a node is INFO QUEUE Step 1: [Displaying the menu] FRONT REAR NULL Repeat While(True) Write( Implementing Queue using linked list) Write( 1- Insert an element into queue) Write( 2- Delete an element from the queue) Write( 3- Display the queue) Write( 4- Exit from output window) Read(CHOICE) [Calling the functions] Select Case( CHOICE) Case 1: Write( Enter the element to be pushed into the stack) Read(DATA) Call INSERT(DATA) Case 2: DATA Call DEL( ) If (DATA -99) Then Write( Element popped is: DATA) Case 3: Call DISPLAY( ) Case 4: Write( Sure you want to exit? If yes press y) Read(ANS) If (ANS = y or ANS = Y) Then EXIT(0) Default: Write( Invalid choice) Step 3: [Finished] Exit LINK

Step 2:

Procedure. INSERT(X). This procedure is inputting an element passed on as parameter into the queue. FRONT is a pointer that points to the element in the front of queue while REAR is a pointer pointing to the end of queue. X is the variable containing the elemant. PTR1 checks the allocation of addition of new node. Structure of node is as follows 239

INFO QUEUE Step 1: [Checking memory for allocation] PTR1 <= QUEUE If (PTR1 = NULL) Then Write( Memory is full)

LINK

Step 2:

[Inputting or inserting the element] INFO(PTR1) X LINK(PTR1) NULL If (REAR = NULL) Then FRONT REAR PTR1 Else LINK(REAR) PTR1 REAR PTR1 Write( Element was added into the list) [Finished] Return

Step 3:

Procedure. DEL( ). This procedure deletes inputted elements from the front of the queue. FRONT is a pointer that points to the element in the front of queue while points to the element in the front of the queue while REAR is a pointer pointing to the end of queue. DUMMY is an integer variable to store element temporarily. PTR1 is used to check the deletion of node. Structure of node is as follows INFO QUEUE Step 1: [Sorting of element in the array] PTR1 FRONT If (PTR1 = NULL) Then Write( Queue is empty. Deletion not possible) Return(-99) Else DUMMY INFO(PTR1) If (LINK(PTR1) = NULL) Then FRONT REAR NULL Else FRONT LINK(PTR1) Restore(PTR1) Step 2: [Finished] Return(DUMMY) This produre is displaying the elements of queue. Here PTR is 240 LINK

Procedure. DISPLAY( ).

used to traverse through the list. FRONT is a pointer that points to the element in the front of queue while REAR is a pointer pointing to the end of queue. COUNT is a n integer variable keeping count of current node. Structure of node is as follows INFO QUEUE Step 1: [Displaying the elements on the screen] COUNT 1 PTR FRONT If (PTR = NULL) Then Write( Queue is empty) Else Write( Elements in stack are: ) Repeat While(PTR NULL) Write(INFO(PTR)) PTR LINK(PTR) COUNT COUNT + 1 Step 2: [Finished] Return LINK

241

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 void insert(int); int del( ); void display( ); struct queue { int info; struct queue*link; }*front,*rear; void main( ) { int ch,data; char ans; front=rear=NULL; while(1) { clrscr( ); printf("\n Implementing Queue using linked list. "); printf("\n---------------------------------------"); printf("\n 1- Insert an element into queue."); printf("\n 2- Delete an element from the queue."); printf("\n 3- Display the elements of the queue."); printf("\n 4- Exit from output wimdow."); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter element to be inserted into queue :- "); fflush(stdin); scanf("%d",&data); insert(data); break; case 2: data= del( ); if(data != -99) printf("\n Element deleted is : %d",data); printf("\n\n Press any key to continue."); fflush(stdin); getch( ); break; 242

case 3: display( ); printf("\n\n Press any key to continue."); fflush(stdin); getch( ); break; case 4: printf("\n You have chossen to exit"); printf("\n Are you sure you want to exit?"); printf("\n If yes press y."); fflush(stdin); scanf("%c",&ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n\n Invalid choice."); getch( ); break; } } } void insert(int x) { struct queue*ptr1; ptr1=(struct queue*)malloc(sizeof(struct queue)); if(!ptr1) printf("\n\n Queue is ful."); else { ptr1->info = x; ptr1->link = NULL; if(!rear) front= rear= ptr1; else { rear->link = ptr1; rear= ptr1; } printf("\n New element was added to list."); } } int del( ) { int dummy; struct queue*ptr1; ptr1= front; 243

if(!ptr1) { printf("\n\n Queue is empty."); printf("\n Deletion is not possible !!! "); return(-99); } else { dummy= ptr1->info; if(ptr1->link == NULL) front= rear= NULL; else front= ptr1->link; free(ptr1); return(dummy); } } void display( ) { int count= 1; struct queue*ptr; ptr= front; if(!ptr) printf("\n\n Queue is empty"); else { printf("\n Elements in queue are : \n"); while(ptr != NULL) { printf("\n\t %d %d ", count, ptr->info); ptr= ptr->link; count++; } } getch( ); }

244

Q. 32. Ans.

Write pseudo code and program for implementing Josephus problem.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm uses various procedures to be performed on the linked list to find out a lucky winner in a game. N is a variable used to keep number of count and ANS is an integer variable to hold selected option input. C is a pointer variable. Structure of node is as follows INFO PASS Step 1: [Calling of functions] Call CREATE( ) Call DISPLAY( ) [Entering number for count] Write( Enter number for count: ) Read(N) [Displaying the winner] C Call DEL(N) Write( The winner is: ) [Finished] Return LINK

Step 2:

Step 3:

Step 4:

Procedure. CREATE( ). This procedure creates linked list with initially given information. This procedure is recursive in nature and Calls itself from within to add next information to the list. PTR and PTR1 are pointers. PTR is used for traversing through the list and PTR1 is used to confirm addition of newly allocated nodes. ANS is a character variable used for checking the choice. HEAD is the pointer to the first node with initial value of NULL. Structure of node is as follows INFO PASS Step 1: [Allocating memory] PTR1 <= PASS If (PTR1 = NULL) Then Write( Memory is full) [Entering names of participating candidates] Write( Enter candidates name: ) Read(NAME(PTR1)) [Inserting the node information in linked list] If (HEAD= NULL) 245 LINK

Step 2:

Step 3:

Then HEAD PTR1 LINK(PTR1) HEAD Else PTR HEAD Repeat for While(LINK(PTR) HEAD) PTR LINK(PTR) LINK(PTR) PTR1 LINK(PTR1) HEAD Write( Do you have more names? If yes press Y) Read(ANS) If (ANS = y or ANS = Y) Then Call CREATE( ) Function. DEL(N). This function deletes information from the list. PTR is a pointer used to transverse through the list. PTR1 is used to check allocation of new node. DUMMY is a temporary pointer used to store name. COUNT is an integer variable used for keeping track of current position. HEAD is the pointer to the first node. Structure of node is as follows INFO Step 1: LINK

PASS [Deleting names according to count] PTR HEAD Repeat for While(LINK(PTR) PTR) COUNT 1 Repeat for While(COUNT < N) PTR1 PTR PTR LINK(PTR) COUNT COUNT + 1 LINK(PTR1) LINK(PTR) Restore(PTR) PTR LINK(PTR1) DUMMY NAME(PTR) Step 2: [Finished] Return(DUMMY) Procedure. DISPLAY( ). This procedure inserts given information in between of list. PTR1 is a pointer used to transverse for keeping track of current position. HEAD is the pointer to the first node. Structure of node is as follows INFO Step 1: LINK

PASS [Checking list availability] PTR1 HEAD Write( Elements in list are: ) Repeat for While(LINK(PTR1) HEAD) Write(NAME(PTR1)) PTR1 LINK(PTR1) Write(NAME(PTR1)) [Finished] 246

Step 3:

Return

247

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct pass { char name[20]; struct pass*link; }*head= NULL; void create( ); char *del(int); void display( ); void main( ) { int n; char ans,*c; clrscr( ); create( ); display( ); printf("\n Enter the number for count :"); fflush(stdin); scanf("%d",&n); c= del(n); printf("\n The lucky winner is : %s", c); getch( ); } void create( ) { struct pass *ptr,*ptr1; char ans; clrscr( ); ptr1= (struct pass*)malloc(sizeof(struct pass)); if(!ptr1) printf("\n\n Memory is Full!!"); else { printf("\n Enter the candidate's name: "); fflush(stdin); gets(ptr1->name); if(head== NULL) { head= ptr1; ptr1->link = head; } else { 248

ptr= head; while(ptr->link != head) ptr = ptr->link; ptr->link = ptr1; ptr1->link = head; } printf("\n Do you have more name? \n If press 'Y': "); fflush(stdin); scanf("%c",&ans); if(ans == 'y' || ans == 'Y') create( ); else display( ); } getch( ); } char *del(int n) { int count; char *dummy; struct pass *ptr,*ptr1; ptr=head; while(ptr->link != ptr) { count=1; while(count < n) { ptr1= ptr; ptr= ptr->link; count++; } ptr1->link = ptr->link; free(ptr); ptr= ptr1->link; } dummy= ptr->name; return(dummy); } void display( ) { struct pass *ptr1; ptr1= head; if(!ptr1) printf("\n Linked list is empty !!!"); else { printf("\n Elements in list are: \n"); while(ptr1->link != head) { printf("\n\t %s", ptr1->name); ptr1= ptr1->link; 249

} printf("\n\t %s",ptr1->name); } getch( ); }

250

Q. 33. Ans.

Write pseudo code and program for implementing Towers of Hanoi.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is to Call procedures in order to move the given number of disks from one needle to another. N is an integer variable for storing number of disks. Step 1: [Entering number of disks and Calling function] Write( Enter number of disks to be moved: ) Read(N) Call MOVE(N, 1, 3, 2) [Finished] Exit

Step 2:

Procedure. MOVE(N, A, B, C). This procedure moves disks from one needle to another needle. A, B and C are integer variables holding the number of needles 1, 3 and 2 respectively. N is the integer variable holding number of disks to be moved. Step 1: [Moving the disks] If (N > 0) Then Call MOVE(N-1, A, C, B) Write( Move disks from to , A, B) Call MOVE(N-1, C, B, A) [Finished] Return

Step 2:

251

Program Coding
#include<stdio.h> #include<conio.h> void move(int,int,int,int); void main( ) { int n; clrscr( ); printf("\n Enter the number of disks to be moved : "); fflush(stdin); scanf("%d",&n); move(n, 1, 3, 2); getch( ); } void move(int n,int a,int b,int c) { if(n > 0) { move(n-1, a, c, b); printf("\n Move the disk from a to b: %d %d", a, b); move(n-1, c, b, a); } getch( ); }

252

Q. 34. Ans.

Write pseudo code and program for implementing string palindrome using array.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm shows different operations for checking a string palindrome in a stack. I is the loop control variable. L contains length of string. A represents the stack. ANS is a character variable for storing selected option by the user. Step 1: [Entering string] Write( Implementing palindrome check using stack. ) Write( Enter your string: ) Read(A) [Entering string into stack and checking it] L STALEN(A) Repeat for I 1, 2, 3, , L Call PUSH(A[I]) Call PALIN(L) Write( Do you want to enter more string? If yes, press y: ) Read(ANS) If (ANS y or ANS Y) Then EXIT(0) [Finished] EXIT

Step 2:

Step 2:

Procedure. PUSH(C). This procedure pushes or inserts each element into the stack. C is a character variable that contains an element. A represents the stack. TOP is a pointer pointing to stacks front or top. Step 1: [Pushing elements into stack] If (TOP = 29) Then Write( Stack overflow) Else TOP TOP + 1 B[TOP] C [Finished] Return

Step 2:

Procedure. POP( ). This procedure pops or deletes each element from the stack. DUMMY is a temporary integer variable that contains an element. A represents the stack. TOP is a pointer pointing to stacks front or top. Step 1: [Popping elements from stack] If (TOP = 0) Then Write( Stack underflow) Return( -99) Else DUMMY B[TOP] TOP TOP 1 253

Step 2:

[Finished] Return(DUMMY)

Procedure. PALIN(X). This procedure checks the string in stack to see if it is a palindrome or not. X is an integer variable that contains the strings length. A and B represents the stack. DUMMY and FLAG are temporary character and integer variables. I is a loop control variable. Step 1: [Checking elements in the stack] Repeat for I 1, 2, 3, , X DUMMY Call POP( ) If (DUMMY A[I]) Then FLAG 1 If (FLAG = 1) Then Write( String _ is not a palindrome, A) Else Write( String _ is a palindrome, A) [Finished] Return

Step 2:

254

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> int top= -1 char a[30],b[30]; void push(char); char pop( ); void palin(int); void main( ) { int i,l; char ans; while(1) { clrscr( ); printf("\n\t Implementing palindrome check using stack."); printf("\n\t--------------------------------------------"); printf("\n\n Enter your string:- "); fflush(stdin); gets(a); l=strlen(a); for(i= 0; i<l i++) push(a[i]); palin(l); printf("\n\n Do you want to enter more string? "); printf("\n If yes, press 'y' "); fflush(stdin); scanf("%c",&ans); if(ans != y || ans != Y) exit(0); } } void push(char c) { if(top==29) printf("\n\n Stack overflow"); else { top++; b[top]=c; } } char pop( ) { char dummy; if(top== -1) 255

{ printf("\n\n Stack underflow"); getch( ); return(-99); } else { dummy=b[top]; top--; return(dummy); } } void palin(int x) { int i,flag=0; char dummy; for(i= 0;i< x; i++) { dummy= pop( ); if(dummy != a[i]) { flag=1; break; } } if(flag == 1) printf("\n\n The string %s is not a palindrome...",a); else printf("\n\n The string %s is a palindrome...",a); getch( ); }

256

Q. 35. Ans.

Write pseudo code and program for implementing binary search tree.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show menu of various operations to be performed on the binary search tree. ANS is a character variable and CHOICE is an integer variable to hold selected option input. Z and T (structure pointers) are variables used to store returned values. Structure of node is as follows LEFT INFO RIGHT Step 1: [Displaying the menu] Repeat While(True) Write('Implementing binary search tree') Write(' 1- .Creation of tree') Write(' 2-.Insertion of element') Write(' 3-.Deletion'of element) Write(' 4-.Complete check of tree') Write(' 5-.To find maximum element of the tree') Write( 6- To find.minimum element of the tree') Write(' 7-.Search for an element in the tree) Write(' 8-.Preorder traversing (NLR) in tree) Write(' 9-.Inorder traversing (LNR) in tree') Write(' 10- Postorder traversing (LRN) in tree) Write(' 11- Reverse Preorder traversing(NRL) in tree) Write(' 12-.Reverse Inorder traversing (RNL) in tree) Write(' 13- Reverse Postorder traversing(RLN) in tree') Write(' 14-.To find count of leaf in the tree) Write(' 15-.To find count of nodes in the tree) Write(' 16-.To find height of the tree)') Write(' 17- EXIT from output window') Write(Enter your choice) Read(CHOICE) Calling of functions for each operation] Select Case(CHOICE) Case 1: Call CREATE( ) Case 2: Call INSERT( ) Case 3: Call DELETION( ) Case 4: Call COMPL_CHECK( ) Case 5: Z Call MAX( ) Write( Maximum Value is: Z) Case 6: Z Call MIN( ) Write( Minimum Value is: Z) Case 7: Write( Enter value to be searched) Read(Z) T Call SEARCH( ) If (T) Then Write(Element found at location : T) Case 8: Write(Elements in tree after preorder traversing is :) 257

BST

Step 2:

Call PRE_ORDER(ROOT) Case 9: Write(Elements in tree after inorder traversing is:) Call IN_ORDER(ROOT) Case 10: Write(Elements in tree after postorder traversing is:) Call POST_ORDER(ROOT ) Case 11: Write(Elements in tree after Reverse preorder traversing :) Call RPREORDER(ROOT) Case 12: Write( Elements in tree after Reverse inorder traversing is:) Call RINORDER(ROOT) Case 13: Write(Elements in tree after Reverse postorder traversing :) Call RPOSTORDER(ROOT) Case 14: Call COUNT_OF_LEAF( ROOT) Write( Number of leaves in tree is:,COUNT) Case 15: Call COUNT_OF_NODES( ) Write(Number of nodes in tree is:COUNT) Case 16: Z HEIGHT(ROOT) Write(Height of the tree is:, Z) Case 17: Write(Ypu have chosen to exit) Write(Sure you want to exit? If yes press y) Read(ANS) If (ANS = y or ANS = Y) Then EXIT(0) Default: Write(Invalid choice ) Step 3: [Finished] Exit Procedure. CREATE( ). This procedure creates binary search tree with initially given information. This procedure is recursive in nature and Calls itself from within to add next information to the list. PTR1, PTR2 and PTR3 are pointers. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. PTR1 is used for traversing through the list and PTR2 is used to point to the newly allocated node. PTR3 is used to locate the parent node for the new node. ANS is a character variable used for checking the choice. KEY is an integer field in structure for storing information of the list. Root is the pointer to the first node with value of NULL. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Allocating memory for new node] PTR2 BST If (PTR2 = NULL) Then Write(Memory is full) Return Step 2: [Entering the information] Write( Enter information:) Read(KEY(PTR2)) LEFT(PTR2) RIGHT(PTR2) NULL Step 3: [Inserting the node information linked list] If (ROOT = NULL) Then ROOT PTR2 Else PTR1 ROOT 258

Repeat While(PTR1 NULL) PTR3 PTR1 If (KEY(PTR!) < KEY(PTR2)) Then PTR1 RIGHT PTR1 Else PTR1 LEFT (PTR1) Step 4: [Checking for additional data] Write( Do you have more information) Write(If yes press y) Read(ANS) If (ANS = y or ANS = Y) Then Call AREATE( ) Step 3: [Finished] Return Procedure. INSERT( ). This procedure inserts given information in the list by linkining the new node with the previously created node. PTR1, PTR2 and PTR3 are pointers. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. PTR1 is used for traversing through the list and PTR2 is used to point to the newly allocated node. PTR3 is used to locate the parent node for the new node. ANS is a character variable used for checking the choice. KEY is an integer field in structure for storing information of the list. Root is the pointer to the first node with value of NULL. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Allocating memory for new node] PTR2 BST If (PTR2 = NULL) Then Write(Memory is full) Return [Entering the information] Write( Enter information:) Read(KEY(PTR2)) LEFT(PTR2) RIGHT(PTR2) NULL [Inserting the node information linked list] If (ROOT = NULL) Then ROOT PTR2 Else PTR1 ROOT Repeat While(PTR1 NULL) PTR3 PTR1 If (KEY(PTR!) < KEY(PTR2)) Then PTR1 RIGHT PTR1 Else PTR1 LEFT (PTR1) If (XKEY(PTR3) < KEY(PTR2)) Then RIGHT(PTR3) PTR2 Else LEFT(PTR3) PTR2 259

Step 2:

Step 3:

Step 4:

[Finished] Return Procedure. RPOSTORDER(BST*PTR). This procedure is recursive in nature and Calls itself from within to print next information in the list. In this procedure information from its right and left is printed respectively, then the parent node information is printed. PTR is a pointer used for pointing and traversing through the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. ROOt is the pointer to the first node with initial value of NULL. KEY is an integer field in structure for storing information of the list. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Checking left and right nodes] If (RIGHT(PTR)) Then Call RPOSTORDER(RIGHT(PTR)) If (LEFT(PTR)) Then Call RPOSTORDER(LEFT(PTR))

Step 2:

[Checking value of ptr] If (PTR) Then Write(KEY(PTR)) [Finished] Return

Step 3:

Procedure. RINORDER(BST*PTR). This procedure is recursive in nature and Calls itself from within to printf next information in the list. In this procedure information from right, parent node and then left node information are printed respectively. PTR is a pointer used fro pointing and traversing through the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. ROOT is the pointer to the first node with initial value of NULL. KEY is an integer field in structure for storing information of the list. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Checking left node] If (RIGHT(PTR)) Then Call RINORDER(RIGHT(PTR)) Step 2: [Checking value of ptr] If (PTR) Then Write(KEY(PTR)) Step 3: [Checking right node] If (LEFT(PTR)) Then Call RINORDER(LEFT(PTR)) Step 4: [Finished] 260

Return Procedure. RPREORDER(BST*PTR). This procedure is recursive in nature and Calls itself from within to print next information in the list. In this procedure information from its parent node is printed respectively. PTR is a pointer used fro pointing and traversingthrough the list. LEFT and RIGTH are pointers used to store addresses of neighbouring nodes. ROOT is the pointer to the first node with initial value of NULL. KEY is an integer field in structure for storing information of the list. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Checking value of PTR] If (PTR) Then WRITE(KEY(PTR)) Step 2: [Checking left and right node] If (RIGHT(PTR)) Then Call RPREORDER(RIGHT(PTR)) If (LEFT(PTR)) Then Call RPREORDER(LEFT(PTR)) [Finished] Return

Step 3:

Procedure. POSTORDER(BST*PTR). This procedure is recursive in nature and Calls itself from within to print next information in the list. In this procedure information from its left and right node information are printed respectively then information of parent node is printed. PTR is a pointer used for pointing and traversing through the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. ROOT is the pointer to the first node with initial value of NULL. Key is an integer field in structure for storing for information of the list. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Checking left and right nodes] If (LEFT(PTR)) Then Call POSSTORDER(LEFT(PTR)) If (RIGHT(PTR)) Then Call POSTORDER(RIGHT(PTR))

Step 2:

[Checking value of PTR] If (PTR) Then Write(KEY(PTR)) [Finished] Return 261

Step 3:

Procedure. INORDER( BST*PTR ). This procedure is recursive in nature and Calls itself from within to print next information in the list. In this procedure information from its left, parent node and then right node information are printed respectively. PTR is a pointer used for pointing and traversing through the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. ROOT is the pointer to the first node with initial value of NULL. KEY is an integer field in structure for storing information of the list. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Checking left node] If (LEFT(PTR)) Then Call INORDER(LRFT(PTR)) [Checking value of PTR] If (PTR) Then Write(KEY(PTR)) [Checking right node] If (RIGHT(PTR)) Then Call INORDER(RIGHT(PTR)) [Finished] Return

Step 2:

Step 3:

Step 4:

Procedure. PREORDER(BST*PTR). This procedure is recursive in nature and Calls itself from within to print next information in the list. In this procedure information from within to print next information are printed respectively. PTR is a pointer used for pointing and traversing through the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. . ROOT is the pointer to the first node with initial value of NULL. KEY is an integer field in structure for storing information of the list. Structure of node is as follows LEFT Step 1: [Checking value of PTR] If (PTR) Then Write(KEY(PTR)) [Checking left and right nodes] If (LEFT(PTR)) Then Call PREORDER(LEFT(PTR)) If (RIGHT(PTR)) Then Call PREORDER(RIGHT(PTR)) Step 3: [Finished] 262 INFO RIGHT BST

Step 2:

Return Procedure. SEARCH(X). This procedure is used to search for an element in the existing list. PTR is a pointer used for pointing and traversing through the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. . ROOT is the pointer to the first node with initial value of NULL. KEY is an integer field in structure for storing information of the list. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Searching for element] PTR ROOT Repeat While((PTR) and (KEY(PTR) X) If (X < KEY(PTR)) Then PTR LEFT(PTR) Else PTR RIGHT(PTR) If (PTR = NULL) Then Write( Search was unsuccessful) [Finished] Return(PTR)

Step 2:

Procedure. MAXIMUM( ). This procedure is used to print the maximum value from the list. PTR is a pointer used for pointing and traversing through the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. . ROOT is the pointer to the first node with initial value of NULL. KEY is an integer field in structure for storing information of the list. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Finding maximum element of tree] PTR ROOT Repeat While(RIGHT(PTR) NULL) PTR RIGHT(PTR) F KEY(PTR) [Finished] Return(F)

Step 2:

Procedure. MINIMUM( ). This procedure is used to print the minimum value from the list. PTR is a pointer used for pointing and traversing through the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. . ROOT is the pointer to the first node with initial value of NULL. KEY is an integer field in structure for storing information of the list. Structure of node is as follows

263

LEFT Step 1:

INFO

RIGHT

BST

[Finding maximum element of tree] PTR ROOT Repeat While(LEFT(PTR) NULL) PTR LEFT(PTR) F KEY(PTR) [Finished] Return(F)

Step 2:

Procedure. COUNT_OF_NODES(BST*PTR). This procedure is recursive in nature and Calls itself from within to print the number of nodes present in the list. PTR is a pointer used for pointing and traversing through the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. . ROOT is the pointer to the first node with initial value of NULL. COUNT is a variable used to keep track of current position. KEY is an integer field in structure for storing information of the list. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Counting number of nodes] If (PTR NULL) Then COUNT COUNT + 1 If (LEFT(PTR) NULL) Then Call COUNT_OF_NODES(KEFT(PTR)) If (RIGHT(PTR) NULL) Then Call COUNT_OF_NODES(RIGHT(PTR)) [Finished] Return

Step 2:

Procedure. COUNT_OF_LEAF(BST*PTR). This procedure is recursive in nature and Calls itself from within to print the number of leaves(nodes having no child) present in the list. PTR is a pointer used for pointing and traversing through the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. . ROOT is the pointer to the first node with initial value of NULL. C is a variable used to keep track of current position. KEY is an integer field in structure for storing information of the list. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Counting number of leaves] If (PTR NULL) Then If ((LEFT(PTR) + NULL) and (RIGHT(PTR) = NULL)) 264

Then If Then If Then Step 2: [Finished] Return

CC+1 (LEFT(PTR) NULL) Call COUNT_OF_LEAF(LEFT(PTR)) (RIGHT(PTR) NULL) Call COUNT_OF_LEAF(RIGHT(PTR))

Procedure. HEIGHT(BST*PTR). This procedure is recursive in nature and Calls itself from within the list. LEFT and RIGHT are pointers used to store addresses of neighbouring nodes. X an Y are variable used to store height of left and right side respectively. ROOT is the pointer to the first node with initial value of NULL. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Calculating the height of tree] If (PTR C) Then X Call HEIGHT(LEFT(PTR)) Y Call HEIGHT(RIGHT(PTR)) If (X > Y) Then Return(X + 1) Else Return(Y + 1) Step 2: [Finished] Return(0) Procedure. COMPL_CHECK( ). This procedure is used to check the completeness ( count of number of leaf nodes in terms of 2n) of the tree. LEFT and RIGHT are pointers used to store addresse of neighbouring nodes. H and G are variables for storing height and number of nodes respectively. ROOT is the pointer to the first node with initial value of NULL. C is a variable used to keep track of current position. Structure of node is as follows LEFT Step 1: INFO RIGHT BST

[Completely checking the tree] H HEIGHT(ROOT) G Call POW(2, H 1) C0 Call COUNT_OF_LEAF(ROOT) If (G = C) Then Write( Binary tree is complete) Else Write(Binary tree is not complete) [Finished] Return 265

Step 2:

Program Coding

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> #define NULL 0 struct bst { int key; struct bst *left,*right; }; struct bst *root=NULL; struct bst *parent; void create( ); void insert( ); void compl_check( ); int maximum( ); int minimum( ); struct bst *search(int x); void preorder(struct bst *ptr); void inorder(struct bst *ptr); void postorder(struct bst *ptr); void rpreorder(struct bst *ptr); void rinorder(struct bst *ptr); void rpostorder(struct bst *ptr); void count_of_leaf(struct bst *ptr); void count_of_nodes(struct bst *ptr); int height(struct bst *ptr); int count= 0,c= 0; struct bst *a[30]; void main( ) { int ch,z; struct bst *t; while(1) { clrscr( ); printf("\n\t Implementing Binary Search Tree"); printf("\n\t---------------------------------"); printf("\n\n\t 1- Creation of tree"); printf("\n\t 2- Insertion of element"); printf("\n\t 3- Complete check of tree"); printf("\n\t 4- To find maximum value of tree"); printf("\n\t 5- To find minimum value of tree"); printf("\n\t 6- Search for an element in the tree"); printf("\n\t 7- Preorder traversing(NLR) in tree"); printf("\n\t 8- Inorder traversing(LNR) in tree"); printf("\n\t 9- Postorder traversing(LRN) in tree"); printf("\n\t 10- Reverse Preorder traversing(NRL) in tree"); 266

printf("\n\t 11- Reverse Inorder traversing(RNL) in tree"); printf("\n\t 12- Reverse Postorder traversing(RLN) in tree"); printf("\n\t 13- To find count of leaf in the tree"); printf("\n\t 14- To find count of nodes in the tree"); printf("\n\t 15- To find the height of the tree"); printf("\n\t 16- Exit from output window"); printf("\n\n Enter your choice:- "); scanf("%d",&ch); switch(ch) { case 1: create( ); break; case 2: insert( ); break; case 3: compl_check( ); break; case 4: z=maximum( ); printf("\n Maximum value in tree is : %d",z); getch( ); break; case 5: z=minimum( ); printf("\n Minimum value in tree is : %d",z); getch( ); break; case 6: printf("\n Enter value to be searched: "); scanf("%d",&z); t=search(z); if(t) printf("\n Element found at address: %d",t); getch( ); break; case 7: printf("\n Elements in tree after Preorder traversing is :"); preorder(root); getch( ); break; case 8: printf("\n Elements in tree after Inorder traversing is :"); inorder(root); getch( ); break; case 9: printf("\n Elements in tree after Postorder traversing is :"); postorder(root); getch( ); break; case 10: printf("\n Elements in tree after Reverse Preorder traversing "); rpreorder(root); getch( ); break; case 11: printf("\n Elements in tree after Reverse Inorder traversing is:"); rinorder(root); getch( ); 267

break; case 12: printf("\n Elements in tree after Reverse Postorder traversing:"); rpostorder(root); getch( ); break; case 13: count_of_leaf(root); printf("\n Number of leaves in tree is:%d",count); getch( ); break; case 14: count_of_nodes(root); printf("\n Number of nodes in tree is:%d",count); getch( ); break; case 15: z=height(root); printf("\n Height of the tree is:%d",z); getch( ); break; case 16: exit(0); break; default: printf("\n Invalid choice"); printf("\n Press any key to continue"); fflush(stdin); getch( ); break; } } } void create( ) { char ans; struct bst *ptr1,*ptr2,*ptr3; ptr2=(struct bst *)malloc(sizeof(struct bst)); if(!ptr2) printf("\n Memory is full"); else { printf("\n Enter information:"); scanf("%d",&ptr2->key); ptr2->left= ptr2->right= NULL; if(root == NULL) root= ptr2; else { ptr1= root; while(ptr1) { ptr3= ptr1; if(ptr1->key < ptr2->key) ptr1= ptr1->right; else 268

ptr1= ptr1->left; } if(ptr3->key < ptr2->key) ptr3->right = ptr2; else ptr3->left = ptr2; } printf("\n Do you have more information?"); printf("\n If yes press y:"); fflush(stdin); scanf("%c",&ans); if(ans == 'y' || ans == 'Y') create( ); } getch( ); } void insert( ) { struct bst *ptr1,*ptr2,*ptr3; ptr2=(struct bst *)malloc(sizeof(struct bst)); if(!ptr2) printf("\n Memory is full"); else { printf("\n Enter information:"); scanf("%d",&ptr2->key); ptr2->left= ptr2->right= NULL; if(root == NULL) root= ptr2; else { ptr1= root; while(ptr1) { ptr3= ptr1; if(ptr1->key < ptr2->key) ptr1= ptr1->right; else ptr1= ptr1->left; } if(ptr3->key < ptr2->key) ptr3->right= ptr2; else ptr3->left= ptr2; } } getch( ); } void postorder(struct bst *ptr) { if(ptr) 269

{ if(ptr->left) postorder(ptr->left); if(ptr->right) postorder(ptr->right); printf("\n %d",ptr->key); } getch( ); } void inorder(struct bst *ptr) { if(ptr) { if(ptr->left) inorder(ptr->left); printf("\n %d",ptr->key); if(ptr->right) inorder(ptr->right); } getch( ); } void preorder(struct bst *ptr) { if(ptr) { printf("\n %d",ptr->key); if(ptr->left) preorder(ptr->left); if(ptr->right) preorder(ptr->right); } getch( ); } void rpreorder(struct bst *ptr) { if(ptr) { printf("\n %d",ptr->key); if(ptr->right) rpreorder(ptr->right); if(ptr->left) rpreorder(ptr->left); } getch( ); } void rinorder(struct bst *ptr) { if(ptr) { 270

if(ptr->right) rinorder(ptr->right); printf("\n %d",ptr->key); if(ptr->left) rinorder(ptr->left); } getch( ); } void rpostorder(struct bst *ptr) { if(ptr) { if(ptr->right) rpostorder(ptr->right); if(ptr->left) rpostorder(ptr->left); printf("\n %d",ptr->key); } getch( ); } struct bst *search(int x) { struct bst *ptr; ptr=root; while((ptr)&&(ptr->key!=x)) { if(x<ptr->key) ptr=ptr->left; else ptr=ptr->right; } if(ptr==NULL) printf("\n Search was unsuccessful"); return(ptr); }

int maximum( ) { struct bst *ptr; int f; ptr=root; while(ptr->right) ptr= ptr->right; f= ptr->key; return(f); } int minimum( ) 271

{ struct bst *ptr; int f; ptr= root; while(ptr->left) ptr= ptr->left; f= ptr->key; return(f); } void count_of_nodes(struct bst *ptr) { if(ptr) { count++; if(ptr->left) count_of_nodes(ptr->left); if(ptr->right) count_of_nodes(ptr->right); } getch( ); } void count_of_leaf(struct bst *ptr) { if(ptr) { if((ptr->left == NULL) && (ptr->righ t== NULL)) c++; if(ptr->right) count_of_leaf(ptr->right); if(ptr->left) count_of_leaf(ptr->left); } } int height(struct bst *ptr) { int x,y; if(ptr) { x= height(ptr->left); y= height(ptr->right); if(x > y) return(x+1); else return(y+1); } else return(0); } 272

void compl_check( ) { int h,g; h= height(root); g= pow(2,h-1); c= 0; count_of_leaf(root); if(g == c) printf("\n Binary tree is complete"); else printf("\n Binary tree is not complete"); getch( ); }

273

Q.36. Ans.

Write pseudo code and program for implementing polynomial manipulation.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm consist of for Calling various operations that is to be done on a polynomial. CHOICE is an integer variable for storing selected option. INFO POLY Step 1: LINK

[Menu for operation] Repeat While(True) Write( Program for Polynomial Manipulation) Write( 1- Evaluation of polybomial) Write( 2- Addition of polynomial) Write( 3- Subtraction of polynomial) Write( 4- Multiplication of polynomial) Write( 5- Exit) Write( Enter your choice: ) Read(CHOICE) [Calling of functions] Select Case(CHOICE) Case 1: Call EVALUATION( ) Call DISPLAY( ) Case 2: Call ADDITION( ) Call DISPLAY( ) Case 3: Call SUBTRACTION( ) Call DISPLAY( ) Case 4: Call MULTIPLICATION( ) Call DISPLAY( ) Case 5: EXIT(0) Default: Write( Invalid Choice) [Finished] Exit

Step 2:

Step 3:

Procedure. INPUT( ). This procedure inserts a polynomial expression into link-list. CH is for storing option. PTR1 and PTR2 are pointers. PTR1 is used to traverse through the list. PTR2 is used to confirm addition of the first node. Structure of node is as follows INFO POLY Step 1: [Inserting expression into list] PTR2 <= POLY If (PTR2 NULL) Then Write( Memory is full) Else 274 LINK

LINK(PTR2) NULL If (HEAD = NULL) Then HEAD PTR2 Else PTR1 HEAD Repeat While(LINK(PTR1) = NULL) PTR1 LINK(PTR1) LINK(PTR1) PTR2 Write( Enter coefficient of term :) Read(COEFF(PTR2)) Write( Enter the power of term :) Read(POW(PTR2)) Write( Enter more terms 1- for YES 2- for NO) Read(CH) If (CH = 1) Then Call INPUT( ) Step 3: [Finished] Return

Procedure. DISPLAY( ). This procedure displays the information in the link-list. PTR1 is a pointer. PTR1 is used to traverse through the list. HEAD is the pointer to the first node. Structure of node is as follows INFO POLY Step 1: [Displaying elements in list] If (PTR = NULL) Then Write( Polnomial empty) Else Repeat While(PTR = NULL) If (COEFF(PTR) > 0) Then Write( + ) Write(COEFF(PTR)) If Then Step 2: [Finished] Return (POW(PTR) = 0) Goto Step 2 LINK

Procedure. EVALUATION( ). This procedure evaluates the information in the linklist. SUM is used to store total of addition. X is used for storing entered value. RES is for storing result of addition. PTR1 is a pointer and used to store total of addition. HEAD is the pointer to the first node. Structure of node is as follows 275

INFO POLY Step 1: [Evaluating the information] PTR HEAD If (HEAD = NULL) Then Call INPUT( ) Write( Enter the value of X: ) Read(X)

LINK

Repeat While(PTR = NULL) RES (COEFF(PTR)) * (POW(X,POW(PTR)) SUM SUM + RES PTR LINK(PTR) Write( Sum is: SUM) Step 2: [Finished] Return

Procedure. MULTIPLICATION( ) This procedure evaluates the information in the link-list. SUM is used to store total of addition. X is used for storing entered value. RES is for storing result of addition. PTR1, PTR2, PTR3, PTR4 and PTR5 are pointers. PTR1 is used to traverse through the list. HEAD1 and HEAD2 are the pointers to the first node. Structure of node is as follows INFO POLY Step 1: [Multiplying polynomial] Write( Enter first polynomial) Call INPUT( ) HEAD1 HEAD Write( Enter second polynomial: ) Call INPUT( ) HEAD2 HEAD HEAD NULL PTR1 HEAD1 Repeat While(PTR1 = NULL) PTR2 HEAD2 Repeat While (PTR2 = NULL) PTR3 <= POLY LINK(PTR3) NULL If Then Else (PTR3 NULL) Return COEFF(PTR3) (COEFF(PTR1))*(COEFF(PTR2)) POW(PTR3) (POW(PTR1))*(POW(PTR2)) If (HEAD = NULL) Then HEAD PTR3 276 LINK

Else PTR4 HEAD Repeat While((PTR4 NULL) and (POW(PTR4) > POW(PTR3) PTR5 PTR4 PTR4 LINK(PTR4) If (PTR4 NULL) Then LINK(PTR5) PTR3 Else If Then Else COEFF(PTR4) COEFF(PTR4)+COEFF(PTR3) ((COEFF(PTR4)) NULL) LINK(PTR5) LINK(PTR4) Restore(PTR4) PTR2 LINK(PTR2) PTR1 LINK(PTR10 Step 2: [Finished] Return If Then (POW(PTR4) < POW(PTR3)) LINK(PTR5) PTR3 LINK(PTR3) PTR4

Procedure. ADDITION( ). This procedure is used for adding the information in the link-list. SUM is used to store total of addition. X is used for storing entered value. RES is for storing result of addition. PTR1, PTR2, PTR3, PTR4 and PTR5 are pointers. PTR1 is used to traverse through the list. HEAD1 and HEAD2 are the pointers to the first node. Structure of node is as follows INFO POLY Step 1: [Adding polynomial] Write( Enter first polynomial) Call INPUT( ) HEAD1 HEAD Write( Enter second polynomial: ) Call INPUT( ) HEAD2 HEAD HEAD NULL PTR1 HEAD1 PTR2 HEAD2 Repeat While((PTR1 NULL) and (PTR2 NULL)) PTR3 <= POLY LINK(PTR3) NULL If Then (PTR3 NULL) Return 277 LINK

If Then

((POW(PTR1) > POW(PTR2)) COEFF(PTR3) COEFF(PTR1) POW(PTR3) POW(PTR1) PTR1 LINK(PTR1)

Else If ((POW(PTR1) < POW(PTR2)) Then COEFF(PTR3) COEFF(PTR2) POW(PTR3) POW(PTR2) PTR2 LINK(PTR2) Else COEFF(PTR3) COEFF(PTR1) + COEFF(PTR2) POW(PTR3) LINK(PTR1) PTR1 LINK(PTR1) PTR2 LINK(PTR2) If Then (COEFF(PTR3) 0) If (HEAD = NULL) Then HEAD PTR3 Else PTR4 HEAd Repeat While(LINK(PTR4) NULL) PTR4 LINK(PTR4)

Repeat While(PTR1 NULL) PTR3 <= POLY LINK(PTR3) NULL If (PTR3 NULL) Then Return Else PTR4 HEAD Repeat While(LINK(PTR4) NULL) PTR4 LINK(PTR4) LINK(PTR4) PTR3 COEFF(PTR3) COEFF(PTR1) POW(PTR3) POW(PTR1) PTR1 LINK(PTR1) Repeat While(PTR2 NULL) PTR3 <= POLY LINK(PTR3) NULL If (PTR3 NULL) Then Return Else PTR4 HEAD Repeat While(LINK(PTR4) NULL) PTR4 LINK(PTR4) LINK(PTR4) PTR3 COEFF(PTR3) COEFF(PTR2) POW(PTR3) POW(PTR2) PTR2 LINK(PTR2) Procedure. SUBTRACTION( ). This procedure is used to subtract the information in 278

the link-list. SUM is used to store total of addition. X is used for storing entered value. RES is for storing result of addition. PTR1, PTR2, PTR3, PTR4 and PTR5 are pointers. PTR1 is used to traverse through the list. HEAD1 and HEAD2 are the pointers to the first node. Structure of node is as follows INFO POLY Step 1: [Adding polynomial] Write( Enter first polynomial) Call INPUT( ) HEAD1 HEAD Write( Enter second polynomial: ) Call INPUT( ) HEAD2 HEAD HEAD NULL PTR1 HEAD1 PTR2 HEAD2 Repeat While((PTR1 NULL) and (PTR2 NULL)) PTR3 <= POLY LINK(PTR3) NULL If Then If Then (PTR3 NULL) Return ((POW(PTR1) > POW(PTR2)) COEFF(PTR3) COEFF(PTR1) POW(PTR3) POW(PTR1) PTR1 LINK(PTR1) LINK

Else If ((POW(PTR1) < POW(PTR2)) Then COEFF(PTR3) COEFF(PTR2) POW(PTR3) POW(PTR2) PTR2 LINK(PTR2) Else COEFF(PTR3) COEFF(PTR1) + COEFF(PTR2) POW(PTR3) LINK(PTR1) PTR1 LINK(PTR1) PTR2 LINK(PTR2) If Then (COEFF(PTR3) 0) If (HEAD = NULL) Then HEAD PTR3 Else PTR4 HEAD Repeat While(LINK(PTR4) NULL) PTR4 LINK(PTR4)

Repeat While(PTR1 NULL) PTR3 <= POLY 279

LINK(PTR3) NULL If (PTR3 NULL) Then Return Else PTR4 HEAD Repeat While(LINK(PTR4) NULL) PTR4 LINK(PTR4) LINK(PTR4) PTR3 COEFF(PTR3) COEFF(PTR1) POW(PTR3) POW(PTR1) PTR1 LINK(PTR1) Repeat While(PTR2 NULL) PTR3 <= POLY LINK(PTR3) NULL If (PTR3 NULL) Then Return Else PTR4 HEAD Repeat While(LINK(PTR4) NULL) PTR4 LINK(PTR4) LINK(PTR4) PTR3 COEFF(PTR3) -COEFF(PTR2) POW(PTR3) POW(PTR2) PTR2 LINK(PTR2)

280

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> #define NULL 0 void display( ); void input( ); void addition( ); void subtraction( ); void multiplication( ); void evaluation( ); struct poly { int coeff,pow; struct poly*link; }*head=NULL; void main( ) { int ch; while(1) { clrscr( ); printf("\n\n\t Program for Polynomial Manipulation"); printf("\n\n\t 1- Evaluation of polynomial"); printf("\n\t 2- Addition of polynomial"); printf("\n\t 3- Subtraction of polynomial"); printf("\n\t 4- Multiplication of polynomial"); printf("\n\t 5- Exit"); printf("\n\n Enter your choice:- "); scanf("%d",&ch); switch(ch) { case 1: evaluation( ); display( ); break; case 2: addition( ); display( ); break; case 3: subtraction(); display( ); break; case 4: multiplication( ); display( ); break; case 5: exit(0); break; default: printf("\n Invalid choice"); getch( ); break; 281

} } } void input( ) { int ch; struct poly *ptr1,*ptr2; ptr2=(struct poly*)malloc(sizeof(struct poly)); if(!ptr2) { printf("\n Memory is full"); getch( ); } else { ptr2->link= NULL; if(head == NULL) head=ptr2; else { ptr1= head; while(ptr1->link) ptr1= ptr1->link; ptr1->link= ptr2; printf("\nEnter the coefficient of term:- "); scanf("%d",&ptr2->coeff); printf("\n Enter the power of term:- "); scanf("%d",&ptr2->pow); printf("\n Enter more terms, 1.for Yes, 2.for No"); scanf("%d",&ch); if(ch == 1) input( ); } } } void display( ) { struct poly *ptr; ptr=head; if(ptr== NULL) { printf("\n Polynomial Empty"); getch( ); } else { while(ptr) { if(ptr->coeff > 0) printf("+"); printf("%d",ptr->coeff); 282

if(ptr->pow == 0) break; printf("x^ %d",ptr->pow); ptr= ptr->link; } } } void evaluation( ) { struct poly *ptr; int res,sum=0,x; ptr=head; if(head == NULL) input( ); printf("\n Enter the value of x :- "); scanf("%d",&x); while(ptr) { res=(ptr->coeff)*(pow(x,ptr->pow)); sum+ =res; ptr= ptr->link; } printf("\n\n Sum: %d",sum); getch( ); } void multiplication() { struct poly *head1,*head2,*ptr1,*ptr2,*ptr3,*ptr4,*ptr5; printf("\n Enter first polynomial: "); input(); head1= head; head= NULL; printf("\n Enter second polynomial: "); input( ); head2= head; head= NULL; ptr1= head1; while(ptr1) { ptr2=head2; while(ptr2) { ptr3= (struct poly*)malloc(sizeof(struct poly)); ptr3->link= NULL; if(!ptr3) return; 283

else { ptr3->coeff= (ptr1->coeff)*(ptr2->coeff); ptr3->pow= (ptr1->pow)*(ptr2->pow); if(head == NULL) head= ptr3; else { ptr4= head; while((ptr4 != NULL) && (ptr4->pow > ptr3->pow)) { ptr5= ptr4; ptr4= ptr4->link; } if(!ptr4) ptr5->link = ptr3; else if(ptr4->pow < ptr3->pow) { ptr5->link = ptr3; ptr3->link = ptr4; } else ptr4->coeff+ =ptr3->coeff; if(!ptr4->coeff) { ptr5->link = ptr4->link; free(ptr4); } } } ptr2= ptr2->link; } ptr1= ptr1->link; } getch( ); } void addition( ) { struct poly *head1,*head2,*ptr1,*ptr2,*ptr3,*ptr4; printf("\n Enter first polynomial: "); input( ); head1=head; head=NULL; printf("\n Enter second polynomial: "); input( ); head2=head; head=NULL; ptr1=head1; 284

ptr2=head2; while((ptr1!=NULL)&&(ptr2!=NULL)) { ptr3=(struct poly*)malloc(sizeof(struct poly)); ptr3->link=NULL; if(!ptr3) return; if(ptr1->pow > ptr2->pow) { ptr3->coeff= ptr1->coeff; ptr3->pow= ptr1->pow; ptr1= ptr1->link; } else if(ptr1->pow < ptr2->pow) { ptr3->coeff= ptr2->coeff; ptr3->pow= ptr2->pow; ptr2= ptr2->link; } else { ptr3->coeff= ptr1->coeff + ptr2->coeff; ptr3->pow= ptr1->pow; ptr1= ptr1->link; ptr2= ptr2->link; } if(ptr3->coeff != 0) { if(head == NULL) head= ptr3; else { ptr4= head; while(ptr4->link != NULL) ptr4= ptr4->link; ptr4->link= ptr3; } } } while(ptr1!=NULL) { ptr3=(struct poly *)malloc(sizeof(struct poly)); ptr3->link= NULL; if(!ptr3) return; else { ptr4= head; while(ptr4->link != NULL) ptr4= ptr4->link; ptr4->link= ptr3; 285

ptr3->coeff= ptr1->coeff; ptr3->pow= ptr1->pow; ptr1= ptr1->link; } } while(ptr2 != NULL) { ptr3=(struct poly *)malloc(sizeof(struct poly)); ptr3->link=NULL; if(!ptr3) return; else { ptr4= head; while(ptr4->link != NULL) ptr4= ptr4->link; ptr4->link= ptr3; ptr3->coeff= ptr2->coeff; ptr3->pow= ptr2->pow; ptr2= ptr2->link; } } getch( ); } void subtraction( ) { struct poly *head1,*head2,*ptr1,*ptr2,*ptr3,*ptr4; printf("\n Enter first polynomial: "); input( ); head1=head; head=NULL; printf("\n Enter second polynomial: "); input( ); head2= head; head= NULL; ptr1= head1; ptr2= head2; while((ptr1 != NULL) && (ptr2 != NULL)) { ptr3= (struct poly*)malloc(sizeof(struct poly)); ptr3->link= NULL; if(!ptr3) return; if(ptr1->pow > ptr2->pow) { ptr3->coeff= ptr1->coeff; ptr3->pow= ptr1->pow; 286

ptr1= ptr1->link; } else if(ptr1->pow < ptr2->pow) { ptr3->coeff=ptr2->coeff; ptr3->pow= ptr2->pow; ptr2= ptr2->link; } else { ptr3->coeff= ptr1->coeff + ptr2->coeff; ptr3->pow= ptr1->pow; ptr1= ptr1->link; ptr2= ptr2->link; } if(ptr3->coeff != 0) { if(head==NULL) head = ptr3; else { ptr4= head; while(ptr4->link != NULL) ptr4= ptr4->link; ptr4->link= ptr3; } } } while(ptr1 != NULL) { ptr3=(struct poly *)malloc(sizeof(struct poly)); ptr3->link= NULL; if(!ptr3) return; else { ptr4= head; while(ptr4->link != NULL) ptr4= ptr4->link; ptr4->link= ptr3; ptr3->coeff= ptr1->coeff; ptr3->pow= ptr1->pow; ptr1= ptr1->link; } } while(ptr2 != NULL) { ptr3= (struct poly *)malloc(sizeof(struct poly)); ptr3->link= NULL; if(!ptr3) return; else { 287

ptr4= head; while(ptr4->link != NULL) ptr4= ptr4->link; ptr4->link= ptr3; ptr3->coeff= -ptr2->coeff; ptr3->pow= ptr2->pow; ptr2= ptr2->link; } } getch( ); }

288

Q. 37. Ans.

Write pseudo code and program for inputting an array and to sort them using heap sort.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm inputs the elements and sort them in an orderly way according to heap sort technique and then displays them on the screen.

Step 1: Step 2:

[Inputting the element] Call INPUT( ) [Displaying the element in an unsorted way] Write( Unsorted array is:) Call DISPLAY( ) [Sorting of elements] Call HEAP_SORT( ) [Displaying the element in an ordered way] Write( Sorted array is: ) Call DISPLAY( ) [Finished] Exit This procedure inputs the different elements into the array for sorting them into order. N is an integer variable holding number of elements. A represents the array. I is the loop-control variable. [Entering elements into array] Write( Enter number of elements: ) Read(N) Write( Enter elements: ) Repeat For I 1, 2, 3, , N Read(A[I])

Step 3: Step 4:

Step 5:

Procedure. INPUT( ).

Step 1:

Step 2:

[Finished] Return Procedure. DISPLAY( ). This procedure displays the elements in the array on the screen. A represents the array. I is the loop-control variable. Step 1: [Displaying elements] Repeat For I 1, 2, 3, , N Write(A[I]) [Finished] Return

Step 2:

Procedure. HEAP_SORT( ). This procedure sorts an unsorted array into a sorted array(complete or nearly complete binary tree) using heap sort technique. A represents the array. DUMMY is a temporary integer variable. I is the 289

loop-control variable. HEAP_SIZE is the number of elements in the array. Step 1: [Sorting the array] Call BUILD_HEAP( ) Repeat For I N, N-1, , 1 STEP-1 DUMMY A[1] A[1] A[I] A[I] DUMMY HEAP_SIZE HEAP_SIZE 1 Call HEAPIFY(1) Step 2: [Finished] Return Procedure. BUILD_HEAP( ). This procedure converts an unsorted array into a heap (complete or nearly complete binary tree) using heap sort technique. A represents the array. HEAP_SIZE is the number of elements in the array. I is the loop-control variable. N is an variable containing number of elements. Step 1: [Converting into heap] HEAP_SIZE N Repeat For I N/2, , 1 Call HEAPIFY(I) Step 2: [Finished] Return

Procedure. HEAPIFY(I). Thisprocedure moves elements from root position until either it ends up in a position where root property is satisfied or it hits a leaf node. A represents the array. LARGE is the integer variable for storing maximum value of tree. :LFT represents element of left sub tree and RGT represents right sub tree element. HEAP_SIZE is the number of elements in the array. I is the loop-control variable. DUMMY is a temporary integer variable. Step 1: [Moving elements from root position] RGT LFT + 1 If ((LFT HEAP_SIZE) and (A[LFT] > A[LARGE]) Then LARGE LFT Else LARGE I If ((RGT HEAP_SIZE) and (A[RGT] > A[LARGE]) Then LARGE RGT If (LARGE I) Then DUMMY A[I] A[I] A[LARGE] A[LARGE] DUMMY Step 2: [Finished] Return (Call HEAPIFY(LARGE))

290

Program Coding
#include<stdio.h> #include<conio.h> int a[20],heap_size,n; void input( ); void display( ); void heapify(int); void build_heap( ); void heap_sort( ); void main( ) { clrscr( ); input( ); clrscr( ); printf("\n\n Unsorted array:- \n"); display( ); heap_sort( ); printf("\n\n Sorted array is:- \n"); display( ); getch( ); } void input( ) { int i; printf("\n Enter number of elements:- \n"); scanf("%d",&n); printf("\n Enter elements:- "); for(i= 1; i <= n; i++) scanf("%d",&a[i]); getch( ); } void display( ) { int i; for(i= 1;i<= n; i++) printf("\n %d",a[i]); getch( ); } void heap_sort( ) { int i,dummy; build_heap( ); for(i= n; i> 1; i--) { dummy= a[1]; a[1]= a[i]; 291

a[i]= dummy; heap_size--; heapify(1); } } void build_heap( ) { int i; heap_size=n; for(i= n/2; i> 0; i--) heapify(i); } void heapify(int i) { int large,dummy,lft,rgt; lft= 2*i; rgt= lft+1; if((lft <= heap_size) && (a[lft] > a[i])) large= lft; else large= i; if((rgt <= heap_size) && (a[rgt] > a[large])) large= rgt; if(large != i) { dummy= a[i]; a[i]= a[large]; a[large]= dummy; heapify(large); } }

292

Q. 38. Ans.

Write pseudo code and program for implementing sparse matrices.

PSEUDO CODE
Algorithm MAIN( ). This algorithm is used to show the menu operations on matrices and Calling of all functions. VAL is a variable for storing values from the keyboard. CHOICE is an integer variable and used for storing chosen option. I is an loop-control variable. J is the variable that tells the position of next element.

Step 1:

[Menu of operations to be performed] Repeat While (True) Write( Progem for sparse matrix) Write( 1- Input) Write( 2- Transpose) Write( 3- Addition) Write( 4- Subtraction) Write(5- Exit) Write( Enter your choice: ) Read(CHOICE) [Performing different functions] Select Case (CHOICE) Case 1: S 0 Write( Enter dimensions: ) Read(S.M,S.N) Write( Enter the elements:) Repeat For I 1, 2, 3, , S.M Repeat For J 1, 2, 3, , S.N Read(VAL) If (VAL) Then S Call PUT(VAL,I,J,S) Write( The original matrix is:) Call DISPLAY(S) Case 2: S1 0 Write( Enter dimensions: ) Read(S.M,S.N) Write( Enter the elements:) Repeat For I 1, 2, 3, , S.M Repeat For J 1, 2, 3, , S.N Read(VAL) If (VAL) Then S Call PUT(VAL,I,J,S) Write( The original matrix is:) Call DISPLAY(S1) Call TRANSPOSE(S1) Write( Transpose of matrix is:) Call DISPLAY(S1) Case 3: A.P B.P 0 Write( Enter dimension for 1 st matrix:) 293

Step 2:

Read(A.M,A.N) Write( Enter the elements:) Repeat For I 1, 2, 3, , A.M Repeat For J 1, 2, 3, ,A.N Read (VAL) If (VAL) Then A Call PUT(VAL, I, J, A) Write( Enter dimension for 1 st matrix:) Read(B.M,B.N) Write( Enter the elements:) Repeat For I 1, 2, 3, , B.M Repeat For J 1, 2, 3, ,B.N Read (VAL) If (VAL) Then B Call PUT(VAL, I, J, B) Write(The original 1 st matrix is:) Call DISPLAY(A) Write(The original 2 nd matrix is:) Call DISPLAY(B) If Then Else ((A.M B.M) || (A.N B.N)) Write(Addition is not possible) C Call ADD(A,B) Write( Resultant matrix is:) Call DISPLAY(C) Case 4: A.P B.P 0 Write( Enter dimension for 1 st matrix:) Read(A.M,A.N) Write( Enter the elements:) Repeat For I 1, 2, 3, , A.M Repeat For J 1, 2, 3, ,A.N Read (VAL) If (VAL) Then A Call PUT(VAL, I, J, A) Write( Enter dimension for 1 st matrix:) Read(B.M,B.N) Write( Enter the elements:) Repeat For I 1, 2, 3, , B.M Repeat For J 1, 2, 3, ,B.N Read (VAL) If (VAL) Then B Call PUT(VAL, I, J, B) Write(The original 1 st matrix is:) Call DISPLAY(A) Write(The original 2 nd matrix is:) Call DISPLAY(B) 294

If Then Else

((A.M B.M) || (A.N B.N)) Write( Subtraction is not possible) C Call ADD(A,B) Write( Resultant matrix is:) Call DISPLAY(C)

Case 5: EXIT(0) Default: Write( Invalid Choice) Step 3: [Finished] Exit

Function. PUT(VALUE, I, J, STRUCT MATRIX T). This is a procedure to enter the given information into sparse matrix. VALUE is a variable for storing values. T is the matrix and P is the number of non-zero elements. Step 1: [Initializing values] T.P T.P + 1 T.ROW[T.P] I T.COLUMN[T.P] J T.ELEMENT[T.P] VALUE Write(T.P, T.ROW[T.P], T.COLUMN[T.P]) Step 2: [Finished] Return(T) Procedure. DISPLAY( STRUCT MATRIX T). This is the procedure to display the information in the sparse matrix. X is the loop control variable. T is the matrix. Step 1: [Displaying elements] Write( ROW COLUMN ELEMENT) Repeat For I 1, 2, 3, , T.P Write(T.ROW[X], T.COLUMN[X], T.ELEMENT[X]) [Finished] Return

Step 2:

Procedure. TRANSPOSE(STRUCT MATRIX T). This is a procedure to transfer the elements of rows into columns and that of columns into rows in the sparse matrix. DUMMY is a temporary variable. I is the loop-control variable. T is the matrix. Step 1: [Transposing of matrix] Repeat For I 1, 2, 3, , T.N DUMMY T.ROW[I] T.ROW[I] T.COLUMN[I] T.COLUMN DUMMY [Finished] Return(T) 295

Step 2:

Procedure. ADD(STRUCT MATRIX X, STRUCT MATRIX Y). This is a procedure to add two matrices. SUM is a temporaray variable to store added values R and S are integer variables to store addeneds. I, A and B are the loop control variable and J is the variable that tells the position of next element. X and Y are the two matrices. P is number of non zero elements. N is number of columns and M is the number of rows. Step 1: [Initializing variables] TEMP1.P 0 TEMP1.M X.M TEMP1.N X.N [Adding the matrices] Repeat For I 1, 2, 3, ,X.M Repeat For J 1, 2, 3, ,X.N SUM 0 RS0 Repeat For A 1, 2, 3, , X.P If ((X.ROW[A] = I) and (X.COLUMN[A] = J)) Then R X.ELEMENT[A] Repeat For B 1, 2, 3, , Y.P If ((Y.ROW[B] = I) and (Y.COLUMN[B] = J)) Then S Y.ELEMENT[B] SUM R + S If (SUM) Then TEMP1 PUT(SUM, I, J, TEMP1) Step 3: [Finished] Return(TEMP1)

Step 2:

Procedure. SUB(STRUCT MATRIX X, STRUCT MATRIX Y). This is a procedure to add two matrices. SUBT is a temporaray variable to store subtracted values R and S are integer variables to store addeneds. I, A and B are the loop control variable and J is the variable that tells the position of next element. X and Y are the two matrices. P is number of non zero elements. N is number of columns and M is the number of rows. Step 1: [Initializing variables] TEMP1.P 0 TEMP1.M X.M TEMP1.N X.N Step 2: [Subtracting the matrices] Repeat For I 1, 2, 3, ,X.M Repeat For J 1, 2, 3, ,X.N SUM 0 RS0 Repeat For A 1, 2, 3, , X.P If ((X.ROW[A] = I) and (X.COLUMN[A] = J)) Then R X.ELEMENT[A] 296

Repeat For B 1, 2, 3, , Y.P If ((Y.ROW[B] = I) and (Y.COLUMN[B] = J)) Then S Y.ELEMENT[B] SUBT R - S If (SUBT) Then TEMP1 PUT(SUM, I, J, TEMP1) Step 3: [Finished] Return(TEMP1)

Procedure. MULTIPLY(STRUCT MATRIX X,STRUCT MATRIX Y). This is a procedure to multiply two sparse matrices. MULT is a temporary variable to store multiplied values R and S are integer variables to store subtends. I, A and B are the loop control variable and J and K are the variables that tells the position of next element. X and Y are the two matrices. P is number of non zero elements, N is number of columns and M is the number of rows. Step 1: [Initializing variables] TEMP1.P 0 TEMP1.M X.M TEMP1.N Y.N [Multiplying the matrices] Repeat For I 1, 2, 3, ,X.M Repeat For J 1, 2, 3, ,Y.N MULT 0 RS0 Repeat For K 1, 2, 3, , X.N RS0 Repeat For A 1, 2, 3, , X.P If ((X.ROW[A]=I) and (X.COLUMN[A]=K)) Then R X.ELEMENT[A] Repeat For B 1, 2, 3, , Y.P If ((Y.ROW[B]= I) and (Y.COLUMN[B]=J)) Then S Y.ELEMENT[B] MULT MULT +( R * S) If (MULT) Then TEMP1 PUT(MULT, I, J, TEMP1) Step 3: [Finished] Return(TEMP1)

Step 2:

297

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> struct matrix { int n,m,p; int row[20],column[20],element[20]; }s,s1,a,b,c,temp1; struct matrix put(int,int,int,struct matrix); struct matrix multiply(struct matrix,struct matrix); struct matrix add(struct matrix,struct matrix); struct matrix sub(struct matrix,struct matrix); struct matrix tranpose(struct matrix); void display(struct matrix); void main() { int val,choice,i,j; while(1) { clrscr( ); printf("\n Program for sparse matrix"); printf("\n----------------------------"); printf("\n\n 1- Input"); printf("\n\n 2- Tranpose"); printf("\n\n 3- Addition"); printf("\n\n 4- Subtraction"); printf("\n\n 5- Multiplication"); printf("\n\n 6- Exit"); printf("\n\n Enter your choice: "); scanf("%d",&choice); clrscr( ); switch(choice) { case 1: s.p= -1; printf("\n Enter dimensions: "); scanf("%d %d",&s.m,&s.n); printf("\n Enter the elements: "); for(i= 0;I <s.m; i++) { for(j= 0; j< s.n; j++) { scanf("%d",&val); if(val) s=put(val,i,j,s); } } 298

printf("\n The original matrix is: \n"); display(s); break; case 2: s1.p= -1; printf("\n Enter dimensions: "); scanf("%d %d",&s1.m,&s1.n); printf("\n Enter the elements: "); for(i= 0; i< s1.m;I ++) { for(j= 0;j < s1.n; j++) { scanf("%d",&val); if(val) s1=put(val,i,j,s1); } } printf("\n The original matrix is: \n"); display(s1); s1= tranpose(s1); printf("\n Tranpose of matrix is: \n"); display(s1); break; case 3: a.p= b.p= -1; printf("\n Enter dimensions for first matrix: "); scanf("%d %d",&a.m,&a.n); printf("\n Enter the elements: "); for(i= 0; i< a.m; i++) { for(j= 0; j< a.n; j++) { scanf("%d",&val); if(val) a= put(val,i,j,a); } } printf("\n Enter dimensions for second matrix: "); scanf("%d %d",&b.m,&b.n); printf("Enter the elements: "); for(i= 0;I < b.m; i++) { for(j= 0;j < b.n; j++) { scanf("%d",&val); if(val) b= put(val,i,j,b); } } clrscr( ); printf("\n The original first matrix is: \n"); 299

display(a); printf("\n The original second matrix is: \n"); display(b); if((a.m != b.m) || (a.n != b.n)) printf("\n Addition is not possible"); else { c= add(a,b); printf("\n Resultant matrix is: \n"); display(c); } break; case 4: a.p= b.p= -1; printf("\n Enter dimensions for first matrix:"); scanf("%d %d",&a.m,&a.n); printf("\n Enter the elements:"); for(i= 0; i< a.m; i++) { for(j= 0; j< a.n; j++) { scanf("%d",&val); if(val) a= put(val,i,j,a); } } printf("\n Enter dimensions for second matrix:\n"); scanf("%d %d",&b.m,&b.n); printf("\n Enter the elements:"); for(i= 0; i< b.m; i++) { for(j= 0;j <b.n; j++) { scanf("%d",&val); if(val) b= put(val,i,j,b); } } clrscr( ); printf("\n The original first matrix is: \n"); display(a); printf("\n The original second matrix is: \n"); display(b); if((a.m != b.m) || (a.n != b.n)) printf("\n Subtraction is not possible"); else { c=sub(a,b); printf("\n Resultant matrix is:\n"); display(c); 300

} break; case 5: a.p= b.p= -1; printf("\n Enter dimensions for first matrix: "); scanf("%d %d",&b.m,&b.n); printf("\n Enter the elements:"); for(i= 0; i< b.m; i++) { for(j= 0; j< b.n; j++) { scanf("%d",&val); if(val) b=put(val,i,j,b); } } clrscr( ); printf("\n The original first matrix is: \n"); display(a); printf("\n The original second matrix is: \n"); display(b); if(a.n != b.m) printf("\n Multiplication is not possible"); else { c=multiply(a,b); printf("\n Resultant matrix is:\n"); display(c); } break; case 6: exit(0); break; default: printf("\n Invalid choice"); break; } getch( ); } } struct matrix put(int value,int i,int j,struct matrix t) { (t.p)++; t.row[t.p]= i; t.column[t.p]= j; t.element[t.p]= value; printf("\n %d %d %d %d",t.row[t.p],t.column[t.p],t.element[t.p]); return(t); 301

} void display(struct matrix t) { int i; printf("\n ROW \t COLUMN \tELEMENT \n"); for(i= 0; i<= t.p; i++) { printf(" %d \t%d \t%d",t.row[i],t.column[i],t.element[i]); printf("\n"); } } struct matrix tranpose(struct matrix t) { int i,dummy; for(i= 0; i<= t.p; i++) { dummy=t.row[i]; t.row[i]=t.column[i]; t.column[i]=dummy; } return(t); } struct matrix add(struct matrix x,struct matrix y) { int sum,r,s,i,j,a,b; temp1.p= -1; temp1.m=x.m; temp1.n=x.n; for(i= 0; i< x.m; i++) { for(j= 0; j< x.n; j++) { sum= 0; r= s= 0; for(a= 0; a<= x.p; a++) { if((x.row[a] == i) && (x.column[a] == j)) { r= x.element[a]; break; } } for(b= 0; b<= y.p; b++) { if((y.row[b] == i) && (y.column[b] == j)) { s= y.element[b]; break; } } 302

sum= r+s; if(sum) temp1=put(sum,i,j,temp1); } } return(temp1); } struct matrix sub(struct matrix x,struct matrix y) { int subt,r,s,i,j,a,b; temp1.p= -1; temp1.m= x.m; temp1.n= x.n; for(i= 0; i< x.m; i++) { for(j= 0; j< x.n; j++) { subt= 0; r= s= 0; for(a= 0; a <= x.p; a++) { if((x.row[a] == i) && (x.column[a] == j)) { r= x.element[a]; break; } } for(b= 0; b<= y.p; b++) { if((y.row[b] == i) && (y.column[b] == j)) { s= y.element[b]; break; } } subt=r-s; if(subt) temp1= put(subt,i,j,temp1); } } return(temp1); } struct matrix multiply(struct matrix x,struct matrix y) { int mult,r,s,i,j,a,b,k; temp1.p= -1; temp1.m=x.m; temp1.n=y.n; for(i= 0;I < x.m; i++) { for(j= 0; j< y.n; j++) 303

{ mult=0; for(k= 0; k < x.n; k++) { r= s= 0; for(a= 0; a<= x.p; a++) { if((x.row[a] == i) && (x.column[a] == k)) { r= x.element[a]; break; } } for(b= 0; b <= y.p; b++) { if((y.row[b] == k) && (y.column[b] == j)) { s= y.element[b]; break; } } mult+= (r*s); } if(mult) temp1=put(mult,i,j,temp1); } } return(temp1); }

304

Q. 39. Ans.

Write pseudo code and program for converting infix expression to suffix.

PSEUDO CODE
Algorithm. MAIN( ). This algorithm converts an infix expression into a postfix expression. It Calls different procedures to perform operations for conversion. C is a character variable for storing data. I is array variable. INFIX is an array for storing infix expression. POLISH is an array for storing suffix expression.

Step 1:

[Entering expression into array] Write( Enter the arithmetic expression in infix form, ending with #:) Repeat While((C GETCHAR( )) #) II+1 INFIX[I] C II+1 INFIX[I] # [Converting and displaying expression] Write( Expression in infix form is,INFIX) Call CONVERT( ) Write( Expression in suffix form is,POLISH) [Finished] Exit This procedure inserts or pushes charater into array. S represents array. TOP is a pointer that points to the top of the array. CH is for storing passed on character.

Step 2:

Step 3:

Procedure. PUSH(CH).

Step 1:

[Inserting data into array] TOP TOP + 1 S[TOP] CH [Finished] Return This procedure deletes or pops character from the array and returns the result to main function. H is used for storing character. S represents the array. TOP is a pointer that points to the top of array.

Step 2:

Procedure. POP( ).

Step 1:

[Deleting or popping data] H S[TOP] TOP TOP 1 [Finished] Return(H) This procedure converts infix expression to suffix. R, F1, F2 305

Step 2:

Procedure. CONVERT( ).

and RANK stores rank of the expression. I is array variable. J points to next data in array. NEXT is a character variable for storing data. TEMP is a temporary character variable. INFIX is an array for storing suffix expression. POLISH is an array for storing suffix expression. TOP is a pointer that points to the top of array. Step 1: [Inserting expression in stack] RANK 0 IJ0 TOP TOP + 1 S[TOP] # NEXT INFIX[I] II+1 [Converting the expression] Repeat While(NEXT #) If ((NEXT = +) or (NEXT = -)) Then F1 1 Else If ((NEXT = *) or (NEXT = /)) Then F1 2 Else F1 3 If Then Else If Then Else If Else If Then F2 3 (F1 < F2) JJ+1 POLISH[J] TEMP Write( Expression in suffix form is, POLISH) If ((TEMP = +) or(TEMP = -) or (TEMP = *) or(TEMP = /)) Then R -1 Else If (TEMP = #) R0 RANK RANK + 1 If (RANK < 1) Then Wrie( Invalid Expression) Repeat While(F1 F2) Call PUSH(NEXT) NEXT INFIX[I] II+1 Repeat While(S[TOP] #) TEMP Call POP( ) JJ+1 306 ((S[TOP] = +) or (S[TOP] = -)) F2 1 ((S[TOP] = *) or (S[TOP] = /)) F2 2 (S[TOP] = #) F2 0

Step 2:

POLISH[J] TEMP Write( Expression in suffix form is,POLISH) If ((TEMP = +) or(TEMP = -) or (TEMP = *) or (TEMP = /)) Then R -1 Else If (TEMP = #) R0 RANK RANK + 1 If (RANK < 1) Then Wrie( Invalid Expression) If (RANK 1) Then Write(Invalid Expression) Step 3; [Finished] Return

307

Program Coding
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> char s[20],polish[20]='/0'; char infix[20]='/0'; int top=-1; void convert( ); void push(char); char pop( ); void main( ) { char c; int i= 0; clrscr( ); printf("\n Enter the arthimetic expression in infix form, ending with'#':"); while((c= getchar( )) != '#') { infix[i++]= c; } infix[i++]= '#'; printf("\n Expression in infix form is: %s",infix); convert( ); printf("\n Expression in suffix form is: %s",polish); getch( ); } void push(char ch) { s[++top]= ch; } char pop() { char h; h= s[top--]; return h; } void convert( ) { int r,f1,f2,rank=0,i=0,j=0; char next,temp; s[++top]= '#'; next= infix[i++]; while(next != '#') { do 308

{ if((next == '+') || (next == '-')) f1= 1; else if((next == '*') || (next == '/')) f1= 2; else f1= 3; if((s[top] == '+')||(s[top] == '-')) f2= 1; else if((s[top] == '*') || (s[top] == '/')) f2= 2; else if(s[top] == '#') f2= 0; else f2= 3; if(f1< f2) { polish[j++]= temp; temp= pop( ); printf("\n Expression in suffix form is: %s",polish); if((temp == '+')||(temp == '-')||(temp == '*')||(temp == '/')) r= -1; else if(temp=='#') r= 0; else r= 1; rank= rank + r; if(rank< 1) { printf("\n Invalid expression"); getch( ); exit(0); } } } while(f1 <= f2); push(next); next= infix[i++]; } while(s[top] != '#') { temp= pop( ); polish[j++]= temp; printf("\n Expression in suffix form is: %s",polish); if((temp == '+')||(temp == '-')||(temp == '*')||(temp == '/')) r= -1; else if(temp == '#') r= 0; else r= 1; rank=rank+r; 309

if(rank < 1) { printf("\n Invalid choice"); getch( ); exit(0); } } if(rank != 1) { printf("\n Invalid expression"); getch( ); } }

310

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