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
 
 

Image

#include <iostream.h>
#include <conio.h>
#define MAX_NODE 50

struct node{
    int vertex;
    int weight;
    node *next;
};

struct fringe_node{
    int vertex;
    fringe_node *next;
};

node *adj[MAX_NODE]; //For storing Adjacency list of nodes.
int totNodes; //No. of Nodes in Graph.
const int UNSEEN=1,FRINGE=2,INTREE=3; //status of node.
int status[MAX_NODE];//status arr for maintaing status.
fringe_node *fringe_list;//singly link list

void createGraph(){
    node *newl,*last;
    int neighbours;
    cout<<\"\\n\\n---Graph Creation---\\n\\n\";
    cout<<\"Enter total nodes in graph : \";
    cin>>totNodes;
    for(int i=1;i<=totNodes;i++){
        last=NULL;
        cout<<\"Total Neighbours of \"<<i<<\" : \";
        cin>>neighbours;
        for(int j=1;j<=neighbours;j++){
            newl=new node;
            cout<<\"Neighbour #\"<<j<<\" : \";
            cin>>newl->vertex;
            cout<<\"Weight    #\"<<j<<\" : \";
            cin>>newl->weight;
            newl->next=NULL;
            if(adj[i]==NULL)
                adj[i]=last=newl;
            else{
                last->next = newl;
                last = newl;
            }
        }
    }
}

//Insert node in a fring_list at Begining.
void Insert_Beg(int item){
      fringe_node *newl;
      newl = new fringe_node;
      newl->vertex = item;
      newl->next = NULL;
      newl->next = fringe_list;
      fringe_list = newl;
}

//Delete element at pos position from fringe_list.
void del(int pos){
   //to points to previous node from where
   //to insert
   int i;
   fringe_node *tmp,*delnode;
   for(i=1,tmp=fringe_list; i < (pos-1); tmp=tmp->next,i++);

   delnode = tmp->next;
   tmp->next = tmp->next->next;
   delete(delnode);
}

void print_path(int s,int d,int parent[]){
    if(d==s)
       cout<<\"    \"<<s;
    else{
       print_path(s,parent[d],parent);
       cout<<\"-->\"<<d;
    }
}

void shortestPath(){
    int i,x,parent[MAX_NODE],edge_count,w,wt,v;
    int min_dist,y,dist[MAX_NODE],stuck;
    int source,destination;
    node *ptr1;
    fringe_node *ptr2;

    cout<<\"Enter Source Node : \";
    cin>>source;
    cout<<\"Enter Destination Node : \";
    cin>>destination;

    fringe_list=NULL;
    for(i=1;i<=totNodes;i++){
        status[i]=UNSEEN;
        parent[i]=0;
    }

    status[source]=INTREE;
    dist[source]=0;
    x=source;
    stuck=0;
    while( (x != destination) && (!stuck))
    {
        ptr1=adj[x];
        while(ptr1!=NULL){
            y=ptr1->vertex;
            wt=ptr1->weight;
            if((status[y]==FRINGE) && (dist[x]+wt < dist[y]))
            {
                parent[y]=x;
                dist[y] = dist[y] + wt;
            }
            else if(status[y]==UNSEEN){
                status[y]=FRINGE;
                parent[y]=x;
                dist[y] = dist[y] + wt;
                Insert_Beg(y);
            }
            ptr1=ptr1->next;
        }
        if(fringe_list==NULL)
            stuck=1;
        else{
            x=fringe_list->vertex;
            min_dist=dist[x];
            ptr2=fringe_list->next;
            while(ptr2!=NULL){
                w=ptr2->vertex;
                if(dist[w] < min_dist)
                {
                    x=w;
                    min_dist=dist[w];
                }
                ptr2=ptr2->next;
            }
            del(x);
            status[x]=INTREE;
        }
    }
    if(parent[destination]==0)
        cout<<\"No path from \"<<source<<\" to \"<<destination;
    else
    {
        cout<<\"\\n\\nShortest Path\\n\";
        print_path(source,destination,parent);
    }
}


void main(){
    clrscr();
    cout<<\"*****Minimum Spaning Tree (MST)*****\\n\";
    createGraph();
    cout<<\"\\n===Minimum Spaning Tree===\\n\";
    shortestPath();
    getch();
}

    Related Post:
  1. Program to estimate the value of Third Derivative of the function at the given points from the given data using Central Difference Formula of order 2

  2. Program to fill different types of geometric shapes using Flood Fill Algorithm

  3. Program to illustrate the use of call-by-refrence method using pointers

  4. Program of KSCANNER

  5. Program to read a Non-Linear equation in one variable, then evaluate it using Newton-Raphson Method and display its kD accurate root

  6. Program to illustrate the use of parameterless function

  7. Program to illustrate the use of the continue statement

  8. Program of doubly link list

  9. Program that reads marks of a students and computes and displays grade

  10. Program to draw a line using Digital Differential Analyzer (DDA) Algorithm

  11. Program that prints first 20 integers (using while loop)

  12. Program to read a Non-Linear equation in one variable, then evaluate it using Secant Method and display its kD accurate root

  13. Program to illustrate the use of call-by-refrence method in functions

  14. Program to show the implementation of Hashing with Insert, Delete and search functionality using double-ended Linked List

  15. Program that prints all the even numbers b/w 0 to 50 ( using while, do-while and for loop )

  16. Program that uses this DFA and validates whether an entered string is valid integer or not.

  17. Program to illustrate an example of Polymorphism ( Pure Virtual functions ).

  18. Program of cohen sutherland Line clipping Algorithm

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

  20. Program to solve the mystery of Towers of Hanoi using recursion

 
 
Didn't find what you were looking for? Find more on Program of Shortest Path for Given Source and Destination (using Dijkstras Algo.)