DATA STRUCTURES C This PPT is Dedicated to
- Slides: 118
DATA STRUCTURES ( C++ ) This PPT is Dedicated to my inner controller AMMA BHAGAVAN – ONENESS Founders. Developed by, S. V. G. REDDY, Associate professor, Dept. of CSE, GITAM UNIVERSITY. EDITED BY, M. Siva Naga Prasad student of M. tech(SE).
STACK USING ARRAYS • • Let us take an array a[5] and take a variable top points to -1. PUSH: Ø To INSERT the element in to stack using top. Ø Here we check for the OVERFLOW condition. POP: Ø To RETRIEVE elements from the stack using top. Ø Here we check for the UNDERFLOW condition. This concept is nothing but LIFO. SOURCE CODE: /* Program To Implement Stack using Array */ #include < iostream. h > #include < conio. h > #include < stdlib. h > #define MAX 10
class stack { private : int sp, a [ MAX ]; public : void init ( ); void push ( int ); void pop ( ); void display ( ); void count ( ); }; void stack : : init ( ) { sp = - 1; } void stack : : push ( int data) { if (sp = = ( MAX – 1 ) ) { cout<<"n STACK OVERFLOW. . . . n"; return; }
sp + + ; a [ sp ] = data; } void stack : : pop ( ) { if ( sp < 0 ) { cout<<"n STACK UNDERFLOW. . . n"; return; } cout<<"n POPED DATA IS : : : "<<a[sp]; sp - - ; } void stack : : display ( ) { cout << "n DATA PRESENT IN A STACK IS : : : n"; for ( int i = sp ; i > = 0 ; i - -) cout << a [ i ] <<"t"; } void stack : : count ( ) { cout<<"n NUMBER OF ELEMENTS IN A STACK ARE : : : "<<(sp+1); };
void main ( ) { stack ob; int data, ch; clrscr ( ); ob. init ( ); cout<<"n*****STACK OPERATIONS*****n"; cout<<"n 1. Push Operation"; cout<<"n 2. Pop Operation"; cout<<"n 3. Display Operation"; cout<<"n 4. Count Operation"; cout<<"n 5. Exit Operation"; cout<<"n*******************n"; do { cout<<"n ENTER YOUR CHOICE : : "; cin>>ch; switch ( ch ) { case 1: cout<<"n ENTER ELEMENT TO BE INSERTED : : : "; cin>>data; ob. push ( data ); break; case 2: ob. pop ( ); break; case 3: ob. display ( ); break; case 4: ob. count ( ); break; case 5: exit ( 0 ); defualt: cout<<"n. INVALID CHOICE "; } } while ( ch ! = 5 ); getch ( );
OUTPUT:
STACK USING LINKED LIST • • We will create a linked list and insert an element ‘ 10’ and address as ‘ 0’. using top for the first node. For second node insert data element ‘ 20’ and insert first node address at second node address field. For third node insert data element ‘ 30’ and insert second node address at third node address field. after thirty we will stop the process. If we want to print the elements 30, 20, 10 will be displayed, Thiss follows LIFO conceot.
Source code: #include<conio. h> #include<iostream. h> class st { public: struct node { int data; struct node *next; }*start, *temp, *top; st() { start=temp=top=NULL; } void create() { int d; cout<<"Enter data"; cin>>d; if(start==NULL) { start=new node; start->data=d;
start->next=NULL; top=start; } else { temp=new node; temp->data=d; temp->next=top; top=temp; } } void disp() { while(top!=NULL) { cout<<top->data<<"t"; top=top->next; } } }; void main() { st ob; int ch; clrscr();
while(ch) { cout<<"Enter ur choice"; cout<<"0 STOPn 1 CREATEn 2 READ"; cin>>ch; if(ch==1) ob. create(); else if(ch==2) ob. disp(); } } OUTPUT:
QUEUE USING ARRAYS • • Here we will take an array a[5], and two variables front, rear points to -1. WRITE: Ø Here will insert the element into the queue using rear variable. Ø Here check for the Overflow condition. READ: Ø Here we will retrieve the elements from the queue using front variable. Ø Here check for the Underflow condition. This follows the FIFO concept. rear -1 front 0 1 2 3 4
SOURCE CODE: /* Program To Implement Queue using Array */ #include< iostream. h > #include< conio. h > #include< process. h > #define MAX 10 class queue { private : int front, rear, a [ MAX ]; public : void init ( ); void write ( int ); void read ( ); void count ( ); void display ( ); }; void queue : : init ( ) { front = rear = - 1; } void queue : : write ( int data) { if ( rear = = ( MAX - 1 ) ) cout<<"n QUEUE IS OVERFLOW. . . "; else a [ + + rear ] = data; } void queue : : read ( ) { if( front = = rear ) cout<<"n QUEUE IS UNDERFLOW. . . "; else cout<<"n DELETED ELEMENT IN QUEUE IS : : "<<a[++front]; }
void queue : : count ( ) { cout<<"n NUMBER OF ELEMENTS IN A QUEUE ARE : : "<<(rear-front); } void queue : : display ( ) { cout<<"n ELEMENTS IN A QUEUE ARE: : "; for( int i = (front + 1); i < = rear; i + + ) cout<< a [ i ]<<"t"; } void main ( ) { queue ob; int ch, data; clrscr ( ); ob. init ( ); cout<<"n*****QUEUE OPERATIONS****n"; cout<<"n 1. Write "; cout<<"n 2. Read "; cout<<"n 3. Count"; cout<<"n 4. Display"; cout<<"n 5. Exit"; cout<<"*************n";
do { cout<<"n ENTER YOUR CHOICE : : "; cin>>ch; switch ( ch ) { case 1: cout<<"n ENTER ELEMENT TO BE INSERTED IN QUEUE : : "; cin>>data; ob. write ( data ); break; case 2: ob. read ( ); break; case 3: ob. count ( ); break; case 4: ob. display ( ); break; case 5: exit ( 0 ); break; default : cout<<"n INVALID CHOICE. . . "; } } while( ch ! = 5 ); getch ( ); }
OUTPUT:
Queue using linked list • • Here we will create linked list with ‘n’ nodes one after another 10, 20, 30 etc. If we try to print the elements it will display as 10, 20, 30. which follows FIFO concept.
SOURCE CODE: /* Program To Implement Queue using Linked List */ #include < iostream. h > #include< conio. h > #include < alloc. h > #define NULL 0 class node { int data; node *next; public: void create ( node *); void print ( node *); }; void node : : create (node *list) { cout<<"n ENTER THE INPUT NO : : "; cout<<"n TYPE 999 AT THE END : : "; cin>>list->data; if(list -> data = = 999) list->next = NULL; else { list -> next = new node; create( list -> next); } return;
void node : : print (node *list) { if( list -> next ! = 0) { cout<< list->data; cout<<"->"; } else return; print( list -> next); } void main ( ) { node *head, ob; clrscr ( ); head = new node; ob. create ( head ); cout<<"n QUEUE ELEMENTS ARE: : "; ob. print( head ); cout<<"999"; getch ( ); }
OUTPUT:
BINARY TREE USING RECURSION • • • A binary tree is a tree data structure in which each node has at most two children. Typically the child nodes are called left and right. Binary trees are commonly used to implement binary search trees and binary heaps. Starting at the root of a binary tree, there are three main steps that can be performed and the order in which they are performed define the traversal type. There are 3 types of traversals: Ø 1. Pre-Order Ø 2. In-Order Ø 3. Post-Order To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node: 1. Visit the root. 2. Traverse the left sub tree. 3. Traverse the right sub tree. To traverse a non-empty binary tree in in order, perform the following operations recursively at each node, starting with the root node: 1. Traverse the left sub tree. 2. Visit the root. 3. Traverse the right sub tree. To traverse a non-empty binary tree in post order, perform the following operations recursively at each node, starting with the root node: 1. Traverse the left sub tree. 2. Traverse the right sub tree. 3. Visit the root.
BINARY TREE: 15 7 22 Preorder: - 15, 7, 22 will be displayed. Post order: - 7, 22, 15 will be displayed. In order: - 7, 15, 22 will be displayed.
SOURCE CODE: /* Program To Implement Binary Tree Traversing */ #include < iostream. h > #include < conio. h > class bstree { public: struct node { int data; node *left; node *right; }*head; void create (node *); void inorder (node *); void preorder (node *); void postorder (node *); }; void* bstree: : create(node *list) { node *temp 1, *temp 2; int val; if(list = = NULL) { list = new node; cout<<"n. Enter Data Element: : "; cin>>list->data; list -> left = list -> right = NULL; } else
{ cout<<"n enter the data element"; cin>>val; temp 1 = list; while( temp 1 ! = NULL ) { temp 2 = temp 1; if(temp 1 -> data > val) temp 1 = temp 1 -> left; else temp 1 = temp 1 -> right; } if(temp 2 -> data > val) { temp 2 -> left = new node; temp 2 = temp 2 -> left; temp 2 -> data = val; temp 2 -> left = temp 2 -> right = NULL; } else { temp 2 -> right = new node; temp 2 = temp 2 -> right; temp 2 -> data = val; temp 2 -> left = temp 2 -> right = NULL; } } return (list); }
void bstree: : inorder(node *root) { if( ! root ) return; inorder( root -> left ); cout<<root->data<<"t"; inorder( root -> right ); } void bstree: : preorder(node*root) { if( ! root ) return; cout<<root->data<<”t”; preorder( root -> left ); preorder( root -> right); } void bstree: : postorder(node*root) { if( ! root) return; postorder( root -> left ); postorder( root -> right ); cout<<root->data<<”t”; }
void main ( ) { node n, *head; head = NULL; clrscr ( ); cout<<"n. Create A Binary Treen"; head=n. create ( head ); cout<<"n the inorder traversal gives the following nodes"; n. inorder ( head ); getch ( ); } OUTPUT:
BINARY SEARCH TREE 15 7 22 ØA tree having left child less than parent and right child grater than the parent. ØTraversals are same as binary tree.
SOURCE CODE: /* Program to implement Binary search tree */ #include < iostream. h > #include < conio. h > class btree { private : struct btreenode { btreenode *leftchild ; int data ; btreenode *rightchild ; } *root; public: btree ( ) ; void buildtree ( int num ) ; static void insert ( btreenode **sr, int num ) ; void traverse ( ) ; static void inorder ( btreenode *sr ) ; static void preorder ( btreenode *sr ) ; static void postorder ( btreenode *sr ) ; static void del ( btreenode *sr ) ; ~btree ( ) ; } ;
btree : : btree ( ) { root = NULL ; } void btree : : buildtree ( int num ) { insert ( &root, num ) ; } void btree : : insert ( btreenode **sr, int num ) { if ( *sr == NULL ) { *sr = new btreenode ; ( *sr ) -> leftchild = NULL ; ( *sr ) -> data = num ; ( *sr ) -> rightchild = NULL ; return ; } else // search the node to which new node will be attached { // if new data is less, traverse to left if ( num < ( *sr ) -> data ) insert ( & ( ( *sr ) -> leftchild ), num ) ; else // else traverse to right insert ( & ( ( *sr ) -> rightchild ), num ) ; } return ; }
void btree : : traverse( ) { cout << "n. IN - ORDER TRAVERSAL : : " ; inorder ( root ) ; cout << "n. PRE - ORDER TRAVERSAL : : " ; preorder ( root ) ; cout << "n. POST - ORDER TRAVERSAL : : " ; postorder ( root ) ; } void btree : : inorder ( btreenode *sr ) { if ( sr != NULL ) { inorder ( sr -> leftchild ) ; cout << "t" << sr -> data ; inorder ( sr -> rightchild ) ; } else return ; } void btree : : preorder ( btreenode *sr ) { if ( sr != NULL ) { // print the data of a node cout << "t" << sr -> data ; // traverse till leftchild is not NULL preorder ( sr -> leftchild ) ; // traverse till rightchild is not NULL preorder ( sr -> rightchild ) ; }
else return ; } void btree : : postorder ( btreenode *sr ) { if ( sr != NULL ) { postorder ( sr -> leftchild ) ; postorder ( sr -> rightchild ) ; cout << "t" << sr -> data ; } else return ; } btree : : ~btree( ) { del ( root ) ; } void btree : : del ( btreenode *sr ) { if ( sr != NULL ) { del ( sr -> leftchild ) ; del ( sr -> rightchild ) ; } delete sr ; }
void main( ) { btree bt ; int req, i = 1, num ; clrscr(); cout << "n SPECIFY THE NUMBER OF ITEMS TO BE INSERTED : : " ; cin >> req ; while ( i + + <= req ) { cout << "n ENTER THE DATA : : " ; cin >> num ; bt. buildtree ( num ) ; } bt. traverse( ) ; getch(); } OUTPUT:
SPARSE MATRIX AIM: Write a program in C++ to implement ADDITION and MULTIPLICTION of two SPARSE matrixes. THEORY: If a lot of elements from a matrix have a value 0 then the matrix is known as SPARSE MATRIX. If the matrix is sparse we must consider an alternate way of representing it rather the normal row major or column major arrangement. This is because if majority of elements of the matrix are 0 then an alternative through which we can store only the non-zero elements and keep intact the functionality of the matrix can save a lot of memory space. Example: Sparse matrix of dimension 7 x 7. COLUMNS 0 1 2 3 4 5 6 0 0 -5 0 0 0 1 0 4 0 0 7 2 0 0 9 0 0 ROWS 3 0 2 0 0 0 4 1 0 2 0 0 5 0 0 0 0 6 0 0 8 0 0
A common way of representing non-zero elements of a sparse matrix is the 3 -tuple forms. In this form each non-zero element is stored in a row, with the 1 st and 2 nd element of this row containing the row and column in which the element is present in the original matrix. The 3 rd element in this row stores the actual value of the nonstore element. For example 3 -tuple representation of the sparse matrix as shown in below. int spmat[10][3]={ 7, 7, 9, 0, 3, -5, 1, 1, 4, 1, 6, 7, 2, 4, 9, 3, 1, 3, 3, 3, 2, 4, 0, 11, 4, 2, 2, 6, 2, 8 }
SOURCE CODE: /*Program to demonstrate addition and multiplication of Two Sparse Matrix */ #include < iostream. h > #include < conio. h > #define x 25 class sparce { private: int a [ x ], b [ x ], c [ x ], m, n, p, q; public: void init ( ); void input ( ); void add ( ); void mul ( ); void display ( int [25], int ); void convert( int [25], int ); }; void sparce : : init ( ) { int i, j; for(i = 0; i < x; i + + ) for( j = 0; j < x; j + +) c [ i ] [ j ] = 0; }
void sparce : : input() { int i, j; cout<<"n. Enter order Of First matrix: : "; cin>>m>>n; cout<<"n. Enter order Of Second matrix: : "; cin>>p>>q; cout<<"n. Enter"<<m*n<<"Elements Into First Matrixn"; for(i=0; i<m; i++) for( j = 0; j < n; j + + ) cin>> a[ i ] [ j ]; cout<<"n. Enter"<<p*q<<"Elements Into Second Matrixn"; for(i = 0; i < p ; i + + ) for ( j = 0; j < q ; j + + ) cin>>b [ i ] [ j ]; } void sparce : : add ( ) { int i, j; if( m = = p && n = = q ) { for( i = 0 ; i < m ; i + + ) for( j = 0; j < n; j + + ) c[ i ] [ j ] = a [ i ][ j ] + b [ i ] [ j ]; convert( c, m, n); } else cout<<"n. Addition Is Not Possible"; }
void sparce : : mul ( ) { int i, j, k; if(n = = p) { for( i = 0; i < m; i + +) for( j = 0; j < q; j + + ) for( k = 0; k < n; k + + ) c[ I ] [ j ] + = a [ I ] [ k ] * b [ k ] [ j ]; convert(c, m, n); } else cout<<"n Multiplecation Is Not Possible"; } void sparce : : display(int c[25], int m, int n) { int i, j; for( i = 0 ; i < m; i + + ) { for( j = 0 ; j < n ; j + + ) cout<<c [ i ] [ j ]<<"t"; cout<<"n"; } }
void sparce : : convert(int c[25], int m, int n) { int i, j, k = 1, t = 0; int sp[25]; for( i = 0 ; i < m ; i + +) for( j = 0 ; j < n ; j + + ) if(c [ i ] [ j ] ! = 0 ) { sp [ k ] [ 0 ] = i; sp [ k ] [ 1 ] = j; sp [ k ] [ 2 ] = c [ i ] [ j ]; k + + ; t + + ; } sp[ 0 ] = m; sp[ 0 ] [ 1 ] = n; sp[ 0 ] [ 2 ] = t; display( sp, k, 3); } void main ( ) { sparce ob; clrscr ( ); ob. init ( ); ob. input ( ); cout<<"n. Addition of Two Sparce Matrixn"; ob. add ( ); ob. init ( ); cout<<"n. Multiplecation Of Two Sparce Matrixn"; ob. mul ( ); getch ( ); }
OUTPUT:
INFIX TO POSTFIX CONVERTIONic • • • Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P. Step 1. Push “(“ onto stack and add “)” to the end of Q. 2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until the stack is empty. 3. If an operand is encountered , add it to p. 4. If a left parenthesis is encountered , push it onto stack. • 5 If an operator * is encountered , then: a. repeatedly pop from stack and top each operator (on the top of stack ) which has the same precedence or higher precedence than *. b. Add * to stack. • 6. If a right parenthesis is encountered , then: a. repeatedly from stack and add to P each operator (on the top of stack) until a left parenthesis is encountered. b. remove the left parenthesis [ Do not add the left parenthesis top] [End of if structure] [End of step 2 loop] • 7. Exit
(A+(B*C-(D/E^F)*G)*H) Symbol scanned 1 A 2 + 3 ( 4 B 5 * 6 C 7 8 ( 9 D 10 / 11 E 12 ^ 13 F 14 ) 15 * 16 G 17 ) 18 * 19 H 20 ) stack ( (+ (+( (+(* (+((+(-(/ (+(-(/^ (+((+(-* (+ (+* Expression P A AB AB ABC*D ABC*DEF ABC*DEF^/G*ABC*DEF^/G*-H*+
SOURCE CODE: /* Program To implement infix to postfix Expression */ #include < iostream. h > #include< process. h > #include < conio. h > char stack[30], postfix[30], infix[30]; int top = - 1; int pri( char x ) { int value; switch ( x ) { case ')': value=0; break; case '+': case '-': value=1; break; case '*': case '/': case '%': value=2; break; case '^': value=3; break; case '(': value=4; break; default: cout<<"INVALID EXPRESSION !!!!!!"; exit(1); } return value; }
void push ( char x ) { top = top + 1; stack [top] = x; } char stacktop ( ) { return stack [ top ]; } int isalnum (char x) { return ( (x>='0' && x<='9') ||( x>='a' && x<='z') || ( x>='A' && x<='Z')); } char pop( ) { return stack[top - - ]; }
void intopost(char infix[ ], char postfix[ ]) { int i, j=0; char c, pc; for ( i = 0; ( c = infix[ i ] ) != '