INDERPRASTHA ENGINEERING COLLEGE

GHAZIABAD

COMPUTER SC. & ENGINEERING DEPARTMENT

LAB MANUAL
DATA STRUCTURE LAB
TCS-352
(B.TECH. - 3 SEMESTER)
RD

1

INDEX
1. PROGRAMS BASED ON RECURSION.
2. PROGRAMS BASED ON STRINGS.
3. PROGRAMS BASED ON ARRAYS.
4. PROGRAMS BASED ON STACK.
5. PROGRAMS BASED ON QUEUES.
6. PROGRAMS BASED ON LINKED LIST.
7. IMPLEMENT THE VARIOUS SEARCHING ALGORITHMS
8. IMPLEMENTATION OF VARIOUS SORTING ALGORITHMS
9. PROGRAM BASED ON BINARY TREES.
10. PROGRAMS BASED ON GRAPH.
11. PROGRAMS BASED ON SIMULATION OF HASHING
12. PROGRAMS BASED ON SPARSE MATRIX

NOTE: 1-10 ARE AS PER THE UPTU SYLLABUS AND 11 & 12 ARE BEYOND THE SYLLABUS

2

DATA STRUCTURE LAB (ECS - 352)
LIST OF PROGRAMS
1. PROGRAMS BASED ON RECURSION
I.
II.
III.
IV.
V.
VI.

PROGRAM FOR CALCULATION OF FACTORIAL OF THE GIVEN NUMBER.
PROGRAM FOR FINDING NTH FIBONACCI NUMBER AND ANALYZING ITS RUN TIME
PROGRAM FOR FINDING NTH FIBONACCI NUMBER USING ARRAY
PROGRAM FOR BINARY SEARCH
PROGRAM FOR TOWERS OF HANOI
PROGRAM FOR NATURAL NUMBER MULTIPLICATION

2. PROGRAMS BASED ON STRINGS.
I.
II.
III.
IV.

WRITE A PROGRAM TO INPUT A STRING AND CALCULATE THE LENGTH OF THE STRING
WRITE A PROGRAM TO COPY ONE STRING TO ANOTHER.
WRITE A PROGRAM TO CONCATENATE TWO STRINGS.
WRITE A PROGRAM TO ACCEPT A STRING AND CONVERT ALL THE CHARACTERS OF THE
UPPERCASE.
V. WRITE A PROGRAM TO PRINT A STRING IN ALPHABETICAL ORDER.
VI. WRITE A PROGRAM TO COMPARE TWO STRINGS
VII. WRITE A PROGRAM FOR PATTERN MATCHING

STRING FROM LOWERCASE TO

3. PROGRAMS BASED ON ARRAYS.
I. PROGRAM FOR INSERTION OF AN ELEMENT AT SPECIFIED LOCATION IN THE ARRAY
II. PROGRAM FOR DELETION OF AN ELEMENT FROM THE SPECIFIED LOCATION IN THE ARRAY
III. PROGRAM FOR DELETION OF ALL OCCURANCES OF X FROM THE ARRAY

4. PROGRAMS BASED ON STACKS.
I. WRITE A MENU DRIVEN PROGRAM TO IMPLEMENT THE PUSH, POP AND DISPLAY OPTION OF THE STACK WITH THE HELP
OF STATIC MEMORY ALLOCATION.
II. WRITE A MENU DRIVEN PROGRAM TO IMPLEMENT THE PUSH, POP AND DISPLAY OPTION OF THE STACK WITH THE HELP
OF DYNAMIC MEMORY ALLOCATION.
III. WRITE A PROGRAM TO CONVERT A INFIX EXPRESSION TO POSTFIX EXPRESSION.
IV. WRITE A PROGRAM TO CONVERT A INFIX EXPRESSION TO PREFIX EXPRESSION.
V. WRITE A PROGRAM TO EVALUATE THE GIVEN POSTFIX EXPRESSION.
VI. PROGRAM TO CHECK THE VALIDITY OF THE GIVEN EXPRESSION BASED ON THE PARENTHESIS
VII. PROGRAM TO CHECK THE VALIDITY OF THE GIVEN EXPRESSION BASED ON THE BRACKETS
VIII. PROGRAM TO CHECK IF THE GIVEN STRING IS PALINDROME USING STACKS
IX. SIMULATION OF TWO STACKS ON A SINGLE ARRAY

5. PROGRAMS BASED ON QUEUES.
I. WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS OPERATIONS ON A LINEAR QUEUE WITH THE HELP OF
STATIC MEMORY ALLOCATION.
II. WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS OPERATIONS ON A LINEAR QUEUE WITH THE HELP OF
DYNAMIC MEMORY ALLOCATION.
III. WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS OPERATIONS ON A CIRCULAR QUEUE WITH THE HELP
OF STATIC MEMORY ALLOCATION.
IV. WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS OPERATIONS ON A DEQUEUE QUEUE WITH
THE HELP OF STATIC MEMORY ALLOCATION.
V. WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS OPERATIONS ON A PRIORITY QUEUE WITH THE
HELP OF STATIC MEMORY ALLOCATION.
VI. SIMULATION OF AIR TRAFFIC CONTROL SYSTEM

6. PROGRAMS BASED ON LINKED LIST.
I. WRITE A MENU DRIVEN PROGRAM TO IMPLEMEN VARIOUS OPERATIONS ON A LINEAR LINKED LIST.
II. WRITE A MENU DRIVEN PROG TO IMPLEMENT VARIOUS OPERATIONS ON A CIRCULAR LINKED LIST
III. WRITE A FUNCTION TO CONCATENATE TWO LINEAR LINKED LIST.

3

PROGRAM FOR GENERATION OF HUFFMAN CODES VI. WRITE A C PROGRAM FOR WARSHALL’S ALGORITHM FOR SHORTEST PATH WRITE A C PROGRAM FOR WARSHALL’S ALGORITHM FOR TRANSITIVE CLOSURE 11. INSERTION. PROGRAMS BASED ON BINARY TREES. PROGRAM FOR FINDING THE BALANCE OF EVERY NODE IN THE BINARY TREE IX. PROGRAM FOR CREATION OF BINARY TREE II. MAXIMUM. II. WRITE A MENU DRIVEN PROGRAM TO IMPLEMENT VARIOUS OPERATIONS ON A BINARY SEARCH TREE (MINIMUM. PROGRAM FOR IMPLEMENTATION OF JOSEPHUS PROBLEM IS NOT POSSIBLE IN 7. III. IV.IV. PROGRAMS BASED ON SEARCHING ALGORITHMS. TRAVERSAL & DELETION). PROGRAM FOR FINDING TRANSPOSE OF THE GIVEN SPARSE MATRIX. X. PROGRAM FOR ADDITION OF VERY LONG INTEGER NUMBERS (STORAGE OF THESE NUMBERS INT OR LONG INT) VII. PROGRAM FOR CONCATENATION OF TWO CIRCULARLINKED LISTS VIII. A PROGRAM TO INPUT HOW MANY NUMBERS AND THEN INPUT THE NUMBERS AND THEN SEARCH ANY NUMBER WITH THE HELP OF INDEX SEQUENTIAL SEARCH 8. PROGRAMS BASED ON GRAPHS. WRITE BINARY SEARCH. IV. I. PROGRAM FOR SUBSTRACTION OF TWO SPARSE MATRIX. PROGRAMS BASED ON SPARSE MATRIX. WRITE A PROGRAM TO FIND THE LOGICAL SUCCESSOR OF A NODE IN A BINARY SEARCH TREE V. PROGRAMS BASED ON HASHING SIMULATION OF HASHING ON THE ARRAY/ CALCUTAION OF HASH ADDRESSES BASED ON THE ARRAY SIZE FURTHER OPEN ADDRESSING AND COLLISION RESOLUTION BY LINEAR PROBING. WRITE A PROGRAM TO INPUT HOW MANY NUMBERS AND THEN INPUT THE NUMBERS AND THEN SEARCH ANY NUMBER WITH THE HELP OF III. PROGRAMS BASED ON SORTING ALGORITHMS. PROGRAM FOR FINDING THE HEIGHT OF THE GIVEN BINARY TREE VIII. I. WRITE A PROGRAM TO INPUT HOW MANY NUMBERS AND THEN INPUT THE NUMBERS AND THEN SEARCH ANY NUMBER WITH THE HELP OF SEQUENTIAL/LINEAR SEARCH. PROGRAM FOR DELETION OF ALL THE OCCURANCES OF X FROM LINKED LIST XIII. PROGRAM FOR CREATION OF GENERAL TREE AND CONVERSION OF THE SAME TO BINARY TREE 10. WRITE A MENU DRIVEN PROGRAM TO IMPLEMENT VARIOUS TRAVERSAL OPERATIONS IN A BINARY TREE. III. WRITE A C PROGRAM FOR DEPTH FIRST SEARCH OF A GRAPH. WRITE A MENU DRIVEN PROG TO IMPLEMENT VARIOUS OPERATIONS ON A DOUBLY LINKED LIST V. PROGRAM FOR COUNTING THE NUMBER OF NODES IN THE BINARY TREE VII. BUBBLE SORT HEAP SORT. 12. PROGRAM FOR SPLITTING THE GIVEN LINKED LIST. PROGRAM FOR ADDITION OF TWO POLYNOMIALS VI. SELECTION SORT INSERTION SORT MERGE SORT RADIX SORT SHELL SORT QUICK SORT 9. NOTE: THE PRGRAMS IN ITALICS FONT ARE BEYOND THE SYLLABUS 4 . I. I. II. PROGRAM FOR ADDITION OF TWO SPARSE MATRIX. WRITE A C PROGRAM FOR BREADTH FIRST SEARCH OF A GRAPH. III. II. PROGRAM FOR CREATING ANOTHER COPY OF THE LINKED LIST IX. PROGRAM FOR MERGING TWO ORDERED LINKED LISTS XI. PROGRAM FOR INSERTION OF A NO IN THE GIVEN ORDERED LINKED LIST XII. PROGRAM FOR CONCATENATION OF TWO CIRCULAR LINKED LISTS XIV.

PROGRAM FOR FINDING NTH FIBONACCI NUMBER IX. –1 OBJECT: PROGRAMS BASED ON RECURSION. PROGRAM FOR NATURAL NUMBER MULTIPLICATION 5 . MAIN ADVANTAGE OF RECURSION IS THAT WE DIVIDE AND THEN CONQUER THE PROBLEMS WITH THE HELP OF RECURSION.ASSIGNMENT NO. THE THE LIMITATION OF USING THE RECURSION IS THAT WE CAN ONLY CREATE THOSE PROGRAMS WITH THE HELP OF RECURSION IN WHICH THE NEXT STEP DEPENDS UPON THE PREVIOUS STEP. PROBLEMS VII. PROGRAM FOR CALCULATION OF FACTORIAL OF THE GIVEN NUMBER. VIII. PROGRAM FOR TOWERS OF HANOI XI. PROGRAM FOR BINARY SEARCH X. THEORY AND CONCEPTS THE FUNCTION WHICH IS CALLED BY ITSELF IS CALLED RECURSIVE FUNCTION AND THIS PROCESS IS KNOWN AS RECURSION.

