This action might not be possible to undo. Are you sure you want to continue?

com INTRODUCTION TO C C has emerged as the most widely used programming language for software development. Its features allow the development of well-structured programs. Most computers directly support its data types and control structures, resulting in the construction of efficient programs. It is independent of any particular machine architecture or operating system, which makes it easy to write portable programs. It is this contribution of rich control structure and data types, portability and conciseness that has contributed to the popularity of C. History of C C programming language is basically developed for UNIX Operating System. UNIX was developed in 1969 at bell telephone laboratories. It was entirely written on PDP 7 assembly language. After UNIX has been implemented Ken Thompson implemented a compiler for a new language called B used for transporting UNIX onto other machines. B was heavily influenced by BCPL (Basic Cambridge Programming Language) written for writing system software. B was latter modified by Dennis Ritchie who was also working at bell Labs. He named the successor C. Unix was later rewritten in C by Dennis Ritchie, Thompson and others by 1973. C Program Structure A basic fact about computer programming is that all programs can be written using a combination of only three control structures: Sequential, Selective and repetitive. The sequential structure consists of a sequence of program statements that are executed one after another in order, the selective structure consists of a test for a condition followed by alternative paths that the program can follow, and the repetitive structure consists of program statements that are repeatedly executed while some condition holds. The sequential structure can be pictorially represented as follows Entry Statement 1 Statement 2 Statement 3 Exit All C programs are made up of one or more functions, each performing a particular task. Every program has a special function named main. It is special because the execution of any program starts at the beginning of its main function.

1

www.technicalsymposium.com

For More Notes and questions log on to www.technicalsymposium.com A typical C program has following sections 1. Preprocessor Directives 2. Global Variable Declarations 3. Functions In a C program, preprocessor directive, if present, should come first followed by global variable definition if any. Variable Declaration in C 1. 2. 3. 4. 5. The variable can be 31 characters long. The variable can be any of a-z, A-Z, 0-9 and the underscore. Should not be a keyword. First character must be an alphabet The variable is case sensitive

Data Types Every programming language has its own data type. The basic data types in C are Int - an integer Float – a single precision floating point number Char - a character in C character set Double – a double precision floating point number Variables Variables are data objects that are manipulated in a program. Information can be stored in a variable and recalled later. Variables must be declared before they can be used in a program. Constants A constant is an entity whose value does not change during program execution. Constants are of five different types 1. 2. 3. 4. Integer Constants Floating point Constants Character Constants String Constants

2

www.technicalsymposium.com

For More Notes and questions log on to www.technicalsymposium.com C Operators The operators in C include 1. 2. 3. 4. 5. 6. 7. Arithmetic Assignment Relational Increment and Decrement Bit Logical or Boolean Conditional expression

INPUT / OUTPUT The important aspects of C programming language are its ability to handle input and output (I/O). A program using input / output functions must include the standard header file (stdio.h) in it using the directive. Printf functions (CONIO.H, STDIO.H) printf – sends formatted output to stdout fprintf – sends formatted output to a stream cprintf – sends formatted output to the text window on the screen Scanf Function Scanf - reads data from stdin Fscanf – reads data from stream The GETCHAR and PUTCHAR Function Getchar, putchar (STDIO.h) getchar is a macro that gets a character from stdin putchar is a macro outputs a character on stdout

The GETCH and GETCHE Function getch gets a character from console but does not echo to the screen getche gets a character from console and echoes to the screen

gets, puts gets() - gets a string from stdin puts() – outputs a string to stdout

3

www.technicalsymposium.com

The while loop should be used in applications that do not require the modification of any variables at each iteration. Else part is optional. FOR LOOP This is used when the statements are to be executed more than once. When the condition becomes false. SYNTAX While (condition) Statements The statement may be a single statement or a block of statements that is to be repeated. program control passes to the line after the loop code. The condition may be any expression. If the condition is true then statement 1 is executed else statement 2 is executed (if it exists).com . if its value is true. Else Statement 2.For More Notes and questions log on to www. The statements are executed while the condition is true. LOOPS IN C WHILE LOOP While loop provides the mechanism for looping as long as a specified condition is met. The if statement allows conditional execution of a group of statements. then the following statements are executed.technicalsymposium.com CONDITIONAL STATEMENTS If (condition) Statement When an if statement is encountered in a program. This is the most widely used iteration construct. with true being any non-zero value.technicalsymposium. If-else Statement SYNTAX If condition Statement 1. condition is evaluated. 4 www. The for loop supported by C is much more powerful than its counterpart in any other programming language.

h> # include<conio. int a[25]. seven=0. The condition is a relational expression that determines when the loop will terminate. sodd=0. clrscr().exp3) { statements. printf(“\n The Sum of Odd number is %d”. } printf(“\n The Sum of Even number is %d”.technicalsymposium.i<n. exp2 is condition checking. printf(:\n Enter the total number to be entered:”).For More Notes and questions log on to www. else sodd=sodd+a[I]. getch(). scanf(“%d”.technicalsymposium. } 5 www. i. Write a C program to determine the sum of odd and even numbers. exp3 is either an increment or decrement statement.sodd).h> main() { int n.com SYNTAX For (exp1.com .seven). The initialization is usually an assignment statement that is used to set the loop control variable.&n). printf(“\n Enter the values”). # include<stdio. ……………. The increment determines how the loop control variable change each time the loop is repeated. for(i=0. 1. } Generally exp1 is an initialization.i++) { if(a[ i]%2==0) seven=seven+a[i].exp2.

Write a C program to count the number of positive.npos). else nzero=nzero+1. i.h> #include<conio.technicalsymposium. # include <stdio. } printf(“\n The number of positive value is %d”.h> main() { int n. printf(“\n The number of negative value is %d”. getch(). Write a C program for temperature conversion.&faren).For More Notes and questions log on to www. clrscr().com .cen.cen).&n).technicalsymposium. npos=0. printf(“\n Enter the farenheit value :”). printf(“\n Enter the values”). scanf(“%d”.i++) { if(a[ i]>0) npos=npos+1. printf(“\n The equivalent Centigrade value is %d”. nneg=0. } 3. nzero=0.nzero). for(i=0.com 2. } 6 www. negative and zero number in the given list of numbers.h> main() { int faren. int a[25]. clrscr(). scanf(“%d”. if(a[I]<0) nneg=nneg+1.i<n. getch(). printf(“\n The number of zeros is %d”. #include<stdio.h> # include <conio. cen=(faren-32)+5/9.nneg). printf(:\n Enter the total number to be entered:”).

technicalsymposium.technicalsymposium. e=(e*10)+s. clrscr().com . scanf(“%d”.h> #include<conio.h> main() { int n. getch(). p=p/10. clrscr(). e=0. i.e. Write a C program to find whether the given number is palindrome or not.h> #include<conio.com 4.i<=n/2. } 5. scanf(“%d”. p=n. #include<stdio.i++) { if(n%i= =0) printf(“\n the given number is not prime”). int p. } if(n%i) printf(“\n the given number is prime”). Write a C program to check whether the number is prime or not. while(p!=0) { s=p%10. break. i. #include<stdio.&n). printf(:\n Enter the total number to be entered:”). } 7 www.h> main() { int n. printf(:\n Enter the number :”).s.For More Notes and questions log on to www. for(i=2.&n).

For More Notes and questions log on to www.technicalsymposium.com if(e= = n) printf(“\n the given number is palindrome”); else printf(“\n the given number is not a palindrome”); getch(); } 6. Write a C program to find the sum of digits. #include<stdio.h> #include<conio.h> main() { int n,q,r,s=0; clrscr(); printf(“\n Enter the no"); scanf(“%d”,&n); while(n!=0) { q=n/10; r=n-q*10; s=s+r; n=q; } printf(“\n the sum of digits :%d”,s); getch(); } 7. Write a program to find whether the given number is perfect or not. #include<stdio.h> #include<conio.h> main() { int a = 0; int m; printf(“Enter a number to check whether it is a perfect number or not \n”); printf(“ Enter a number \n”); scanf(“%ld”,&n); for (m=0;m<n;m++)

8

www.technicalsymposium.com

For More Notes and questions log on to www.technicalsymposium.com { if (n % m = = 0 ) a = a + m; } if (a = = n) printf(“the given number is perfect number \n”); else printf(“the given number is not a perfect number \n”); getch(); 8. Write a program to find whether the given number is Armstrong or not. #include<stdio.h> #include<conio.h> main() { int s = 0; int c= 0; int m,n,b; printf(“Enter a number to check whether it is a perfect number or not \n”); printf(“ Enter a number \n”); scanf(“%ld”,&b); n = b; while (b>0) { c = b % 10; s = s + (c*c*c); b = b / 10; } if (s = = n) printf(“the given number is armstrong number \n”); else printf(“the given number is not a armstrong number \n”); getch(); } 9. Write a C program to find the given number using linear search method. #include<stdio.h> #include<conio.h> main() { int n,a[30],sea,flag; clrscr();

9

www.technicalsymposium.com

For More Notes and questions log on to www.technicalsymposium.com printf(“\n Enter the number of terms :”); scanf(“%d”,&n); printf(“\n Enter the values:”); for(i=0;i<n;i++) scanf(“%d”,a[i]); printf(“\n Enter the number to be searched :”); scanf(“%d”,&sea); for(i=0;i<n;i++) { if(a[i] = = sea) { flag=1; break; } else flag=0; } if(flag= = 1) printf(“\n The given number %d is present in the position number %d”,sea,i); else printf(“\n The given number is not present”); getch(); } 10. Write a C program to find the given number using binary search method. #include<stdio.h> #include<conio.h> main() { int n,a[30],sea,flag,x,y,t; int low,high,mid; clrscr(); printf(“\n Enter the number of terms :”); scanf(“%d”,&n); printf(“\n Enter the values:”); for(i=0;i<n;i++) scanf(“%d”,a[i]); for(x=0;x<n-1;x++) for(y=x+1;y<n;y++) { if(a[x]>a[y]) {

10

www.technicalsymposium.com

H> #include <CONIO. a[x]=a[y]. void fibo(int). if(t= = a[mid]) { printf(“\n the number %d is present in the position %d”. } if(mid = =1 | | mid= = n) break. high=n.technicalsymposium. if(t<a[mid]) high=mid-1.com . for(x=0. } 11. flag=0.t.technicalsymposium. a[y]=t. } if(flag) printf(“\n The given number is not present”). scanf(“%d”. low=0. break.&sea).H> void main() { int n.mid).x++) printf(“%d\n”. getch().For More Notes and questions log on to www. printf(“\n Enter the number to be searched :”).a[x]). 11 www.x<n. Write a program to print fibonacci series using functions #include <STDIO. if(t>a[mid]) low=mid+1. } } printf(“\n The sorted numbers are :”).com t=a[x]. while(low<=high) { mid=(low+high)/2.

