Sunteți pe pagina 1din 28

ALPHA ARTS AND SCIENCE COLLEGE PORUR, CHENNAI - 600 116.

DEPARTMENT OF COMPUTER APPLICATIONS

LAB MANUAL - PROGRAMMING IN C++ & Data Structures SAZ31

NAME CLASS SECTION

: : II B.C.A :

SEMESTER : III SEMESTER

University of Madras 2012 2013

LIST OF PROGRAMS

1. Implement PUSH, POP operations of stack using Arrays. 2. Implement PUSH, POP operations of stack using Pointers. 3. Implement add, delete operations of a queue using Arrays. 4. Implement add, delete operations of a queue using Pointers. 5. Conversion of infix to postfix using stack operations 6. Postfix Expression Evaluation. 7. Addition of two polynomials using Arrays and Pointers. 8. Creation, insertion, and deletion in doubly linked list. 9. Binary tree traversals (in-order, pre-order, and post-order) using linked list. 10.Depth First Search and Breadth first Search for Graphs using Recursion.

PROGRAMMING IN C++ Concepts And Syntax INTRODUCTION C++ is a statically typed, compiled, general purpose, case-sensitive, free-form programming language that supports procedural, object-oriented, and generic programming. C++ is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features. C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New Jersey as an enhancement to the C language and originally named C with Classes but later it was renamed C++ in 1983. C++ is a superset of C, and that virtually any legal C program is a legal C++ program. Note: A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Object-Oriented Programming: C++ fully supports object-oriented programming, including the four pillars of object-oriented development: 1. Encapsulation 2. Data hiding 3. Inheritance 4. Polymorphism Along with class, obeject and message passing. Learning C++: The most important thing to do when learning C++ is to focus on concepts and not get lost in language technical details. The purpose of learning a programming language is to become a better programmer; that is, to become more effective at designing and implementing new systems and at maintaining old ones. C++ supports a variety of programming styles. You can write in the style of Fortran, C, Smalltalk, etc., in any language. Each style can achieve its aims effectively while maintaining runtime and space efficiency.

Use of C++: C++ is used by hundreds of thousands of programmers in essentially every application domain. C++ is being highly used to write device drivers and other software that rely on direct manipulation of hardware under realtime constraints. C++ is widely used for teaching and research because it is clean enough for successful teaching of basic concepts. Anyone who has used either an Apple Macintosh or a PC running Windows has indirectly used C++ because the primary user interfaces of these systems are written in C++

C++ Program Structure: A simple code that would print the words Hello World. #include <iostream> using namespace std; // main() is where program execution begins. int main() { cout << "Hello World"; // prints Hello World return 0; } Compile & Execute C++ Program: Lets look at how to save the file, compile and run the program. Please follow the steps given below: 1. Open a text editor and add the code as above. 2. Save the file as : hello.cpp 3. Press alt+ F9 to compile your program. 4. Press ctrl+ F9 to run your program. 5. You will be able to see ' Hello World ' printed on the window. C++ Identifiers: A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).

C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is a case sensitive programming language. C++ Keywords: The following list shows the reserved words in C++. These reserved words may not be used as constant or variable or any other identifier names. asm auto bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum explicit export extern false float for friend goto if inline int long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while

Decisions C offers four ways of making decisions like the ones above. They are listed here below. The method which is numbered 2b was encountered in connection with the C preprocessor; its purpose is very similar to 2a. 1: if (something_is_true) { /* do something */ }

2a: if (something_is_true) { /* do one thing */ } else { /* do something else */ } 2b: ? (something_is_true) : /* do one thing */ : /* do something else */ 3: switch (choice) { case first_possibility : /* do something */ case second_possibility : /* do something */ .... }

Nested ifs and logic Consider the following statements which decide upon the value of some variable i. Their purposes are exactly the same. if ((i > 2) && (i < 4)) { printf ("i is three"); } or: if (i > 2) { if (i < 4)

{ printf ("i is three"); } } Loops Decisions can also be used to make up loops. They allow the programmer to build a sequence of instructions which can be executed again and again, with some condition deciding when they will stop. There are three kinds of loop in C. They are called:

while do ... while for