/****************************************************************/ void main() { int n. } /****************************************************************/ Output: Enter the number 5 Factorial of the given number is:=> 120 6 . clrscr().fact). fact=factorial(n).&n).fact. } /****************************************************************/ /*********************** Function Definitions *****************/ /****************************************************************/ int factorial(int n) { if(n==0) return 1. printf("The factorial of the given number is:=>\t%d". printf("enter the number "). scanf("%d". else return(n*factorial(n-1)). getch()./****************************************************************/ /* Prog for Finding factorial of a number using recursion */ /****************************************************************/ /* Function Prototyping */ int factorial(int).

&n). found in between **/ /****************************************************************/ /****************** Pre-processoer directives *******************/ /****************************************************************/ #include<time./****************************************************************/ /**** Prog for finding nth Fibonacci number using recursion and analyzing its run time *****/ /*** Des: The program has ambiguous recursive calls for the same no. scanf("%ld". } /****************************************************************/ /***************** Function Definitions *************************/ /****************************************************************/ long int fib(long int n) { if(n==1||n==2) return 1. t2=time(NULL). } /****************************************************************/ 7 . getch(). clrscr(). printf("\nTime elapsed is%ld".t2.f. f=fib(n).h> /****************************************************************/ /***************** Function prototyping *************************/ /****************************************************************/ long int fib(long int ).h> #include<stdlib. printf("\n\n%ld". time_t t1. else return(fib(n-1)+fib(n-2)). /****************************************************************/ void main() { long int n. t1=time(NULL).t2-t1).f).

Output 8 .

} /****************************************************************/ /********************** Function Definitions ************************/ /****************************************************************/ long int fib(long int n) { if(n==1||n==2) { a[n]=1. /****************************************************************/ void main() { long int n. clrscr().h> #include<stdlib.t2.&n).f. } } /****************************************************************/ 9 ./****************************************************************/ /* Prog for finding nth Fibonacci number & analyzing its run time */ /* By: Akhilesh Kumar Srivastava */ /* Des: The Use of Array finds the significant improvement in run time and does not make the repeatative calls */ /****************************************************************/ /******************* Pre-processoer directives ************************/ /****************************************************************/ #include<time. return 1.t2-t1).f). scanf("%ld".} else { if(a[n]==0) { a[n]=(fib(n-1)+fib(n-2)). time_t t1. t1=time(NULL). } return(a[n]).h> /****************************************************************/ /* Function Prototyping */ /****************************************************************/ long int fib(long int). getch(). /************ Global declaration of Arrays ****************************/ long int a[100]. printf("\n\n%ld". f=fib(n). printf("\nTime elapsed is%ld". t2=time(NULL).

Output 10 .

else printf("\nThe element is present at %d location in the array".int). if(y==-1) printf("\nThe element is not present in the array"). if(a[mid]==x) return mid.h> #include<conio.mid+1.20}.high.low. } /****************************************************************/ /* Function Definition */ /****************************************************************/ int binary_search(int a[].8.10. /****************************************************************/ void main() { int a[10]={2.18.int x) { int mid.int. mid=(low+high)/2.y. else { if(x<a[mid]) binary_search(a.16. clrscr(). } } /****************************************************************/ Output: 11 .0. scanf("%d"./****************************************************************/ /* Program for Binary Search recursively */ /* By: Akhilesh Kumar Srivastava */ /* Des: This is O(logn) Algo average case and O(1) in Best case */ /****************************************************************/ #include<stdio.n.x).6. int x.y+1).12.int low. else binary_search(a. if(low>high) return -1. getch().int high. y=binary_search(a.x). printf("Enter the element to be searched").mid-1.int.9.14.h> /****************************************************************/ /* Function Prototyping */ /****************************************************************/ int binary_search(int[].x).4.&x).

12 .

scanf("%d".int A.int C) { if(n>=1) { tow_of_hanoi(n-1.int. tow_of_hanoi(n-1.int.2. printf("Enter two number of disks").3).int B.B.&n). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ void tow_of_hanoi(int n. printf("\nTransfer disk from \t%d\t to \t%d\t".1.A).B).A./****************************************************************/ /* Prog for Towers of Hanoi Problem with 3 toweres & n disks */ /* By: Akhilesh Kumar Srivastava */ /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void tow_of_hanoi(int. /****************************************************************/ void main() { int n.int).A.C. getch().B).C. tow_of_hanoi(n. } } /****************************************************************/ Output:-> ----------Enter two number of disks3 Transfer disk from 1 to 2 Transfer disk from 1 to 3 Transfer disk from 2 to 3 Transfer disk from 1 to 2 Transfer disk from 3 to 1 Transfer disk from 3 to 2 Transfer disk from 1 to 2 -------------------------------------------Enter two number of disks4 Transfer disk from 1 to 3 Transfer disk from 1 to 2 Transfer disk from 3 to 2 Transfer disk from 1 to 3 Transfer disk from 2 to 1 Transfer disk from 2 to 3 Transfer disk from 1 to 3 Transfer disk from 1 to 2 Transfer disk from 3 to 2 Transfer disk from 3 to 1 Transfer disk from 2 to 1 Transfer disk from 3 to 2 Transfer disk from 1 to 3 Transfer disk from 1 to 2 Transfer disk from 3 to 2 ------------------------------------------- 13 . clrscr().

Enter two number of disks5 Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer Transfer disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk disk from from from from from from from from from from from from from from from from from from from from from from from from from from from from from from from 1 1 2 1 3 3 1 1 2 2 3 2 1 1 2 1 3 3 1 3 2 2 3 3 1 1 2 1 3 3 1 to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to 2 3 3 2 1 2 2 3 3 1 1 3 2 3 3 2 1 2 2 1 3 1 1 2 2 3 3 2 1 2 2 14 .

c=natural_mul(a. clrscr().b. getch().&a). printf("\n%d".&b). printf("Enter the first no:=>\t").b).y-1))./****************************************************************/ /******* Prog for Natural Number multiplication using recursion ********/ /*** By: Akhilesh Kumar Srivastava ***/ /****************************************************************/ /****************************************************************/ /***************** Function prototyping *************************/ /****************************************************************/ int natural_mul(int. /****************************************************************/ void main() { int a.int y) { if(y==1) return x. } /****************************************************************/ /***************** Function defintion ***************************/ /****************************************************************/ int natural_mul(int x.c. scanf("%d". } /****************************************************************/ Output 15 .int). printf("Enter the second no:=>\t").c). scanf("%d". else return (x+natural_mul(x.

XIV. WRITE A PROGRAM TO INPUT A STRING AND CALCULATE THE LENGTH OF THE STRING WITHOUT USING LIBRARY FUNCTIONS. X. WRITE A PROGRAM TO ACCEPT A STRING AND CONVERT ALL THE CHARACTERS OF THE STRING FROM LOWERCASE TO UPPERCASE .ASSIGNMENT NO. –2 OBJECT: . IX. XI.PROGRAMS BASED ON STRINGS. WRITE A PROGRAM TO COMPARE TWO STRINGS WITHOUT USING LIBRARY FUNCTIONS. VIII. XII. WRITE A PROGRAM FOR PATTERN MATCHING 16 . WRITE A PROGRAM TO COPY ONE STRING TO ANOTHER WITHOUT USING LIBRARY FUNCTIONS. XIII. WRITE A PROGRAM TO PRINT A STRING IN ALPHABETICAL ORDER. WRITE A PROGRAM TO CONCATENATE TWO STRINGS.

i). scanf("%s". while(s[i]!='\0') i++. getch(). printf("Enter the string:=>\t"). } /************************************************************/ OUTPUT: Enter the string:=> akhileshsrivastava Length of the string is:=> 18 Enter the string:=> inderprastha Length of the string is:=> 12 17 . int i=0. printf("Length of the string is:=> %d". clrscr().s)./************************************************************/ /******** PROGRAM FOR FINDING THE LENGTH OF THE STRING ********/ /******** BY: AKHILESH SRIVASTAVA **************/ /******** DES: THE STRING MAY NOT INCLUDE SPACES IN BETWEEN *******/ /************************************************************/ void main() { char s[25].

getch(). int i=0.i). printf("Length of the string is:=> %d". while(s[i]!='\0') i++. scanf("%[^\n]"./************************************************************/ /******** PROGRAM FOR FINDING THE LENGTH OF THE STRING ********/ /******** BY: PUNEET SHAH **************/ /******** DES: THE STRING MAY INCLUDE SPACES IN BETWEEN *******/ /************************************************************/ void main() { char s[25].s). } /************************************************************/ Output: Enter the string:=> akhilesh srivastava Length of the string is:=> 19 Enter the string:=> Engineering College Bikaner Length of the string is:=> 27 18 . printf("Enter the string:=>\t"). clrscr().

while(s[i]!='\0') { s[i]=s[i]+32. getch().s)./******************************************************************/ /***** PROG FOR CONVERSION OF UPPERCASE CHARACTERS TO LOWERCASE IN THE STRING *****/ /******** BY: AKHILESH SRIVASTAVA **************************/ /******************************************************************/ void main() { char s[25].s). } /************************************************************/ Output: Enter the string:=> MYNAME The Result string is :=> myname Enter the string:=> INDERPRASTHA The Result string is :=> inderprastha 19 . int i=0. scanf("%s". clrscr(). i++. printf("Enter the string:=>\t"). } printf("\nThe result string is:=> %s".

s). } /************************************************************/ Output: Enter the string:=> myname The Result string is :=> MYNAME Enter the string:=> inderprastha The Result string is :=> INDERPRASTHA 20 ./******************************************************************/ /****** PROG FOR CONVERSION OF LOWERCASE CHARACTERS TO UPPERCASE IN THE STRING ******/ /******** BY: AKHILESH SRIVASTAVA **************************/ /******************************************************************/ void main() { char s[25]. printf("Enter the string:=>\t"). i++. getch().s). clrscr(). while(s[i]!='\0') { s[i]=s[i]-32. int i=0. scanf("%s". } printf("\nThe result string is:=> %s".

scanf("%s". int i=0.j=0. while(s1[i]!='\0') { d[i]=s1[i].s2[25].s2). } d[i]='\0'.d). printf("Enter the second string:=>\t"). i++. getch().s1). i++. scanf("%s".d[50]. } /************************************************************/ Output Enter the first string:=> amit Enter the second string:=> abh The Concatenated string is:=> amitabh ENTER THE FIRST STRING:=> ENTER THE SECOND STRING:=> ENGINE ERING THE CONCATENATED STRING IS:=> ENGINEERING 21 . } while(s2[j]!='\0') { d[i]=s2[j]. printf("Enter the first string:=>\t"). clrscr(). printf("\nThe Concatenated string is:=> %s"./********************************************************************/ /*********** PROG FOR CONCATENATION OF TWO STRINGS ******************/ /******** BY: AKHILESH SRIVASTAVA ***************************/ /*******************************************************************/ void main() { char s1[25]. j++.

else { while(s1[i]!='\0') { if(s1[i]==s2[i]) i++. if(l1>l2) printf("\nFirst String is larger than the second one"). scanf("%s". else { if(l1<l2) printf("\nSecond String is larger than the first one").j. printf("Enter the first string:=>\t").s2).t.s2[25]. printf("Enter the second string:=>\t").l1=0. } /************************************************************/ 22 .s1). } if(i==l1) printf("\nStrings are same").l2=0. else printf("\nStrings are different"). } } getch(). scanf("%s"./******************************************************************/ /********** PROG FOR COMARISION OF TWO STRINGS *********************/ /******** BY: AKHILESH SRIVASTAVA **************************/ /******************************************************************/ void main() { char s1[25]. else break. while(s1[l1]!='\0') l1++. clrscr(). int i=0. while(s2[l2]!='\0') l2++.

Output Enter the first string:=> akhilesh Enter the second string:=> akhilesh Strings are same Enter the first string:=> akhilesh Enter the second string:=> kumar First String is larger than the second one Enter the first string:=> myname Enter the second string:=> engineering Second String is larger than the first one 23 .

d[25]. printf("\nThe destination string is:=> %s". scanf("%s".s). i++.s). scanf("%s". while(s[i]!='\0') { d[i]=s[i]. printf("\nThe source string is:=> %s". clrscr().d). printf("Enter the source string:=>\t"). printf("Enter the destination string:=>\t").d). getch(). int i=0./*******************************************************************/ /*********** PROG FOR COPYING ONE STRING TO THE OTHER ***************/ /************ BY: AKHILESH SRIVASTAVA **********************/ /******************************************************************/ void main() { char s[25]. } d[i]='\0'. } /************************************************************/ OUTPUT Enter the source string:=> akhilesh Enter the destination string:=> kumar The source string is:=> akhilesh The destination string is:=> akhilesh 24 .

while(s[l]!='\0') l++. getch().j<l.i++) { for(j=i+1.s).j++) { if(s[j]<s[i]) { t=s[i]. s[i]=s[j]. s[j]=t. for(i=0.i<l-1.s). } } } printf("\nThe sorted string is:=> %s". int i. clrscr(). scanf("%s".t. } /************************************************************/ OUTPUT Enter the string:=> inderprastha The sorted string is:=> aadehinprrst 25 . printf("Enter the string:=>\t").l=0./******************************************************************/ /********** PROG FOR PRINTING A STRING IN THE ALPHABETICAL ORDER *****/ /************* BY: AKHILESH SRIVASTAVA *********************/ /******************************************************************/ void main() { char s[25].j.

char s1[30].char s2[]) { int i. i++) { j=0. int len2=strlen(s2).pos). } else { printf("pattern is matching %d".h> #include<string. k=i. printf("\nEnter the second string:=>\t").h> #include<conio. scanf("%s". scanf("%s".i<(len1). for(i=0.s2). } /****************************************************************/ /***************** Function Definitions *************************/ /****************************************************************/ int xstrsearch(char s1[].s2-j=%c\n". printf("\nEnter the first string:=>\t").s1[k]. printf("s1-k=%c.j. int pos.h> /****************************************************************/ /***************** Function prototyping *************************/ /****************************************************************/ int xstrsearch(char s1[].s2). 26 .char s2[]). if(pos<0) { printf("\npattern dose not match").s1). /****************************************************************/ void main() { clrscr().s2[j]). char s2[10]. } getch(). pos=xstrsearch(s1. int len1=strlen(s1)./*****************************************************************/ /*********** Prog for String Pattern matching ************************/ /*********** By: Akhilesh Kumar Srivastava *************************/ /****************************************************************/ /****************** Pre-processoer directives *******************/ /****************************************************************/ #include<stdio.k.

..s2-j=c matching from .s2-j=o matching from ...s2-j=c s1-k=n..s1[k]..s2-j=c s1-k=r..s2[j])..s2-j=c s1-k=c...s1-k=l..s2-j=c s1-k=n. k++.s2-j=c s1-k=g.s2-j=e pattern is matching 11 27 .s2-j=c matching from .....s2-j=c s1-k=e..s1-k=%c.s2-j=e matching from ...s1-k=e.... } } return(-1).s1-k=g.s2-j=c s1-k=i....s2-j=c s1-k=n.s2-j=l matching from ...while(s1[k]==s2[j]&&j<len2) { printf("matching from ..s1-k=l.s2-j=%c\n".s2-j=c s1-k=e.s1-k=c...s2-j=c s1-k=g.s1-k=e.s2-j=c s1-k=i.. } /****************************************************************/ OUTPUT Enter the first string:=> engineeringcollege Enter the second string:=> college s1-k=e.s1-k=o. } if(j==len2) { return(i)..s2-j=g matching from .s2-j=l matching from . j++..

PROBLEMS :IV.DOUBLE DIMENSION ARRAY IS USED TO STORE THE SAME DATA TYPE NUMBERS IN MATRIX FORM. TH 2-> DOUBLE DIMENSION ARRAY:. FOR EXAMPLE IF WE DEFINE INT A[3][3]. WHEN WE STORE THE VARIOUS NUMBERS OF THE SAME DATA TYPE THEN WE HAVE TO USE THE CONCEPT OF ARRAY. THE FIRST ELEMENT IS STORED AT THE POSITION OF A[0] AND 5 ELEMENT IS STORED AT THE POSITION OF A[4]. PROGRAM FOR DELETION OF AN ELEMENT FROM THE SPECIFIED LOCATION IN THE ARRAY 28 . THIS MEANS A IS AN ARRAY OF 3 ROWS AND 3 COLUMNS.ASSIGNMENT NO. THIS MEANS A IS AN ARRAY OF 5 INTEGERS AND 10 BYTES MEMORY IS ALLOCATED FOR A.SINGLE DIMENSION ARRAY IS USED TO STORE THE SAME DATA TYPE NUMBERS. FOR EXAMPLE IF WE DEFINE INT A[5]. SO WE CAN SAY THAT ARRAY IS THE COLLECTION OF SAME DATA TYPE. THE SYNTAX FOR DEFINING THE SINGLE DIMENSIONAL ARRAY IS AS FOLLOWS:<DATA TYPE><ARRAY NAME>[SIZE]. THE FIRST ELEMENT IS STORED AT THE POSITION OF A[0][0] AND THE LAST ELEMENT IS STORED AT THE POSITION OF A[2][2]. –3 OBJECT:. THE SYNTAX FOR DEFINING THE SINGLE DIMENSIONAL ARRAY IS AS FOLLOWS:<DATA TYPE><ARRAY NAME>[SIZE OF ROW][SIZE OF COLUMN]. ARRAY IS CLASSIFIED INTO FOLLOWING CATEGORIES:1->SINGLE DIMENSION ARRAY 2->DOUBLE DIMENSION ARRAY 1-> SINGLE DIMENSION ARRAY:. PROGRAM FOR INSERTION OF AN ELEMENT AT SPECIFIED LOCATION IN THE ARRAY V. THEORY AND CONCEPTS:ARRAY PLAYS A VERY IMPORTANT ROLE IN C LANGUAGE.PROGRAMS BASED ON ARRAYS.

/****************************************************************/ int a[20]={5. start: printf("\n\n\n1. break. printf("\nRemoved item is:=> \t%d". printf("4. scanf("%d".1. scanf("%d". 29 . scanf("%d". Removal from the array\n"). traverse().9.&c). printf("The array is=>\n"). int n=7.x). traverse(n). void traverse(). traverse().2. printf("\nEnter the location where to insert"). scanf("%d". printf("Enter your choice\t").3.y).h> /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void insert(int. printf("3. int remov(int).&y). printf("\nThe array is:=>\n"). printf("===========================\n").&y). insert(x. clrscr(). DELETION. printf("\nThe array is:=>\n"). char ch.7}./****************************************************************/ /* PROG FOR ARRAY INSERTION. case 2: printf("\nEnter the location from where to remov"). printf("2. x=remov(y). /****************************************************************/ void main() { int x. Traversal the array\n"). switch(c) { case 1: printf("\nEnter the element to be inserted").y.8. exit\n").&x).c.int). Insertion in the array\n"). break. TRAVERSAL*/ /* BY: AKHILESH KUMAR SRIVASTAVA */ /****************************************************************/ #include<stdio.

int y) { int i. scanf("%c". x=a[y].i>=y. } /****************************************************************/ void traverse() { int i. for(i=y.&ch).i<n. fflush(stdin).case 3: printf("\nThe array is:=>\n"). } /****************************************************************/ int remov(int y) { int i. for(i=0.i++) printf("%d\t".i--) a[i+1]=a[i]. a[y]=x.x. default: printf("\nWrong choice"). n=n-1. case 4: exit(1). break. } /****************************************************************/ /* Function Definitions */ /****************************************************************/ void insert(int x.a[i]). n=n+1. if(ch=='y'||ch=='Y') goto start. traverse(). return x. } printf("\n\nDo you wish to continue? (y/n) "). break. } /****************************************************************/ 30 .i<n.i++) a[i]=a[i+1]. for(i=n-1.

Removal from the array 3. Removal from the array 3. Insertion in the array 2. Traversal the array 4.OUTPUT The array is=> 5 8 2 3 1 9 7 1. exit =========================== Enter your choice 1 Enter the element to be inserted43 Enter the location where to insert3 The array is:=> 5 8 2 43 3 1 9 7 Do you wish to continue? (y/n) y 1. Traversal the array 4. Insertion in the array 2. exit =========================== Enter your choice 2 Enter the location from where to remov6 Removed item is:=> The array is:=> 5 8 2 43 9 3 1 7 Do you wish to continue? (y/n) 31 .

clrscr().&j). printf("\n\n****************************************************************").i++) { printf("%d.h> #include<conio.i++) { printf("\n\nEnter no =").c++) { z[c]=z[c+1].int j) { int b=0.z[100].VAIBHAV VERMA */ /* CSE Second Year Third Semester */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. while(b<j) { if(z[b]==a) { for(c=b. } printf("\n\nafter detetion %d the list is =".d. = ").c.h> /****************************************************************/ /********************** Function Definition *********************/ /****************************************************************/ void delet(int a. } j--.e.i<j.i./****************************************************************/ /*TO DELETE ALL THE OCCURENCES OF ANY NUMBER IN AN ARRAY*/ /*BY .b. printf("\n\n ****TO DELET AL THE OCCURENCES**** ").c.i<j.i.". } } /*******************************************************************/ void main() { int a.d.int z[100]. for(i=0.c<j. char w.a). scanf("%d".j. scanf("%d". do { printf("\n\nEnter totl no.z[i]). for(i=0.&z[i]). 32 . } else b++.

printf("\n\nTo continue y/n = "). scanf(" %c".7.&a). } /*******************************************************************/ Output ****TO DELET AL THE OCCERENCES**** ******************************************************************************** Enter totl no.2. delet(a.i<j.2.&w).4.4.z. ******************************************************************************** To continue y/n = 33 .". for(i=0. = 6 Enter no =2 Enter no =6 Enter no =7 Enter no =2 Enter no =4 Enter no =2 U have enter =2.} printf("\n\nU have enter ="). printf("\n\n***************************************************************").j).6. Enter the no u wnt to delet = 2 after detetion 2 the list is =6. } printf("\n\nEnter the no u wnt to delet = ").}while((w=='y')||(w=='Y')). scanf("%d".i++) { printf("%d.7.z[i]).

(4) WRITE A PROGRAM TO CONVERT A INFIX EXPRESSION TO PREFIX EXPRESSION.TO INITITALIZE S AS AN EMPTY STACK. THEORY AND CONCEPTS:A STACK IS ONE OF THE MOST COMMONLY USED DATA STRUCTURE . 6-> INFIX TO POSTFIX CONVERSION. 2->PUSH(S.TO PUSH ELEMENT I ONTO STACK S. (6) PROGRAM TO CHECK THE VALIDITY OF THE GIVEN EXPRESSION BASED ON THE PARENTHESIS (7) PROGRAM TO CHECK THE VALIDITY OF THE GIVEN EXPRESSION BASED ON THE BRACKETS (8) PROGRAM TO CHECK IF THE GIVEN STRING IS PALINDROME. 3-> PARENTHESIS CHECKER 4-> MATHEMATICAL NOTATION TRANSLATION.TO FIND THE TOP ELEMENT OF THE STACK 5->EMPTY(S):.PROGRAMS BASED ON STACK. 3->POP(S):. THERE IS NEED TO SAVE THE CURRENT VALUES OF PARAMETERS .TO CHECK IF THE STACK IS EMPTY 1-> STACKS ARE USED TO PASS PARAMETERS BETWEEN FUNCTIONS. –4 OBJECT:. POP AND DISPLAY OPTION OF THE STACK WITH THE HELP OF STATIC MEMORY ALLOCATION. A STACK. 4->STACKTOP(S):. 5-> EVALUATING MATHEMATICAL EXPRESSIONS. PROBLEMS :(1) WRITE A MENU DRIVEN PROGRAM TO IMPLEMENT THE PUSH. (2) WRITE A MENU DRIVEN PROGRAM TO IMPLEMENT THE PUSH.ASSIGNMENT NO. 7-> QUICK SORT ALGORITHM. 2-> HIGH LEVEL PROGRAMMING LANGUAGES.I):. (3) WRITE A PROGRAM TO CONVERT A INFIX EXPRESSION TO POSTFIX EXPRESSION. SUCH AS STACK FOR BOOKKEEPING . ALSO CALLED A LAST IN FIRST OUT(LIFO) SYSTEM.TO ACCESS AND REMOVE THE TOP ELEMENT OF THE STACK. POP AND DISPLAY OPTION OF THE STACK WITH THE HELP OF DYNAMIC MEMORY ALLOCATION. IS A LINEAR LIST IN WHICH INSERTIONS AND DELETIONS CAN TAKE PLACE ONLY AT ONE END. THAT PROVIDES SUPPORT FOR RECURSION USE IN EACH RECURSIVE CALL. CALLED THE TOP. (9) PROGRAM FOR SIMULATION OF TWO STACKS ON A SINGLE ARRAY 34 . ON A CALL TO A FUNCTION. THE INSERTION AND DELETION OPERATIONS IN STACK TERMINOLOGY ARE KNOWN AS PUSH AND POP OPERATION. REMEMBER PASCAL. (5) WRITE A PROGRAM TO EVALUATE THE GIVEN POSTFIX EXPRESSION. LOCAL VARIABLES AND THE RETURN ADDRESS . THE FOLLOWING OPERATIONS ARE PERFORMED ON STACKS:1->INIT(S):. C ETC. THE PARAMETERS AND LOCAL VARIABLES ARE STORED ON A STACK.

&x). case 2: printf("\nEnter the element to be inserted"). scanf("%d".&c). int empty(struct stack*). int c. char ch. int pop(struct stack*). int top. void push(struct stack*. printf("5. break. clrscr(). switch(c) { case 1: init(&s). printf("===========================\n"). int stacktop(struct stack s). Displaying the top Element of the stack\n"). }. Insertion in the stack\n"). /****************************************************************/ /* Structure Declaraion */ /****************************************************************/ struct stack { int item[STACKSIZE].h> #define STACKSIZE 100 #define TRUE 1 #define FALSE 0 /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void init(struct stack*).int x). printf("3. Initialisation of stack\n"). printf("Enter your choice\t"). 35 . Removal from the stack\n")./****************************************************************/ /* Prog for Primitive operations on stack*/ /* By: Akhilesh Kumar Srivastava */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. start: printf("1. printf("2. /****************************************************************/ /****************************************************************/ void main() { struct stack s.x. printf("4. scanf("%d". exit\n").

} s->top++.x). } printf("\nDo you wish to continue? (y/n) "). default: printf("\nWrong choice"). if(ch=='y'||ch=='Y') goto start. case 4: x=stacktop(s). case 3: x=pop(&s). printf("\nRemoved item is:=> \t%d". } /****************************************************************/ /***************** Insertion of an item in stack ****************/ /****************************************************************/ void push(struct stack *s.&ch). break.push(&s. break. 36 . scanf("%c". else return FALSE. exit(1). break. fflush(stdin). break.x).x). } /****************************************************************/ /******************* Function Definitions ***********************/ /****************************************************************/ /********************* Initialization of stack ******************/ /****************************************************************/ void init(struct stack *s) { s->top=-1. } /****************************************************************/ /******************* checking if stack is empty *****************/ /****************************************************************/ int empty(struct stack *s) { if(s->top==-1) return TRUE.int x) { if(s->top==STACKSIZE-1) { printf("stack overflow"). case 5: exit(1). printf("\nStacktop item is:=> \t%d".

} /****************************************************************/ 37 . y=s. s->top--. } /****************************************************************/ /*************** Removal of an item from stack ******************/ /****************************************************************/ int pop(struct stack *s) { int y.s->item[s->top]=x. } /****************************************************************/ /************** Displaying top element of the stack *************/ /****************************************************************/ int stacktop(struct stack s) { int y. return(y). exit(1).item[s. } y=s->item[s->top]. return(y). if(empty(s)) { printf("Stack Underflows").top].

Insertion in the stack 3. Removal from the stack 4. exit =========================== Enter your choice 4 Stacktop item is:=> 100 Do you wish to continue? (y/n) 38 . exit =========================== Enter your choice 2 Enter the element to be inserted200 Do you wish to continue? (y/n) y 1. exit =========================== Enter your choice 3 Removed item is:=> 200 Do you wish to continue? (y/n) y 1. exit =========================== Enter your choice 2 Enter the element to be inserted100 Do you wish to continue? (y/n) y 1.Output 1. Insertion in the stack 3. Initialisation of stack 2. Displaying the top Element of the stack 5. Displaying the top Element of the stack 5. Initialisation of stack 2. Displaying the top Element of the stack 5. Initialisation of stack 2. Removal from the stack 4. exit =========================== Enter your choice 1 Do you wish to continue? (y/n) y 1. Removal from the stack 4. Removal from the stack 4. Insertion in the stack 3. Displaying the top Element of the stack 5. Initialisation of stack 2. Initialisation of stack 2. Insertion in the stack 3. Insertion in the stack 3. Removal from the stack 4. Displaying the top Element of the stack 5.

if(symb>='a'&&symb<='z') { postfix[j]=symb. char stacktop(struct stack s). } else { while(!empty(&s)&&prcd(stacktop(s). printf("Enter the infix expression:=>\t").postfix[20]. j++. }.symb). 39 . while(infix[i]!='\0') { symb=infix[i].char). int i=0.infix). } i++. scanf("%s".symb)) { topsymb=pop(&s). j++.topsymb. int top./****************************************************************/ /* Prog for Conversion of Infix expression to Postfix using Stacks */ /* By: Akhilesh Kumar Srivastava */ /* Des: The evaluation is based on simulation of operator stack */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #define STACKSIZE 100 #define TRUE 1 #define FALSE 0 /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void init(struct stack*). postfix[j]=topsymb. /****************************************************************/ /* Structure Declaraion */ /****************************************************************/ struct stack { char item[STACKSIZE]. /****************************************************************/ void main() { char infix[20].char x). char pop(struct stack*). } push(&s.symb. void push(struct stack*. int prcd(char. struct stack s. int empty(struct stack*). init(&s).j=0.

exit(1). } /****************************************************************/ /******************* Function Definitions ***********************/ /****************************************************************/ /************* Primitive operations on stack ********************/ /****************************************************************/ void init(struct stack *s) { s->top=-1. postfix[j]=topsymb. printf("\nthe postfix expression is:=> %s".} while(!empty(&s)) { topsymb=pop(&s). 40 .postfix). return(y). else return FALSE. j++.char x) { if(s->top==STACKSIZE-1) { printf("stack overflow"). } /****************************************************************/ char pop(struct stack *s) { char y. if(empty(s)) { printf("Stack Underflows"). } /****************************************************************/ int empty(struct stack *s) { if(s->top==-1) return TRUE. s->item[s->top]=x. exit(1). s->top--. } postfix[j]='\0'. getch(). } s->top++. } y=s->item[s->top]. } /****************************************************************/ void push(struct stack *s.

else return TRUE. } /****************************************************************/ /******************** Precedence Function ***********************/ /* The function checks the precedence of operator over b.char b) { if(a=='$') { if(b=='$') return FALSE. it returns true and false otherwise */ /****************************************************************/ int prcd(char a. else return TRUE. If a has higher precedence than b. return FALSE.} /****************************************************************/ char stacktop(struct stack s) { char y. } if(a=='*'||a=='/') { if(b=='$') return FALSE.top]. return(y). y=s. } if(a=='+'||a=='-') { if(b=='+'||b=='-') else } return TRUE.item[s. } /****************************************************************/ Output Enter the infix expression:=> a+b*c-d/e$f$g the postfix expression is:=> abc*+defg$$/- 41 .