st. fibo(n). getch(). scanf(“%d”. clrscr().I.row.technicalsymposium. printf(“\t %d \t %d”.&co).ct. printf(“Enter Column order of Matrix A : “). Program to perform the matrix additions #include <stdio.ct). scanf(“%d”. I++.technicalsymposium.k. st = ct. } 12 www.For More Notes and questions log on to www. st = 1.ft. scanf(“%d”.j.b[10][10]. ft = st.&row). ft = 0. printf(“\n Enter the number of terms to be in the series \n “). printf(“Enter Row order of Matrix A : “).&r). printf(“\t\t Matrix Addition\n”).com clrscr().c[10][10]. while(I<=num-2) { ct = ft + st. if ((row!=r) || (col != co) ) { printf(“Matrix Multiplication is impossible\n”). printf(“Enter Column order of Matrix B : “).&col).st).com .ft. } void fibo(int num) { int I=1.co. printf(“\t%d”.col.&n). printf(“\t\t PROGRAM TO PRINT THE FIBONACCI SERIES \n”).h> #include <conio.r. } } 12. scanf(“%d”. printf(“Enter Row order of Matrix B : “). getch(). scanf(“%d”.h> void main() { int a[10][10].

com else { printf(“Enter First Matrix Elements : “).I<r.I++) { fact = fact * I.c[I][j]). } void factorial(double x) { double fact = 1.&b[I][j]).I++) for (j=0.&a[I][j]).com .I++) for (j=0. factorial(n).I++) { for (j=0. for (I=0. printf(“program to print the factorial\n”). printf(“\n\n Enter the number : “).technicalsymposium.h> #include <conio.I<row.technicalsymposium. scanf(“%d”.j++) scanf(“%d”. Program to print the factorial number #include <stdio. } } 13 .&n).j<col.I++) for (j=0.For More Notes and questions log on to www.j++) c[I][j] = a[I][j] + b[I][j].fact).j<col. for (I=0. for(I=1.j++) { printf(“%t%d”. for (I=0.h> void main() { int n.I<=n. printf(“Enter Second Matrix Elements : “).I<row.j++) scanf(“%d”. getch(). for (I=0. } pritnf(“\n”). printf(“The factorial of a given number is %d\n”. printf(“The resultant matrix is \n”).j<co. } 13 www.I<row.j<col. clrscr().

int *pv.to). char from. &ws. int ws = 0. int n. int *po). transfer(n.&n). 14 www.h> #include <conio. } void transfer(int n. int digits = 0. white space characters and in a line of text using pointers #include <stdio.com 14.to. &vowels.n.’c’). int *pc. clrscr(). scan_line(line. transfer(n-1. printf(“Move disk %d from %c to %c \n”.from.’r’.char). } return. printf(“\t\t TOWERS OF HANOI \n”).temp. char temp) { if(n>0) { transfer(n-1.to). char to. &cons.For More Notes and questions log on to www.from).char.h> void main() { void transfer(int. Program to implement the Tower of Hanoi #include <stdio. int cons = 0. consonants. getch(). void scan_line(char line[]. &digits. scanf(“%d”. Program to count the number of vowels. int other = 0. int vowels = 0. } 15.’1’.char.technicalsymposium. &other). digits.h> main() { char line[80]. printf(“How many disks ? “).temp. printf(“Enter a line of text \n”).technicalsymposium.line). int *pw. int pd.com . scanf(“%[^\n].from.

} void scan_line(char line[]. return(0).x=1. int count = 0.cons.j. for(i=0. else if (c = = ‘ ‘ | | c = = ‘\0’) ++ *pw. clrscr(). } getch().technicalsymposium. } 16. while((c = toupper(line[count])) != ‘\0’) { if (c = = ‘A’ | | c = = ‘E’ | | c = =’I’ || c = = ‘O’ || c = = ‘U’) ++ *pv. } return.j<=i.i++) { for(j=0.h> void main() { int n. printf("\t\t\t===============\n"). else ++ *po.&n).ws.x).i<n.other). else if ( c > = ‘0’ && c < = ‘9’) ++ *pd . Program to implement to Floyds Triangle #include <stdio.digits. x++. ++ count. else if (c > = ‘A’ && c < = ‘Z’) ++ *pc.com printf(“%d %d %d %d %d”.com .technicalsymposium. int *pc. int *pv.For More Notes and questions log on to www. } printf("\n").j++) { printf("%4d". printf("Enter the no of Lines:"). scanf("%d". printf("\t\t\tFloyds Triangle\n").vowels.int *po) { char c. int *pw.i. int *pd. } 15 www.