while The simplest of the three loops is the while loop. In common language while has a fairly obvious meaning: the while-loop has a condition: while (condition) { statements; } and the statements in the curly braces are executed while the condition has the value "true" ( 1 ). There are dialects of English, however, in which "while" does not have its commonplace meaning, so it is worthwhile explaining the steps which take place in a while loop. do..while The do..while loop resembles most closely the repeat..until loops of Pascal and BASIC except that it is the `logical opposite'. The do loop has the form: do { statements; } while (condition) Notice that the condition is at the end of this loop. This means that a do..while loop will always be executed at least once, before the test is made to determine whether it should continue. This is the only difference between while and do..while. A do..while loop is like the "repeat .. until" of other languages in the following sense: if the condition is NOTed using the ! operator, then the two are identical.

repeat == until(condition) For

do

while (!condition)

The most interesting and also the most difficult of all the loops is the for loop. The name for is a hangover from earlier days and other languages. It is not altogether appropriate for C's version of for. The name comes from the typical description of a classic for loop: A for loop normally has the characteristic feature of controlling one particular variable, called the control variable. That variable is somehow associated with the loop. For example it might be a variable which is used to count "for values from 0 to 10" or whatever. The form of the for loop is: for (statement1; condition; statement2) { }

For normal usage, these expressions have the following significance. statement1 This is some kind of expression which initializes the control variable. This statement is only carried out once before the start of the loop. e.g. i = 0; condition This is a condition which behaves like the while loop. The condition is evaluated at the beginning of every loop and the loop is only carried out while this expression is true. e.g. i < 20; statement2 This is some kind of expression for altering the value of the control variable. In languages such as Pascal this always means adding or subtracting 1 from the variable. In C it can be absolutely anything. e.g. i++ or i *= 20 or i /= 2.3 ... Function Declarations: A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. A function declaration has the following parts: return_type function_name( parameter list ); Calling a Function: While creating a C++ function, you give a definition of what the function has to do. To use a function, you will have to call or invoke that function.

When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program. To call a function you simply need to pass the required parameters along with function name and if function returns a value then you can store returned value. Function Arguments: If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function. The formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit. While calling a function, there are two ways that arguments can be passed to a function: Call Type Call by value Description This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

Call by reference

POINTERS: C++ pointers are easy and fun to learn. Some C++ tasks are performed more easily with pointers, and other C++ tasks, such as dynamic memory allocation, cannot be performed without them. As you know every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory.

What Are Pointers? A pointer is a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it. The general form of a pointer variable declaration is: type *var-name; Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Following are the valid pointer declaration: int *ip; // pointer to an integer double *dp; // pointer to a double float *fp; // pointer to a float char *ch // pointer to character The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to. Classes & Objects in Detail: There are further interesting concepts related to C++ Classes and Objects which we will discuss in various sub-sections listed below: Concept Class member functions Description A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. A class member can be defined as public, private or protected. By default members would be assumed as private. A class constructor is a special function in a class that is called when a new object of the class is created. A destructor is also a special function which is called when created object is deleted. The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously.

Class access modifiers

Constructor & destructor

C++ copy constructor

C++ friend functions

A friend function is permitted full access to private and protected members of a class. With an inline function, the compiler tries to expand the code in the body of the function in place of a call to the function. Every object has a special pointer this which points to the object itself. A pointer to a class is done exactly the same way a pointer to a structure is. In fact a class is really just a structure with functions in it. Both data members and function members of a class can be declared as static.

C++ inline functions

The this pointer in C++

Pointer to C++ classes

Static members of a class

Constructors and Destructors in C++: The process of creating and deleting objects in C++ is not a trivial task. Every time an instance of a class is created the constructor method is called. It is used to initialize the objects. The constructor has the same name as the class and it doesn't return any type, while the destructor's name it's defined in the same way, but with a '~' in front and it is used the reclaim the space which is allocated for an object.

CONCEPTS OF DATA STRUCTURES Stacks Think of a stack of newspapers, or trays in a cafeteria. The only item that can be taken out (or even seen) is the most recently added item; a stack is a Last-In-First-Out (LIFO) data structure. Here are the stack operations: OPERATION Stack() void push(Object ob) Object pop() DESCRIPTION (constructor) create an empty stack add ob to the top of the stack remove and return the item from the top of the stack (error if the stack is empty)

Implementation of stack using array The push method is like the version of the List add method that adds an object to the end of the list (because items are always pushed onto the top of the stack). Note that it is up to us as the designers of the Stack class to decide which end of the array corresponds to the top of the stack. We could choose always to add items at the beginning of the array, or always to add items at the end of the array. However, it is clearly not a good idea to add items at the beginning of the array since that requires moving all existing items, except when the array is full. Here are before and after pictures, illustrating the effects of a call to push:

The pop method needs to remove the top-of-stack item, and return it, as illustrated below.

Queues The conceptual picture of a queue is something like this: -----------------values in ----> items in the queue ----> values out -----------------^ ^ | | this is the rear of this is the front of the queue the queue Think of people standing in line. A queue is a First-In-First-Out (FIFO) data structure. Items can only be added at the rear of the queue, and the only item that can be removed is the one at the front of the queue. Here are the queue operations: OPERATION Queue() void enqueue(Object ob) Object dequeue() DESCRIPTION (constructor) create an empty queue add ob to the rear of the queue remove and return the item from the front of the queue (error if the queue is empty)

Implementing Queues The main difference between a stack and a queue is that a stack is only accessed from the top, while a queue is accessed from both ends (from the rear for adding items, and from the front for removing items). This makes both the array and the linked-list implementation of a queue more complicated than the corresponding stack implementations. LINKED LIST There Are Two Types Of Linked List Singly Linked List Doubly Linked List

Singly-linked List Singly-linked list is a list of elements in which the elements of the list can be placed anywhere in heap memory. All of list elements are linked together with each other using an explicit link field, that is, by storing the address of the next element inthe link field of the previous element. Singly-linked list has a dynamic size and its size can be determined at runtime not compile time. Here the picture which demonstrate a singly-linked list. Their are four elements in the list. The head pointer is the pointer pointing to the first element of the list.

Add a New Element to a Singly-linked List This picture demonstrates how to add a new elemen to the list. The process is simple as follows: - If the existing list is empty we need to insert a new element (or node) as the starting node - Otherwise we traverses the existing list to get the pointer to the last node of it; 1. Create a new node. 2. Change the next pointer of the last node to the new node. 3. The next pointer of new node is pointed to NULL and it becomes the last node of the list.

Doubly Linked List A doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that arereferences to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically asentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked listsformed from the same data items, but in opposite sequential orders.

A doubly linked list whose nodes contain three fields: an integer value, the link to the next node, and the link to the previous node. The two node links allow traversal of the list in either direction. While adding or removing a node in a doubly linked list requires changing more links than the same operations on a singly linked list, the operations are simpler and potentially more efficient (for nodes other than first nodes) because there is no need to keep track of the previous node during traversal or no need to traverse the list to find the previous node, so that its link can be modified.

Binary tree traversal: Preorder, Inorder, and Postorder: In order to illustrate few of the binary tree traversals, let us consider the below binary tree:

Preorder traversal: To traverse a binary tree in Preorder, following operations are carriedout (i) Visit the root, (ii) Traverse the left subtree, and (iii) Traverse the right subtree. Therefore, the Preorder traversal of the above tree will outputs: 7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10 Inorder traversal: To traverse a binary tree in Inorder, following operations are carried-out (i) Traverse the left most subtree starting at the left external node, (ii) Visit the root, and (iii) Traverse the right subtree starting at the left external node. Therefore, the Inorder traversal of the above tree will outputs: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Postorder traversal: To traverse a binary tree in Postorder, following operations are carriedout (i) Traverse all the left external nodes starting with the left most subtree which is then followed by bubble-up all the internal nodes, (ii) Traverse the right subtree starting at the left external node which is then followed by bubble-up all the internal nodes, and (iii) Visit the root. Therefore, the Postorder traversal of the above tree will outputs: 0, 2, 4, 6, 5, 3, 1, 8, 10, 9, 7

BREADTH FIRST SEARCH AND DEPTH FIRST SEARCH Traversal of graphs and digraphs To traverse means to visit the vertices in some systematic order. You should be familiar with various traversal methods for trees: preorder: visit each node before its children. postorder: visit each node after its children. inorder (for binary trees only): visit left subtree, node, right subtree.

We also saw another kind of traversal, topological ordering, when I talked about shortest paths. Today, we'll see two other traversals: breadth first search (BFS) and depth first search (DFS). Both of these construct spanning trees with certain properties useful in other graph algorithms. We'll start by describing them in undirected graphs, but they are both also very useful for directed graphs. Breadth First Search This can be throught of as being like Dijkstra's algorithm for shortest paths, but with every edge having the same length. However it is a lot simpler and doesn't need any data structures. We just keep a tree (the breadth first search tree), a list of nodes to be added to the tree, and markings (Boolean variables) on the vertices to tell whether they are in the tree or list. Depth first search Depth first search is another way of traversing graphs, which is closely related to preorder traversal of a tree. Recall that preorder traversal simply visits each node before its children

ALGORITHMS EX.NO. 1 AIM: To create a program for stack using arrays Implement PUSH, POP operations of stack using Arrays.

ALGORITHM: Step 1: Start the program execution. Step 2: Define a class with necessary variables and functions. Step 3: Declare the variable top and element Step 4: Create an array to store 10 elements. Step 5: Create a function for push, pop, and display operations. Step 6: End of the program execution.

PUSH FUNCTION: Step 1: Start the push function Step 2: In push function check whether top is equal to 10, else Step 3: Read the value for number of elements to be entered as m Step 4: Repeat the loop for i =0 , until i less than or equal to 10 Step 5: Read the value for element and store it in s[top] Step 6: Increment the value of top by 1. Step 7: End of loop Step 8: End of Push function

POP FUNCTION: Step 1: Start the Pop function

Step 2: Check the condition if top is equal to 0, else Step 3: Move the s[top] to the element Step 4: Decrement the value of top by 1 Step 5: End of Pop function

DISPLAY FUNCTION: Step 1: Start the Display function Step 2: Check whether top is equal to 0 Step 3: Print stack is empty, else Step 4: Repeat the loop for i=0, until i is less than or equal to top Step 5: Print the value of element Step 6: Increment the value of i Step 7: End of Display function.

EX.NO.2 Implement PUSH, POP operations of stack using Pointers. AIM: To Write a Program for Stack operations using pointer ALGORITHM: Step 1: Start the program execution. Step 2: Create a class stack with necessary variables and functions. Step 3: Define pointers to the necessary variables. Step 4: Declare a function for push, pop and display Step 5: End of the program execution. PUSH FUNCTION Step 1: Start Step 2: Declare a pointer variable temp Step 3: Create a memory allocation for pointer variable temp by using malloc function Step 4: Read the value of item Step 5: move the item to pointer variable data (temp->data = item) Step 6: move the null to pointer variable next (temp ->next = null) Step 7: check whether the condition top is equal to null, then Step 8: move temp to top, else Step 9: move the top to pointer variable next (temp ->next=top) Step 10: Stop POP FUNCTION Step 1: Start Step 2: check the condition whether top is equal to null, then Step 3: Print stack is empty, else Step 4: Move the top to temp and top pointer next to top (top = top -> next) Step 5: Remove the item temp -> data Step 6: Stop Display Function Step 1: Start Step 2: check the condition whether top is equal to null, then Step 3: Print stack is empty, else Step 4: Move the top to temp Step 5: While the condition temp is not equal to null Step 6: Print the data temp-> data Step 7: Move the temp -> next to temp Step 8: Stop

EX.NO.3

Implement add, delete operations of a queue using Arrays.

AIM: To write a program for queue operations using arrays.

ALGORITHM: Step 1: start the program execution. Step 2: Create a class with necessary variables and functions. Step 3: Declare an array to store 10 elements. Step 4: Define functions for insert, delete and display operations. Step 5: Stop Insert function Step 1: Start Step 2: Check the condition if rear is equal to n Step 3: Print Queue is full, else Step 4: Check the condition whether front and rear are equal to zero Step 5: Assign the value of front and rear as 1, else Step 6: Increment the value of rear by 1 Step 7: Move the inserted item to queue[rear] Step 8: Print the item inserted Step 9: Stop Delete Function Step 1: Start Step 2: Check whether front is equal to zero Step 3: Print queue is empty, else Step 4: Move queue [front] to item Step 5: if front is equal to rear then Step 6: Assign the value front and rear is equal to zero, else Step 7: Increment the value of front by 1 Step 8: Print the item deleted Step 9: Stop

EX.NO.4

Implement add, delete operations of a queue using Pointers

AIM: To write a program to implement queue operations using pointers ALGORITHM: Step 1: Start the program execution. Step 2: create a class with the name queue Step 3: Declare necessary pointer variables. Step 4: Declare a function for the insert, delete and display operations. Step 5: Stop

Insert function Step 1: Start Step 2: Create newnode by using malloc function Step 3: Move item to newnode->info Step 4: Check whether front and rear is equal to null, then Step 5: Move newnode to front and rear and null to newnode->next, else Step 6: move newnode to rear->next and newnode->next to null Step 7: Move newnode to rear Step 8: Stop Delete function Step 1: start Step 2: Check whether front is equal to rear, then Step 3: Move null to front and rear, else Step 4: Move front->next to front Step 5: Stop Display Fuction Step 1: Start Step 2: initialize the variable I as integer Step 3: Move front to ptr and assign I is equal to q Step 4: Check the while condition ptr is not equal to null Step 5: print I and ptr->info Step 6: Move ptr->next to ptr Step 7: Increment the value of I by 1 Step 8: Stop

Ex.No.5

Conversion of infix to postfix using stack operations

AIM: To write a program for conversion of infix expression into postfix expression using stack operations ALGORITHM: Step 1: Start Step 2: Create a class expression Step 3: Declare a variable I for infix and P for postfix. Step 4: Push (left parenthesis onto stack. Step 5: Add ) right parenthesis to the end of expression I. Step 6: Scan I from left to right and repeat step 4 for each element of I Until the stack becomes empty. Step 7: If the scanned element is: (a) an operand then add it to P. (b) a left parenthesis then push it onto stack. (c) an operator then: (i) Pop from stack and add to P each operator which has the same or higher precedence then the scanned operator. (ii) Add newly scanned operator to stack. (d) a right parenthesis then: (i) Pop from stack and add to P each operator until a left parenthesis is encountered. (ii) Remove the left parenthesis. [End of Step 4 If] [End of step 3 For Loop] Step 8: Stop

Ex.No.6

Postfix Expression Evaluation

AIM: To Write a C++ Program for Postfix Expression Evaluation ALGORITHM: Step 1: Start Step 2: Create a class expression Step 3: Declare a variable P as postfix. Step 4: Add a ) right parenthesis at the end of P. Step 5: Scan P from left to right and repeat steps 3 & 4 for each element of P until ) is encountered. Step 6: If an operand is encountered, push it onto stack. Step 7: If an operator is encountered then: (a) Pop the top two elements from stack, where A is the top element and B is the next to top element. (b) Evaluate B or A. (c) Place the result of (b) back on stack. [End of Step 4 If] [End of step 2 For Loop] Step 8: Set VALUE equal to the top element on the stack. Step 9: Stop

Ex.No.7

Addition of two polynomial Numbers using Arrays and Pointers

AIM: To Write a program for addition of two polynomials using arrays and pointers ALGORITHM: Step 1: Define a function to read the coefficient and exponent terms of the polynomial number until exponent term is zero Step 2: Define a function to add the two polynomials with the following comparisons Step 3: If the exponent term in the first polynomial is greater than the exponent in the second polynomial, add the node of the first polynomial with the resultant polynomial Step 4: If the exponent term in the first polynomial is lees than the exponent in the second polynomial, add the node of the second polynomial with the resultant polynomial Step 5: If the exponent term in the first polynomial is equal to the exponent in the second polynomial, add both the coefficient of the first and second polynomial and the node to the resultant polynomial Step 6: Traverse both the polynomial according to the above comparison upto the NULL value of both the polynomials are reached . Step 7: Display the resultant polynomial.

EX.NO.8

Creation, insertion, and deletion in doubly linked list

AIM: To write a program for creation, insertion and deletion by using doubly linked list. ALGORITHM: Step 1: start Step 2: Create class to store the data element Step 3: Declare the following functions (i) Insert element in front of the list. (ii) Insert element at the end of the list. (iii) Remove an element from the front. (iv) Remove an element from the back. (v) insert an element before a particular element. (vi) insert an element after a particular element. (vii) Remove an element before a particular element. (viii)Remove an element after a particular element. Step 4: Stop the program execution. Insert Before Function: Step 1: Start Step 2: Create a pointer variable of class type as *newNode Step 3: move node to new node and nodeB->prev to newNode->prev Step 4: move nodeb to newNode->next and value to newNode->value Step 5: Check whether the condition nodeB->prev is equal to null, then Step 6: Move newNode to this->Front else Step 7: Move newNode to nodeB->prev Step 8: Stop Insert Front Function: Step 1: Start Step 2: Create a pointer variable of class type node as *newNode Step 3: Check whether the condition this->front is equal to null Step 4: move newNode to this->fornt and newNode to this->back Step 5: Fix null to previous node and next node Step 6: Assign value to newNode->Value, else Step 7: Call the function insertbefore Step 8: Stop

Insert After Function: Step1: Start Step 2: Declare a pointer variable *newNode of type node Step 3: Move nodeB->next to newNode->next, nodeB to newNode->prev and value to newNode->value Step 4: Check whether the condition nodeB->next is equal to null, then Step 5: Move newNode to this->back, else Step 6: Move newNode to nodeB->next Step 7: Stop Insert Back Function: Step 1: Start Step 2: Check whether this->back is null, then Step 3: Call the function insertfront, else Step 4: Call the function insertAfter Remove Front Function: Step 1: Start Step 2: call the function removeNode by the value this->front Step 3: Stop Remove Back Function: Step 1: Start Step 2:Call the function removeNode by the value this->back Step 3: Stop Remove Before Function: Step 1:Start Step 2: check the condition this->front is equal to nodeB->prev Step 3: Move nodeB to this->front and null to this->front->prev, else Step 4: Call the function removeNode (nodeB->prev) Step 5:Stop Remove After Function: Step 1: Start Step 2:Check whether nodeA->next is equal to this->back, then Step 3:Move nodeA to this->back and null to this->back->next, else Step 4: call the function removeNode Step 5: Stop

EX.NO.9 AIM:

Binary tree traversals (in-order, pre-order, and post-order) using linked list.

To Write a program for binary tree traversals inorder, preorder and postorder. ALGORITHM: Step 1: Start the program execution. Step 2: Create a Class. Step 3: Create functions for inorder, preorder and postorder tree traversals. Step 4: Stop InorderFunction Step 1: Start Step 2:Check the function whether root is not equal to null Step 3:Call the function inorderprint with root->left Step 4: Print the root->item Step 5:Call the function inorderprint with root->right Step 6: stop Preorder Function Step 1: Start Step 2:Check the function whether root is not equal to null Step 3: Print the root->item Step 4:Call the function preorderprint with root->left Step 5:Call the function preorderprint with root->right Step 6: stop Postorder Function Step 1: Start Step 2:Check the function whether root is not equal to null Step 3:Call the function postorderprint with root->left Step 4:Call the function postorderprint with root->right Step 5: Print the root->item Step 6: stop

EX.NO.10 Recursion.

Depth First Search and Breadth first Search for Graphs using

AIM: To Write a program for Depth first search and Breadth first search graphs using recursion ALGORITHM: DFS Step 1: Start Step 2:Read the value of n Step 3: Set for loop as i<n as i=0 Step 4:Set for loop as j>n as j=0 Step 5: Read adjacency of adj[i][j] Read dfs(s) Step 6:Set for loop i<n and execute check whether (!vis[i]) then Dfs(i) Step 7: Stop BFS Step 1: Start Step 2: Read the size value in choice=1 and get the vertex value Step 3: Set for loop of condition i=1 and i<n Step 4: Read the number of edges Step 5: Set for loop of condition j<= no. of edges and j=1 Step 6: Read the edge position Step 7: if Ch=2 then check a[i][j]==1 then check v[j]=0 printf v[j] Step 8: If ch==3 for display set for loop i<=n and print vt[i] Step 9: Set for loop j<=n and then print the a[i][j] Step 10: Stop

S-ar putea să vă placă și