strrev(infix). char pop(struct stack*). j++. }.h> #define STACKSIZE 100 #define TRUE 1 #define FALSE 0 /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void init(struct stack*). } else { while(!empty(&s)&&!prcd(symb.infix)./****************************************************************/ /* Prog for Conversion of Infix expression to Prefix using Stack*/ /* By: Akhilesh Kumar Srivastava */ /* Des: The evaluation is based on simulation of operator stack */ /****************************************************************/ /****************** Pre-processoer directives *******************/ /****************************************************************/ #include<string. init(&s). void push(struct stack*. struct stack s.topsymb. j++. char stacktop(struct stack s). while(infix[i]!='\0') { symb=infix[i].j=0.stacktop(s))) { topsymb=pop(&s).char). scanf("%s". int empty(struct stack*). } 42 . int i=0. int top.symb. /****************************************************************/ void main() { char infix[20]. /****************************************************************/ /* Structure Declaraion */ /****************************************************************/ struct stack { char item[STACKSIZE].prefix[20]. if(symb>='a'&&symb<='z') { prefix[j]=symb.char x). printf("Enter the infix expression:=>\t"). prefix[j]=topsymb. int prcd(char.

} /****************************************************************/ char pop(struct stack *s) { char y. printf("\nthe prefix expression is:=> %s".push(&s. } /****************************************************************/ int empty(struct stack *s) { if(s->top==-1) return TRUE. getch(). else return FALSE. } while(!empty(&s)) { topsymb=pop(&s). } /****************************************************************/ /******************* Function Definitions ***********************/ /****************************************************************/ /************* Primitive operations on stack ********************/ /****************************************************************/ void init(struct stack *s) { s->top=-1. s->item[s->top]=x. exit(1).char x) { if(s->top==STACKSIZE-1) { printf("stack overflow"). exit(1). } i++.prefix). prefix[j]=topsymb. 43 . j++.symb). } prefix[j]='\0'. if(empty(s)) { printf("Stack Underflows"). } s->top++. } /****************************************************************/ void push(struct stack *s. strrev(prefix).

it returns true and false otherwise */ /****************************************************************/ int prcd(char a. If a has higher precedence than b. } /****************************************************************/ Output Enter the infix expression:=> a+b*c/d the prefix expression is:=> +a/*bcd 44 .} y=s->item[s->top]. return(y). } if(a=='*'||a=='/') { if(b=='$') return FALSE.item[s.top]. } /****************************************************************/ /******************** Precedence Function ***********************/ /* The function checks the precedence of operator over b. return FALSE. s->top--. } if(a=='+'||a=='-') { if(b=='+'||b=='-') else } return TRUE. return(y). else return TRUE.char b) { if(a=='$') { if(b=='$') return FALSE. else return TRUE. y=s. } /****************************************************************/ char stacktop(struct stack s) { char y.

} printf("\nValue of the expression is:=> %d". push(&s. int top. void push(struct stack*. /****************************************************************/ void main() { char post[20]. int pop(struct stack*). init(&s).opnd2). scanf("%s". struct stack s. } i++. }.int x).opnd2. if(symb>='0'&&symb<='9') push(&s. else { opnd2=pop(&s).int x.opnd1.symb-'0').symb.pop(&s)). while(post[i]!='\0') { symb=post[i].int y). int val(char op. printf("Enter the Postfix expression:=>\t").post). } 45 .value).opnd1. opnd1=pop(&s). /****************************************************************/ /* Structure Declaraion */ /****************************************************************/ struct stack { int item[STACKSIZE].value./****************************************************************/ /* Program for Evaluation of Postfix expression using Stacks */ /* By: Akhilesh Kumar Srivastava */ /* Des: The evaluation is based on simulation of operand stack */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #define STACKSIZE 100 #define TRUE 1 #define FALSE 0 /****************************************************************/ /* Function Prototyping */ void init(struct stack*). int empty(struct stack*). getch(). int i=0. value=val(symb.

} /****************************************************************/ int pop(struct stack *s) { int y.int x) { if(s->top==STACKSIZE-1) { printf("stack overflow"). if(empty(s)) { printf("Stack Underflows"). s->top--.int opnd2) { int v. } s->top++. s->item[s->top]=x. } y=s->item[s->top]. break. } /****************************************************************/ int empty(struct stack *s) { if(s->top==-1) return TRUE. 46 . switch(op) { case '+': v=opnd1+opnd2./****************************************************************/ /******************* Function Definitions ***********************/ /****************************************************************/ /************* Primitive operations on stack ********************/ /****************************************************************/ void init(struct stack *s) { s->top=-1. return(y). case '*': v=opnd1*opnd2. } /****************************************************************/ void push(struct stack *s.int opnd1. else return FALSE. exit(1). exit(1). break. } /****************************************************************/ int val(char op.

} /****************************************************************/ Output Enter the Postfix expression:=> 68*422$/-52*+ Value of the expression is:=> 58 47 .break.opnd2).case '/': v=opnd1/opnd2. break. break. case '$': v=pow(opnd1. } return v. case '-': v=opnd1-opnd2.

opnd2. struct stack ops./****************************************************************/ /* Program for Evaluation of Prefix expression using Stacks */ /* By: Akhilesh Kumar Srivastava */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #define STACKSIZE 100 #define TRUE 1 #define FALSE 0 /****************************************************************/ /* Function Prototyping */ void init(struct stack*). getch(). init(&opnds). opnd1=pop(&opnds)-'0'. value=val(op.op.symb). if(symb>='0'&&symb<='9') push(&opnds. } } } i++. op=pop(&ops). while(pre[i]!='\0') { symb=pre[i]. void push(struct stack*.pop(&opnds)-'0'). /****************************************************************/ void main() { char pre[20].opnd2). int empty(struct stack*).opnds.pre). init(&ops).symb).char x). }. char pop(struct stack*). } printf("\nValue of the expression is:=> %d".int y). push(&opnds. printf("Enter the Prefix expression:=>\t").value+'0'). int i=0.int x.opnd1.symb. else { if(symb=='+'||symb=='-'||symb=='*'||symb=='/'||symb=='$') push(&ops. int top. scanf("%s".opnd1. clrscr(). 48 . int val(char op. else { if(symb==')') { opnd2=pop(&opnds)-'0'. /****************************************************************/ /* Structure Declaraion */ /****************************************************************/ struct stack { char item[STACKSIZE].value.

}
/****************************************************************/
/******************* Function Definitions ***********************/
/****************************************************************/
/************* Primitive operations on stack ********************/
/****************************************************************/
void init(struct stack *s)
{
s->top=-1;
}
/****************************************************************/
int empty(struct stack *s)
{
if(s->top==-1)
return TRUE;
else
return FALSE;
}
/****************************************************************/
void push(struct stack *s,char x)
{
if(s->top==STACKSIZE-1)
{
printf("stack overflow");
exit(1);
}
s->top++;
s->item[s->top]=x;
}
/****************************************************************/
char pop(struct stack *s)
{
int y;
if(empty(s))
{
printf("Stack Underflows");
exit(1);
}
y=s->item[s->top];
s->top--;
return(y);
}
/****************************************************************/
int val(char op,int opnd1,int opnd2)
{
int v;
switch(op)
{
case '+': v=opnd1+opnd2;
break;
case '*': v=opnd1*opnd2;
break;
case '/': v=opnd1/opnd2;
break;
case '-': v=opnd1-opnd2;
break;
case '$': v=pow(opnd1,opnd2);break;
}
return v;
}

49

Output:->
--------Enter the Prefix expression:=>

(+12)

Value of the expression is:=> 3
----------------------------------------Enter the Prefix expression:=>
(+(*42)3)
Value of the expression is:=> 11
-----------------------------------------

50

/****************************************************************/
/* Prog for Validity of expression based on parenthesis */
/* By: Akhilesh Kumar Srivastava */
/****************************************************************/
/****************** Pre-processoer directives *******************/
#define STACKSIZE 100
#define TRUE 1
#define FALSE 0
/****************************************************************/
/* Structure declararion */
/****************************************************************/
struct stack
{
char item[STACKSIZE];
int top;
};
/****************************************************************/
/* Function Prototyping */
/****************************************************************/
void init(struct stack *);
void push(struct stack *,char x);
char pop(struct stack *);
int empty(struct stack *);
/****************************************************************/
void main()
{
struct stack s;
int i;
char x;
char exp[20];
clrscr();
init(&s);
printf("Enter the infix expression with paranthesis");
scanf("%s",exp);
for(i=0;exp[i]!='\0';i++)
{
if(exp[i]=='(')
push(&s,exp[i]);
if(exp[i]==')')
x=pop(&s);
}
if(empty(&s))
printf("\n\tThe Expression is correct");
else
printf("\n\tThe Expression is not correct");
getch();
}
/****************************************************************/
/* Function Definitions */
51

char x) { if(ps->top==STACKSIZE-1) { printf("stack Overflows"). else return FALSE. exit(1). if(empty(ps)) { printf("Stack Underflows"). } /****************************************************************/ /*************** Removing an item from the stack ****************/ /****************************************************************/ char pop(struct stack *ps) { char x. } /****************************************************************/ 52 . } /****************************************************************/ /*************** Inserting an item in the stack *****************/ /****************************************************************/ void push(struct stack *ps. } /****************************************************************/ /**************** checking if the stack is empty ****************/ /****************************************************************/ int empty(struct stack *ps) { if(ps->top==-1) return TRUE. ps->item[ps->top]=x. ps->top--. return x. } x=ps->item[ps->top]./****************************************************************/ /************** Initializing the stack **************************/ /****************************************************************/ void init(struct stack *ps) { ps->top=-1. exit(1). } ps->top++.

Output Enter the infix expression with paranthesisa+(b-c*(d/e)) The Expression is correct Enter the infix expression with paranthesisa+(b-c*(d-e) The Expression is not correct 53 .

clrscr()./****************************************************************/ /* Prog for Prog for Validity of expression based on [. char exp[20]. /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void init(struct stack *). /****************************************************************/ void main() { struct stack s. char x.char x). for(i=0. } if(exp[i]=='}') { if(stacktop(&s)=='{') x=pop(&s). else { if(exp[i]==']') { if(stacktop(&s)=='[') x=pop(&s). void push(struct stack *. printf("Enter the infix expression with paranthesis").exp). { & ( brackets*/ /* By: Akhilesh Kumar Srivastava */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #define STACKSIZE 100 #define TRUE 1 #define FALSE 0 /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct stack { char item[STACKSIZE]. int i. char pop(struct stack *). int top. scanf("%s".exp[i]!='\0'. char stacktop(struct stack *). }.exp[i]). 54 .i++) { if(exp[i]=='('||exp[i]=='{'||exp[i]=='[') push(&s. init(&s). int empty(struct stack *).

} x=ps->item[ps->top]. } } } if(empty(&s)) printf("\n\tThe Expression is correct"). } /****************************************************************/ /*************** Removing an item from the stack ****************/ /****************************************************************/ char pop(struct stack *ps) { char x. getch(). } /****************************************************************/ /*************** Inserting an item in the stack *****************/ /****************************************************************/ void push(struct stack *ps. exit(1). if(empty(ps)) { printf("Stack Underflows").} if(exp[i]==')') { if(stacktop(&s)=='(') x=pop(&s). exit(1). ps->item[ps->top]=x. 55 . else printf("\n\tThe Expression is not correct"). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Initializing the stack **************************/ /****************************************************************/ void init(struct stack *ps) { ps->top=-1.char x) { if(ps->top==STACKSIZE-1) { printf("stack Overflows"). } ps->top++.

x=ps->item[ps->top]. return x. } /****************************************************************/ /**************** checking if the stack is empty ****************/ /****************************************************************/ int empty(struct stack *ps) { if(ps->top==-1) return TRUE. } /****************************************************************/ Output Enter the infix expression with paranthesisa+[b-{c+(d/e)}] The Expression is correct Enter the infix expression with paranthesisa+[b-c*{d/e]} The Expression is not correct 56 .ps->top--. else return FALSE. } /****************************************************************/ /*********** Returning the top element of the stack *************/ /****************************************************************/ char stacktop(struct stack *ps) { char x. return x.

clrscr(). printf("Enter the string"). scanf("%s". void push(struct stack*. while(!empty(&s)) { if(str[i]!=stacktop(s)) { flag='f'. char str[20]. init(&s). char pop(struct stack*). int top. } i=0. char stacktop(struct stack s). }. while(str[i]!='\0') { push(&s./****************************************************************/ /*Prog for Finding if the given string is palindrome using stacks*/ /* By: Akhilesh Kumar Srivastava */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #define STACKSIZE 100 #define TRUE 1 #define FALSE 0 /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void init(struct stack*). } else { x=pop(&s). /****************************************************************/ /* Structure Declaraion */ /****************************************************************/ struct stack { char item[STACKSIZE].str[i]).flag='f'.str). /****************************************************************/ /****************************************************************/ void main() { struct stack s. int i=0. int empty(struct stack*).char x). 57 .x. i++. break.

} /****************************************************************/ /******************* checking if stack is empty *****************/ /****************************************************************/ int empty(struct stack *s) { if(s->top==-1) return TRUE. exit(1). } /****************************************************************/ /*************** Removal of an item from stack ******************/ /****************************************************************/ char pop(struct stack *s) { char y. s->item[s->top]=x. } /****************************************************************/ /******************* Function Definitions ***********************/ /****************************************************************/ /********************* Initialisation of stack ******************/ /****************************************************************/ void init(struct stack *s) { s->top=-1.char x) { if(s->top==STACKSIZE-1) { printf("stack overflow"). } s->top++. else return FALSE. else printf("\nEntered string is a palindrome"). } i++. } /****************************************************************/ /***************** Insertion of an item in stack ****************/ /****************************************************************/ void push(struct stack *s.flag='t'. } if(flag=='f') printf("\nEntered string is not a palindrome"). getch(). if(empty(s)) 58 .

} y=s->item[s->top]. exit(1). return(y). y=s. return(y).top]. } /****************************************************************/ /************** Displaying top element of the stack *************/ /****************************************************************/ char stacktop(struct stack s) { char y. } /****************************************************************/ Output Enter the string Akhilesh Entered string is not a palindrome Enter the string dalda Entered string is a palindrome 59 .{ printf("Stack Underflows"). s->top--.item[s.

60 .top1.top2./********************************************************************/ /*To Simulate TWO stacks on a single array*/ /* By: Vaibhav Verma */ /* CSE Second Year 'D' Section */ /********************************************************************/ /******************** Pre-processoer directives *********************/ #include<stdio.h> #define size 6 //defining the max size of an array /********************************************************************/ /****declaration of stack using structures**** here we tack two top pointers for respective stacks*/ /********************************************************************/ struct stack { int a[size].int n) { if(n==1) return( s->top1== -1).h> #include<conio. } /********************************************************************/ /*defining push operation (TO PUSH ANY ELEMENT IN A STACK) for stacks*/ /********************************************************************/ void push(struct stack *s.h> #include<process. int x. }s. /********************************************************************/ /*defining empty operation (to check empty condition) for stacks*/ /********************************************************************/ int empty(struct stack *s.int n) { if(n==1) s->top1=-1. return( s->top2== size). } /********************************************************************/ /*defining init operation (TO INITIALISE THE STACK) for stacks*/ /********************************************************************/ void init(struct stack *s.h> #include<dos.int n) { if(n==1) { if(s->top1+1==s->top2) { printf("First stack overflow").h> #include<stdlib. else s->top2=size.

} s->a[++(s->top1)]=x.n)) { printf("%dth stack underflow". } } /********************************************************************/ /*defining pop operation (TO EMOVE THE TOPMOST ELEMENT OF STACK) for stacks*/ /********************************************************************/ int pop(struct stack *s.exit(1). } else if(n==2) { if(s->top2-1==s->top1) { printf("Second stack overflow").(s->a[s->top2])). } } /********************************************************************/ /*defining printstack operation(TO PRINT PRESENT STACK CONDITION) for stacks*/ /********************************************************************/ void printstack(struct stack *s. return( s->a[(s->top2)++]).n). exit(1). exit(1). } if(n==1) return( s->a[(s->top1)--]). if(!empty(s. else printf(" Top item of first stack is =%d".n)) { if(n==1) printf(" Top item of first stack is =%d".n)) 61 .(s->a[s->top1])).int n) { int i. } /********************************************************************/ /*defining stacktop operation(TO RETURN THE TOPMOST ELEMENT) for stacks*/ /********************************************************************/ void stacktop(struct stack *s.int n) { if(empty(s.int n) { if(!empty(s. } s->a[--(s->top2)]=x.

". for(i=size-1. scanf("%d".i>=s->top2.i%10).&i).1). goto print. case 2: printf("Enter the no u want to push = ").i++) printf("%d.l.").s->a[i]).j.2).i%10).i%10). printstack(&s. printf("The %dth stack is initialised ".". } else if(n==2) { printf("The curnt element in SECOND the stack is = "). AFTER the option no. //init both stacks printf("\n\nEnter the required option " "\nFor init press = 11/12" "\nFor push press = 21/22" "\nFor pop press = 31/32" "\nFor stack top = 41/42" "\nFor exit press = 5"). clrscr(). switch(i/10) { case 1: init(&s.i--) printf("%d. push(&s.{ if(n==1) { printf("The curnt element in FIRST the stack is = "). goto print. } } } /********************************************************************/ void main() { int i. printf("\n\nNOTE:-To enter the stack add the stack no. for(i=0. init(&s. if(empty(&s.i%10).pop(&s.i%10)).k. print:printf("\n\nEnter the option = "). scanf("%d". init(&s.i%10)) 62 .i%10.j.i<=s->top1.&j). case 3: printf("\nThe element removed from stack %d is = %d\n".s->a[i]).

} getch().i%10). goto print. } 63 .printf("Stack is now empty ").i%10). case 4: stacktop(&s. case 5: exit(1). printstack(&s.

Enter the option = 21 Enter the no u want to push = 6 The curnt element in FIRST the stack is = 1. Enter the option = 22 Enter the no u want to push = 7 The curnt element in SECOND the stack is = 2. Enter the option = 11 The 1th stack is initialised Enter the option = 12 The 2th stack is initialised Enter the option = 21 Enter the no u want to push = 1 The curnt element in FIRST the stack is = 1./***************************************************************************/ Output /***************************************************************************/ Enter the required option For init press = 11/12 For push press = 21/22 For pop press = 31/32 For stack top = 41/42 For exit press =5 NOTE:-To enter the stack add the stack no. Enter the option = 22 Enter the no u want to push = 4 Second stack overflow *************************************************************************** Enter the required option For init press = 11/12 64 .5.6.8.7. Enter the option = 22 Enter the no u want to push = 8 The curnt element in SECOND the stack is = 2.8.5. Enter the option = 21 Enter the no u want to push = 5 The curnt element in FIRST the stack is = 1. Enter the option = 22 Enter the no u want to push = 2 The curnt element in SECOND the stack is = 2. AFTER the option no.

AFTER the option no. Enter the option = 21 Enter the no u want to push = 6 The curnt element in FIRST the stack is = 5. Enter the option = 22 65 .For push press For pop press For stack top For exit press = 21/22 = 31/32 = 41/42 =5 NOTE:-To enter the stack add the stack no.6. Enter the option = 11 The 1th stack is initialised Enter the option = 12 The 2th stack is initialised Enter the option = 21 Enter the no u want to push = 5 The curnt element in FIRST the stack is = 5. Enter the option = 31 The element removed from stack 1 is = 6 The curnt element in FIRST the stack is = 5. AFTER the option no. Top item of first stack is =5 Enter the option = 31 The element removed from stack 1 is = 5 Stack is now empty Enter the option = 31 1th stack underflow **************************************************************************** Enter the required option For init press = 11/12 For push press = 21/22 For pop press = 31/32 For stack top = 41/42 For exit press =5 NOTE:-To enter the stack add the stack no. Enter the option = 21 Enter the no u want to push = 5 The curnt element in FIRST the stack is = 5.

5.Enter the no u want to push = 6 The curnt element in SECOND the stack is = 6. Enter the option = 41 Top item of first stack is =7 Enter the option = 42 Top item of first stack is =5 Enter the option = 5 /***************************************************************************/ 66 .7. Enter the option = 21 Enter the no u want to push = 7 The curnt element in FIRST the stack is = 5. Enter the option = 22 Enter the no u want to push = 5 The curnt element in SECOND the stack is = 6.

FOR EXAMPLE LINES AT TICKET COUNTERS AT CINEMA HALLS. CALLED THE REAR OF THE LIST. THEY ARE ARRANGED IN ORDER OF ARRIVAL . RAILWAY STATIONS. PROBLEMS :(1) WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS OPERATIONS ON A LINEAR QUEUE WITH THE HELP OF STATIC MEMORY ALLOCATION. SO THE DATA STRUCTURE IS QUEUE. (5) WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS OPERATIONS ON A PRIORITY QUEUE WITH THE HELP OF STATIC MEMORY ALLOCATION. 67 . 4->EMPTY(Q):. THE FOLLOWING OPERATIONS ARE PERFORMED ON QUEUE:1->INIT(Q):. ARE QUEUES. (2) WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS OPERATIONS ON A LINEAR QUEUE WITH THE HELP OF DYNAMIC MEMORY ALLOCATION. THE FOLLOWING ARE THE APPLICATIONS OF QUEUES:1->THERE ARE SEVERAL ALGORITHMS THAT USE QUEUES TO SOLVE PROBLEMS EFFICIENTLY. –5 OBJECT:. 4->VIRTUALLY EVERY REAL LIFE LINE(SUPPOSED TO BE) A QUEUE. THUS ESSENTIALLY JOBS SENT TO A PRINTER ARE PLACED ON A QUEUE. (3) WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS OPERATIONS ON A CIRCULAR QUEUE WITH THE HELP OF STATIC MEMORY ALLOCATION.PROGRAMS BASED ON QUEUES. A QUEUE IS A LINEAR LIST IN WHICH INSERTIONS CAN TAKE PLACE AT ONE END OF THE LIST. THEORY AND CONCEPTS:IN THE CONTEXT OF DATA STRUCTURES . 3->REMOVE(Q):. FOR EXAMPLE WE HAVE USED QUEUE FOR PERFORMING LEVEL ORDER TRAVERSAL OF A BINARY TREE AND FOR PERFORMING BREADTH FIRST SEARCH ON A GRAPH.I):-TO INSERT(ADD) ELEMENT I IN A QUEUE Q. 3->THERE IS A KIND OF COMPUTER NETWORK WHERE DISK IS ATTACHED TO ONE COMPUTER KNOWN AS FILE SERVER. (4) WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS OPERATIONS ON A DEQUEUE QUEUE WITH THE HELP OF STATIC MEMORY ALLOCATION.TO CHECK IF THE QUEUE IS EMPTY. THE BEHAVIOR OF A QUEUE IS LIKE A FIRST IN FIRST OUT SYSTEM.ASSIGNMENT NO. AND DELETIONS CAN TAKE PLACE ONLY AT OTHER END CALLED THE FRONT OF THE LIST. BUS STANDS ETC.TO ACCESS AND REMOVE AN ELEMENT OF THE QUEUE. 2->WHEN THE JOBS ARE SUBMITTED TO A NETWORKED PRINTER. BECAUSE THE SERVICE IS PROVIDED ON FIRST COME FIRST SERVED BASIS. USERS ON OTHER COMPUTERS ARE GIVEN ACCESS TO FILES ON A FIRST COME FIRST SERVED BASIS.TO INITIALIZE Q AS AN EMPTY QUEUE 2->INSERT(Q.

printf("===========================\n"). Remove from the queue\n").int x). start: printf("1. clrscr(). switch(c) { case 1: init(&q). int rear. break. }. break.h> #define MAXQUEUE 100 #define TRUE 1 #define FALSE 0 /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct queue { int item[MAXQUEUE]. /****************************************************************/ void main() { struct queue q. exit\n"). char ch. case 2: printf("\nEnter the element to be inserted").&x). 68 . scanf("%d"./****************************************************************/ /* Prog for Primtive operations on linear queue */ /* By: Akhilesh Kumar Srivastava */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void init(struct queue *pq). printf("3. printf("2. int front. void insert(struct queue *pq. init(&q).c.&c). Insert in the queue\n"). printf("4. scanf("%d". insert(&q. Initialise the queue\n"). int x.x). int remov(struct queue *pq). printf("Enter your choice\t"). int empty(struct queue *pq).

x). default: printf("\nWrong choice"). pq->front=0. } /****************************************************************/ /******************** Removal from the queue ********************/ /****************************************************************/ int remov(struct queue *pq) { int x. exit(1). } /****************************************************************/ /******************** Insertion in the queue ********************/ /****************************************************************/ void insert(struct queue *pq. } printf("\nDo you wish to continue? (y/n) "). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /******************** Iniailizing the queue *********************/ /****************************************************************/ void init(struct queue *pq) { pq->rear=-1. case 4: exit(1). fflush(stdin). } pq->rear++. pq->item[pq->rear]=x. scanf("%c". if(empty(pq)) { printf("Queue Underflows"). return x. } 69 . pq->front--.&ch). } x=pq->item[pq->front]. printf("\nRemoved item is:=> \t%d".case 3: x=remov(&q). if(ch=='y'||ch=='Y') goto start. break. break. exit(1).int x) { if(pq->rear==MAXQUEUE-1) { printf("Queue Overflows").

Remove from the queue 4. Initialise the queue 2. Insert in the queue 3. Remove from the queue 4. Insert in the queue 3. Remove from the queue 4. exit =========================== Enter your choice 3 Removed item is:=> 100 Do you wish to continue? (y/n) y 1./****************************************************************/ /***********Checking if the queue is empty **********************/ /****************************************************************/ int empty(struct queue *pq) { if(pq->rear-pq->front+1==0) return TRUE. } /****************************************************************/ Output 1. Initialise the queue 2. Insert in the queue 3. Initialise the queue 2. Insert in the queue 3. exit =========================== Enter your choice 2 Enter the element to be inserted500 70 . else return FALSE. Remove from the queue 4. exit =========================== Enter your choice 1 Do you wish to continue? (y/n) y 1. Initialise the queue 2. exit =========================== Enter your choice 2 Enter the element to be inserted100 Do you wish to continue? (y/n) y 1.

printf("Enter your choice\t"). int front. start: printf("1.&c). }. Remove from the queue\n"). printf("===========================\n").h> #define MAXQUEUE 100 #define TRUE 1 #define FALSE 0 /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct queue { int item[MAXQUEUE]. int rear. int empty(struct queue *pq). switch(c) { case 1: case 2: init(&q). clrscr().c./****************************************************************/ /* Prog for Primtive operations on Circular queue */ /* By: Akhilesh Kumar Srivastava */ /* Des: Overcomes the limitaitons of linear Queue */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. int remov(struct queue *pq). scanf("%d". break.&x). printf("3. int x. Insert in the queue\n"). char ch. 71 . /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void init(struct queue *pq). Initialise the queue\n").int x). scanf("%d". exit\n"). printf("2. printf("4. printf("\nEnter the element to be inserted"). /****************************************************************/ void main() { struct queue q. init(&q). void insert(struct queue *pq.

case 3:
case 4:
default:
}

insert(&q,x); break;
x=remov(&q);
printf("\nRemoved item is:=> \t%d",x); break;
exit(1);
printf("\nWrong choice"); break;

printf("\nDo you wish to continue? (y/n) ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
goto start;
getch();
}
/****************************************************************/
/* Function Definitions */
/****************************************************************/
void init(struct queue *pq)
{
pq->rear=MAXQUEUE-1;
pq->front=MAXQUEUE-1;
}
/****************************************************************/
void insert(struct queue *pq,int x)
{
if(pq->rear==MAXQUEUE-1)
pq->rear==0;
else
pq->rear++;
if(pq->front==pq->rear)
{
printf("Queue Overflows");
exit(1);
}
pq->item[pq->rear]=x;
}
/****************************************************************/
int remov(struct queue *pq)
{
int x;
if(empty(pq))
{
printf("Queue Underflows");
exit(1);
}
if(pq->front==MAXQUEUE-1)
pq->front=0;
else
pq->front++;
72

x=pq->item[pq->front];
return x;
}
/****************************************************************/
int empty(struct queue *pq)
{
if(pq->rear==pq->front)
return TRUE;
else
return FALSE;
}
/****************************************************************/

Output
1. Initialise the queue
2. Insert in the queue
3. Remove from the queue
4. exit
===========================
Enter your choice
1
Do you wish to continue? (y/n) y
1. Initialise the queue
2. Insert in the queue
3. Remove from the queue
4. exit
===========================
Enter your choice
2
Enter the element to be inserted100
Do you wish to continue? (y/n) y
1. Initialise the queue
2. Insert in the queue
3. Remove from the queue
4. exit
===========================
Enter your choice
3
Removed item is:=> 100
Do you wish to continue? (y/n) y
1. Initialise the queue
2. Insert in the queue
3. Remove from the queue
4. exit
===========================
Enter your choice
2
Enter the element to be inserted500

73

/********************************************************************/
/*To Implement Ascending Priority Queue */
/* By: Rakesh Menon */
/* CSE Second Year 'D' Section */
/********************************************************************/
/******************** Pre-processoer directives *********************/
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
/********************************************************************/
/****************************************************************/
/* Structure declararion */
/****************************************************************/
struct priorityqueue
{
int info;
struct priorityqueue *next;
};
/********************************************************************/
typedef struct priorityqueue pq;
/************ Global declaration of Pointers ***********************/
pq *front,*rear;
/********************************************************************/
/* Function Definitions */
/********************************************************************/
void *getnode()
{
pq *ptr;
ptr=(pq*)malloc(sizeof(pq));
return ptr;
}
/********************************************************************/
void init(pq **front,pq **rear)
{
*front=(pq*)NULL;
*rear=(pq*)NULL;
}
/********************************************************************/
void insert(int item)
{
pq *ptr;
ptr=getnode();
ptr->info=item;
if(front==(pq*)NULL && rear==(pq*)NULL)
{
ptr->next=(pq*)NULL;
front=rear=ptr;
}
else
{
74

l++. } if(pos==0) 75 . ptr=ptr->next. ptr=front.pos=0. int min=front->info.x. } } /********************************************************************/ int delbeg(pq **front) { int p. while(l<pos) { ptr=ptr->next. int x. (*p)->next=q->next. rear=ptr. free(q). } ptr=front. while(ptr!=(pq*)NULL) { if(ptr->info<min) { min=ptr->info. p=(*front)->info. x=q->info. if((*front)==rear) (*front)=rear=(pq*)NULL. rear->next=ptr.ptr->next=(pq*)NULL.l=1. return x. } /********************************************************************/ int delprio() { pq *ptr. } c++. q=(*p)->next. int c=0. return p. else (*front)=(*front)->next. } /********************************************************************/ int delafter(pq **p) { pq *q. pos=c.

if(front==(pq*)NULL) rear=(pq*)NULL. scanf("%d". if(front==(pq*)NULL && rear==(pq*)NULL) printf("\nQueue is empty"). printf("Priority Queue\n"). else { x=delprio().\n\n"). scanf("%d". p=p->next.&ch).Insert element. insert(item).EXIT. printf("1.&item). printf("2. printf("3. printf("--------------\n"). case 2:if(front==(pq*)NULL && rear==(pq*)NULL) printf("\nNo elements left to delete\n"). printq().\n\n").x).x.item. do { clrscr(). while(p!=(pq*)NULL) { printf("%d ". } } /********************************************************************/ void main() { int ch=1. printf("Enter choice:").{ x=delbeg(&front).Delete element ASCENDING. printf("%d\n". 76 . switch(ch) { case 1:printf("Enter element for insertion:"). return x.\n\n\n").&rear). init(&front. p=front. } else x=delafter(&ptr). break.p->info). } /********************************************************************/ void printq() { pq *p. getch().

} printq(). break. case 3:exit(0). default:break. } /********************************************************************/ 77 . } }while(ch!=3). getch().

9-> DELETING AFTER A GIVEN ELEMENT OF THE LIST. 1->LINEAR LINKED LIST:. ALSO CALLED SINGLY LINKED LIST OR ONE WAY LIST. 1->TO CREATE AN EMPTY LIST. 3-> CIRCULAR LINKED LIST. STRUCT NODE_TYPE *NEXT. 4-> HEADER LINKED LIST. 4->INSERTING AN ELEMENT AT THE BEGINNING OF THE LIST.IN A DOUBLY LINKED LIST. CALLED THE NEXTPOINTER FIELD. } NODE *START. CALLED THE LINK FIELD OR NEXTPOINTER FIELD. CONTAINS THE ADDRESS OF THE NEXT NODE IN THE LIST. EACH NODE IS DIVIDED INTO TWO PARTS:- • • • FIRST PART CALLED PREVIOUSPOINTER FIELD. 2-> DOUBLY LINKED LIST OR TWO WAY LIST. EACH NODE IS DIVIDED INTO TWO OR MORE PARTS. CONTAINS THE ADDRESS OF THE PRECEDING ELEMENT IN THE LIST. 6-> INSERTING AN ELEMENT AFTER A GIVEN ELEMENT. CONTAINS THE ADDRESS OF THE SUCCEEDING ELEMENT IN THE LIST. 8-> DELETING FROM THE END OF THE LIST. THE LINEAR ORDER IS GIVEN BY POINTERS. 2->DOUBLY LINKED LIST:. 78 . THE THIRD PART.PROGRAMS BASED ON LINKED LIST. • SECOND PART. –6 OBJECT :. THE FOLLOWING OPERATIONS ARE PERFORMED ON LINEAR LINKED LIST. THEORY AND CONCEPTS:A LINKED LIST IS A LINEAR COLLECTION OF DATA ELEMENTS. WE CAN REPRESENT THE LINEAR LINKED LIST AS FOLLOWS:TYPEDEF STRUCT NODE _TYPE { INT INFO. A LINKED LIST CAN BE OF THE FOLLOWING TYPES:1-> LINEAR LINKED LIST OR ONE WAY LIST. THE SECOND PART CONTAINS THE INFORMATION OF THE ELEMENT.IN A LINEAR LINKED LIST.ASSIGNMENT NO. 7-> DELETING FROM THE BEGINNING OF THE LIST. 10-> REVERSING A LIST. 2->TRAVERSING A LIST 3->SEARCHING AN ELEMENT IN THE LIST. EACH NODE IS DIVIDED INTO TWO PARTS:• FIRST PART CONTAINS THE INFORMATION OF THE ELEMENT. CALLED NODES. ALSO CALLED SINGLY LINKED LIST OR ONE WAY LIST. 5-> INSERTING AN ELEMENT AT THE END OF THE LIST.

7-> DELETING FROM THE BEGINNING OF THE LIST. 5->TO REPRESENT SPARSE MATRICES. 2->TRAVERSING A LIST 3->SEARCHING AN ELEMENT IN THE LIST. QUEUES. }node. node *head. THE FOLLOWING OPERATIONS ARE PERFORMED ON LINEAR LINKED LIST. 6-> INSERTING AN ELEMENT AFTER A GIVEN ELEMENT. 4->INSERTING AN ELEMENT AT THE BEGINNING OF THE LIST. 1->TO CREATE AN EMPTY LIST. PROBLEMS (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) (15) WRITE A MENU DRIVEN PROGRAM TO IMPLEMENT VARIOUS OPERATIONS ON A LINEAR LINKED LIST. 9-> DELETING AFTER A GIVEN ELEMENT OF THE LIST. 4->TO MANIPULATE POLYNOMIALS. 79 . PROGRAM FOR CREATING LINKED LIST BY UNION OF ELEMENTS OF TWO LINKED LISTS. 3->TO PERFORM ARITHMETIC OPERATIONS ON LONG INTEGERS. WRITE A FUNCTION TO CONCATENATE TWO CIRCULAR LINKED LIST. TREES AND GRAPHS. 10-> REVERSING A LIST. 2->TO MAINTAIN A DIRECTORY OF NAMES. WRITE A MENU DRIVEN PROGRAM TO IMPLEMENT VARIOUS OPERATION ON A DOUBLY LINKEDLIST WRITE A PROGRAM TO ADD TWO VERY LONG INTEGERS USING LINKED LIST (THE NUMBERS CAN NOT BE STORED IN INTEGERS & LONG INTEGERS ) (16) WRITE A PROGRAM FOR POLYNOMIAL ADDITION. Struct node_type *next.*tail. int info.WE CAN REPRESENT THE DOUBLY LINKED LIST AS FOLLOWS:typedef struct node_type { Struct node_type *prev. WRITE A PROGRAM FOR INSERTION IN THE ORDERED LINKED LIST WRITE A PROGRAM FOR MERGING TWO ORDERED LINKED LISTS WRITE A PROGRAM FOR DELETION OF ALL OCCURANCES OF X FROM LINKED LIST WRITE A PROGRAM FOR CONCATENATION OF TWO LINKED LISTS WRITE A PROGRAM FOR SPLITTING THE GIVEN LINKED LIST FROM MID WRITE A PROGRAM TO REVERSE THE GIVEN LINKED LIST WRITE A MENU DRIVEN PROGRAM TO IMPLEMENT VARIOUS OPERATIONS ON A CIRCULAR LINKED LIST PROGRAM FOR CREATING THE ANOTHER COPY OF THE LINKED LIST. PROGRAM FOR IMPLEMENTATION OF JOSEPHUS PROBLEM PROGRAM FOR CREATING LINKED LIST BY INTERSECTION OF ELEMENTS OF TWO LINKED LISTS. THE FOLLOWING ARE THE APPLICATIONS OF LINKED LISTS:1->TO IMPLEMENT OTHER DATA STRUCTURES SUCH AS STACKS. 5-> INSERTING AN ELEMENT AT THE END OF THE LIST. 8-> DELETING FROM THE END OF THE LIST.

int next.int). printf("===========================\n"). Insert in the beginning\n"). int delafter(int). printf("8. availablelist(). void insbeg(int). int getnode(void). int x. int delbeg(). printf("5. printf("6. void insend(int). void traverse(). Traverse\n"). 80 . Insert in the end\n"). /****************************************************************/ void main() { int p. printf("3. exit\n"). /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void availablelist(void)./****************************************************************/ /* PROG FOR PRIMTIVE OPERATIONS ON LINEAR LINKED LIST */ /* BY: AKHILESH KUMAR SRIVASTAVA */ /* DES: THE IMPLEMENTATION OF LINKED LIST IS STATIC USING ARRAY */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. Delete from the end\n"). int list=-1. int avail. printf("2.c. Delete after a node\n").y. void freenode(int).h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct nodetype { int info. Delete from the beginning\n"). begin: printf("1. printf("7. Insert after a node\n"). int delend(). void insafter(int. printf("4. }. struct nodetype node[100]. char ch.

scanf("%d". case 7: traverse(). break.delend()).delbeg()). insbeg(x). break. fflush(stdin).&c). scanf("%d". for(p=list. scanf("%c".printf("Enter your choice\t"). break. printf("The deleted item is:=> %d".info!=y&&p!=-1. insafter(p.node[p]. insend(x).delafter(p)). case 3: printf("\nEnter the element to be inserted").&x). } printf("Do you wish to continue? (y/n)"). default: printf("\nWrong choice"). scanf("%d". } 81 . break.&y). break.p=node[p].&ch). if(ch=='y'||ch=='Y') goto begin. case 2: printf("\nEnter the element to be inserted after which node"). case 6: printf("The deleted item is:=> %d".next) . printf("\nEnter the element to be inserted").&x).p=node[p].info!=y&&p!=-1.x). case 8: exit(1). break.node[p].next) . case 5: for(p=list. case 4: printf("The deleted item is:=> %d". break. scanf("%d".&x). switch(c) { case 1: printf("\nEnter the element to be inserted"). scanf("%d". break.

return p. avail=p. p=avail.i<99.i++) node[i].next=-1. } /****************************************************************/ /***************** Traversing the Linked list ******************/ /****************************************************************/ void traverse() { int p=list. printf("\nThe Linked List is\n"). avail=node[avail]. for(i=0./****************************************************************/ /* Function Definitions */ /****************************************************************/ void availablelist(void) { int i.next=i+1.next. while(p!=-1) { printf("\t%d". } } 82 .node[p]. node[i].next=avail. } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(int p) { node[p]. p=node[p].next. } /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ int getnode(void) { int p.info). avail=0.

node[p].next=list. int x.next=q.next. } /****************************************************************/ 83 . node[q]. p=list. freenode(p).info=x. p=getnode().next=-1.next. } /****************************************************************/ /**** Insertion of a node in the beginning of the liked list ****/ /****************************************************************/ void insbeg(int x) { int p. return x. node[p]./****************************************************************/ /*Insertion of a node after a node pointed by pointed by pointer p*/ /****************************************************************/ void insafter(int p.info=x. list=p.next=q. list=node[p].int x) { int q.next. node[q]. while(node[p]. q=getnode(). node[q].info.next=node[p]. node[p].q. } /****************************************************************/ /**** deletion of a node from the beginning of the liked list ***/ /****************************************************************/ int delbeg() { int p.info=x. x=node[p].next!=-1) p=node[p]. node[p]. node[q]. q=getnode(). } /****************************************************************/ /******* Insertion of a node in the end of the liked list *******/ /****************************************************************/ void insend(int x) { int p=list.

info.next. } /****************************************************************/ /******* Deletion of a node from the end of the liked list ******/ /****************************************************************/ int delend() { int p=list. } node[q]. x=node[p].next=node[p]. p=node[p].next!=-1) { q=p.q=-1. int x.next=-1.next=-1. freenode(p). while(node[p]. int x. return x. q=node[p]. x=node[p]. node[q].next./*deletion of a node after a node pointed by pointed by pointer p*/ /****************************************************************/ int delafter(int p) { int q. } /****************************************************************/ 84 .info.next. return x. node[q]. freenode(p).

exit =========================== Enter your choice 2 85 . exit =========================== Enter your choice 7 The Linked List is 100 200Do you wish to continue? (y/n)y 1. exit =========================== Enter your choice 3 Enter the element to be inserted200 Do you wish to continue? (y/n)y 1. Insert in the end 4. Insert in the beginning 2. Delete from the beginning 5. Delete from the beginning 5. Insert in the end 4. Insert in the beginning 2. exit =========================== Enter your choice 1 Enter the element to be inserted100 Do you wish to continue? (y/n)y 1. Delete from the end 7. Insert in the beginning 2. Delete after a node 6. Delete from the end 7. Insert after a node 3. Traverse 8. Delete after a node 6. Insert in the end 4. Delete after a node 6. Delete from the end 7. Delete after a node 6. Insert in the end 4. Traverse 8. Delete from the end 7. Insert after a node 3. Traverse 8. Insert after a node 3. Insert in the beginning 2. Insert after a node 3. Delete from the beginning 5. Traverse 8. Delete from the beginning 5.Output: 1.

Enter the element to be inserted after which node100 Enter the element to be inserted300 Do you wish to continue? (y/n)y 1. Traverse 8. Delete from the beginning 5. Insert in the end 4. Traverse 8. Delete from the end 7. Insert in the end 4. Insert after a node 3. Insert in the end 4. Insert in the beginning 2. exit =========================== Enter your choice 6 The deleted item is:=> 200Do you wish to continue? (y/n) 86 . Insert in the beginning 2. Insert after a node 3. Delete from the beginning 5. exit =========================== Enter your choice 4 The deleted item is:=> 100Do you wish to continue? (y/n)y 1. Insert in the beginning 2. exit =========================== Enter your choice 7 The Linked List is 100 300 200Do you wish to continue? (y/n)y 1. Delete after a node 6. Delete from the end 7. Delete after a node 6. Delete after a node 6. Traverse 8. Delete from the end 7. Delete from the beginning 5. Insert after a node 3.

87 . Insert in the end\n"). int delbeg(). }. Delete from the beginning\n"). struct node *next. void freenode(NODEPTR). void traverse(). Insert after a node\n"). Delete after a node\n"). printf("8. Traverse\n"). printf("4. Insert in the beginning\n").y. int delend().int).h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { int info. typedef struct node* NODEPTR. printf("3. exit\n"). int x. int delafter(int). void insend(int). printf("7. /****************************************************************/ /**************** Global initialisation of Linked List **********/ NODEPTR list=NULL. printf("6./****************************************************************/ /* PROG FOR PRIMTIVE OPERATIONS ON LINEAR LINKED LIST */ /* BY: AKHILESH KUMAR SRIVASTAVA */ /* DES: THE IMPLEMENTATION OF LINKED LIST IS DYNAMICALLY */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. void insafter(int. char ch. printf("2.c. begin: printf("1. Delete from the end\n"). void insbeg(int). printf("5. /****************************************************************/ void main() { NODEPTR p. /****************************************************************/ /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void).

return p. break. break.&ch). default: printf("\nWrong choice"). break. case 4: printf("The deleted item is:=> %d". break. printf("Enter your choice\t"). case 5: printf("\nEnter the element to be inserted after which node").&y).&x). } printf("Do you wish to continue? (y/n)"). break. break. break.&x).delafter(y)). case 3: printf("\nEnter the element to be inserted"). case 2: printf("\nEnter the element to be inserted after which node"). 88 .delbeg()). scanf("%d". case 6: printf("The deleted item is:=> %d". scanf("%d". } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p. break. scanf("%c". printf("The deleted item is:=> %d". scanf("%d".x). if(ch=='y'||ch=='Y') goto begin. fflush(stdin). insbeg(x). case 7: traverse(). insafter(y. scanf("%d".&x). scanf("%d".&c). printf("\nEnter the element to be inserted"). scanf("%d".delend()).&y). switch(c) { case 1: printf("\nEnter the element to be inserted"). case 8: exit(1). p=(NODEPTR)malloc(sizeof(struct node)).printf("===========================\n"). insend(x).

} /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p).p=p->next) . } /****************************************************************/ /******* Insertion of a node in the end of the liked list *******/ /****************************************************************/ void insend(int x) { 89 .int x) { NODEPTR p. q->info=x. list=p. printf("\nThe Linked List is\n"). } /****************************************************************/ /***************** Traversing the Linked list ******************/ /****************************************************************/ void traverse() { NODEPTR p=list. p->next=list. p->info=x. } /****************************************************************/ /**** Insertion of a node in the beginning of the liked list ****/ /****************************************************************/ void insbeg(int x) { NODEPTR p.p->info!=y&&p!=NULL. q->next=p->next. while(p!=NULL) { printf("\t%d". } } /****************************************************************/ /*Insertion of a node after a node pointed by pointed by pointer p*/ /****************************************************************/ void insafter(int y. p=p->next.q. p->next=q.p->info). for(p=list. q=getnode(). p=getnode().

NODEPTR p=list,q;
q=getnode();
q->info=x;
q->next=NULL;
while(p->next!=NULL)
p=p->next;
p->next=q;
}
/****************************************************************/
/**** deletion of a node from the beginning of the liked list ***/
/****************************************************************/
int delbeg()
{
NODEPTR p;
int x;
p=list;
list=p->next;
x=p->info;
freenode(p);
return x;
}
/****************************************************************/
/*deletion of a node after a node pointed by pointed by pointer p*/
/****************************************************************/
int delafter(int y)
{
NODEPTR p=list,q;
int x;
for(p=list;p->info!=y&&p!=NULL;p=p->next)
;
q=p->next;
q->next=p->next;
x=p->info;
freenode(p);
return x;
}
/****************************************************************/
/******* Deletion of a node from the end of the liked list ******/
/****************************************************************/
int delend()
{
NODEPTR p=list,q=NULL;
int x;
while(p->next!=NULL)
{
q=p;
p=p->next;
}
q->next=NULL;
90

x=p->info;
freenode(p);
q->next=NULL;
return x;
}
/****************************************************************/

Output:
1. Insert in the beginning
2. Insert after a node
3. Insert in the end
4. Delete from the beginning
5. Delete after a node
6. Delete from the end
7. Traverse
8. exit
===========================
Enter your choice
1
Enter the element to be inserted100
Do you wish to continue? (y/n)y
1. Insert in the beginning
2. Insert after a node
3. Insert in the end
4. Delete from the beginning
5. Delete after a node
6. Delete from the end
7. Traverse
8. exit
===========================
Enter your choice
3
Enter the element to be inserted200
Do you wish to continue? (y/n)y
1. Insert in the beginning
2. Insert after a node
3. Insert in the end
4. Delete from the beginning
5. Delete after a node
6. Delete from the end
7. Traverse
8. exit
===========================
Enter your choice
7
The Linked List is
100 200Do you wish to continue? (y/n)y
1. Insert in the beginning
2. Insert after a node
3. Insert in the end
4. Delete from the beginning
91

5. Delete after a node
6. Delete from the end
7. Traverse
8. exit
===========================
Enter your choice
2
Enter the element to be inserted after which node100
Enter the element to be inserted300
Do you wish to continue? (y/n)y
1. Insert in the beginning
2. Insert after a node
3. Insert in the end
4. Delete from the beginning
5. Delete after a node
6. Delete from the end
7. Traverse
8. exit
===========================
Enter your choice
7
The Linked List is
100 300 200Do you wish to continue? (y/n)y
1. Insert in the beginning
2. Insert after a node
3. Insert in the end
4. Delete from the beginning
5. Delete after a node
6. Delete from the end
7. Traverse
8. exit
===========================
Enter your choice
4
The deleted item is:=> 100Do you wish to continue? (y/n)y
1. Insert in the beginning
2. Insert after a node
3. Insert in the end
4. Delete from the beginning
5. Delete after a node
6. Delete from the end
7. Traverse
8. exit
===========================
Enter your choice
6
The deleted item is:=> 200Do you wish to continue? (y/n)

92

/****************************************************************/ void main() { NODEPTR p. insbeg(30). int delafter(int). void freenode(NODEPTR).&x). void insend(int). insbeg(100). void traverse(). insbeg(50). insbeg(70). typedef struct node* NODEPTR.int).q=NULL. int delend().h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { int info. scanf("%d". /****************************************************************/ /**************** Global initialisation of Linked List **********/ NODEPTR list=NULL./****************************************************************/ /* Prog for insertion in an ordered linked lists */ /* By: Akhilesh Kumar Srivastava */ /* Des: The implementation of linked list is dynamically */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. void insafter(int. clrscr(). insbeg(90). }. traverse(). printf("\nEnter your Item to be inserted"). struct node *next. void insbeg(int). /****************************************************************/ /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void). int x. while(x>=p->info&&p!=NULL) { 93 . int delbeg(). p=list.

} /****************************************************************/ /***************** Traversing the Linked list ******************/ /****************************************************************/ void traverse() { NODEPTR p=list. printf("\nThe Linked List is\n").p->info).x). } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p). getch(). else insafter(q->info. } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p. } if(q==NULL) insbeg(x). p=p->next. while(p!=NULL) { printf("\t%d".q=p. } } 94 . p=p->next. p=(NODEPTR)malloc(sizeof(struct node)). return p. traverse().

} /****************************************************************/ /**** Insertion of a node in the beginning of the liked list ****/ /****************************************************************/ void insbeg(int x) { NODEPTR p. p->next=q. list=p.q. p->info=x./****************************************************************/ /*Insertion of a node after a node pointed by pointed by pointer p*/ /****************************************************************/ void insafter(int y. p=getnode(). q=getnode(). } /****************************************************************/ Output The Linked List is 30 50 70 90 100 Enter your Item to be inserted20 The Linked List is 20 30 50 70 90 100 The Linked List is 30 50 70 90 100 Enter your Item to be inserted60 The Linked List is 30 50 60 70 90 100 The Linked List is 30 50 70 90 100 Enter your Item to be inserted110 The Linked List is 30 50 70 90 100 110 95 . p->next=list.p->info!=y&&p!=NULL.int x) { NODEPTR p.p=p->next) . for(p=list. q->info=x. q->next=p->next.

}. void traverse()./****************************************************************/ /* Prog for merging two ordered linked lists */ /* By: Akhilesh Kumar Srivastava */ /* Des: The implementation of linked list is dynamically */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. 96 .x). for(i=0. printf("\nEnter the no of elements in first ordered list:=>\t"). NODEPTR p.int). printf("Enter the elements").&n1).&x).&x). printf("Enter the elements").i++) { scanf("%d".h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { int info. typedef struct node* NODEPTR.list3=NULL.i++) { scanf("%d". traverse(list1).n2.&n2).i<n2. } printf("\nThe First List is:=>\t"). insend(&list1. void insend(NODEPTR*. } printf("\nThe Second List is:=>\t"). printf("\nEnter the no of elements in second ordered list:=>\t"). insend(&list2. scanf("%d".list2=NULL.q. /****************************************************************/ void main() { NODEPTR list1=NULL. struct node *next.x). clrscr(). for(i=0. int n1. /****************************************************************/ /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void). void freenode(NODEPTR). scanf("%d".i<n1.x.i.

} else { insend(&list3. q=q->next.q->info). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p. p=(NODEPTR)malloc(sizeof(struct node)).traverse(list2). } } while(p!=NULL) { insend(&list3.p->info). q=list2. p=p->next. p=list1. } printf("\nThe ordered list is:=>\t"). } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p). q=q->next. p=p->next.q->info). return p. while(p!=NULL&&q!=NULL) { if(p->info<q->info) { insend(&list3. getch(). } /****************************************************************/ /***************** Traversing the Linked list ******************/ 97 . traverse(list3).p->info). } while(q!=NULL) { insend(&list3.

else { while(p->next!=NULL) p=p->next. p->next=q. while(p!=NULL) { printf("\t%d". } } /****************************************************************/ OUTPUT ENTER THE NO OF ELEMENTS IN FIRST ORDERED LIST:=> ENTER THE ELEMENTS100 200 300 400 500 THE FIRST LIST IS:=> 100 200 300 400 ENTER THE NO OF ELEMENTS IN SECOND ORDERED LIST:=> ENTER THE ELEMENTS50 70 150 250 THE SECOND LIST IS:=> 50 70 150 250 THE ORDERED LIST IS:=> 50 70 100 150 5 500 4 200 250 300 400 500 98 . if(*list==NULL) *list=q. } } /****************************************************************/ /******* Insertion of a node in the end of the liked list *******/ /****************************************************************/ void insend(NODEPTR *list. q->next=NULL.int x) { NODEPTR p=*list. q=getnode(). p=p->next. q->info=x./****************************************************************/ void traverse(NODEPTR list) { NODEPTR p=list.q.p->info).

Traverse\n"). }. int delend(NODEPTR*). char ch. printf("8. void freenode(NODEPTR). /****************************************************************/ void main() { NODEPTR list=NULL. exit\n"). Insert in the beginning\n")./****************************************************************/ /* PROG FOR PRIMTIVE OPERATIONS ON CIRCULAR LINKED LIST */ /* BY: AKHILESH KUMAR SRIVASTAVA */ /* DES: THE IMPLEMENTATION OF LINKED LIST IS DYNAMICALLY */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. Insert after a node\n").&c). printf("Enter your choice\t"). struct node *next. void insafter(NODEPTR*. printf("2.int). printf("5. switch(c) { 99 . printf("4. int delafter(NODEPTR*). begin: printf("1. Insert in the end\n"). NODEPTR p. Delete from the beginning\n").c. /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void). int x. typedef struct node* NODEPTR. void insbeg(NODEPTR*. void traverse(NODEPTR).y. printf("6. printf("3.int).int). printf("===========================\n"). void insend(NODEPTR*. printf("7.h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { int info. scanf("%d". Delete from the end\n"). Delete after a node\n"). int delbeg(NODEPTR*).

} printf("Do you wish to continue? (y/n)"). scanf("%d".x). for(p=list. default: printf("\nWrong choice"). insbeg(&list.x). fflush(stdin). if(ch=='y'||ch=='Y') goto begin.p=p->next) . case 2: printf("\nEnter the element to be inserted after which node"). scanf("%d". break.&x).&ch). case 8: exit(1). break. break. case 3: printf("\nEnter the element to be inserted"). break.&x).delafter(&list)). break. insend(&list. printf("\nEnter the element to be inserted"). insafter(&p. break. scanf("%c". printf("The deleted item is:=> %d". scanf("%d". break. p=(NODEPTR)malloc(sizeof(struct node)). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p. } /****************************************************************/ /***************** Traversing the Linked list ******************/ /****************************************************************/ 100 .&y). case 7: traverse(list). scanf("%d". break. return p.&x).case 1: printf("\nEnter the element to be inserted").p->info!=y&&p!=NULL. } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p).p->info!=y&&p!=NULL. case 4: printf("The deleted item is:=> %d". case 5: for(p=list.delbeg(&list)).p=p->next) . case 6: printf("The deleted item is:=> %d".x).delend(&list)).

int x) { NODEPTR q.int x) { NODEPTR q. } /****************************************************************/ /**** deletion of a node from the beginning of the liked list ***/ /****************************************************************/ int delbeg(NODEPTR *list) { NODEPTR p. q=getnode(). int x.p->info). } } /****************************************************************/ /*Insertion of a node after a node pointed by pointed by pointer p*/ /****************************************************************/ void insafter(NODEPTR *p.int x) { NODEPTR p.void traverse(NODEPTR p) { printf("\nThe Linked List is\n").q. 101 . p=getnode(). p->info=x. p->next=(*list)->next. q->next=(*p)->next. (*list)->next=q. q->next=(*list)->next. (*list)->next=p. q->info=x. } /****************************************************************/ /******* Insertion of a node in the end of the liked list *******/ /****************************************************************/ void insend(NODEPTR *list. } /****************************************************************/ /**** Insertion of a node in the beginning of the liked list ****/ /****************************************************************/ void insbeg(NODEPTR *list. (*p)->next=q. (*list)=q. q=getnode(). p=p->next. q->info=x. while(p!=NULL) { printf("\t%d".

freenode(p). return x. x=(*list)->info. return x. freenode(*p). } /****************************************************************/ /******* Deletion of a node from the end of the liked list ******/ /****************************************************************/ int delend(NODEPTR *list) { NODEPTR p=*list. x=(*p)->info. (*list)->next=p->next. return x. q->next=(*p)->next. int x. freenode(r). int x.r. } /****************************************************************/ 102 . while(p->next!=*list) { p=p->next. *list=p. x=p->info.p=(*list)->next.q. } /****************************************************************/ /*deletion of a node after a node pointed by pointed by pointer p*/ /****************************************************************/ int delafter(NODEPTR *p) { NODEPTR q. q=(*p)->next. } p->next=(*list)->next. r=(*list).

clrscr().h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { int info. 103 .i=i-4) { n=0.:=>\n"). /****************************************************************/ void main() { NODEPTR list1=NULL. void addlist(NODEPTR. }. typedef struct node* NODEPTR./****************************************************************/ /*** Prog for addition of very long integers using Linked List***/ /*** By: Akhilesh Kumar Srivastava ***/ /*** Des: The implementation of the linked list is dynamically **/ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. struct node *next. } else { for(j=0.j<=i. scanf("%s".x). for(i=i1-1.i1. printf("Enter the second no. void freenode(NODEPTR). char x[20]. /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void).list2=NULL.j.j++) n=n*10+(x[j]-'0').i.y[20]. i2=strlen(y). printf("Enter the first no.y).i>=0. if(i>=3) { for(j=i-3. scanf("%s". void insend(NODEPTR*.NODEPTR).j<=i.:=>\n"). i1=strlen(x). int delend(NODEPTR*). void traverse(NODEPTR). int n.j++) n=n*10+(x[j]-'0').i2.int).

getch(). } traverse(list2). } /****************************************************************/ /******* Insertion of a node in the end of the liked list *******/ /****************************************************************/ void insend(NODEPTR*list. p=(NODEPTR)malloc(sizeof(struct node)).j++) n=n*10+(y[j]-'0'). } else { for(j=0.j<=i. } traverse(list1).j<=i.n). return p.i=i-4) { n=0.int x) { NODEPTR p=*list.n).j++) n=n*10+(y[j]-'0'). q=getnode().i>=0.list2). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p. if(i>=3) { for(j=i-3.} insend(&list1. for(i=i2-1. 104 . } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p). } insend(&list2. addlist(list1.q.

sum.sum).NODEPTR list2) { int total. } if(q!=NULL) { q->next=NULL. } /****************************************************************/ /********** Adding the contents of the two Linked Lists *********/ /****************************************************************/ void addlist(NODEPTR list1. insend(&list. freenode(p).q->info=x. else { while(p->next!=NULL) p=p->next. while(p->next!=NULL) { q=p. list2=list2->next. } } /****************************************************************/ /******* Deletion of a node from the end of the liked list ******/ /****************************************************************/ int delend(NODEPTR *list) { NODEPTR p=*list. return x. } x=p->info. p->next=q. q->next=NULL. p=p->next. } else { *list=NULL. while(list1!=NULL&&list2!=NULL) { total=list1->info+list2->info+carry. carry=total/10000.carry=0.q=NULL. if(p==NULL) *list=q. 105 . list1=list1->next. NODEPTR list=NULL. sum=total%10000. int x.

insend(&list. sum=total%10000.p->info). list1=list1->next.} while(list1!=NULL) { total=list1->info+carry. } } /****************************************************************/ /************* Travesring the Linked List ***********************/ /****************************************************************/ void traverse(NODEPTR p) { printf("\nThe Linked List is\n").carry).sum). printf("\n\nThe sum is:=>\t").sum). while(p!=NULL) { printf("\t%d".delend(&list)). } } /****************************************************************/ 106 . while(list!=NULL) { printf("%d". } while(list2!=NULL) { total=list2->info+carry. carry=total/10000. insend(&list. sum=total%10000. list2=list2->next. p=p->next. carry=total/10000. } if(carry==1) insend(&list.

:=> 21231584986513 The Linked List is 1215 6532 4564 2356 1 The Linked List is 6513 8498 2315 21 The sum is:=> 12377688050307728 107 .Output Enter the first no.:=> 123456789123456789 Enter the second no.:=> 17896582365475 The Linked List is 6789 2345 7891 3456 12 The Linked List is 5475 8236 8965 17 The sum is:=> 12347468575822264 Enter the first no.:=> 12356456465321215 Enter the second no.

e.NODEPTR).of terms in first Polynomial:=> \t"). /****************************************************************/ void main() { NODEPTR poly1=NULL.c. void insend(NODEPTR*. struct node *next.&y)./*******************************************************************/ /* Prog for addition of addition of 2 polynomials using Linked List*/ /*** By: Akhilesh Kumar Srivastava ***/ /*** Des: The implementation of the linked list is dynamically *****/ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio.c. void traverse(NODEPTR).&e).i<x.int.of terms in second Polynomial:=> \t"). int x. clrscr().&x).i+1). scanf("%d". insend(&poly1.e). for(i=0.&c. printf("Enter the no.&e).e). scanf("%d%d".i+1). }. int exp.i<y. printf("Enter the no.i.int). scanf("%d%d".h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { int coef. scanf("%d". printf("\nEnter the Second Polynaomial:=>\n").y. for(i=0. void addpoly(NODEPTR.&c. } traverse(poly1).i++) { printf("Enter the coef & exp of the %dth term". insend(&poly2. 108 . typedef struct node* NODEPTR. /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void).i++) { printf("Enter the coef & exp of the %dth term".poly2=NULL. printf("\n\nEnter the First Polynaomial:=>\n").c.

q->next=NULL. 109 .NODEPTR list2) { NODEPTR list=NULL. getch().list1->coef+list2->coef. return p. } /****************************************************************/ /******* Insertion of a node in the end of the liked list *******/ /****************************************************************/ void insend(NODEPTR*list. if(p==NULL) *list=q.int y) { NODEPTR p=*list.} traverse(poly2). p=(NODEPTR)malloc(sizeof(struct node)). q->exp=y.list1->exp).int x. p->next=q. } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p. q->coef=x. list2=list2->next. } } /****************************************************************/ /********** Adding the contents of the two Polynomials **********/ /****************************************************************/ void addpoly(NODEPTR list1. addpoly(poly1.q. list1=list1->next. q=getnode().poly2). while(list1!=NULL&&list2!=NULL) { if(list1->exp==list2->exp) { insend(&list. else { while(p->next!=NULL) p=p->next.

list2->exp).list2->exp). } /****************************************************************/ /************* Travesring the Linked List ***********************/ /****************************************************************/ void traverse(NODEPTR p) { printf("\nThe Linked List is\n").p->coef).list2->coef. } while(list2!=NULL) { insend(&list.} else { if(list1->exp>list2->exp) { insend(&list.list1->exp). } } /****************************************************************/ 110 . } } } while(list1!=NULL) { insend(&list.list1->coef.list2->coef. } printf("\tThe resultant polynomial is:=>\t"). while(p!=NULL) { printf("%d". printf("x%d + ". list2=list2->next. p=p->next. list1=list1->next.list1->coef. } if(list1->exp<list2->exp) { insend(&list. list2=list2->next.p->exp). traverse(list).list1->exp). list1=list1->next.

of terms in first Polynomial:=> 5 Enter the no.Output Enter the no.of terms in first Polynomial:=> 4 Enter the no.of terms in second Polynomial:=> 3 Enter the First Polynaomial:=> Enter the coef & exp of the 1th term6 9 Enter the coef & exp of the 2th term5 8 Enter the coef & exp of the 3th term-7 3 Enter the coef & exp of the 4th term5 2 Enter the coef & exp of the 5th term6 0 The Linked List is 6x9 + 5x8 + -7x3 + 5x2 + 6x0 + Enter the Second Polynaomial:=> Enter the coef & exp of the 1th term8 8 Enter the coef & exp of the 2th term4 3 Enter the coef & exp of the 3th term-2 0 The Linked List is 8x8 + 4x3 + -2x0 + The resultant polynomial is:=> The Linked List is 6x9 + 13x8 + -3x3 + 5x2 + 4x0 + 111 .of terms in second Polynomial:=> 4 Enter the First Polynaomial:=> Enter the coef & exp of the 1th term9 7 Enter the coef & exp of the 2th term5 6 Enter the coef & exp of the 3th term-4 2 Enter the coef & exp of the 4th term8 0 The Linked List is 9x7 + 5x6 + -4x2 + 8x0 + Enter the Second Polynaomial:=> Enter the coef & exp of the 1th term-8 5 Enter the coef & exp of the 2th term6 6 Enter the coef & exp of the 3th term2 2 Enter the coef & exp of the 4th term-5 0 The Linked List is -8x5 + 6x6 + 2x2 + -5x0 + The resultant polynomial is:=> The Linked List is 9x7 + 5x6 + -8x5 + 6x6 + -2x2 + 3x0 + Enter the no.

list3=NULL.x). typedef struct node* NODEPTR. for(i=0. } printf("Enter the no of elements in first list:=> ").n1.x).i<n1. /****************************************************************/ void main() { NODEPTR list1=NULL. insend(&list1.n2. for(i=0. } if(list1==NULL&&list2==NULL) list3=NULL. int i. insend(&list2.&x). else 112 .int). struct node *next. scanf("%d".&n1). scanf("%d". printf("\nEnter the no of elements in first list:=> ").i++) { scanf("%d". void freenode(NODEPTR). printf("\nEnter the elements of first linked list:=> "). printf("\nEnter the elements of first linked list:=> "). NODEPTR p./****************************************************************/ /******* Prog for concatenation of two Linear Linked Lists ******/ /****************** By: Akhilesh Kumar Srivastava ***************/ /***** Des: The implementation of linked list is dynamically ****/ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio.&n2). /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void). void insend(NODEPTR*. }.i<n2.&x).i++) { scanf("%d".h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { int info. void traverse(NODEPTR).list2=NULL.x.

{ if(list1==NULL) list3=list2. p->next=list2. } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p). } /****************************************************************/ /***************** Traversing the Linked list ******************/ /****************************************************************/ void traverse(NODEPTR p) { printf(“\n”). else { if(list2==NULL) list3=list1. else { p=list1. getch(). } } } printf("\nThe Concatenated list is:=>\t"). return p. } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p. p=p->next. while(p->next!=NULL) p=p->next. p=(NODEPTR)malloc(sizeof(struct node)). while(p!=NULL) { printf("\t%d".p->info). 113 . traverse(list3). list3=list1.

int x) { NODEPTR p=*list.} } /****************************************************************/ /******* Insertion of a node in the end of the liked list *******/ /****************************************************************/ void insend(NODEPTR *list. q=getnode(). } } /****************************************************************/ Output: Enter the no of elements in first list:=> 5 Enter the elements of first linked list:=> 100 200 300 520 150 Enter the no of elements in first list:=> 6 Enter the elements of first linked list:=> 10 20 30 25 35 60 The Concatenated list is:=> 100 200 300 520 150 10 20 30 25 35 60 114 .q. q->info=x. q->next=NULL. if(*list==NULL) *list=q. else { while(p->next!=NULL) p=p->next. p->next=q.

i++.x). NODEPTR p. insend(&list. i=1.&x). struct node *next. } printf("\n%d". void insend(NODEPTR*.i++) { scanf("%d". int i. for(i=0.count=0. scanf("%d". /****************************************************************/ void main() { NODEPTR list=NULL. void freenode(NODEPTR). traverse(list1). printf("\nThe First list is:=>\t").i<n. }. void traverse(NODEPTR).list1=NULL. while(i<(count/2)) { p=p->next. while(p!=NULL) { count++. 115 . printf("\nEnter the no of elements in the list:=> ").list2=NULL.n.int).x. typedef struct node* NODEPTR. list1=list. p=list.&n). } p=list.count). traverse(list2). p=p->next. /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void).h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { int info. printf("\nThe First list is:=>\t")./****************************************************************/ /******* Prog for Splitting the given Linear Linked Lists *******/ /****************** By: Akhilesh Kumar Srivastava ***************/ /***** Des: The implementation of linked list is dynamically ****/ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. } list2=p->next. p->next=NULL. printf("\nEnter the elements of first linked list:=> ").

q. return p. q->info=x. if(*list==NULL) *list=q.p->info).getch(). } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p). } } /****************************************************************/ 116 . } /****************************************************************/ /***************** Traversing the Linked list ******************/ /****************************************************************/ void traverse(NODEPTR p) { while(p!=NULL) { printf("\t%d". p->next=q. else { while(p->next!=NULL) p=p->next. p=(NODEPTR)malloc(sizeof(struct node)). } } /****************************************************************/ /******* Insertion of a node in the end of the liked list *******/ /****************************************************************/ void insend(NODEPTR *list. q->next=NULL. q=getnode(). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p. p=p->next.int x) { NODEPTR p=*list.

Output:-> ---------Enter the no of elements in the list:=> 10 Enter the elements of first linked list:=> 100 200 300 400 500 600 700 800 900 999 10 The First list is:=> 100 200 300 400 500 The First list is:=> 600 700 800 900 999 --------------------------------------------------------------------Enter the no of elements in the list:=> 7 Enter the elements of first linked list:=> 100 200 300 400 500 600 700 7 The First list is:=> The First list is:=> 100 400 200 500 300 600 700 117 .

n. printf("\nEnter the elements of linked list:=> "). traverse(list2).x). getch(). while(list1!=NULL) { x=delbeg(&list1).i. void traverse(NODEPTR). printf("\nEnter the no of elements in the list:=> "). scanf("%d". } printf("\nThe reversed linked list is:=>\n"). insend(&list1. int delbeg(NODEPTR*).i++) { scanf("%d". int x.x). void insbeg(NODEPTR*. struct node *next.int). traverse(list1). for(i=0. } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p. return p. }.list2=NULL. void freenode(NODEPTR).int). typedef struct node* NODEPTR. p=(NODEPTR)malloc(sizeof(struct node)). insbeg(&list2. /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void). /****************************************************************/ void main() { NODEPTR list1=NULL.&n).i<n.&x). 118 .h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { int info. } printf("\nThe given linked list is")./****************************************************************/ /* Program to reverse the given Linear Linked List */ /* By: Akhilesh Kumar Srivastava */ /* Des: The implementation of linked list is dynamically */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. void insend(NODEPTR*.

p=getnode(). q=getnode().int x) { NODEPTR p.int x) { NODEPTR p=*list.} /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p). } 119 . } /****************************************************************/ /***************** Traversing the Linked list ******************/ /****************************************************************/ void traverse(NODEPTR p) { while(p!=NULL) { printf("\t%d". int x.q. return x. q->next=NULL. p=*list.p->info). freenode(p). *list=p. else { while(p->next!=NULL) p=p->next. q->info=x. p->next=q. x=p->info. } } /****************************************************************/ /**** Insertion of a node in the beginning of the liked list ****/ /****************************************************************/ void insbeg(NODEPTR *list. } } /****************************************************************/ /**** deletion of a node from the beginning of the liked list ***/ /****************************************************************/ int delbeg(NODEPTR *list) { NODEPTR p. p->info=x. p=p->next. if(*list==NULL) *list=q. p->next=*list. *list=p->next. } /****************************************************************/ /******* Insertion of a node in the end of the liked list *******/ /****************************************************************/ void insend(NODEPTR *list.

/****************************************************************/ Output:-> --------Enter the no of elements in the list:=> 5 Enter the elements of linked list:=> 100 200 300 400 500 The given linked list is The reversed linked list is:=> 100 500 200 400 300 300 400 200 500 100 120 .

(3) WRITE A PROGRAM TO INPUT HOW MANY NUMBERS AND THEN INPUT THE NUMBERS AND THEN SEARCH ANY NUMBER WITH THE HELP OF INDEX SEQUENTIAL SEARCH . IS WE DO USE THIS APPROACH IN OUR DAILY LIFE. METHOD. AND WE SEARCH IN THE OTHER HALF. THE ONLY WAY TO SEARCH FOR GIVEN ELEMENT ITEM IS TO COMPARE ITEM WITH EACH ELEMENT OF A ONE BY ONE. THE SEARCH IS SAID TO BE SUCCESSFUL IF THE GIVEN ELEMENT IS FOUND I. PROBLEMS :(1) WRITE A PROGRAM TO INPUT HOW MANY NUMBERS AND THEN INPUT THE NUMBERS AND THEN SEARCH ANY NUMBER WITH THE HELP OF SEQUENTIAL /LINEAR SEARCH . THEN FOR THE SUBSEQUENT SEARCH ONE HALF IS DISCARDED . FOR EXAMPLE. (2) WRITE A PROGRAM TO INPUT HOW MANY NUMBERS AND THEN INPUT THE NUMBERS AND THEN SEARCH ANY NUMBER WITH THE HELP OF BINARY SEARCH .ASSIGNMENT NO. OTHERWISE UNSUCCESSFUL . THIS CALLED LINEAR SEARCH OR SEQUENTIAL SEARCH. –7 OBJECT:- IMPLEMENT THE VARIOUS SEARCHING ALGORITHMS THEORY AND CONCEPTS:SEARCHING IS THE PROCESS OF FINDING THE LOCATION OF GIVEN ELEMENT IN THE LINEAR ARRAY. OBVIOUSLY. 121 .E. WE DON’T SEARCH PAGE BY PAGE. THIS PROCESS IS CONTINUED TILL OTHER WE HAVE LOCATED THE REQUIRED TERM OR THAT TERM IS MISSING FROM THE DICTIONARY . WE OPEN THE DICTIONARY IN THE MIDDLE (ROUGHLY) TO DETERMINE WHICH HALF CONTAINS THE TERM BEING SOUGHT. SUPPOSE WE WANT TO FIND THE MEANING OF THE TERM MODEM IN A COMPUTER DICTIONARY . CALLED BINARY SEARCH. WHICH WILL INDICATED BY THE FACT THAT AT THE END WE WILL BE LEFT WITH ONLY ONE PAGE. USED TO FIND THE LOCATION OF THE GIVEN ELEMENT . THE ELEMENT DOES EXISTS IN THE ARRAY. THERE ARE TWO APPROACHES TO SEARCH OPERATION:• LINEAR SEARCH • BINARY SEARCH LINEAR SEARCH:GIVEN NO INFORMATION ABOUT THE ARRAY A. THE BEST SEARCHING ALGORITHM. WHICH TRAVERSES A SEQUENTIALLY TO LOCATE ITEM IS BINARY SEARCH:SUPPOSE THE ELEMENTS OF THE ARRAY A ARE SORTED IN ASCENDING ORDER( IF THE ELEMENTS ARE NUMBERS ) OR DICTIONARY ORDER( IF THE ELEMENTS ARE STRINGS).

} return -1.7. /****************************************************************/ /* Function Prototyping */ /****************************************************************/ int seq_search(int. else printf("Element isnot present in the array").10.int.x).3.WORST CASE & O(1) IN BEST CASE */ /****************************************************************/ /************* Global declaration of Array *********************/ int a[10]={5.55./****************************************************************/ /* PROGRAM FOR SEQUENTIAL SEARCH */ /* BY: AKHILESH KUMAR SRIVASTAVA */ /* DES: THIS IS O(N) ALGO IN AVG. if(y>=0) printf("Element %d is present at %d location in the array". for(i=low. scanf("%d".i++) { if(a[i]==x) return i.&x).int high.2}. /****************************************************************/ void main() { int x.6.int).int x) { int i. printf("\nenter the element to be searched").9.i<=high.20.y. y=seq_search(0.x. } /****************************************************************/ /* Function Definitions */ /****************************************************************/ int seq_search(int low. } /****************************************************************/ Output-> -------enter the element to be searched30 Element isnot present in the array enter the element to be searched9 Element 9 is present at 9 location in the array enter the element to be searched15 Element 15 is present at 3 location in the array 122 . getch().15.y+1).9.