j. x=3. else i=(i*(j-m+1))/m.j<n.technicalsymposium. scanf("%d".k>0.com 17. Program to implement sine series #include <stdio.&n).&terms).k. int terms.sum=0. } 18.m<=j.h> void main() { int i=1. Program to implement to Pascal Triangle #include <stdio.com . printf("\nEnter the number of terms:").sign=1.++j) { for(k=35-2*j.14/180*d. printf("%4d".m. for(m=0.k--) printf(" "). } getch(). printf("\t\t\tPascal Triangle\n"). printf("\nEnter the X value:"). clrscr().i).fact(int). printf("Enter the no of Lines:").++m) { if((m==0)||(j==0)) i=1. scanf("%d". clrscr(). scanf("%f". } printf("\n").h> #include <math. printf("\t\t\t Sine Series \n").For More Notes and questions log on to www.&d).n. for(j=0.technicalsymposium. 16 www.i. printf("\t\t\t =========== \n"). printf("\t\t\t===============\n").x.h> void main() { float d.

nameff[80]. Programs on Manipulations on strings #include <stdio.i+=2) { sum=sum+sign*pow(x.technicalsymposium. scanf("%d". scanf("%s".com .i<=n. printf("2. int i.l.h> void main() { int ch.Reverse\n").l1.Length\n").sum). clrscr().For More Notes and questions log on to www.Replace\n"). printf("Enter the first string \n").com for(i=1. scanf("%s". } 19.namer[80]. printf("1.++i) f*=i. return(f). printf("Choice:"). printf("\t\t\tManipulations on Strings\n"). sign=-sign.i.&ch).Find\n").ans='y'. switch(ch) { case 1: { printf("\t\tConcatenation\n").sign.name1[80].k.m. printf("Enter the second string \n"). while(name[i]!='\0') 17 www. printf("5.2f)is %8.c. for(i=1. printf("\t\t=============\n").technicalsymposium.name2[80].Concatenation\n"). printf("\t\t\t========================\n"). i=j=0.4f". getch(). } float fact(int n) { float f=1. printf("3.name). printf("4.j.name1). char name[80].i)/fact(i).i<=terms.d. } printf("\nThe value of sine(%4.

i=j=0. for(i=0. printf("Enter the string \n"). printf("\t\t====\n"). l=strlen(name).++i) { c=0. j++. printf("\t\t=======\n"). 18 www. scanf("%s".technicalsymposium.name). printf("Resultant String in name2 is%s".For More Notes and questions log on to www. break.name1). sign=0. l1=strlen(name1).name1).com . break. } while(name1[j]!='\0') { name2[i]=name1[j]. printf("\nThe reversed String is%s". i++. scanf(" %[^\n]".name2).com { name2[i]=name[i].i<l. } name2[i]='\0'. } case 2: { printf("\t\tReverse\n"). printf("\nEnter first string:"). name1[j]='\0'. } case 3: { printf("\n\t\tFind\n"). while(name[i]!='\0') i++.technicalsymposium. printf("Enter search string:"). while(--i>=0) name1[j++]=name[i]. scanf(" %[^\n]".name). if(name[i]==name1[c]) { m=i. i++.

for(k=0. } if(sign!=0) printf("The given string is not present"). j=0.++k) name2[k]=' '. } if(sign==0) { printf("The given string is present"). while(i<l) { j=0. fflush(stdin). } else sign=1. } } if(k<0)break. puts("Enter replace string:"). exit(1). l=strlen(name). } case 4: { i=0. c++. 19 www.name). printf("\nIts starting position is%d"." ").i+1). scanf(" %[^\n]".For More Notes and questions log on to www.namer).technicalsymposium. k=-1.com . fflush(stdin). strcpy(nameff." ").com while(name1[c]!='\0'&&sign!=1) { if(name[m]==name1[c]) { m++. scanf(" %[^\n]".k<80. fflush(stdin). scanf(" %[^\n]". puts("Enter the string:"). puts("Enter find string"). break. strcat(namer.name1).technicalsymposium.

name). ++j. strcat(nameff.name1))==0) { strcat(nameff." ").com while(name[i]!=' '&&name[i]!='\0') { name2[j]=name[i]." ").For More Notes and questions log on to www. } } puts("string after replacement"). strcat(nameff. } name2[j]='\0'. } else { strcat(nameff.technicalsymposium. break. scanf(" %[^\n]". puts(nameff). } 20 www.name2). } case 5: { i=0. printf("Enter String:"). if((strcmp(name2. ++i.i). printf("\nThe length of the given string is%d".technicalsymposium. break. } } getch(). while(name[i]!='\0') i++.com .namer). ++i.

To see the program’s output select Window | User screen or press Alt+F5 Key. 2. developed the programming language C in 1972. Normally the editor is provided along with the compiler. Or press F3 Key 3. To implement the above steps. We may compile and run the programs from the Dos command Line like tcc Filename <Enter>. we may run it and view the output by typing Filename <Enter> Problem solving using computer: To solve a problem using a computer.com Data Structures An introduction to C: Dennis Ritchie at AT & T Bell laboratory. Run the program by selecting Run | Run. Murray Hill. After the program is compiled.For More Notes and questions log on to www. From the Ms Dos prompt start C by typing ‘tc’. the programmer develops a program and the developed program is entered and edited with the help of an editor. 21 www. Or press Ctrl+F9 Key 4.technicalsymposium. The languages BCPL and B mainly influenced it. Open a file by selecting File | Open | File name from the IDE menu.technicalsymposium. the following steps are required : A program is developed using a high level programming language (program development) The developed program is entered into a commuter (Program editing). New Jersey. After editing the program. the compilation commands us used for the translation process. The edited program is translated and is produced as an executable machine code. It was named as C to present it as the successor of B language which was Designed earlier by Ken Thompson in 1970 for the first UNIX system on the DECPDP-7 Computer. How to run C program: 1. The Executable machine code is run in the computer to carry out the actual task (execution). Then the execution command is used to run the program to get the desired output.com .

Object code is an intermediate code between the source code and the executable code. Linking: Linker performs the linking of libraries with the object code. When running a program.com . Sometimes programs are divided into modules and these modules are compiled separately and then linked by the linker and executed. High-level languages are machine independent. Since a computer system cannot follow programs written in a high language. the following files will be created automatically. OBJ (Object file) EXE (Executable file) Bak (Backup file) SWP (Swap file) Data Structures Definition Data Structure is a specialized format for storing data so that the data’s can be organized in an efficient way.com Compilation: High-level languages allow some English –like words and mathematical expressions that facilitate the better understanding of the logic involved in a program. which produces an executable machine code.technicalsymposium.technicalsymposium. Translation of a high-level language program to allow level language program is done by software known as Compiler. to make the generated object code into an executable machine code. Thus the object code becomes an input to the linker.For More Notes and questions log on to www. Classification Primitive Example: • Integer • Real • Character • Pointer • Logical Non – Primitive • • Linear Example: •Linear List Stack Queue Non – Linear Example: • Graph •Tree 22 www. high language programs are translated into lowlevel language programs and then executed.

Head → next = head 23 www. The operations done on a list are: Insertion Deletion Insertion Insertion in the head node To insert a node in the head node.com Array An array is a finite collection of similar elements stored in contiguous location. Let the new node be ‘Temp’ and the head node be ‘Head’. They are: Single Linked List Doubly Linked List Singly Circular Linked List Doubly Circular Linked List Single Linked List Node structure Data Field Pointer Field The data field contains the data elements that have to be stored in the list. then the insertion is Temp → data = X.com .technicalsymposium. just change the pointer field of the new node to point to the head node.For More Notes and questions log on to www. The pointer will point the next node in the list. The operations done on an array are: • Insertion • Deletion • Changing a particular element Linked List There are three types of linked lists.technicalsymposium.

‘next’ to the pointer field. ‘next’ to the pointer field. and then the deletion is Head → next = head Deletion in the middle node To delete a node in the middle we need to change two pointers. just point the head node as the second node. The pointers used are ‘data’ for the data field. the data to be inserted is ‘X ’then the insertion is Present → next =Temp Temp → next =null Temp → data = X Deletion Deletion in the head node To delete a node in the head node. the data to be inserted is ‘X ’then the insertion is Temp → data = X Present → next = temp Temp → next = future Insertion in the last node To insert an element in the last position just change the pointer field of the present last node to point to the new node. the data to be inserted is ‘X ’then the insertion is 24 www. ‘next’ to the pointer field. The pointers used are ‘data’ for the data field. Let the node to be deleted is ‘Temp’ and the node previous to the node to be deleted is ‘Present’ and. Let the new node be ‘Temp’ and the present node is ‘Present’. the next node to the present node is ‘future’. The pointers used are ‘data’ for the data field. then set the pointer field of the new node to NULL. Let the head node be ‘Head’. The pointers used are ‘data’ for the data field. the next node to the present node is ‘future’.com .For More Notes and questions log on to www. Let the last node be ‘Temp’ and the previous node is ‘Present’.technicalsymposium. Let the new node be ‘Temp’ and the present node is ‘Present’ and. ‘next’ to the pointer field.technicalsymposium.com Insertion in the middle node To insert in the middle node we need to change two pointers. the data to be inserted is ‘X ’then the insertion is Present → next = future Deletion in the last node To delete an element in the last position just change the pointer field of the previous node to the last to null.

The two pointers used for accessing is top & bottom pointer. Queue The information in this list is processed in the same order as it was received.For More Notes and questions log on to www.com . The pointers used are ‘data’ for the data field. and then change the pointer field of the new node to point to the first node. This type of 25 www.technicalsymposium. ‘next’ to the pointer field.technicalsymposium. the new node to be inserted to be new. change the pointer field of the previous node to the current last to point the first node. If top<= we can not delete. due to this circular arrangement the traversing become quite easier. PUSH – Storing the element intoBottom Pointer the stack. Check top<= allowed size if yes increment the top position and store the value in the top position. Let the last node be last. Otherwise decrement the top by one and return the top+1 element. the previous node to the current last node to be pre.com Previous → next =NULL Singly Circular Linked List The advantage of using Circular Linked List is the last null pointer is replaced and the pointer field of the last node points to the first node. that is first in first out order (FIFO) or a first – come first – served (FCFS) basis. The insertion and deletion in the first and middle are same as singly linked list except the last node. the data to be inserted is ‘X ’then the deletion is Prev → next = first Stack An important subclass of lists permits the insertion and deletion of an element to occur only at one end. insert the new node after the current last node. POP . the data to be inserted is ‘X ’then the insertion is Last → next = new New → next =first Deletion • Deletion in the last node To delete a node in the last position. Let the last node be last. the first node in the list to be first. Insertion • Insertion in the last node To insert a node in the last position. The deletion is referred to as ‘pop’.Deleting the element from the stack. A linear list of this type is known as ‘stack’. The pointers used are ‘data’ for the data field. the first node in the list to be first. ‘next’ to the pointer field. The insertion is referred to as ‘push’.

Deletion : If front =0 then queue underflow Else Increment the front pointer and return the front-1 value Tree An important class of digraph. Binary Tree If a tree has at most of two children. A directed tree is an acyclic digraph which has one node called root with in degree 0.com frequently used list is known as queue.technicalsymposium. 4. Pre – order method In – order method Post – order method Converse Pre – order method Converse In – order method Converse post – order method 26 www. We have two pointers to access the queue. 6. If the elements in the binary tree are arranged in the following order Left element is lesser than the root Right element is greater then the root No duplication of elements Then such binary tree is called as Binary Search Tree Operations performed in a binary tree are: o Inserting a node o Deleting a node o Traversing the tree Traversing Methods 1. 2. If the ordering of the node at each level is prescribed then the tree is called as ordered tree. They are 1. then such tree is called as Binary tree. The length of the path from root to particular node level of the node. The node with out degree as 0 is called as leaf. Front (used for deletion) 2. while other nodes have in degree 1. 5. Rear (Used for insertion) Insertion : if rear>n queue overflow else increment the rear pointer and insert the value in the rear position.For More Notes and questions log on to www. which involves for the description of hierarchy.technicalsymposium. An isolated node is also called as directed tree. Every directed tree must have at least one node.com . 3.

or leave in the same order as the original permutations. Traverse the left sub tree 3.technicalsymposium. What about equal keys? – May be we need to sort on secondary keys. Process the root 2.technicalsymposium. Traverse the right Sub tree 3. Supposedly. the most fundamental algorithmic problem 1. What is sorting? It is the problem of taking an arbitrary permutation of n items and rearranging them into the total order. Traverse the left sub tree 2. Process the root Sorting Sorting is. Issues in Sorting Increasing or Decreasing Order? . Sorting is fundamental to most other algorithmic problems. Traverse the left sub tree 2. 3.For More Notes and questions log on to www.com . 27 www. and these ideas can be used to solve many other problems.com Pre – order method This method gives the tree key value in the following manner: 1. Traverse the right Sub tree Post – order method This method gives the tree key value in the following manner: 1. 25% of all CPU cycles are spent sorting 2. Traverse the right Sub tree In – order method This method gives the tree key value in the following manner: 1. Process the root 3. without doubt. for example binary search.The same algorithm can be used by both all we need do is change to in the comparison function as we desire. Many different approaches lead to useful sorting algorithms.

For example if we remove 9 9 /\ 8 2 /\ 6 4 then we reheapify by copying rightmost leaf to root (4 becomes the root) 4 /\ 8 2 / 6 28 www. higher numbers give better service highly dynamic.Alphabetizing is sorting text strings.b) should return ``<''. Applications of Sorting One reason why sorting is so important is that once a set of items is sorted. messages coming and going frequently need efficient insert new message and remove highest priority message Removal causes heap to be reheapified.technicalsymposium.For More Notes and questions log on to www. John? We can ignore all three of these issues by assuming a comparison function which depends on the application. etc. Example: messages processed by priority at a server • • • messages given priority weighting. many other problems become easy.com . Below is a heap. Is Brown-Williams before or after Brown America before or after Brown. 9 /\ 8 2 /\ 6 4 A heap provides a representation for a priority queue. and libraries have very complicated rules concerning punctuation. ``>''. or ''=''.technicalsymposium.com What about non-numerical data? . Compare (a. Heaps A heap is a complete binary tree with values stored in its nodes such that no child has a value bigger than the value of the parent.

we divide the array of items to be sorted into two partitions and then call the quick sort procedure recursively to sort the two partitions. i. So overall the algorithm has a worst case time complexity of O(log n). Hoarer. 8 /\ 4 2 / 6 Then we would swap 4 with 6. As we will see. It has two phases: • • The partition phase and The sort phase. we divide the problem into two smaller ones and conquer by solving the smaller ones.technicalsymposium.A.technicalsymposium. So in the above tree. The algorithm just traverses one path in the tree.com . Quick sort is a very efficient sorting algorithm invented by C. most of the work is done in the partition phase . For each node on that path it performs at most two comparisons and one swap (3 operations -> constant time). This makes Quick sort a good example of the divide and conquers strategy for solving problems. The sort phase simply sorts the two smaller problems that are generated in the partition phase.For More Notes and questions log on to www. which is O(log n) in length. first we would swamp 4 with 8.) In quick sort.it works out where to divide the work. swap the parent with the highest priority child. (You've already seen an example of this approach in the binary search procedure.com and then we recursively reestablish the heap property as follows: if the parent is greater than a child.R. Keep swapping until no more swaps are possible. 8 /\ 6 2 / 4 The final swap yields a heap! The cost of removing an item (reheapifiying after removing the item) is O(log n). Space complexity is O(n) since a sequential array representation can be used. Thus the conquer part of the quick sort routine looks like this: 29 www.e.

First Partition Sort Left Partition in the same way For the strategy to be effective.For More Notes and questions log on to www. Frequently the graph searches start at an arbitrary vertex. we choose a pivot element and arrange that all the items in the lower part are less than the pivot and all those in the upper part greater than it.com quicksort( void *a. pivot-1 ). where n is the number of vertices and m the number of edges. int high ) { int pivot. In the most general case. depth-first and breadth-first search.technicalsymposium. Dijkstra) solves the problem of finding the shortest path from a point in a graph (the source) to a destination. 30 www. int low. for example a win position in a game. Graph Traversal Systematic traversals of graph are similar to preorder and post order traversal for trees. high ).technicalsymposium. The searches are efficient if they are done in O(n + m). Dijkstra's Algorithm Djikstra's algorithm (named after its discover. To do this. quicksort( a. or to solve a specific problem on a particular graph. /* Termination condition! */ if ( high > low ) { pivot = partition( a. low. quicksort( a. } } Initial Step .the first element is a convenient one. hence this problem is sometimes called the single-source shortest paths problem. pivot+1.com . we don't know anything about the items to be sorted. Graph traversal can be used to determine the general characteristic of the graph. It turns out that one can find the shortest paths from a given source to all points in a graph in the same time. low. E. for example: • • • Routing phone calls. or packets Planning a car trip Locate particular vertices. There are two graph traversals. the partition phase must ensure that all the items in one part (the lower part) and less than all those in the other (upper) part. so that any choice of the pivot element will do .W. high ).

called the top of the stack.com . queues and linked lists.com Depth-first Search We start the graph traversal at arbitrary vertices. The linear data structures are stacks. Let us draw a stack containing integers as in the following figure. Sparse Matrix : A matrix consists of more number of zeros is called sparse matrix. strings and records with which we can build new types called structured or composite types such as stacks. Once the matrix is stored as it is then there is wastage of memory. char and double. and trees etc. Data structures are sometimes called Data types. The non-linear data structures are trees and graphs.For More Notes and questions log on to www. is a pile of coins. Second example could be a pile of trays or a books lying one above the other. Stacks Definition: A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end.technicalsymposium. which permits the selection of only its end element . The first example of stack. DATA STRUCTURES Definition: Data structure is collection of data elements organized in a specified manner and accessing functions are defined to store and retrieve individual data elements. The structured data types can be categorized as linear and non-linear. queues. There are four basic or atomic or primitive data types in C. Then we go deeper in one of the adjacent vertices. For an efficient memory utilization the sparse matrix can be stored in a linear form. Breath-First Search Breadth-first search visit all adjacent vertices before going deeper. and all edges. The linear form can be of array type or linked list type. They are int. Classification of Data Structure: A data type may be defined as a set and the elements of the set are called the values of the type. and go down a particular branch until we reach a dead end. The Simple data types built from primitives are arrays . Then we back up and go as deep possible. pointers. float. 5 9 1 3 7 top 31 www.technicalsymposium. In this way we visit all vertices.

It returns true if stack is empty and returns false otherwise.technicalsymposium. we must ensure that the stack is not empty. 5 is the current of the stack. Let us see how stack shrinks or grows when we pop or push an element in the following figures. PUSH & POP OPERATIONS: When we add an element to a stack. e) empty(s) .technicalsymposium. Therefore.To insert the element I on top of the stack s.To create s as an empty stack. We can define the following necessary operations on stack. . push(s. there are several primitives operations. Push (8) on the stack 8 5 9 1 3 7 top Push (4) on to stack 4 8 5 9 1 3 7 top 32 www. it is not possible to pop the stack.com Here. If a stack is empty and it contains no element.For More Notes and questions log on to www. we say that we pop it from the stack. pop(s) top(s) . it will be 5. Operations on Stacks: Associated with the stack . If we add any element in the stack. it will be placed on top of 5 . and if we delete an element .To return the top element of stack(s).To check whether the stack is empty or not.i) . before popping an element. a) b) c) d) create(s) .com . we stay that we push it on the stack and if we delete an element from a stack. which is on top of the stack.To remove the top element of the stack and to return the removed element as a function value.

