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 illustrate the implementation of Reflection Transformation about x-axix, y-axis and w.r.t origin

  2. Program that reads a number and displays it with its digits in reverse order using do-while loop

  3. Program to illustrate unary operator increment operator overloading with return type

  4. Program to illustrate the use of default argument function

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

  6. Program to convert points to rectangle coordinates and polar coordinates

  7. Program to find largest element of an array

  8. Program to draw a line using Bresenhams Line Algorithm (BLA) for lines with slopes positive and less than 1

  9. Program to convert an Infix Expression into a Postfix Expression using Linked List as a Stack

  10. Program to input matrix table

  11. Program that performs array operations like insert,delete, search, sort, merge and display

  12. PROGRAM FOR ACCEPTING MEASUREMENTS IN METERS AND FEETS AND SUMMING THEM UP IN THE FORMAT THAT THE USER WISHES

  13. Program that reads a number and prints even if the number is even and prints odd if the number is odd.

  14. Program to tokenized a given string

  15. Program to estimate the Integral value of the function at the given points from the given data using Trapezoidal Rule

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

  17. Program to illustrate the binary operator(+) overloading without creating a new object

  18. Program to draw a Circular Arc using Trigonometric Method

  19. Program to draw a Luddo Board

  20. Program to find the value of sin at any given angle

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