10.&x). if(y==-1) printf("\nThe element is not present in the array"). getch().6. /****************************************************************/ void main() { int a[10]={2. else { if(x<a[mid]) high=mid-1.mid.int).h> /****************************************************************/ /* Function Prototyping */ /****************************************************************/ int binary_search(int[].y+1).16. while(low<=high) { mid=(low+high)/2.18.x). int high=n-1.int x) { int low=0. } /****************************************************************/ /* Function Definition */ /****************************************************************/ int binary_search(int a[]. printf("\nEnter the element to be searched"). scanf("%d". int x./****************************************************************/ /* PROGRAM FOR BINARY SEARCH ITERATIVELY */ /* BY: AKHILESH KUMAR SRIVASTAVA */ /* DES: THIS IS O(LOGN) ALGO AVERAGE CASE AND O(1) IN BEST CASE */ /****************************************************************/ #include<stdio.n. y=binary_search(a. else low=mid+1.20}. else printf("\nThe element is present at %d location in the array". } /*********************************************/ Output-> -------Enter the element to be searched10 The element is present at 5 location in the array Enter the element to be searched12 The element is present at 6 location in the array Enter the element to be searched17 The element is not present in the array 123 .y.12.14.int.int n.4. } } return (-1).8.10. if(a[mid]==x) return mid.h> #include<conio.

THIS LOGICAL ORDER MAY BE ASCENDING OR DESCENDING IN CASE OF NUMERIC VALUES OR DICTIONARY ORDER IN CASE OF ALPHANUMERIC VALUES THE FOLLOWING ARE THE SORTING TECHNIQUES:• BUBBLE SORT • SELECTION SORT • INSERTION SORT • MERGE SORT • QUICK SORT • HEAP SORT.ASSIGNMENT NO. –8 OBJECT:- IMPLEMENTATION OF VARIOUS SORTING ALGORITHMS THEORY AND CONCEPTS:SORTING IS THE PROCESS OF ARRANGING THE ELEMENTS IN SOME LOGICAL ORDER. 124 . • RADIX SORT • SHELL SORT PROBLEMS :WRITE A PROGRAM FOR ALL THE SORTING TECHNIQUES.

i<n. for(i=0. bubble_sort(a.int n) { int i.n). for(j=0.j++) { if(a[j]>a[j+1]) { t=a[j]. getch(). for(i=0.j. comp='t'.i. } } } } /****************************************************************/ Output-> -------Enter the no of elements in the array 7 Enter the array elements 10 9 6 7 3 2 1 The sorted set is:=> 1 2 3 6 7 9 10 125 . /****************************************************************/ void main() { int a[20]. char comp='t'. clrscr().i++) scanf("%d". a[j+1]=t.n. printf("Enter the no of elements in the array"). for(i=0.int).i++) { comp='f'.j<n-i-1. printf("\nThe sorted set is:=>\n").&a[i]). scanf("%d". printf("Enter the array elements").i++) printf("\t%d". a[j]=a[j+1].t.a[i]). } /****************************************************************/ /* Function Definition */ /****************************************************************/ void bubble_sort(int a[].i<n-1&&comp=='t'.&n).i<n./****************************************************************/ /* Program for Bubble Sort */ /* By: Akhilesh Kumar Srivastava */ /* Des: This is O(n2) Algo in AVG & WORST case & O(n) in BEST case */ /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void bubble_sort(int[].

a[i]=t. /****************************************************************/ void main() { int a[20]. } /****************************************************************/ /* Function Definition */ /****************************************************************/ void selection_sort(int a[].&a[i]).n).i++) printf("\t%d".i.t.i<n.j<n-1. selection_sort(a.i++) scanf("%d".i<n-1.i<n.n. for(i=0. printf("Enter the no of elements in the array"). for(i=0. printf("\nThe sorted set is:=>\n"). getch().int n) { int i.int). for(i=0.a[i]).j. a[j]=a[i].&n). } } } } /****************************************************************/ Output-> -------Enter the no of elements in the array 7 Enter the array elements 10 9 6 7 3 2 1 The sorted set is:=> 126 . scanf("%d". printf("Enter the array elements").i++) { for(j=i+1./********* *******************************************************/ /* Program for Selection Sort */ /* By: Akhilesh Kumar Srivastava */ /* Des: This is O(n2) Algo in all three case */ /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void selection_sort(int[].j++) { if(a[j]<a[i]) { t=a[j].

j--) { if(a[j]>a[j+1]) { t=a[j]. a[j+1]=t. getch().&n).a[i]). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ void insertion_sort(int a[]. for(i=0. printf("Enter the no of elements in the array").i<=n-1.j.i.i<n.i<n. a[j]=a[j+1].t.n. } } } /****************************************************************/ Output-> -------Enter the no of elements in the array 7 Enter the array elements 10 9 6 7 3 2 1 127 . /****************************************************************/ void main() { int a[20].i++) printf("\t%d".int).&a[i]). printf("Enter the array elements"). printf("\nThe sorted set is:=>").1 2 3 6 7 9 10 /****************************************************************/ /* Program for Insertion Sort */ /* By: Akhilesh Kumar Srivastava */ /* Des: This is O(n2) Algo in AVG & WORST case & O(n) in BEST case */ /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void insertion_sort(int[].j>=0. for(i=0.n). scanf("%d". } else break. insertion_sort(a. for(i=1.int n) { int i.i++) scanf("%d".i++) { for(j=i-1.

The sorted set is:=> 1 2 3 6 7 9 10 128 .

n-1).high). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ void quicksort(int *A.int).a[i]). while(A[j]>=v) { j--. int up) { int i=low.i++) { printf("\t%d".i.low.j+1. /****************************************************************/ void main() { int a[50].&n).int low. quicksort(A.int high) { int j.int. A[i]=A[j].low.0. } } /****************************************************************/ /* Taking the 1st element as pivot & partitions the array in two halfs*/ /****************************************************************/ int partition(int *A. if(low<high) { j=partition(A./****************************************************************/ /* Program for Quick Sort */ /* By: Akhilesh Kumar Srivastava */ /* Des: This is O(nlogn) Algo in avg case & O(n2) in Worst case */ /****************************************************************/ /* Function Prototyping */ /****************************************************************/ int partition(int*. quicksort(A.int. } getch().i<n.int).&a[i]). quicksort(a. } if(i<j) { t=A[i].n. 129 . do { do { i++. printf("\nEnter the no of elements in the array:=> "). printf("\nSorted set is \n"). void quicksort(int*.high). for(i=0. }while(A[i]<=v).int low.int v=A[low]. for(i=0.j-1). printf("\nEnter the Array elements=> \n"). scanf("%d". int j=up.i<n.i++) scanf("%d". int t.

if(i>j) { t=A[low]. } }while(i<=j). A[j]=t. } /****************************************************************/ Output-> -------Enter the no of elements in the array:=> 6 Enter the Array elements=> 100 50 150 200 30 10 Sorted set is 10 30 50 100 150 Enter the no of elements in the array:=> 8 200 Enter the Array elements=> 35 15 20 25 90 60 55 70 Sorted set is 15 20 35 25 55 60 70 90 130 . A[low]=A[j]. } return j.A[j]=t.

getch().k=low. } } /****************************************************************/ /* Merging the two sorted array to form a single sorted array */ /****************************************************************/ void merge(int *a. } /****************************************************************/ /* Partitioning the array in the arrays of 1 element each & merging them together to find the single sorted array using divide & conquer approach */ /****************************************************************/ void mergesort(int*A. k++. /****************************************************************/ void main() { int n./****************************************************************/ /* Program for Merge Sort */ /* By: Akhilesh Kumar Srivastava */ /* Des: This is O(nlogn) Algo in avg case & O(n2) in Worst case */ /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void mergesort(int*. i++. void merge(int*.int.mid+1.l. } 131 . mergesort(a.n-1). while(i<=mid&&j<=high) { if(a[i]<a[j]) { b[k]=a[i]. j++.int).j=mid+1.mid).int. int i=low. mergesort(A. i++.low. clrscr().high).high).int mid.int.a[i]).int high) { int mid. merge(A. } } while(i<=mid) { b[k]=a[i].int low. k++.int).i. } while(j<=high) { b[k]=a[j].i<n.a[50]. k++. scanf("%d". k++.i++) printf("\t%d". j++. mergesort(A.low. for(i=0.int low.&n). if(low<high) { mid=(low+high)/2.int high) { int b[50].i<n.&a[i]). printf("\nEnter the Array elements=> \n"). for(i=0. printf("\nSorted set is \n"). } else { b[k]=a[j].0. printf("\nEnter the no of elements in the array:=> ").i++) scanf("%d".mid.

} } /****************************************************************/ Output-> -------Enter the no of elements in the array:=> 6 Enter the Array elements=> 100 50 150 200 30 10 Sorted set is 10 30 50 100 150 Enter the no of elements in the array:=> 8 200 Enter the Array elements=> 35 15 20 25 90 60 55 70 Sorted set is 15 20 35 25 55 60 70 90 132 .l<=high.for(l=low.l++) { a[l]=b[l].

i++) printf("\n%d". if((j+1)<=n) { if(A[j]<A[j+1]) 133 . clrscr().i<=n. heapify(A.&A[1]).i>=2.n.B[i]).i>=1./****************************************************************/ /* Program for Heap Sort */ /* By: Akhilesh Kumar Srivastava */ /* Description: This is O(nlogn) Algorithm in all three cases */ /****************************************************************/ /* Function Prototyping */ /*****************************/ heapify(int *. scanf("%d". printf("\nEnter the Array Elements:=>").i--) adjust(A.j.i++) scanf("%d".int). for(i=n. printf("Sorted set is:=>\n\n"). adjust(A.n). for(i=1. } } /*****************************/ heapify(int *A.i--) { swap(&A[i].n). for(i=n/2.&n).n).int i. /*****************************/ void main() { int B[20]. } /*****************************/ adjust(int *A. for(i=1. while((2*i)<=n) { j=2*i. printf("Enter the no of Elements int the array:=>"). getch().int). heapsort(B.i.i. } /****************************************************************/ /* Function Definitions */ /****************************************************************/ heapsort(int *A. adjust(int *.int).int n) { int i.int n) { int i.int.1.i-1).int n) { int j.&B[i]). heapsort(int *.i<=n.

*b=t. else break.int *b) { int t.} j=j+1. } /*****************************/ Output-> -------Enter the no of elements in the array:=> 6 Enter the Array elements=> 100 50 150 200 30 10 Sorted set is 10 30 50 100 150 Enter the no of elements in the array:=> 8 200 Enter the Array elements=> 35 15 20 25 90 60 55 70 Sorted set is 15 20 35 25 55 60 70 90 134 . } } /*****************************/ swap(int *a. t=*a.&A[i]). *a=*b. i=j. if(A[i]<A[j]) swap(&A[j].

scanf("%d". } /****** Traversing the list to find the sorted set *************/ printf("\n"). printf("\n\nEnter the no. for(i=0. /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void).i++) { x=delbeg(&list1).i<n.i<n. }.int). } /********** Insertion according to the First digit *************/ for(i=0. int delbeg(NODEPTR*). void insert(NODEPTR*.i.h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { int info. NODEPTR p.x./****************************************************************/ /* Prog for Radix Sort using Linear Linked list */ /* By: Akhilesh Kumar Srivastava */ /* Des: This is O(n) Algorithm */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio.i++) { x=delbeg(&list2).i++) { scanf("%d". /****************************************************************/ void main() { NODEPTR list1=NULL.&x). traverse(list3). int radix.n).int. insert(&list2. void traverse(NODEPTR).x.x%10).list2=NULL.(x/100)%10). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ 135 . struct node *next. getch(). insert(&list1.x. insert(&list3.&n). typedef struct node* NODEPTR.i<n. of elements"). int n. /*********** Insertion according to the last digit *************/ printf("Enter %d 3-digit numbers".x.(x/10)%10).list3=NULL. void freenode(NODEPTR). } /******** Insertion according to the second last digit *********/ for(i=0.

r.q=NULL.int rad) { NODEPTR p=*list. r->next=NULL. p=p->next. if(*list==NULL) *list=r. else { while(p!=NULL&&rad>=p->radix) { q=p. } } /****************************************************************/ /**** deletion of a node from the beginning of the liked list ***/ /****************************************************************/ int delbeg(NODEPTR *list) 136 . q->next=r. r=getnode().p->info). *list=r. } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p). } /****************************************************************/ /*********************** Ordered Insertion *********************/ /****************************************************************/ void insert(NODEPTR *list.int x. p=(NODEPTR)malloc(sizeof(struct node)). r->info=x. p=p->next. return p./****************************************************************/ NODEPTR getnode(void) { NODEPTR p. } if(q==NULL) { r->next=*list. while(p!=NULL) { printf("\t%d". } else { r->next=q->next. } } } /****************************************************************/ /***************** Traversing the Linked list ******************/ /****************************************************************/ void traverse(NODEPTR p) { printf("\nThe Sorted List is\n"). r->radix=rad.

return x. of elements5 Enter 5 3-digit numbers123 365 987 563 210 The Sorted List is 123 210 365 563 987 Enter the no. x=p->info. of elements8 Enter 8 3-digit numbers100 359 254 777 698 365 444 420 The Sorted List is 100 254 359 365 420 444 698 777 137 . freenode(p). p=*list. int x. } /****************************************************************/ Output:-> ---------Enter the no.{ NODEPTR p. *list=p->next.

} } 138 . c++. for(i=0. } l=pow(10.j<n.j++) num[i][j]=0.j++) { if(num[i][j]!=0) { a[c]=num[i][j]./**************************************************************/ /* Program for Radix Sort by two-dimension arrays */ /* By: Yogesh Sharma */ /* CSE Second Year. c++.c.i<=9.h> # include<stdio.l. for(i=0.i<=9.p).int n ) { static int num[10][10]. flag++.j<=10.flag=0.j.i++) { b[i]=a[i]/l.i++) { for(j=0.i<n. Section 'D' */ /* Des: The algorithm achieves the O(n) Complexity */ /**************************************************************/ /***************** Pre-processoer directives ******************/ /**************************************************************/ # include<conio.h> /**************************************************************/ /********** Global declararion of the array *******************/ int a[100].j<=9. for(i=0. if(y==i) { num[i][c]=a[j].i<=9.//a copy of a is craeted in b// int i.i++) { c=0. /**************************************************************/ /************* Function Definitions ***************************/ /**************************************************************/ int radix(int p.y. for(j=0.j++) { y=b[j]%l. } } } } c=0.b[100].h> # include<math.i++) { for(j=0. } while(flag<=n) { for(i=0.

n. y=NULL.y.a[i]).&n).i++) printf(" %d ". } printf("\n").i++) scanf("%d". y=a[1].} return 0. p++.n). printf("\n\nEnter the number of elements-"). y=y/10.i<n.&a[i]).i<n. scanf("%d". } /**************************************************************/ void main() { int i. for(i=0. } /**************************************************************/ Output-> --------Enter the number of elements-6 123 253 987 222 364 111 111 123 222 253 364 987 Enter the number of elements-5 110 107 365 254 888 107 110 254 365 888 139 . while(y>0) { radix(p. getch(). for(i=0.p=1.

int n. shellsort(x[]./**************************************************************/ /* Program for Shell Sort */ /* By: Yogesh Sharma */ /* Des: The algorithm achieves the O(nlogn) Complexity */ /**************************************************************/ /***************** Pre-processoer directives ******************/ #include <stdio. for(incr=0. scanf("%d".k-=span) x[span+k]=x[k].j<n. for(j=span.incr<numinc.i.k>=0&&y<x[k].c). scanf("%d".int incrmnts[10].i. x[k+span]=y. for(i=0.x[i]).int numinc) { int incr.j<n.i<n.i).incrmnts[].i++) { printf("\nenter the %dth element of arrat".y.c=0.h> /**************************************************************/ /********* Function Definitions *******************************/ /**************************************************************/ void shellsort(int x[20].incrmnts[10].n.j++) { y=x[j].n. for(i=0.j++) incrmnts[c++]=j. scanf("%d". clrscr(). } for(j=1.span. } getch().&n).i<n.j.h> #include <conio. }/**************************************************************/ 140 .k.numinc. printf("\nenter the num of elements in array").incr++) { span=incrmnts[incr].&x[i]). for(k=j-span.i++) { printf("\ the %dth element of array is". } } } /**************************************************************/ void main() { int x[20].&x[i]).

IN THE RIGHT SUB TREE OF THE ROOT ARE LARGER THAN THE KEY IN THE NODE. COMMON OPERATIONS ON BINARY AND BINARY SEARCH TREES:SOME OF THE OPERATIONS THAT ARE COMMONLY PERFORMED ON BINARY AS WELL AS BINARY SEARCH TREES:1-> CREATE AN EMPTY TREE. LEAF NODES. ALL KEYS ARE UNIQUE. 8-> REMOVE IT FROM MEMORY. IN THE LEFT SUB TREE OF THE ROOT ARE SMALLER THAN THE KEY IN THE NODE. THE FOLLOWING ARE SOME OF THE OPERATIONS HAVING RELEVANCE TO ONLY BINARY SEARCH TREES:1-> INSERT A NEW NODE/ELEMENT 2-> SEARCH AN ELEMENT.E. 3-> THE KEYS. 6-> FINDING THE LOGICAL SUCCESSOR OF THE GIVEN NODE 141 . REPRESENTATION OF BINARY SEARCH TREE:TYPEDEF STRUCT NODE { STRUCT NODE *LEFT. A NONEMPTY BINARY SEARCH TREE SATISFIES THE FOLLOWING PROPERTIES:1-> EVERY ELEMENT HAS A KEY(OR VALUE) AND NO TWO ELEMENTS HAVE THE SAME KEY I. IF ANY. INT INFO. IF ANY. 4-> THE LEFT AND RIGHT SUB TREES OF THE ROOT ARE ALSO BINARY SEARCH TREES. 5-> DETERMINE THE NUMBER OF INTERNAL NODES I. 4-> DETERMINE THE NUMBER OF ELEMENTS IN IT. 6-> DETERMINE THE NUMBER OF EXTERNAL NODES I. THEORY AND CONCEPTS :A BINARY SEARCH TREE T IS A BINARY TREE THAT MAY BE EMPTY.E. 3-> FIND THE SMALLEST ELEMENT. 7-> DETERMINE ITS MIRROR IMAGE. –9 OBJECT:- PROGRAM BASED ON BINARY TREES.ASSIGNMENT NO. 4->FIND THE LARGEST ELEMENT. 2-> TRAVERSE IT. BNODE *ROOT. 2-> THE KEYS. NON-LEAF NODES. 5->DELETE A NODE.E. STRUCT NODE *RIGHT. }BNODE. 3-> DETERMINE THE HEIGHT.

(5) PROGRAM FOR TRAVERSAL OF THREADED TREES 142 . INSERTION). (4) WRITE A PROGRAM FOR IMPLEMENTATION OF HUFFMAN ALGORITHM TO GENERATE THE VARABLE LENGTH CODES. DELETION.LOGICAL SUCCESSOR. MAXIMUM.PRACTICAL/ASSIGNMENTS:(1) WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS OPERATIONS ON A BINARY SEARCH TREE (MINIMUM. (3) WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS TRAVERSAL TECHNIQUES IN A BINARY TREE NON RECURSIVELY. (2) WRITE A MENU DRIVEN PROGRAM TO IMPLEMENTING THE VARIOUS TRAVERSAL TECHNIQUES IN A BINARY TREE RECURSIVELY. TRAVERSAL.

tree=maketree('A'). pretrav(tree). intrav(tree).h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { char info.Inorder. }. printf("\n\nPostorder traversal is:=>"). return p./****************************************************************/ /* Prog for Binary tree traversal */ /* By: Akhilesh Kumar Srivastava */ /* Des: Three Traversals: Preorder.Postorder are implemented recursively */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. void pretrav(NODEPTR). void freenode(NODEPTR). struct node *right. /****************************************************************/ void main() { NODEPTR tree=NULL. } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ 143 . tree->left->left=maketree('D'). tree->left->left->left=maketree('F'). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p. posttrav(tree). tree->right->left=maketree('E'). getch(). NODEPTR maketree(int). /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void). struct node *left. clrscr(). printf("\n\nInorder traversal is:=>"). printf("\n\nPreorder traversal is:=>"). p=(NODEPTR)malloc(sizeof(struct node)). void posttrav(NODEPTR). tree->right=maketree('C'). void intrav(NODEPTR). typedef struct node* NODEPTR. tree->left=maketree('B').

t->info). } } /****************************************************************/ /***************** Inorder Traversal of tree ******************/ /****************************************************************/ void intrav(NODEPTR t) { if(t!=NULL) { intrav(t->left). p->info=x. } /****************************************************************/ /***************** Preorder Traversal of tree ******************/ /****************************************************************/ void pretrav(NODEPTR t) { if(t!=NULL) { printf("\t%c".void freenode(NODEPTR p) { free(p). printf("\t%c". intrav(t->right).t->info). } } /****************************************************************/ /**************** making a node of tree with info x ************/ /****************************************************************/ NODEPTR maketree(int x) { NODEPTR p. pretrav(t->right). return p. p=getnode(). posttrav(t->right). pretrav(t->left). printf("\t%c". } /****************************************************************/ Output-> -------Preorder traversal is:=> A B D F C E Inorder traversal is:=> F D B A E C Postorder traversal is:=> F D B E C A 144 .t->info). p->left=NULL. p->right=NULL. } } /****************************************************************/ /***************** Preorder Traversal of tree ******************/ /****************************************************************/ void posttrav(NODEPTR t) { if(t!=NULL) { posttrav(t->left).

pretrav(tree). /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void). intrav(tree). create(&tree). printf("Enter the root node"). void intrav(NODEPTR). void freenode(NODEPTR). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ 145 . void pretrav(NODEPTR). posttrav(tree). clrscr(). printf("\n\nInorder traversal is:=>"). printf("\n\nPostorder traversal is:=>").&c). printf("\n\nPreorder traversal is:=>"). scanf("%c". NODEPTR maketree(int).h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { char info. char c. tree=maketree(c). getch(). /****************************************************************/ void main() { NODEPTR tree=NULL. void posttrav(NODEPTR). typedef struct node* NODEPTR. }. struct node *right. struct node *left. create(NODEPTR *t)./****************************************************************/ /* Program for Binary tree creation & traversal recursively */ /* By: Akhilesh Kumar Srivastava */ /* Des: The Insertion process is recursive in nature */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio.

} /****************************************************************/ /***************** Preorder Traversal of tree ******************/ /****************************************************************/ void pretrav(NODEPTR t) { if(t!=NULL) { printf("\t%c". printf("\t%c". intrav(t->right). } } /****************************************************************/ /***************** Inorder Traversal of tree ******************/ /****************************************************************/ void intrav(NODEPTR t) { if(t!=NULL) { intrav(t->left). } } /****************************************************************/ /***************** Preorder Traversal of tree ******************/ /****************************************************************/ void posttrav(NODEPTR t) { if(t!=NULL) { posttrav(t->left). } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p)./****************************************************************/ NODEPTR getnode(void) { NODEPTR p. printf("\t%c". } } 146 .t->info). pretrav(t->right).t->info). return p. pretrav(t->left). posttrav(t->right).t->info). p=(NODEPTR)malloc(sizeof(struct node)).

&x)./****************************************************************/ /**************** making a node of tree with info x ************/ /****************************************************************/ NODEPTR maketree(int x) { NODEPTR p. } } 147 . q=(*t)->left.&x). char y. if(x==1) { printf("Enter the info"). return p. if(x==1) { printf("Enter the info").q. p->info=x.&y). scanf("%c". NODEPTR p.(*t)->info). fflush(stdin). create(&q).&y). scanf("%d". q=(*t)->right. scanf("%c". fflush(stdin). } /****************************************************************/ create(NODEPTR *t) { int x. create(&q). p->left=NULL. printf("Does the left of %c exist?(1/0)". p=getnode(). (*t)->left=maketree(y). p->right=NULL. (*t)->right=maketree(y). scanf("%d".(*t)->info). } printf("Does the right of %c exist?(1/0)".

OUTPUT Enter the root nodeA Does the left of A exist?(1/0)1 Enter the infoB Does the left of B exist?(1/0)0 Does the right of B exist?(1/0)1 Enter the infoC Does the left of C exist?(1/0)0 Does the right of C exist?(1/0)1 Enter the infoD Does the left of D exist?(1/0)0 Does the right of D exist?(1/0)0 Does the right of A exist?(1/0)1 Enter the infoE Does the left of E exist?(1/0)1 Enter the infoF Does the left of F exist?(1/0)0 Does the right of F exist?(1/0)0 Does the right of E exist?(1/0)1 Enter the infoG Does the left of G exist?(1/0)0 Does the right of G exist?(1/0)0 Preorder traversal is:=> AB C D E F G Inorder traversal is:=> B C D A F E G Postorder traversal is:=> D C B F G E A 148 .

ch. fflush(stdin). fflush(stdin).char). void insertleft(NODEPTR*. Inorder Traversal\n"). printf("\t2. void log_succ(NODEPTR. printf("Enter your choice\t").&c). int c./****************************************************************/ /* Prog for Binary Search tree Insertion & Logical successor */ /* By: Akhilesh Kumar Srivastava */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio. case 2: printf("\nInorder traversal is:=>"). case 3: printf("\nEnter the element for logical succesor:=>"). char min(NODEPTR). scanf("%d". printf("\t3. Logical Succesor of the node\n"). /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void). break.x). exit\n"). char a[10]. void insertright(NODEPTR*.q. break. insert_bst(&tree. void freenode(NODEPTR). break. struct node *right. start: printf("\t1.char). void intrav(NODEPTR). struct node *father. switch(c) { case 1: printf("\nEnter the element to be inserted"). printf("\t4. intrav(tree).p. NODEPTR maketree(char).h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct node { char info.char). 149 . /****************************************************************/ void main() { NODEPTR tree=NULL.x). }.char). scanf("%c".&x). scanf("%c". struct node *left. void insert_bst(NODEPTR*. Insert in the BST\n"). typedef struct node* NODEPTR.x. printf("===========================\n").&x). clrscr(). log_succ(tree.

} } /****************************************************************/ /***************** Inorder Traversal of tree ******************/ /****************************************************************/ void intrav(NODEPTR t) { if(t!=NULL) { intrav(t->left).&ch). fflush(stdin). default: printf("\nWrong choice").t->info). p=(NODEPTR)malloc(sizeof(struct node)). 150 .exit(1). scanf("%c". return p. printf("\t%c". } /****************************************************************/ /***************** Preorder Traversal of tree ******************/ /****************************************************************/ void pretrav(NODEPTR t) { if(t!=NULL) { printf("\t%c". if(ch=='y'||ch=='Y') goto start. } } /****************************************************************/ /***************** Preorder Traversal of tree ******************/ /****************************************************************/ void posttrav(NODEPTR t) { if(t!=NULL) { posttrav(t->left). } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p). intrav(t->right). pretrav(t->left). break. pretrav(t->right). } case 4: printf("\nDo you wish to continue? (y/n) ").t->info). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p.