technicalsymposium.technicalsymposium. which keeps shifting constantly as items are popped and pushed. We must store the index of the array containing the top element. Array: Since the elements of the stack are ordered . The other end of the array may be used as a top of the stack. That is why stack is called last in. Implementation of Stacks There are two ways to implement stacks. first out or LIFO in short.com .For More Notes and questions log on to www. 33 www.com Pop an element from the stack 8 5 9 1 3 7 Top Popped element = 4 Pop an element from the stack 5 9 1 3 7 Top Popped element = 8 We may notice that the last item pushed onto a stack is always the first that will be popped from the stack. an obvious choice would be an array as a structure t contains a stack. We can fix one end of the array as bottom of the stack. one using arrays and other is using linked list.

therefore. If there are five elements in the stack.technicalsymposium.technicalsymposium. the value of top will be four and the top element is in elements[4]. To do this we must check whether s.For More Notes and questions log on to www. exit(1). { ts->top = -1. Here s is defined to be a stack containing elements of type integer . We can write our function clearstack as clearstack(ts) struct stack *ts.com We can . struct stack s. # define MAX 50 struct stack{ int top. int elements [5]. A stack is empty when it contains no elements we can indicate this by making top as –1. Let us now consider the PUSH operation . 34 www.an array to hold the elements of the stack. The maximum number of elements in the stack is defined to be 50. We might code the PUSH & POP operations as follows: push(ts. put the new element at the new top. }. increment top indicator ii.com . Int x.top = = -1. declare a stack as a structure containing two fields. “ Stack overflow”).x) Struct stack *ts. and an integer top to indicate the position of the current top of the stack within the array. { if (fullstack(ts)){ printf( “ %s”. } Another operation is to check whether the stack is empty. Elements [0] contain the first element so that the value of top is 0. To push or add an element we must perform the two steps: i.

com } else ts->elements[++(ts->top)] = x.com . } To remove an element or pop an element from the stack. { if (empty(ts)) printf( “ % s” . else return(0). } We can write function empty (s) that returns 1 if the stack is empty and 0 if it is not empty as follows: empty(ts) struct stack *ts.technicalsymposium. else return(ts->elements[ts->top--]). we can write function POP as. Full Stack routine as follows: fullstack (ts) struct stack *ts. else return(0).technicalsymposium. return(0). return.elements at the new top position. } This routine increments the top by 1 and puts x into array s. we must first check the possibility of underflow as it is quite possible that somebody tries to pop an element from an empty stack. A stack is full when ts->top = = MAX-1. before we push an element onto stack. In this routine we use another routine Full Stack which checks whether the stack is full. Therefore.For More Notes and questions log on to www. { if ( ts -> top = = -1) return (1). “ stack underflow”). } 35 www. Pop(ts) struct stack *ts. { if ( ts->top = = MAX-1) return(1).

Node structure: Each node has two fields.technicalsymposium.h> # include < process.e. We can declare such as a stack as follows.For More Notes and questions log on to www. node * next. public : void push(). node temp. void disp().com Stack as a Linked List ( Using Pointers): Using this representation we are using the pool of available nodes and we will never have to test whether a particular stack is full.h> class sta{ struct node { int data.Node representation: A Stack Top element Declaration : ( Using C++) # include <iostream. i. cout << “ Push the element “ << endl. temp = new node. } *stack . 3 ( First node of 36 www.technicalsymposium.com . void pop(). } B C D End node PUSH OPERATION: Void sta :: push() { int n. Data and Next field Data field Next field Stack.

then there are six possible combinations of tree traversal: LVR. and R stand for moving left. respectively. else { temp->next=stack.com . we want to treat each node and its sub trees in the same fashion. and moving right when at a node. To these we assign the names inorder. else { stack temp= stack. temp cout << “Popped element “ << endl. VLR. delete temp. if (stack= = NULL) cout << “ Stack is empty “ << endl. and RLV.technicalsymposium. 37 www. LRV and VLR. if(stack= = NULL) stack=temp. stack=temp.technicalsymposium. and preorder. } } 4 stack 3 stack 3 stack 4 4 temp the stack).com temp cin >> temp->data. postorder. because of the position of the V with respect to the L and the R. } } TREE TRAVERSAL: When traversing a binary tree.For More Notes and questions log on to www. visiting the node. V. temp->next=NULL. cout << temp->data. LRV. If we adopt the convention that we traverse left before right. POP Operation: stack 2 4 3 Void sta :: pop() { temp node *temp. then only three traversals remain : LVR. RVL. VRL. If we let L. stack= stack->next.

3.com . nodeptr->data). Example: Fig.com Procedure for Preorder: 1.1 The result is : + A B Algorithm: void preorder(node *nodeptr) { if ( nodeptr != NULL) { printf(“%d\n”. /* Traverse the right sub tree */ } } Procedure for Inorder: 1. /* Traverse the left sub tree */ perorder(nodeptr->right).2 Traverse the Left sub tree in inorder. Visit the root node Traverse the Right sub tree in inorder. Fig.technicalsymposium.For More Notes and questions log on to www. 3. Visit the root node.technicalsymposium. /* visit the root node */ preorder(nodeptr->left). 38 www. 2. Traverse the Right sub tree in preorder. 2. Traverse the Left sub tree in preorder.

} /* Traverse the left sub tree */ /* Traverse the right sub tree */ /* Visit the root node */ 39 www.com . nodeptr->data). } } Procedure for Postorder: 1. Fig. 2.3 Traverse the Left sub tree in postorder.For More Notes and questions log on to www. inorder(nodeptr->right). printf(“%d\n”. Visit the root node. /* Traverse the left sub tree */ /* Visit the root node */ /* Traverse the right sub tree */ The result is : A B + void postorder( node * nodeptr) { if (nodeptr != NULL) { postorder(nodeptr->left).technicalsymposium.com The result is : A + B void inorder( node *nodeptr) { if ( nodeptr != NULL) { inorder(nodeptr->left). printf(“%d\n”. 3. postorder(nodeptr->right). nodeptr->data). Traverse the Right sub tree in postorder.technicalsymposium.

4 PRE ORDER IN ORDER POSTORDER : A.technicalsymposium.D. 40 www.E.A.technicalsymposium.com } Fig.C.B.F.For More Notes and questions log on to www.E.AND F : B.D.com .C.AND A Fig.F.5.E.C : D.B.

For More Notes and questions log on to www.technicalsymposium.com

PREORDER INORDER POSTORDER

: * +AB/CD : A+B*C/D : AB+CD/*

BINARY SEARCH TREES Definition: A binary search tree is a binary tree. It may be empty. If it is not empty then it satisfies the following properties: 1. 2. 3. 4. Every element has a key and no two elements have the same key. The keys in the left sub tree are smaller than the key in the root. The keys in the right sub tree are larger than the key in the root. The left and right sub trees are also binary search trees.

It has two operations. They are, 1. Insertion 2. Deletion

Example Fig.

41

www.technicalsymposium.com

For More Notes and questions log on to www.technicalsymposium.com

To construct (Insertion) the Binary search tree for the following elements: 25, 15, 27, 13, 17, 26, 29, 28

42

www.technicalsymposium.com

For More Notes and questions log on to www.technicalsymposium.com

43

www.technicalsymposium.com

com .technicalsymposium. the right-child field of 15 is set to 0 .For More Notes and questions log on to www. To delete a leaf node 28 To delete a leaf node 17 2. To delete 28 from the below tree the left child field of its parent is set to 0 and the node disposed. and the node containing 17 is disposed.technicalsymposium. To delete the 17 from this tree.com To delete a particular node from the Binary search tree: 1. Leaf node Deletion of a leaf node is quite easy. Non-leaf node: 44 www.

com . Then we proceed to delete this replacing element from the sub tree from which it was taken. 3.com The deletion of a non-leaf element or node that has only one child is also easy. and the single-child takes the place of the disposed node. to delete the element 15 from the above tree. So. 45 www.For More Notes and questions log on to www. Root node: When the element to be deleted is in a non-leaf node that has two children.technicalsymposium. The node containing the element to be deleted is disposed. the element is replaced by either the largest element in its left sub tree or the smallest one in its right sub tree. we simply change the pointer from the parent node (25) to the single-child node(13).technicalsymposium.

HEAPS Priority Queue: 46 www.technicalsymposium.For More Notes and questions log on to www.com . in its left sub tree or the smallest element . then we replace it by either the largest element.com If we wish to delete the element with key 25 from the above tree. The 17 is moved in to the root and the following tree is obtained. 26 .technicalsymposium. Suppose we opt for the largest element in the left sub tree. 17 . in its right sub tree.

Heap 47 www. A descending priority queue is similar but allows deletion of only the largest item. There are two types of priority queues: An ascending priority queue and a descending priority queue.technicalsymposium. A min heap is a complete binary tree that is also a min tree. Heaps Definition: A max (min) heap is a tree in which the key value in each node is no smaller (larger) than the key values in its children (if any). Heap Min.com .technicalsymposium. An ascending priority queue is a collection of items into which items can be inserted arbitrarily and from which only the smallest item can be removed.com The priority queue is a data structure in which the intrinsic ordering of the elements does determine the results of its basic operations. Max.For More Notes and questions log on to www. A max heap is a complete binary tree that is also a max tree.

com . Fig.For More Notes and questions log on to www.com QUEUE Definition: A queue is an ordered collection of items from which items may be deleted at one end ( called the front of the queue) and into which items may be inserted at the other end ( called rear of the queue).2 3 6 8 OPERATION S ON QUEUES: It has two operations. They are 48 www.technicalsymposium. This data structure is commonly known as FIFO or first-infirst-out.1 3 6 Fig.technicalsymposium.

iv.8) 5 7 8 ENQ(q. CREATEQ(Q) – which creates Q as an empty Queue.which removes the element at the front end of the queue and returns the resulting queue as well as the removed element.9) 5 7 8 9 ENQ(q. EMPTY(Q). DEQ(Q).which returns the front element of the queue without changing the queue. v. i. FRONT(Q).com Insertion Deletion Insertion an element is popularly known as ENQ and deleting an element is known as DEQ. We assume that the queue in initially empty. ii.technicalsymposium.com .For More Notes and questions log on to www.4) 49 www. ENQ(i) – which adds the element I to the rear of a queue and returns the new queue. vi. QUEUESIZE(Q)-which returns the number of entries in the queue. iii. ENQ(q. We can obtain the queue by the following sequence of operations.technicalsymposium.It checks the queue whether it is empty or not and returns true if it is empty and returns false otherwise. A minimum set of useful operations on queue includes the following.

technicalsymposium.com .For More Notes and questions log on to www. Array : Let us implement the queue within an array so that the array holds the elements of the queue.com 5 7 8 9 4 x =DEQ(q) Element 5 is deleted 7 8 9 4 x =DEQ(q) Element 7 is deleted 8 9 4 IMPLEMENTING THE QUEUE There are two ways to implement queue. Initially let us assume that the queue is empty which means front = 0 and rear = -1. 50 www. and another is using Linked list.technicalsymposium. i. Let the size of the array be 4. There are two variables front and rear to indicate the positions of the first and last element of the queue within the array. one using arrays.

7) 3 q[0] 5 q[1] 7 q[2] q[3] front = 0 rear = 2 51 www. Now the queue becomes full.After we have added three elements to the queue rear becomes 2 and front becomes 0.technicalsymposium.technicalsymposium. the value of rear changes to 3. ENQ(q.com Empty Queue: q[0] q[1] q[2] q[3] rear = -1 ( NULL) front = 0(array position) Insertion: There are two variables front and rear to indicate the positions of the first and last element of the queue within the array.com . Now if we add one more elements to the queue from the rear.For More Notes and questions log on to www. Initially let us assume that the queue is empty which means front = 0 and rear = -1.5) 3 q[0] 5 q[1] q[2] q[3] front = 0 rear = 1 ENQ(q. Let the size of the array be 4.3) 3 q[0] q[1] q[2] q[3] front = 0 rear =0 ENQ(q.

The element which is deleted is 3. we delete one element.technicalsymposium.For More Notes and questions log on to www. X= DEQ(q) 5 q[0] x=DEQ(q) 7 q[0] q[1] q[2] 9 q[3] q[1] 7 q[2] 9 q[3] 5 q[1] 7 q[2] 9 q[3] front = 0 rear = 3 front = 1 rear = 3 front = 2 rear = 3 x=DEQ(q) 9 q[0] q[1] q[2] q[3] front = 3 rear = 3 52 www.com . If we wish to add another element.com ENQ(q.technicalsymposium. rear becomes equal to front. This leaves a hole in the first position. which indicates that the queue is full. we must check whether front = rear. and we increment rear by 1.9) [ Q is full ] 3 q[0] Deletion: At this point. To add an element we must increment rear so that it points to the location next to the rear and place an element in that slot of the array. To delete this element we must increment front. to indicate the true first element of the queue and assign the value of that slot to x. To check whether queue is empty or not.

cin>>item. if ( rear >= n) { cout << “ Queue is full \n”.com x=DEQ(q) [ Queue is empty] q[0] q[1] q[2] q[3] front = rear = -1(NULL) Therefore. Queue implementations in ARRAY using C++ class qu{ Public : Int front. 53 www. void get(){ cout<< “ Enter the Queue size “ << endl. q[10]. rear.technicalsymposium. front = rear =-1. rear = rear+1. cin>> n. the condition for full queue is that the next slot of rear is equal to front and the condition for empty queue is that front = rear. void deq(). n .For More Notes and questions log on to www.com . Before we DEQ an element from queue we must make sure that queue is not empty and before we ENQ an element we must ensure that the queue is not full. int I. }. a[10]. return. } void enq(). } else { cout << “ Enter the item to be inserted” <<endl.technicalsymposium. void qu :: enq(){ int item.

4 6 front rear . cout << “ The deleted element : “ << t << endl. } } Implementation of Queue as Linked list Another way of implementing queues is as a linked list. } } void qu :: deq() { int t.com q[rear] = item. front to the first element of the list and rear to the last element of the list. class que { struct node { int data.technicalsymposium. } else { front = fornt +1. Let us have two pointers. *rear. t = q[front]. 8 54 www. } * front. i++.technicalsymposium. if ( front >= rear) { cout << “ Queue is Empty” << endl.com .For More Notes and questions log on to www. node *next. return.

else 4 { rear->next = temp. temp-data = n.technicalsymposium. if ( front = = NULL) front = rear=temp. temp 4 front 5 rear 4 temp 6 front 8 if ( front = = NULL) cout << “ Queue is empty “ << endl. temp->next = NULL.com .com public: void insq(). void que :: insq() { int n. } } void que :: delq() { node *temp. front rear= rear->next.For More Notes and questions log on to www. void delq(). que(){ front = rear = NULL. } }. node *temp. cin >> n. delete temp. else { front = front->next. temp = new node.technicalsymposium. cout << “ Insert the element “ << endl. temp = front. } } DEQUE: rear 55 www. cout << temp->data.

com . in short is a linear list in which insertions and deletions are made to or from either end of the structure. the operating system adds the request at the end of the queue of jobs waiting to be executed. The output-restricted deque allows deletion from only one end and input-restricted deque allows insertions at only one end. which is a pointer.technicalsymposium. Whenever a user requests the system to run a particular program. Each device maintains its own queue of request. LINKED LIST Definition: A collection of node is called list. Similarly there are queues for sharing I/O devices. The first. namely. List ( External pointer) Data or Info Next 56 www. a character. a string or even a large record. which is at the front of the job queue. Deletion Insertion Front Rear Insertion Deletion We can have two variations of a deque. Each node or item in a linked list must contain at least two fields. A double ended queue or deque. it executes the job. field contains the actual element on the list which may be a simple integer. The second field.For More Notes and questions log on to www. Whenever the CPU is free. Queue is used for finding a path using breadth-first-search of graphs. Queues are also very useful in a time-sharing computer system where many users share the system simultaneously. Another useful application of queues is in the solution of problems involving searching a nonlinear collection of states.com A single queue behaves in a FIFO manner in the sense that each deletion removes the oldest remaining item in the structure. contains the address of the next node in the list used to access the next node. Queue Applications: The most useful application of queues is the simulation of a real world situation so that it is possible to understand what happens in a real world in a particular situation without actually observing its occurrence. an information field or data field and the next address field. A node of a linked list may be represented by the following figure. the input-restricted deque and the output –restricted deque.technicalsymposium.

List = null Also. known as the NULL value. To print all the elements of the list. if list = null then return(true) 57 www. then node(p) refers to the node pointed to by p info(p) refers to the data part of that node next(p) refers to the address part of that node info(next(p)) refers to the data part of the next node which node. We will draw linked lists as an ordered sequence of nodes with links being represented by arrows. We can access the first node through the external pointer. 5. Add new elements any in the list 3. Returns true if the list contains no elements.For More Notes and questions log on to www. We can initialize the list by making the external pointer null. We will introduce some notations to be used in algorithms: If p is a pointer to a node. which follows node(p) in the list if next(p) is not null.com . To determine if the list is empty. the second node through next pointer of the first node.com The entire linked list is accessed from an external pointer List pointing to the first node in the list. 4. To check if a particular element is present in the list. This is not a valid address. This only tells us that we have reached the end of the list.technicalsymposium. List 4 5 6 OPERATIONS ON LINKED LIST There are five basic types of operations associated with the list data abstraction: 1. the third node through the next pointer of the second node till the end of the list. To delete a particular element from the list placed anywhere in the list. The next address field of the last node contains a special value. 2.technicalsymposium. we can check whether the list is empty by checking whether the external pointer is null.

otherwise it will return false.com . p Getnode(p) p x Info(x) = p p x List 5 6 58 www.technicalsymposium. To set the data field of the new node to x iii.technicalsymposium. To traverse or to print the elements of a linked list. ii. P = list while list < > null do begin print( info(p)) p= next(p) end Inserting into a Linked list To add a new node containing data value x in the beginning of the list we need to follow the step: i. To set the next field of the new node to point to list iv.com else return(false) This routine will return true if the list is empty. we need to use a temporary pointer. To get a new node which is not in use. To set pointer list point to the new node.For More Notes and questions log on to www. To do this we can write the following algorithm: getnode(p) info(p) = x next(p) = list list = p We are assuming that the operation getnode(p) obtains an empty node and sets the contents of a variable named p to the address of that node. p known as a traversal pointer.

1 Program: #include<stdio. if(top>=n+1) printf("\nstack is overflow\n"). void main() { int n.PUSH\n").DISPLAY\n").&n). scanf("%d". No.temp. printf("Enter the no of elements\n").&x).x. do { printf("1.com .For More Notes and questions log on to www.h> #include<conio. Stack p 6 59 www.EXIT\n").b. printf("enter your choice\n"). scanf("%d". int a[20].i.technicalsymposium.com next(p) = list List 4 5 list = p Sample programs : Ex. clrscr().&b). printf("2.POP\n"). break. printf("3. top=top+1.technicalsymposium. printf("4. switch(b) { case 1: printf("enter the number\n").h> int top=1. a[top]=x.c. else scanf("%d".

} while(c==1).insertion\n").display\n").technicalsymposium.technicalsymposium. printf("%d". 2 Program: #include<stdio. break. getch(). printf("Enter the no of elements\n"). case 4: exit(0). do { printf("1.com break.&n).h> #include<conio. void main() { clrscr().i. temp=a[top]. } printf("\ndo you want to continue(1/0)\n").b.c. No.a[top-1]).com . } Ex.deletion\n"). else { top=top-1. scanf("%d".f=0. scanf("%d".i<top-1.For More Notes and questions log on to www.a[i]). Queue 60 www.te.x. case 2: if (top<=0) printf("stack is underflow\n").&c).r=0.h> int n. printf("4. case 3: for(i=1. int q[20]. } break. printf("2.exit\n"). printf("3.i++) printf("%d-->".

technicalsymposium. scanf("%d". q[r]=x.&b).&x). } int delet() { int te. return(0).com . display(). scanf("%d". break. } if(f==0) f=1. } printf("\ndo you want to continue(1/0)\n"). } while(c==1). getch(). case 4: exit(0). switch(b) { case 1: insert(). break. else { printf("enter the number\n").&c).com printf("enter your choice\n"). r=r+1. display(). scanf("%d". case 3: display(). } insert() { if(r>=n) printf("\nqueue is overflow\n"). if (f==0) 61 www.technicalsymposium.For More Notes and questions log on to www. case 2: delet(). break.

} else { for(i=f. No: 3 Program: #include<stdio.i<r.technicalsymposium.com printf("queue is underflow\n"). struct node *link.q[i]).*temp. struct node { int data. } display() { if(r==0) { printf(" queue is empty"). } Ex. printf("%d".*first. void main() { first=null. head=null.h> #define null 0 int a.r=0.com . } return(0). } return(te).h> #include<conio.q[r]). f=f+1. struct node *head.technicalsymposium.i++) printf("%d-->". else if (f==r) { f=0.*previous. } else { te=q[f].For More Notes and questions log on to www. }.s. Singly Linked list 62 www.

break.display\n").insert last\n"). break.&a).creation\n").delete first\n"). printf("5. case 2: display(). display(). printf("2. break. printf("7. break.delete middle\n"). scanf("%d".For More Notes and questions log on to www. case 8: delmiddle(). display(). case 5: insmiddle(). printf("3. do { printf("1. printf("4. printf("8.technicalsymposium. printf("enter your choice"). display(). switch(a) { case 1: create(). display().technicalsymposium. 63 www. case 7: dellast(). display().insert first\n"). case 3: insfirst(). printf("6. display().com clrscr(). case 4: inslast().delete last\n"). break. break. break.insert middle\n").com . case 6: delfirst().

} 64 www.temp->data). if(head==null) head=first. first->link=null. printf("enter the data"). while(previous->link !=null) previous=previous->link. first=(struct node*) malloc (s).com display().com . else { previous=head. while(temp!=null) { printf("%d->". scanf("%d". } create() { int s.technicalsymposium. return(0). } display() { if(head==null) printf("null first").&first->data). } printf("\ndo you want to continue(1/0)\n"). break. temp=temp->link. scanf("%d". else temp=head. } previous->link=first. } printf("null\n"). case 9: exit(0).For More Notes and questions log on to www. s=sizeof (struct node).&s).technicalsymposium. previous=first. return(0). } while(s==1).

else { s=sizeof (last). if(head==null) printf("list is null"). } return(0).&temp->data). head=temp. } inslast() { int s. temp=(struct node*) malloc(s). last->link=null. scanf("%d". last=(struct node*) malloc(s). printf("enter data\n"). scanf("%d".com . temp=head. temp->link=last.For More Notes and questions log on to www.&last->data). } 65 www. while(temp->link!=null) temp=temp->link. else { s=sizeof (temp). return(0). else head=head->link. printf("enter the data\n"). } delfirst() { int s.com insfirst() { int s.*last.technicalsymposium. temp->link=head. struct node *temp. if (head==null) printf("list is null").technicalsymposium. if (head==null) printf("list is null").

technicalsymposium. temp=(struct node*) malloc(s). count=count+1. } return(0). struct node *pre. next=head->link. struct node *next. count=2.com return(0). pre=pre->link.com . if (head==null) printf("list is null").For More Notes and questions log on to www.*nex. while(next->link!=null) { next=next->link. while((count<f) && (next->link!=null)) { next=next->link.&nex->data). printf("enter the data\n").m.count.technicalsymposium. } dellast() { int s. scanf("%d". scanf("%d". if(head==null) printf("list is null").*pre. } insmiddle() { int s. } 66 www. next=pre->link.f. pre=head. printf("enter the position of the element"). pre=head. else { next=head.&f).*next. } pre->link=next->link. pre=pre->link. else { s=sizeof (temp).

f. count=count+1. pre=pre->link.*nex.com if((count<f) && (next->link==null)) { printf("not possible to insert. the list is contains %d elements".*pre. printf("enter the position of the element"). the list is contains %d elements".count). } if((count<f) && (next->link==null)) { printf("not possible to insert. while((count<f) && (next->link!=null)) { next=next->link. else { s=sizeof (temp). next=pre->link.technicalsymposium. } return(0). } else { pre->link=nex. count=2.com . } 67 www. } delmiddle() { int s.&f). pre=head. } pre->link=next->link. temp=(struct node*) malloc(s). nex->link=next.count). if (head==null) printf("list is null").count. } } return(0). struct node *next. scanf("%d".For More Notes and questions log on to www.technicalsymposium.

new_elt=(list *)malloc(sizeof(list)). typedef struct student list. temp->next=new_elt. adj_elt->prev=new_elt. doubly linked list 68 www. while(temp->next!=NULL) temp=temp->next. struct student *next. } void insert(list *head.*adj_elt. new_elt->next=NULL. list *new_elt.h> #include<stdlib. }. No. 4 Program: 4.com Ex. for(i=1. #include<conio. new_elt->rollno=rollno.For More Notes and questions log on to www.technicalsymposium.int position) { int i.i<position. new_elt->prev=temp.technicalsymposium.h> #include<stdio.i++) temp=temp->next. adj_elt=temp->next.int rollno. Write a C program to implement the Double Linked List.*temp=head. struct student *prev. new_elt->rollno=rollno. new_elt=(list *)malloc(sizeof(list)).h> struct student { int rollno.int rollno) { list *new_elt.com . void add(list *head.*temp=head.

} return found. new_elt->prev=temp.*successor.*temp=head->next. temp=temp->next. int found=0.*predecsor.rollno). break.. predecsor->next=del_elt->next. del_elt=temp. } void print_list(list *head) { if(head->next!=NULL) 69 www. int i. } else printf("\nElement has not Found!Cann't perform Deletion!"). if(found!=0) { while(temp->rollno!=rollno) temp=temp->next.com . } i++. successor=del_elt->next. free(del_elt). temp->next=new_elt.technicalsymposium.For More Notes and questions log on to www. predecsor=del_elt->prev. } void removeElt(list *head. found=find(head. while(temp!=NULL) { if(temp->rollno==rollno) { found=i.int rollno) { list *del_elt.int rollno) { list *temp=head->next. successor->prev=del_elt->prev. } int find(list *head.com new_elt->next=adj_elt. printf("\nOne Element is deleted").technicalsymposium.found.i=1.

while(1) { printf("\n\n1. case 2: printf("\nEnter Rollno of Element to be Inserted:").technicalsymposium. scanf("%d". printf("\nThe List has been deleted!").&rollno). clrscr().&rollno). int position.com { list *temp=head->next. add(head. 70 www. } void make_emptylist(list *head) { head->prev=NULL. scanf("%d".Find\n5. head->prev=NULL. break. } else printf("\n The List is Empty").For More Notes and questions log on to www. switch(option) { case 1: printf("\nEnter Rollno of the New Element:"). scanf("%d". printf("\nThe List:\n"). head=(list *)malloc(sizeof(list*)). head->next=NULL.temp->rollno).&option).Insert a Item\n3.Exit"). printf("\nEnter your Choice:").rollno).Print the List\n6. head->next=NULL. } printf("Null").technicalsymposium. while(temp!=NULL) { printf("%d--> ".Add\n2. printf("\nEnter Position to insert:"). temp=temp->next.Remove a Item\n4.Delete the List\n7.com . } void main() { list *head.rollno.option.

insert(head. case 7: exit(0). else printf("\nElement has not found in the List!"). removeElt(head.s.*previous.position). case 5: print_list(head).rollno).h> int a.position). position=find(head.&rollno). struct node *link.technicalsymposium. break. } Ex. break. case 6: make_emptylist(head).rollno. void main() { 71 www. } } getch(). break. scanf("%d". if(position!=0) printf("\nElement has been found!Position=%d".rollno).technicalsymposium. break.h> #include<conio. case 3: printf("\nEnter the Rollno of the element to Removed:"). No: 5 Program : Circular Singly Linked list #include<stdio. case 4: printf("Enter rollno of Item to be found:"). }. scanf("%d".com scanf("%d".*temp. struct node { int data. struct node *head.For More Notes and questions log on to www.*first.&position).*last. break.com .&rollno).

case 5: insmiddle().&a). clrscr().insert middle\n"). display(). previous=NULL. display().delete last\n").For More Notes and questions log on to www. break. case 2: display(). break. display(). switch(a) { case 1: create(). do { printf("1. break. printf("7.technicalsymposium. printf("6. case 6: delfirst(). display().display\n"). printf("2. printf("5. printf("enter your choice").com . break. case 7: dellast(). scanf("%d".delete first\n"). head=NULL. case 4: inslast(). break.com first=NULL. display().creation\n").technicalsymposium.insert last\n"). break.insert first\n"). printf("3. 72 www. printf("8. printf("4. case 3: insfirst().delete middle\n").

technicalsymposium. first->link=first.&first->data). while(previous->link !=head) previous=previous->link.For More Notes and questions log on to www.com . printf("enter the data"). } last=first. previous->link=first.technicalsymposium. first=(struct node*) malloc (s). scanf("%d". break. } while(s==1). previous=first. case 8: delmiddle(). } printf("\ndo you want to continue(1/0)\n"). previous=first. } create() { int s. if(head==NULL) { head=first.&s). else { 73 www. break. case 9: exit(0).com display(). scanf("%d". s=sizeof (struct node). } else { previous=head. return(0). } display() { if(head==NULL) printf("list is null"). display().

else { s=sizeof (temp).For More Notes and questions log on to www.com temp=head. last->link=temp.temp->data). if(last->link==head) head=NULL.technicalsymposium. if (head==NULL) printf("list is null"). printf("enter data\n"). } insfirst() { int s. else head=head->link. while(temp!=last) { printf("%d->". scanf("%d". } if(temp==last) { printf("%d ->". } } return(0). } return(0).temp->data). 74 www.&temp->data).com . temp=(struct node*) malloc(s). } delfirst() { int s.technicalsymposium. if(head==NULL) printf("list is null"). last->link=head. first=temp. head=temp. temp=temp->link. temp=temp->link. temp->link=head.

*first1. last1=(struct node*) malloc(s). while(next->link!=last) next=next->link. last=next.technicalsymposium. if (head==NULL) printf("list is null"). next->link=head. } return(0). struct node *pre. } 75 www. else { next=head. struct node *last1.technicalsymposium.com . else { s=sizeof (last1). temp->link=last1. if(head==NULL) printf("list is null"). last=last1. else { if(head==last) head=last=NULL.m.For More Notes and questions log on to www. last1->link=head. last1->link=NULL. temp=head. while(temp->link!=head) temp=temp->link. printf("enter the data\n").*next.com return(0). scanf("%d".&last1->data). } dellast() { int s. } inslast() { int s.

f. printf("enter the position of the elemant").&nex->data). else { s=sizeof (temp). else 76 www.*nex.count). } insmiddle() { int s. struct node *next. struct node *next. nex->link=next.count.&f).*nex. while((count<f) && (next->link!=head)) { next=next->link. } if((count<f) && (next->link==head)) { printf("not possible to insert. temp=(struct node*) malloc(s). pre=head.*pre. printf("enter the data\n").*pre.f. pre=pre->link.com } return(0).com .technicalsymposium.technicalsymposium. if (head==NULL) printf("list is null"). } else { pre->link=nex. } delmiddle() { int s. count=2. scanf("%d". the list is contains %d elements".count. next=pre->link. count=count+1.For More Notes and questions log on to www. if (head==NULL) printf("list is null"). } } return(0). scanf("%d".

77 www. } return(0). pre=pre->link.count). No: 6 Program : #include<stdio. } void creation() { printf("\n Enter the value of the root node \n").For More Notes and questions log on to www. scanf("%d". } pre->link=next->link. } Ex. pre=head.&f).h> #include<malloc. printf("enter the position of the elemant").com . count=count+1.technicalsymposium. t=(struct node*)malloc(sizeof(struct node)). struct node *rchild.com { s=sizeof (temp). the list is contains %d elements". count=2. } if((count<f) && (next->link==head)) { printf("not possible to insert.h> Operations On Binary Trees struct node { int data. next=pre->link. struct node *lchild. temp=(struct node*) malloc(s). while((count<f) && (next->link!=head)) { next=next->link.technicalsymposium.

x->data=n.struct node *a) { struct node *x. printf(" %10d ". if(a->data>n) { if((a->lchild)==NULL) { a->lchild=x.&t->data).a->rchild). t->rchild=NULL.technicalsymposium. } } 78 www. inorder(a->rchild).technicalsymposium. a->rchild->lchild=NULL. } } else if(a->rchild==NULL) { a->rchild=x.a->data). } } void inorder(struct node* a) { if(a!=NULL) { inorder(a->lchild). } x=(struct node*) malloc(sizeof(struct node)). t->lchild=NULL. else insert(n.For More Notes and questions log on to www.com . if(a->data !=0) { if(a->data>n) insert(n.com scanf("%d". a->lchild->lchild=NULL. } void insert(int n. a->rchild->rchild=NULL.a->lchild). a->lchild->rchild=NULL.

Inorder \n3.&c). clrscr(). } 79 www. preorder(a->lchild).com . preorder \n4.technicalsymposium. } } void main() { int num. scanf("%d". Insertion \n2.&num).technicalsymposium. switch(c) { case 1:printf(" \n Data to be inserted "). case 2: inorder(t).c.postorder ").a->data). } } void postorder(struct node *a) { if(a!=NULL) { postorder(a->lchild).com void preorder(struct node *a) { if(a!=NULL) { printf(" %10d". break.For More Notes and questions log on to www. postorder(a->rchild). case 4: postorder(t).t). do{ printf("\n 1. break. insert(num. scanf("%d". break. preorder(a->rchild). break. } }while(c<5). printf(" %10d". printf("\n for exit give choice greater than four").a->data). case 3: preorder(t). printf(" \nEnter your choice "). creation().

} struct bstree* findMax(struct bstree *t) { 80 www. return root.h> #include<stdlib.*temp. struct bstree *left.x).technicalsymposium.com .x). }. delete.h> struct bstree { int data. Write a C program to implement the binary tree operations – insert. } else if(x < root->data) root->left=insertbst(root->left. else if(x > root->data) root->right=insertbst(root->right. } struct bstree* findMin(struct bstree *t) { if(t==NULL) return NULL. root->left=root->right=NULL. else if(t->left != NULL) return findMin(t->left).h> #include<stdio. else return t. struct bstree* insertbst(struct bstree *root. struct bstree *root.int x) { if( root == NULL) { root=malloc(sizeof(struct bstree)).com Ex.technicalsymposium. root->data=x. 7 binary tree operation Program: 7.For More Notes and questions log on to www. struct bstree *right. and search. #include<conio. No.

} int find(struct bstree *temp. else return t. } struct bstree* removeElt(struct bstree *t. else return find_min(temp->left).int key) { if (temp==NULL) { return NULL.key).int key) { struct bstree *temp. else if (key > temp->data) return find(temp->right. } } int find_min(struct bstree *temp) { if(temp->left == NULL) return temp->data. else return temp.technicalsymposium. 81 www.com . } int find_max(struct bstree *temp) { if(temp->right == NULL) return temp->data. } else { if(key < temp->data) return find(temp->left.com if(t==NULL) return NULL.technicalsymposium.key). else if(t->right != NULL) return findMax(t->right). else return find_max(temp->right).For More Notes and questions log on to www.

t->data). } return t. } else { temp=t.key).com if (t==NULL) printf("\nNode is not available").com .For More Notes and questions log on to www. free(temp). clrscr(). 82 www. } } void main() { struct bstree *new_elt.root->data). root=NULL. } void print_list(struct bstree *root) { if(root!=NULL) { if(root->left != NULL) print_list(root->left). else if(key < t->data) t->left=removeElt(t->left.technicalsymposium.technicalsymposium. if(t->left==NULL) t=t->right. t->data=temp->data.rollno.key). t->right=removeElt(t->right.info. else if(t->left != NULL && t->right != NULL) { temp=findMin(t->right). if(root->right != NULL) print_list(root->right). else if(t->right==NULL) t=t->left. int option. printf(" %d". else if(key > t->data) t->right=removeElt(t->right.

technicalsymposium.&rollno). break. printf("\n The Minimum Value is:%d". info=find(root. } break.com . else { info=find_min(root). root=removeElt(root.Remove a Item\n3.Insert a Item\n2.info). printf("\n The Maximum Value is:%d". else { info=find_max(root).rollno). scanf("%d".technicalsymposium. break. if(info!=0) printf("\nElement has been found! at Position=%d".rollno). printf("\nEnter your Choice:").rollno).Find\n4.Print the List\n7. scanf("%d". break. case 3: printf("Enter rollno of Item to be found:").&option). case 5: if(root == NULL) printf("\nTree is empty"). if(info == 0) printf("\nElement has not found in the List!").Exit"). case 4: if(root == NULL) printf("\nTree is empty").For More Notes and questions log on to www. case 2: printf("\nEnter the Rollno of the element to Removed:"). } 83 www.Find Minimum Value\n5.Find Maximum Value\n6.com while(1) { printf("\n\n1. switch(option) { case 1: printf("\nEnter Rollno of Element to be Inserted:"). scanf("%d".info).info).&rollno).&rollno). scanf("%d". root=insertbst(root.

i. while(a[high]>v) high--. quicksort(low.int high) { int v. int high) { if(low<high) { int k. k=partition(low.high). } Ex. 8 Program: #include <stdio.pass=1.pass++). void quicksort(int low.For More Notes and questions log on to www. printf("\n"). break.k-1). } } int partition(int low.com break.a[30].h> #include<conio.i++) printf("%6d". do { while(a[i]<=v) i++.i. printf("pass %d\n".i<=n. v=a[low].a[i]).com . case 7: exit(0). No.technicalsymposium. } } getch(). Quick Sort 84 www.t. case 6: if(root == NULL) printf("\nTree is empty"). else print_list(root). quicksort(k+1. for(i=1.h> int n.technicalsymposium.high). i=low.

printf("Enter the elements \n"). quicksort(low.high.For More Notes and questions log on to www.i<=n.h> int x[20]. a[i]=a[high]. 9 Program: Heap Sort #include<conio. } void main() { int low. } Ex.technicalsymposium. void heapsort(). printf("ENTER THE NO. a[high]=t. 85 www.technicalsymposium.&n). No. low=1.com . void adjust(). a[low]=a[high]. } } while(i<high).h> #include<stdio. scanf("%d". a[high]=v. clrscr(). printf(" Enter the array size \n"). void main() { int i.high).n1. n1=n.n.i++) scanf("%d". getch(). high=n. for(i=1.i. scanf("%d".&a[i]). return high. void heapify().com if(i<high) { t=a[i]. OF ELEMENTS :").&n). clrscr().

i++) printf("%d\t". heapsort(). getch().com .i--) { t=x[i]. for(i=1. if(t>=x[j]) break. printf("\npass %d \n". clrscr().x[i]). x[j/2]=x[j].n-(i-1)).technicalsymposium.i>=2. j=2*j.&x[i]). adjust(1. for(i=n.For More Notes and questions log on to www. printf(" \n ARRAY BEFORE SORT :").com printf(“ENTER THE ELEMENTS\n”).i-1). } void heapsort() { int i. t=x[i].j<=n1.t.i<=n.technicalsymposium.x[j]).i<=n. while(j<=n) { if(j<n && x[j] < x[j+1]) j++. j=2*i. for(j=1. for(i=1. int t. x[i]=x[1]. } } void adjust(int i. x[1]=t. heapify().j.i++) scanf("%d".j++) printf(" %d\t". } x[j/2]=t.int n) { int j. 86 www.

i<=no_v.j. for(j=1. #include<conio.j.i).For More Notes and questions log on to www. for(j=1.i).i--) adjust(i.j++) { printf("\t%d". } } void print_graph(int no_v) { int i.i++) printf("\tV%d". No.h> #include<stdio.i++) //Intialize the Graph { visited[i]=0.graph[i][j]).technicalsymposium.i<=no_v. int visited[10].com } void heapify() { int i.h> int graph[10][10]. Write a C program to implement the Depth First Search.j++) graph[i][j]=0.j<=no_v.j<=no_v. void Intilize_Graph(int no_v) { int i.n).j.technicalsymposium.com . 10 Depth first search Program: 10. for(i=1.i<=no_v. } Ex. printf("\nThe Graph(Matrix Representation):\n"). for(i=1.i>=1. for(i=1. } } } 87 www. for(i=n/2.i++) { printf("\nV%d".

i). for(i=1. scanf("%d %d". printf("Depth First Search Tree:\n").start_v. DFS(start_v.i++) { printf("\nEdge:%d-->Start_V & End_V:".&start_v). scanf("%d". graph[s_v][e_v]=1.com .start_v). } print_graph(no_v).&no_v). clrscr().&e_v). } 88 www. printf("Enter the no of Edges in the Graph:"). scanf("%d". scanf("%d".int no_v) { int i.i<=no_e. getch().i<=no_v.no_v). visited[start_v]=1.technicalsymposium. printf("%d-->".no_e. for(i=1. } } } void main() { int no_v. printf("\nDepth First Search:\nEnter the Starting Vertex for Searching:").no_v).e_v. printf("\nEnter the Adjacent vertices of each edge"). printf("\nEnter the No of Vertices in the Graph:").i++) { if(i!=start_v && visited[i]==0 && graph[start_v][i]==1) { DFS(i.For More Notes and questions log on to www. graph[e_v][s_v]=1.&s_v.technicalsymposium. int s_v.j.&no_e).com void DFS(int start_v. int i.

j.h> #include<stdio. struct VertexInfo { int known.For More Notes and questions log on to www. else { printf("\nShortest Path:%d ". int no_v.i++) //Intialize the Graph { vertex[i]. void Intilize_VertexInfo() { int i. if(vertex[e_v].com Ex.known=0. vertex[i]. int prev_vertex.weight[20].prev_vertex!=0) { printf("%d ".distance=1000. while(vertex[e_v].vertex[e_v]. 11 Program: 11.prev_vertex=0.distance). int distance.i<=no_v.vertex[temp].no_e. } } void print_Path(int e_v) { int temp=e_v. } } shortest path of the graph 89 www.com . vertex[i]. } printf("\nThe Distance is:%d".technicalsymposium.prev_vertex.e_v). Write a C program to find the shortest path of a graph. #include<conio.technicalsymposium. No.prev_vertex).h> int start_V[20]. for(i=1.end_V[20]. }vertex[20].prev_vertex==0) printf("\nNo path from Source to Given Destination!"). e_v=vertex[e_v].

&end_V[i].i<=no_e.&no_v).i++) { printf("\nEdge:%d-->Start_V & End_V & Weight:".qw.i). printf("\nEnter Vertices to find the Shortest Path:").distance) { vertex[w].distance+weight[i] < vertex[w]. scanf("%d".com void main() { int s_v. vertex[w]. for(i=1. clrscr().&no_e). for(i=1.For More Notes and questions log on to www. } Intilize_VertexInfo(). } } } 90 www. scanf("%d". int i. while(next_v) { v=next_v. vertex[s_v].&e_v).technicalsymposium. printf("Enter the no of Edges in the Graph:").prev_vertex=v. printf("\nEnter the No of Vertices in the Graph:").j. char ch.&weight[i]).distance=vertex[v]. int min. if(vertex[v]. printf("\nEnter the Adjacent vertices of each edge").i++) { if(start_V[i]==v) { w=end_V[i]. vertex[v].com .distance=0. scanf("%d %d %d".e_v.&s_v.next_v.distance+weight[i].w. int v.technicalsymposium.start_v. next_v=s_v.i<=no_e.index.&start_V[i]. scanf("%d %d".known=1.

} K. } print_Path(e_v).com .&e_v). } getch(). fflush(stdin). 2.L. while(1) { printf("\nDo you want shortest to one more destination:"). if(ch=='y' | ch=='Y') { printf("\nEnter the Destination Vertex:").For More Notes and questions log on to www.technicalsymposium.distance.distance) { min=vertex[j].j++) { if(vertex[j]. scanf("%d". Represent the given sparse matrix using one-dimensional array and linked list.j<=no_v. } } if(min==1000) next_v=0.com min=1000. print_Path(e_v). ch=getchar(). next_v=j. Create a Stack and do the following operations using arrays and linked lists (i)Push (ii) Pop (iii) Peep 91 www. for(j=1. } else break.N COLLEGE OF ENGINEERING. POTTAPALAYAM DEPARTMENT OF COMPUTER APPLICATIONS (MCA) DATA STRUCTURES LAB(MC1606) I SEMESTER – (June 2006 – November 2006) DATA STRUCTURES LAB CYCLE PROGRAMS 1.known==0 && min>vertex[j].technicalsymposium.

Generate the following series of numbers: 92 www. POTTAPALAYAM DEPARTMENT OF COMPUTER APPLICATIONS (MCA) PROGRAMMING LAB (MC1607) I SEMESTER – (June 2006 – November 2006) C LAB CYCLE PROGRAMS 1. Perform the following operations in a given graph (i) Depth first search (ii) Breadth first search 9. Create a binary search tree and do the following traversals (i)In-order (ii) Pre order (iii) Post order 6. Implement the following operations on a binary search tree.For More Notes and questions log on to www. 5. Create a Queue and do the following operations using arrays and linked lists (i)Add (ii) Remove 4. 8. Sort the given list of numbers using heap and quick sort. (i) Insert a node (ii) Delete a node 7. Display the following: (i) Floyd’s triangle (ii) Pascal Triangle 2. Find the shortest path in a given graph using Dijkstra algorithm K.com .L.com 3.technicalsymposium. doubly linked list and circular linked list.technicalsymposium. Implement the operations on singly linked list.N COLLEGE OF ENGINEERING.

Manipulate the strings with following operations.com . Create the sales report for M sales person and N products using two-dimensional array.com Armstrong numbers between 1 to 100 Prime numbers between 1 to 50 Fibonacci series up to N numbers 3. II.technicalsymposium. Factorial 8. 9. Simulate following Banking operations using functions. 93 www. Implement using recursion I. III. (i) Deposit (ii) Withdrawal (iii) Balance Enquiry 7. Find the solution of Towers of Hanoi problem using recursion. 6.For More Notes and questions log on to www. Fibonacci number generation. Generate Student mark sheets using structures. Create a collection of books using arrays of structures and do the following: (i) Search a book with title and author name (ii) Sorts the books on title. (i) Concatenating two strings (ii) Reversing the string (iii) Finding the sub string (iv) Replacing a string (v) Finding length of the string 4. Find the summation of the following series: (i) Sine (ii) Cosine (iii) Exponential 5.technicalsymposium.

- Data Structure Notes
- Data Structure Notes
- C and Data Structure notes
- 27287326-Algorithms-and-Data-Structures
- Wikipedia Data Structures
- Data Structure Full Book Ppt
- 2 Marks Questions
- Data Structures 2008
- 2nd Sem Prog.
- C and Data Structures Unit Wise Important Questions
- Data Structure Lab Mannual for 3rd Sem IT Department
- Data Structure Lab_MCA16-L
- Data structure practicals
- D.S
- Accounts Questions
- Exam Programs
- Rr10203 c and Data Structures
- Data Structures & Algorithms
- Data Structures Cheatsheet
- Data Structures Lab Manual
- 2 Marks
- Data Structure BCA14
- Data Structure MCA12
- Introduction to Design and Analysis Computer Algorithms - Solution Manual
- introduction to final accounts
- Data Structures Cheat Sheet
- Data Structures and Algorithms in C++ 2nd Ed - M. Goodrich, Et Al., (Wiley, 2011) BBS
- note
- Data Structure
- C Programming and Data Structure_notes
- Data Structure Notes

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd