C++ Programming Tutorial

 
 
 

Data File Structure Programs

Array

Insert, edit, delete, append, display, Srch. Insert, delete, merge, delete multiple occurrences Arrays as a Stack in graphics

Stack

Stack operations using array Stack using static memory allocation Stack using dynamic memory allocation Double ended link list as a stack Lnked list as a Stack Infix expr. to Postfix expr. Postfix expr. into an Infix expr. Arrays as a Stack in graphics Stack as an Arithmetic expr. Evaluater Graphical Rep. of Stack Stack to traverse - inodr, postodr, preodr

Queue

Queue using static memory allocation Queue using dynamic memory allocation Circular queue Linked list as a Queue Double Ended linked list as a Queue Graphical Rep. of Queue Arrays as a Linear Queue Array as a Circular Queue Arrays as a Linear Queue ( in graphics ) Arrays as a Circular Queue ( in graphics )

Linked List

Singly link list Circular linked list Doubly link list Linked list as a Queue Linked list as a Stack Double Ended linked list as a Queue Double Ended linked list as a Stack Infix to Postfix - Linked List as Stack Circular doubly link list Single Ended Linked List - Sorting in both odr Hashing - double ended Linked List Sort of link list

Tree

Linked List as a Binary Srch. Tree Set Class using Binary Srch. Tree Maximum depth of Binary Srch. Tree Minimum Spaning Tree Prims algo - minimum spanning tree Traverse binary tree - inodr, preodr, post Find number in binary Srch. tree display levell

Sorting

Bubble Sort Selection Sort Insertion Sort Radix Sort Merge Sort Quick Sort Heap Sort Linear Sort Shell Sort Topological Sort

Searching

Linear Srch. or Sequential Srch. Binary Search Breadth First Search Traversal Depth First Search Traversal Shortest Path-Given Source-Destination-Dijkstras

Hashing

Simple implementation of Hashing Hashing using double-ended Linked List Hashing using Mid-Square Method Example of Hashing n term of the fibonacci series using recursion

Recursion

Factorial of the given number using recursion Mystery of Towers of Hanoi using recursion
 
 
#include <stdio.h>
#include<iostream.h>
#include <conio.h>
#include<alloc.h>
#include<process.h>
#include<ctype.h>