p=maketree(x).char x) { NODEPTR p.x).t->info). return p. p->father=(*m).char x) { NODEPTR p. p->father=(*m). p->info=x. if(x<p->info) p=p->left. printf("\t%c". (*m)->left=p. 151 .char x) { NODEPTR p=*tree. else insertright(&q. if(p!=NULL) { while(p!=NULL) { q=p. } /****************************************************************/ /**********Insertion in binary search tree **********************/ /****************************************************************/ void insert_bst(NODEPTR *tree. p->left=NULL. } else (*tree)=maketree(x). p->father=NULL. p=maketree(x). } /****************************************************************/ /*********** Inserting an element towards right of node m *******/ /****************************************************************/ void insertright(NODEPTR *m. else p=p->right. p->right=NULL.posttrav(t->right). } } /****************************************************************/ /**************** Making a node of tree with info x ************/ /****************************************************************/ NODEPTR maketree(char x) { NODEPTR p. p=getnode(). } /****************************************************************/ /*********** Inserting an element towards left of node m ********/ /****************************************************************/ void insertleft(NODEPTR *m.q=NULL.x). } if(x<q->info) insertleft(&q. (*m)->right=p.

while(r!=NULL) { if(r->info==x) break. } else { q=p->father.x. return(p->info).} /****************************************************************/ /*********** Finding the Logical Suceesor of the node x *********/ /****************************************************************/ void log_succ(NODEPTR tree. } } /****************************************************************/ /*************** Finding the minimum in the subtree *************/ /****************************************************************/ char min(NODEPTR p) { while(p->left!=NULL) p=p->left.q->info).r=tree. else printf("\nLogical Successor does not exists"). if(x<r->info) r=r->left. q=q->father. } p=r. if(p->right!=NULL) { printf("Logical Successor of %c is\t%c".min(p->right)).x. } /****************************************************************/ 152 .q. if(x>r->info) r=r->right. } if(q!=NULL) printf("\nLogical Successor of %c is\t%c". while(p==q->right&&q!=NULL) { p=q.char x) { NODEPTR p.

Inorder Traversal 3. Inorder Traversal 3. Insert in the BST 2. Insert in the BST 2. Insert in the BST 2. exit =========================== Enter your choice 2 Inorder traversal is:=> B C Do you wish to continue? (y/n) Y 1. Inorder Traversal 3. exit =========================== Enter your choice 3 N Enter the element for logical succesor:=> C Logical Successor of C is N 153 . exit =========================== Enter your choice 1 Enter the element to be insertedC Do you wish to continue? (y/n) Y 1.OUTPUT-> -------1. exit =========================== Enter your choice 1 Enter the element to be insertedN Do you wish to continue? (y/n) Y 1. Logical Succesor of the node 4. Insert in the BST 2. Logical Succesor of the node 4. Insert in the BST 2. Logical Succesor of the node 4. Inorder Traversal 3. Inorder Traversal 3. Logical Succesor of the node 4. exit =========================== Enter your choice 1 Enter the element to be insertedB Do you wish to continue? (y/n) Y 1. Logical Succesor of the node 4.

THIS DATA STRUCTURE IS USED TO REPRESENT RELATIONSHIP BETWEEN PAIRS OF ELEMENTS WHICH ARE NOT NECESSARILY HIERARCHICAL IN NATURE . A GRAPH IS DEFINED AS “GRAPH G IS A ORDERED SET(V.E) WHERE V(G) REPRESENTS THE EDGES BETWEEN THESE VERTICES .ASSIGNMENT NO. THEORY AND CONCEPTS OF GRAPHS:GRAPH IS ANOTHER IMPORTANT NON-LINEAR DATA STRUCTURE . • CREATING AN EMPTY GRAPH. (3) WRITE A C PROGRAM FOR WARSHALL’S ALGORITHM FOR SHORTEST PATH (4) WRITE A C PROGRAM FOR WARSHALL’S ALGORITHM FOR TRANSITIVE CLOSURE 154 . CALLED VERTICES AND E(G) REPRESENTATION OF GRAPHS:.PROGRAMS BASED ON GRAPH.E):• • AS AN ADJACENCY MATRIX AS AN ADJACENCY LISTS OPERATIONS ON GRAPHS:THE FOLLOWING ARE THE OPERATIONS THAT ARE PERFORMED ON THE GRAPH. • ENTERING GRAPH INFORMATION • OUTPUTTING A GRAPH • DELETING A GRAPH • BREADTH FIRST SEARCH • DEPTH FIRST SEARCH PROBLEMS :(1) WRITE A C PROGRAM FOR DEPTH FIRST SEARCH OF A GRAPH.THERE ARE TWO WAYS TO REPRESENT A GRAPH G=(V. (2) WRITE A C PROGRAM FOR BREADTH FIRST SEARCH OF A GRAPH. REPRESENT THE SET OF ELEMENTS . –10 OBJECT :.

i<=n. } } for(k=1.j++) { if(a[i][j]>a[i][k]+a[k][j]) a[i][j]=a[i][k]+a[k][j]. clrscr(). printf("Enter the number of nodes in the graph").n.i++) { for(j=1.i++) { for(j=1. for(i=1. printf("Enter the path matrix\n"). } } getch(). } } } printf("The shortest path matrix is:=>\n").k<=n.j<=n.j<=n.k.i<=n.&n). if(max<a[i][j]) max=a[i][j].a[i][j]).&a[i][j]). } } /******* Setting zero elements to be the maximum element ******/ for(i=1. for(i=1.k++) { for(i=1.j<=n.i. for(j=1.j<=n.j++) { printf("\t%d".i<=n./****************************************************************/ /* Prog for Warshall's Algorithm for shortest path */ /* By: Akhilesh Kumar Srivastava */ /* Des: This is a dynamic programming solution with O(n3) complexity*/ /****************************************************************/ void main() { int a[10][10]. } /****************************************************************/ 155 .i++) { printf("\n").i++) { for(j=1.i<=n. scanf("%d".j++) { if(a[i][j]==0) a[i][j]=2*max+1.max.j++) { scanf("%d".j.

OUTPUT-> -------Enter the number of nodes in the graph 3 Enter the path matrix 0 100 20 30 0 250 100 300 0 The shortest path matrix is:=> 120 30 100 100 130 200 20 50 120 156 .

} } getch()./****************************************************************/ /* Prog for Warshall's Algorithm for transitive Closure */ /* By: Akhilesh Kumar Srivastava */ /****************************************************************/ void main() { int a[10][10].i<=n. } /****************************************************************/ 157 . } } for(k=1.j++) { a[i][j]=a[i][j]|(a[i][k]&a[k][j]). clrscr().j<=n. } } } printf("Adjacency matrix of the Transitive Closure is:=>\n").k.j<=n. printf("enter the adjacency matrix").i++) { for(j=1.i<=n. for(i=1.j++) { scanf("%d".j<=n. for(j=1.j.i++) { for(j=1. printf("enter the number of nodes in the graph").i<=n.a[i][j]). for(i=1.k++) { for(i=1.i++) { printf("\n").&a[i][j]).i.j++) { printf("\t%d". scanf("%d".n.k<=n.&n).

i). /****************************************************************/ void main() { int a[10][10]. /****************************************************************/ /* Function Prototyping */ /****************************************************************/ void init(struct queue *pq).&n). clrscr(). l++. int i. } } k=1.l.i<=n.k.&a[i][j]).i++) { printf("\n%d:=>\t".adj[i][k]).n=6. for(j=1. init(&q).i++) { k=1. for(i=1.j<=n. /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct queue { int item[100]. scanf("%d". int front. static int rep[10]. int repeat(). int remove(struct queue *pq). }. k++.l=1. k++. if(a[i][j]!=0) { adj[i][k]=j. for(i=1. struct queue q. static int adj[10][10]. void insert(struct queue *pq./****************************************************************/ /* Prog for Breadth first Search in the GRAPH */ /* By: Akhilesh Kumar Srivastava */ /****************************************************************/ static int BFS[10]. printf("Enter the adjacency matrix\n").int x). k=1. rep[l]=1.x. while(adj[i][k]!=0) { printf("\t%d". int empty(struct queue *pq). int rear. printf("Enter the number of nodes in the graph"). 158 .j++) { scanf("%d".i<=n. } } } printf("\nThe adjacency list is:=>\n").1). insert(&q.j.

for(i=1. } i++.i++) { printf("\t%d".int x) { if(pq->rear==99) { printf("Queue Overflows"). pq->front=0.i<=n. } getch(). } }while(!empty(&q)). pq->item[pq->rear]=x. if(empty(pq)) { printf("Queue Underflows"). } /****************************************************************/ void insert(struct queue *pq. l++. exit(1). } /****************************************************************/ /* Function Definitions */ /****************************************************************/ void init(struct queue *pq) { pq->rear=-1.do { x=remove(&q). exit(1). return x. rep[l]=adj[x][i]. k++.BFS[i]). } /****************************************************************/ int remove(struct queue *pq) { int x. } x=pq->item[pq->front]. } pq->rear++. pq->front++. i=1. while(adj[x][i]!=0) { if(repeat(adj[x][i])!=1) { insert(&q. printf("\nBFS sequence is:=>\t").adj[x][i]). BFS[k]=x. } /****************************************************************/ int empty(struct queue *pq) { if(pq->rear-pq->front+1==0) 159 .

return 1. } /****************************************************************/ int repeat(int x) { int i=1. } return 0. i++. else return 0. while(rep[i]!=0) { if(rep[i]==x) return 1. } /****************************************************************/ Output:-> --------Enter the number of nodes in the graph5 Enter the adjacency matrix 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 The adjacency list is:=> 1:=> 2:=> 3:=> 4:=> 5:=> 2 1 4 1 1 BFS sequence is:=> 4 3 5 2 5 5 1 2 4 3 5 160 .

}. void push(struct stack*.j<=n.adj[i][k]). while(adj[i][k]!=0) { printf("\t%d".l.i<=n. k++.&n). /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct stack { char item[STACKSIZE]. int i. static int adj[10][10].j++) { scanf("%d". void init(struct stack*). for(j=1.n=6. if(a[i][j]!=0) { adj[i][k]=j./****************************************************************/ /* Prog for Depth first Search in the GRAPH */ /* By: Akhilesh Kumar Srivastava */ /****************************************************************/ /****************** Pre-processoer directives *******************/ /****************************************************************/ #include<stdio. for(i=1.j. init(&s). 161 . for(i=1.i++) { k=1.i). struct stack s. char pop(struct stack*). /****************************************************************/ /* Function Prototyping */ /****************************************************************/ int repeat(). int empty(struct stack*).k. printf("Enter the adjacency matrix\n"). } } } printf("\nThe adjacency list is:=>\n").int x).i++) { printf("\n%d:=>\t".&a[i][j]).i<=n. k=1. printf("Enter the number of nodes in the graph").x. clrscr(). static int rep[10].h> #define STACKSIZE 100 #define TRUE 1 #define FALSE 0 /****************************************************************/ /***************** Global Decalration of arrays *****************/ /****************************************************************/ static int DFS[10]. scanf("%d". int top. /****************************************************************/ void main() { int a[10][10].

rep[l]=adj[x][i].l=1. } /****************************************************************/ /***************** Insertion of an item in stack ****************/ /****************************************************************/ void push(struct stack *s. else return FALSE.DFS[i]).1). push(&s. rep[l]=1. k++.int x) { if(s->top==STACKSIZE-1) { printf("stack overflow"). } /****************************************************************/ /******************* checking if stack is empty *****************/ /****************************************************************/ int empty(struct stack *s) { if(s->top==-1) return TRUE. } getch(). } }while(!empty(&s)). do { x=pop(&s).i<=n. l++. while(adj[x][i]!=0) { if(repeat(adj[x][i])!=1) { push(&s. for(i=1.adj[x][i]). 162 .k++. l++. } } k=1. } s->top++. } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /****************************************************************/ /********************* Initialisation of stack ******************/ /****************************************************************/ void init(struct stack *s) { s->top=-1. exit(1). i=1. DFS[k]=x. printf("\nDFS sequence is:=>\t"). } i++.i++) { printf("\t%d".

s->top--. exit(1). } /****************************************************************/ int repeat(int x) { int i=1.s->item[s->top]=x. } /****************************************************************/ Output:-> --------Enter the number of nodes in the graph5 Enter the adjacency matrix 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 The adjacency list is:=> 1:=> 2:=> 3:=> 4:=> 5:=> 2 1 4 1 1 BFS sequence is:=> 4 3 5 2 5 5 4 5 1 2 3 163 . } y=s->item[s->top]. } /****************************************************************/ /*************** Removal of an item from stack ******************/ /****************************************************************/ int pop(struct stack *s) { int y. } return 0. return(y). i++. if(empty(s)) { printf("Stack Underflows"). while(rep[i]!=0) { if(rep[i]==x) return 1.

64 -16 0 -16 0 0 0 0 -16 64 -16 0 -16 0 0 0 0 -16 64 0 0 -16 0 0 -16 0 0 64 -16 0 -16 0 0 -16 0 -16 64 -16 0 -16 0 0 -16 0 -16 64 0 0 0 0 0 -16 0 0 64 -16 0 0 0 0 -16 0 -16 64 0 0 0 0 0 -16 0 -16 0 0 0 0 0 -16 0 -16 64 164 . which can yield enormous savings in memory usage. It is also noticeable in everyday life. of a low density of significant data or connections. And more importantly.ASSIGNMENT NO. with one of them dominant (say a file that stores a handwritten signature) can be encoded as a sparse matrix that contains only row and column numbers for pixels with the non-dominant color. This concept is amenable to quantitative reasoning. Definitions Given a sparse N×M matrix A the row bandwidth for the n-th row is defined as The bandwidth for the matrix is defined as Example A bitmap image having only 2 colors. it is often necessary to modify the standard algorithms and take advantage of the sparse structure of the matrix. useful in combinatorics and application areas such as network theory. manipulating huge sparse matrices with the standard algorithms may be impossible due to their sheer size. –12 OBJECT:- PROGRAM BASED ON SPARSE MATRIX In the mathematical subfield of numerical analysis a sparse matrix is a matrix populated primarily with zeros. The definition of huge depends on the hardware and the computer programs available to manipulate the matrix. When storing and manipulating sparse matrices on the computer. Huge sparse matrices often appear in science or engineering when solving problems for linear models. Sparse data is by its nature easily compressed. Sparsity is a concept.

a practical requirement for a family of matrices to be `usefully' sparse is that they have only O (n) nonzero entries. The difficulty is that sparse data structures include more overhead (to store indices as well as numerical values of nonzero matrix entries) than the simple arrays used for dense matrices. in the matrix also has a major effect on how well sparsity can be exploited. In array JA.Storing a sparse matrix The naive data structure for a matrix is a two dimensional array. The length of row i is determined by IA(i+1) . a (small) constant number of nonzero per row or column. There is therefore a tradeoff in memory requirements between sparse and dense representations and a tradeoff in performance between the algorithms that use them. One example of such a sparse matrix format is the (old) Yale Sparse Matrix Format [1].j of the matrix and can be accessed by the two indices i and j. For this reason. JA is of length NZ. we would like to store and operate on only the nonzero entries of the matrix. In addition to the number of nonzero. most matrices arising from finite difference or finite element discretizations of PDEs satisfy this condition. For a n×m matrix we need at least (n*m) / 8 bytes to represent the matrix when assuming 1 bit for each entry. The array IA stores at IA(i) the position of the first element of row i in the sparse array A. It stores an initial sparse N×N matrix M in row form using three arrays. Depending on the number and distribution of the non-zero entries. but such a policy is not necessarily a clear win in either storage or work. Each entry in the array represents an element ai. JA. or pattern. For n×n matrix we need only n / 8 bytes when assuming 1 bit for each entry. i. A. different data structures can be used and yield huge savings in memory when compared to a naive approach. IA. The basic idea when storing sparse matrices is to only store the non-zero entries as opposed to storing all entries. Ideally. the column index of the element A(j) is stored. Another Definition A good operational definition of sparse matrix is that “a matrix is sparse if it contains enough zero entries to be worth taking advantage of them to reduce both the storage and work required in solving a linear system”. Therefore IA needs to be of length N + 1. A sparse matrix contains many (often mostly) zero entries. NZ denotes the number of nonzero entries in matrix M. their particular locations. independent of the matrix dimension. The array A then is of length NZ and holds all nonzero entries of M. Diagonal matrix A very efficient structure for a diagonal matrix is to store just the entries in the main diagonal as a one dimensional array. For example. and arithmetic operations on the data stored in them usually cannot be performed as rapidly either (due to indirect addressing of operands).e. whereas the same number of nonzero located randomly might offer relatively little advantage. 165 . Sparsity arising from physical problems usually exhibits some systematic pattern that can be exploited effectively.IA(i).

Vector representaion Representing the nonzero elements in the single dimension array using the row major order concept 2. Linked Representaion Representing the elements in the form of Linear linked list with the fields (Data.Tri Diagonal matrix In this matrix. row & column number) 166 . 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 Triangular Matrix 1 1 1 1 1 1 1 0 2 2 2 2 2 2 0 0 3 3 3 3 3 0 0 0 4 4 4 4 0 0 0 0 5 5 5 0 0 0 0 0 6 6 0 0 0 0 0 0 7 Representation of Sparse Matrix 1. the elements in the main diagonal and the diagonals immediately above this and below this are non zero and rest elements are zero.

x. } p=s1. typedef struct sparse* NODEPTR.int). void freenode(NODEPTR).&n2).s=NULL. void traverse(NODEPTR).column):=> "). int column.row.n2.&r.x.r. for(i=0./****************************************************************/ /* Prog for addition of two Sparse Matrix */ /* By: Akhilesh Kumar Srivastava */ /* Des: The Sparse matrix is represented by Linked List */ /****************************************************************/ /****************** Pre-processoer directives *******************/ #include<stdio.r. scanf("%d".int.&x.&r.i<n1. printf("\nEnter the elements of first sparse matrix (data.c.&n1). int row. q=s2. printf("\nEnter the elements of first sparse matrix (data.i.&c).int.&c). 167 .i++) { scanf("%d%d%d".row.h> /****************************************************************/ /* Structure declararion */ /****************************************************************/ struct sparse { int info.i++) { scanf("%d%d%d".c). struct sparse *next. printf("\nEnter the no of nonzero elements in the first Sparse Matrix:=> "). /****************************************************************/ void main() { NODEPTR s1=NULL. /****************************************************************/ /* Function Prototyping */ /****************************************************************/ NODEPTR getnode(void).i<n2. void insend(NODEPTR*.n1.q. scanf("%d". for(i=0.column):=> "). }.c). insend(&s1.&x.r. int x.p.s2=NULL. insend(&s2. } printf("\nEnter the no of nonzero elements in the first Sparse Matrix:=> ").

p=p->next.p->info.p->row. q=q->next.q->row.p->row. } else { insend(&s. p=p->next.while(p!=NULL&&q!=NULL) { if(p->row==q->row) { if(p->column==q->column) { insend(&s.q->column).p->column).q->info.q->row. } else { insend(&s.q->info.q->row.p->column). p=p->next.p->info+q->info. q=q->next.p->row.p->row.p->column). } while(q!=NULL) { insend(&s.p->info.q->column). } } } while(p!=NULL) { insend(&s. } else { if(p->column<q->column) { insend(&s.q->info. q=q->next.p->info.q->column). 168 . } printf("The added Sparse Matrix is:=>\n"). q=q->next. } } } else { if(p->row<q->row) { insend(&s.p->column). p=p->next.

traverse(s);
getch();
}
/****************************************************************/
/* Function Definitions */
/****************************************************************/
/************** Allocating memory for a new node ****************/
/****************************************************************/
NODEPTR getnode(void)
{
NODEPTR p;
p=(NODEPTR)malloc(sizeof(struct sparse));
return p;
}
/****************************************************************/
/***************** freeing the memory not requird ***************/
/****************************************************************/
void freenode(NODEPTR p)
{
free(p);
}
/****************************************************************/
/***************** Traversing the Linked list ******************/
/****************************************************************/
void traverse(NODEPTR p)
{
while(p!=NULL)
{
printf("\t%d",p->info);
printf(" %d",p->row);
printf(" %d",p->column);
p=p->next;
}
}
/****************************************************************/
/******* Insertion of a node in the end of the liked list *******/
/****************************************************************/
void insend(NODEPTR *list,int x,int r,int c)
{
NODEPTR p=*list,q;
q=getnode();
q->info=x;
q->row=r;
q->column=c;
q->next=NULL;
if(*list==NULL)
*list=q;
else
{
while(p->next!=NULL)
169

p=p->next;
p->next=q;
}
}
/****************************************************************/

Output

170

/****************************************************************/
/* Prog for finding the transpose of the given Sparse Matrix */
/* By: Akhilesh Kumar Srivastava */
/* Des: The Sparse matrix is represented by Linked List */
/****************************************************************/
/****************** Pre-processoer directives *******************/
#include<stdio.h>
/****************************************************************/
/* Structure declararion */
/****************************************************************/
struct sparse
{
int info;
int row;
int column;
struct sparse *next;
};
typedef struct sparse* NODEPTR;
/****************************************************************/
/* Function Prototyping */
/****************************************************************/
NODEPTR getnode(void);
void insend(NODEPTR*,int,int,int);
void insafter(NODEPTR*,int,int,int);
void insbeg(NODEPTR*,int,int,int);
void traverse(NODEPTR);
/****************************************************************/
void main()
{
NODEPTR s1=NULL,s2=NULL,p,q,r;
int x,co,ro,n,i;
clrscr();
printf("\nEnter the no of nonzero elements in the Sparse Matrix:=> ");
scanf("%d",&n);
printf("Enter the elements of first sparse matrix (data,row,column):=> ");
for(i=0;i<n;i++)
{
scanf("%d%d%d",&x,&ro,&co);
insend(&s1,x,ro,co);
}
printf("The Original matrix is:=>");
traverse(s1);
p=s1;
printf("\nThe ordered insertion:=>");
while(p!=NULL)
{
q=s2; r=NULL;
if(q==NULL)
insbeg(&s2,p->info,p->column,p->row);
171

p->row).p->column.p->info.p->column. } printf("\nThe transpose matrix is:=>"). else insafter(&r. q=q->next.p->row). getch().p->row).p->info.p->column. } if(q==NULL) insafter(&r. traverse(s2).p->row). q=q->next. p=p->next.p->row). } else //if(p->column==q->row) { while(p->column==q->row&&p->row>q->column&&q!=NULL) { r=q.p->info. else insafter(&r. traverse(s2).p->info.else { while(p->column>q->row&&q!=NULL) { r=q.p->info. } if(r==NULL) insbeg(&s2. } /****************************************************************/ /* Function Definitions */ /****************************************************************/ /************** Allocating memory for a new node ****************/ /****************************************************************/ NODEPTR getnode(void) { NODEPTR p. } } } // printf(""). 172 .p->column. else { if(p->column<q->row) { if(r==NULL) insbeg(&s2.p->column.

p=p->next.int r. q->row=r.p=(NODEPTR)malloc(sizeof(struct sparse)). } } /****************************************************************/ /******* Insertion of a node in the end of the liked list *******/ /****************************************************************/ void insend(NODEPTR *list. q=getnode(). printf(" %d". p->next=q.int c) { NODEPTR p=*list.p->row). if(*list==NULL) *list=q. } /****************************************************************/ /***************** freeing the memory not requird ***************/ /****************************************************************/ void freenode(NODEPTR p) { free(p). } /****************************************************************/ /***************** Traversing the Linked list ******************/ /****************************************************************/ void traverse(NODEPTR p) { printf("\n").p->column).int x.q. else { while(p->next!=NULL) p=p->next. while(p!=NULL) { printf("\t%d".p->info). q->column=c.int x. q->next=NULL. 173 . printf(" %d".int c) { NODEPTR q. } } /****************************************************************/ /*Insertion of a node after a node pointed by pointed by pointer p*/ /****************************************************************/ void insafter(NODEPTR *p. q->info=x. return p.int r.

} /****************************************************************/ Output: 174 . q->next=(*p)->next.q=getnode(). (*p)->next=q. p->next=*list. p=getnode(). q->column=c.int r.int x. } /****************************************************************/ /**** Insertion of a node in the beginning of the liked list ****/ /****************************************************************/ void insbeg(NODEPTR *list. p->row=r. q->row=r.int c) { NODEPTR p. *list=p. p->column=c. p->info=x. q->info=x.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.