// precedence matrix
char prec[6][6] = {\'x\',\'+\',\'*\',\'(\',\')\',\']\',
            \'+\',\'>\',\'<\',\'<\',\'>\',\'>\',
            \'*\',\'>\',\'>\',\'<\',\'>\',\'>\',
            \'(\',\'<\',\'<\',\'<\',\'=\',\'x\',
            \')\',\'>\',\'>\',\'x\',\'>\',\'>\',
            \'[\',\'<\',\'<\',\'<\',\'x\',\'=\'};

struct node
{
    char symbol;
    struct node* lptr;
    struct node* rptr;
}*x, *temp;

struct stk
{
    char oper;                // operations on stack
    struct node* optr;            //
}stack[10];

char exp[20];        // expression to be scanned
int top=-1;                // top of stack
int sb=1;                // stack base pointer
int ssm=0;                // source string marker
int cur=1;                // current string

void main()
{
    char chpr(char,char);
    void inorder(struct node *);
    void postorder(struct node *);
    void preorder(struct node *);
    char res;
    clrscr();
    cout<<\"enter the expression between [] brackets\\n\";
    gets(exp);
    top++;

    stack[top].oper = \'[\';
    while(1)
    {
         if (isalpha(exp[cur]))
         {
        x = (struct node *)malloc(sizeof(struct node));
        x->symbol = exp[cur];
        x->lptr = NULL;
        x->rptr = NULL;
        stack[top].optr = x;
        ssm++;
        }
        else
        {
        res = chpr(stack[top].oper , exp[cur]);
        while(res == \'>\')
        {
          x = (struct node *)malloc(sizeof(struct node));
          x->symbol = stack[top].oper;
          x->lptr = stack[top-1].optr;
          x->rptr = stack[top].optr;
          top--;
          stack[top].optr = x;
          res = chpr(stack[top].oper , exp[cur]);
        }
        if(res == \'<\')
        {
          top++;
          stack[top].oper = exp[cur];
        }
        if(res == \'=\')
        {
          if(stack[top].oper == \'[\')
          break;
          if(stack[top].oper == \'(\')
          {
            temp = stack[top].optr;
            top--;
            stack[top].optr = temp;
          }
        }
        }
       cur++;
    }
node * root=stack[0].optr;

cout<< \"THE OUTPUT OF THE PARSING FROM DIFFERENT TRAVERSALS ARE:\"<<endl;
cout<<endl;
cout<<\"INORDER TRAVERSAL OF TREE\"<<endl;
inorder(root);
cout<<endl;
cout<<\"POSTORDER TRAVERSAL OF THE TREE\"<<endl;
postorder(root);
cout<<endl;
cout<<\"PREORDER TRAVERSAL OF THE TREE\"<<endl;
preorder(root);

getch();

}


char chpr(char x,char y)
{
    int i=0,j=0;
    while(prec[i][0] != x && i<6)
    i++;
    while(prec[0][j] != y && j<6)
    j++;
    return prec[i][j];
}

void preorder(struct node * root)
{
    struct node * ptr;
    ptr = root;
    if(ptr != NULL)
     {
    cout<<ptr->symbol;
    preorder(ptr->lptr);
    preorder(ptr->rptr);
    }
}

void postorder(struct node * root)
{
    struct node * ptr;
    ptr = root;
    if(ptr!=NULL)
    {
      postorder(ptr->lptr);
      postorder(ptr->rptr);
      cout<<ptr->symbol;
    }
}

void  inorder( node * root)
{
    struct node * ptr;
    ptr = root;
    if(ptr!=NULL)
    {
      inorder(ptr->lptr);
      cout<<ptr->symbol;
      inorder(ptr->rptr);
    }
}

/*OUTPUT
enter the expression between [] brackets
[a+b*(c+d)]
THE OUTPUT OF THE PARSING FROM DIFFERENT TRAVERSALS ARE:

INORDER TRAVERSAL OF TREE
a+b*c+d
POSTORDER TRAVERSAL OF THE TREE
abcd+*+
PREORDER TRAVERSAL OF THE TREE
+a*b+cd
*/

    Related Post:
  1. Program to perform insertion sort

  2. Program to estimate value of First Derivative of the function at the given points from the given data using Backward Difference Formula , Forward diff

  3. Program of addition and subtraction of large numbers

  4. Program to coumputes and displays the factorial of the given number ( using Recursive Algorithm )

  5. Program to estimate the value of Second Derivative of the function at the given points from the given data using Central Difference Formula of order 2

  6. Program to fill different types of geometric shapes using Flood Fill Algorithm (Using Linked-List)

  7. Program of DDA line drawing algorithm

  8. To parse a string using First and Follow algorithm and LL-1 parser

  9. Program to illustrate the implementation of 3D Rotation Transformation along z-axis

  10. Program to construct Newtons Backward Difference Interpolation Formula from the given distinct equally spaced data points

  11. Program to implement the Prims Algorithm to solve Minimum Spanning Tree Problem (MST) using Graphics

  12. Program to illustrate the implementation of arrays as a Stack

  13. Program to print an array before and after adding 5 to its contents using pointers

  14. Program that reads a numbers , computes and displays the factorial of the given number using recursion

  15. Program of traversing a binary tree in inorder, preorder and postorder fashion

  16. Program to illusrate data conversion b/w built-in data types and user defined data types (int & float)

  17. Program to create a queue using dynamic memory allocation

  18. Program to illustrate use of arrays as data items in classes

  19. Program of scaling about origin

  20. Program to illustrate the role of constructor in classes

 
 
Didn't find what you were looking for? Find more on Program of stack to traverse in inorder, postorder and preorder