Posts

Showing posts with the label dspm

Program to traverse graphs using BFS.

Image
#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int main() { int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10],m; cout<<"Enter no of vertices "; cin>>n; cout<<"Enter no of edges "; cin>>m; cout<<"\nEDGES \n"; for(k=1;k<=m;k++) { cin>>i>>j; cost[i][j]=1; } cout<<"\nEnter initial vertex "; cin>>v; cout<<"Breadth first search is \n"; cout<<v; visited[v]=1; k=1; while(k<n) { for(j=1;j<=n;j++) if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1) { visit[j]=1; qu[rare++]=j; } v=qu[front++]; cout<<v<<" "; k++; visit[v]=0;visited[v]=1; } cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/"; return 0; } Output:

Program to traverse graphs using DFS.

Image
#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int main() { int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],m; cout<<"Enter no of vertices : "; cin>>n; cout<<"Enter no of edges : "; cin>>m; cout<<"\nEDGES \n"; for(k=1;k<=m;k++) { cin>>i>>j; cost[i][j]=1; } cout<<"Enter initial vertex : "; cin>>v; cout<<"Depth first search order is : "; cout<<v<<" "; visited[v]=1; k=1; while(k<n) { for(j=n;j>=1;j--) if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1) { visit[j]=1; stk[top]=j; top++; } v=stk[--top]; cout<<v<< " "; k++; visit[v]=0; visited[v]=1; } cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/"; return 0; } Output:

Program to traverse a Binary search tree in Pre-order, In-order and Post-order.

Image
#include<stdlib.h> #include<iostream> #include<stdio.h> using namespace std; struct node { int data; struct node* left; struct node* right; }; struct node* newNode(int data) { struct node* node=(struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } void printPostorder(struct node* node) {     if(node==NULL)         return; printPostorder(node->left); printPostorder(node->right); cout<<" "<<node->data; } void printInorder(struct node* node) {     if (node==NULL)         return;     printInorder(node->left);     cout<<" "<<node->data;     printInorder(node->right); } void printPreorder(struct node* node) {     if (node == NULL)         return;     cout<<" "<<no...

Program to sort an array of integers in ascending order using quick sort.

Image
#include<iostream> #include<conio.h> using namespace std; int a[10],l,u,i,j; void quick(int *,int,int); int main() { cout<<"Enter 10 Elements\n"; for(i=0;i<10;i++) cin>>a[i]; l=0; u=9; quick(a,l,u); cout <<"\nSorted Elements\n"; for(i=0;i<10;i++) cout<<a[i]<< " "; cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/"; return 0; } void quick(int a[],int l,int u) {    int p,temp; if(l<u) { p=a[l]; i=l; j=u; while(i<j) {    while(a[i]<=p&&i<j) i++;    while(a[j]>p&&i<=j)    j--; if(i<=j)    { temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[j]; a[j]=a[l]; a[l]=temp; cout <<"\n"; for(i=0;i<10;i++) cout <<a[i]<<" "; quick(a,l,j-1); quick(a,j+1,u); } } ...

Program to sort an array of integers in ascending order using selection sort.

Image
#include<iostream> using namespace std; int main() {     int i,j,n,loc,temp,min,a[30];     cout<<"Enter the number of elements:";     cin>>n;     cout<<"\nEnter the elements\n";     for(i=0;i<n;i++)    {        cin>>a[i];    }     for(i=0;i<n-1;i++)    {        min=a[i];        loc=i;        for(j=i+1;j<n;j++)        {            if(min>a[j])            {                min=a[j];                loc=j;            }        }        temp=a[i];        a[i]=a[loc];   ...

Program to sort an array of integers in ascending order using insertion sort.

Image
#include<iostream> #include<conio.h> using namespace std; int main() {     int a[16], i, j, k, temp;     cout<<"Keep Entering the elements\n";    for (i = 0; i < 16; i++)    {        cin>>a[i];    }    for (i = 1; i < 16; i++)    {        for (j = i; j >= 1; j--)        {            if (a[j] < a[j-1])            {                temp = a[j];                a[j] = a[j-1];                a[j-1] = temp;            }            else                break;        } }   ...

Program to sort an array of integers in ascending order using bubble sort.

Image
#include <iostream> using namespace std; void bubble(int arr[],int n) {   for(int i=0;i<n;++i)     for(int j=0;j<n-i-1;++j)    if (arr[j]>arr[j+1])    {        int temp=arr[j];        arr[j]=arr[j+1];        arr[j+1]=temp;    } } int main() {     int input[] = {10, 50, 21, 2, 6, 66, 802, 75, 24, 170};   int n=sizeof(input)/sizeof(input[0]);   bubble(input,n);     cout<<"Sorted Array : \n";  for(int i=0;i<n;++i)   cout<<input[i]<<" "; cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";     return 0; } Output:

Write a program to implement push and pop operations on a stack using linked list.

Image
#include <iostream> #include <stdio.h> #include <conio.h> using namespace std; struct node {    int data;    node *next; }*front=NULL,*rear=NULL,*p=NULL,*np=NULL; void push(int x) {     np=new node;     np->data=x;     np->next=NULL;    if(front==NULL)    {        front=rear=np;        rear->next=NULL;    }    else    {        rear->next=np;        rear=np;        rear->next=NULL;    } } int remove() {     int x;    if(front==NULL)    {        cout<<"empty queue\n";    }    else    {        p=front;        x=p->data; ...

Write a recursive function for Tower of Hanoi problem.

Image
#include<iostream> using namespace std; int main() { int n; void cal(int,char,char,char); cout<<"How many disks?"; cin>>n; cal(n,'A','B','C'); cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/"; } void cal(int n,char source,char inter,char dest) { if(n==1) cout<<"Disk 1 from "<<source<<" to "<<dest<<endl; else { cal(n-1,source,dest,inter); cout<<"Disk "<<n<<" from "<<source<<" to "<<dest<<endl; cal(n-1,source,dest,inter); } } Output:

Write a program to implement push and pop operations on a queue using linked list.

Image
#include<iostream> #include<stdio.h> #include<conio.h> using namespace std; struct node {     int data;     node *next; }*front = NULL,*rear = NULL,*p = NULL,*np = NULL; void push(int x) {     np = new node;     np->data = x;     np->next = NULL;     if(front == NULL)     {         front = rear = np;         rear->next = NULL;     }     else     {         rear->next = np;         rear = np;         rear->next = NULL;     } } int remove() {     int x;     if(front == NULL)     {         cout<<"empty queue\n";     }     else     {         p = front;         x = p->data;     ...

Program to illustrate the concept of templates.

Image
#include<iostream> using namespace std; template <class t> void display(t x) { cout<<"Displaying function of template : "<<x<<endl; } void display(int x) { cout<<"Explicit Display function : "<<x<<endl; } int main() { display(200); display(12.05); display('a'); cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/"; } Output:

Write a menu driven program to perform insertion and deletion operations in a single linked list

Image
#include<iostream> using namespace std; struct node { int info; struct node *link; }*start; class test { node *create_node(int); public: test() { start=NULL; } void insert_beg(); void insert(); void del(); void display(); }; node *test::create_node(int val) { node *temp; temp=new(struct node); if(temp==NULL) { cout<<"\nMemory not allocated\n"; } else { temp->info=val; temp->link=NULL; return temp; } } void test::insert_beg() { int val; node *temp,*s; cout<<"Enter the value:"; cin>>val; temp=create_node(val); s=start; if(start==NULL) { start=temp; temp->link=NULL; } else { temp->link=start; start=temp; } cout<<"\nNode Inserted!!\n"; } void test::insert() { int pos,val,count=0; node *temp,*s,*ptr; cout<<"\nEnter the position:"; cin>>pos; cout<<"\nEnter the value...

Write a program to implement insertion and deletion operations in a queue using linear array.

Image
#include<iostream> using namespace std; class test { int queue[10]={27,56,43,87,64}; int rear=4,front=0; public: void insert(){ if(rear==9){ cout<<"\nQueue is full"; exit(1); } else{ rear=rear+1; cout<<"\nEnter the Element:"; cin>>queue[rear]; } } int del(){ int item; if(rear==-1){ cout<<"\nQueue is empty"; exit(1); } else{ item=queue[front]; for(int i=front;i<rear;i++) queue[i]=queue[i+1]; rear=rear-1; return item; } } void display(){ if(rear==-1){ cout<<"\nQueue is empty"; exit(1); } else{ for(int i=0;i<=rear;i++) cout<<queue[i]<<" "; } cout<<endl; } }; int main(){ test t; int a=1,o; cout<<"\nThe Queue is:"; t.display(); while(a!=4){ cout<<"\n1. for INSERT\n2. for DELETE\n3...

Write a program to evaluate a postfix expression using stacks.

Image
#include<iostream> #include<stack> #include<string.h> using namespace std; class test { stack<int> s; char exp[20]; int len,a,b,res,op=0; public: void getdata() { cout<<"Enter the Expression( e.g. 24 56 + ) : "; gets(exp); len=strlen(exp); } bool opr(char ch) { if(ch=='+'||ch=='-'||ch=='*'||ch=='/') return true; else return false; } void result(int a,int b,char ch) { switch(ch) { case '+':res=a+b; break; case '-':res=a-b; break; case '*':res=a*b; break; case '/':res=a/b; break; } } void cal() { for(int i=0;i<len;i++) { if(exp[i]==' ') continue; else if(opr(exp[i])) { a=s.top(); s.pop(); b=s.top(); s.pop(); result(a,b,exp[i]); s.push(res); } else { int z=int(exp[i])-'0'; op=op*10+z; if(exp[i+1]==' ...

Write a program to convert an infix expression to a postfix expression using stacks.

Image
#include<iostream> #include<stack> #include<string.h> using namespace std; string InfixToPostfix(string expression); int HasHigherPrecedence(char op1, char op2); bool IsOperator(char C); bool IsOperand(char C); int main() { string expression; cout<<"Enter Infix Expression \n"; getline(cin,expression); string postfix = InfixToPostfix(expression); cout<<"Output = "<<postfix<<endl; cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/"; } string InfixToPostfix(string expression) { stack<char> S; string postfix = ""; for(int i = 0;i< expression.length();i++) { if(expression[i] == ' ' || expression[i] == ',') continue; else if(IsOperator(expression[i])) { while(!S.empty() && S.top() != '(' && HasHigherPrecedence(S.top(),expression[i])) { postfix+= S.top(); S.pop(); } S.push(expression[i...

Write a program to implement push and pop operations on a stack using linear array.

Image
#include<iostream> using namespace std; int stack[10]={27,56,43,87,64}; int top=4; void push(); int pop(); void display(); int main(){ int a,o; cout<<"Stack is:"; display(); cout<<"\n\n1. for PUSH\n2. for POP\n3. for DISPLAY\nEnter your choice:"; cin>>a; switch(a){ case 1:push();   cout<<"\nStack after operation is:";   display();   break; case 2:cout<<"\nElement POPED is:"<<pop();   cout<<"\nStack after operation is:";   display();   break; case 3:display();   break; } cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/"; return 0; } void push(){ if(top==9){ cout<<"\nStack is full"; exit(1); } else{ top=top+1; cout<<"\nEnter the Element:"; cin>>stack[top]; } } int pop(){ int item; if(top==-1){ cout<<"\nStack is...

Write a program to find the location of a given element using Binary Search

Image
#include<iostream> #include<conio.h> using namespace std; int main() { int arr[100],beg,mid,end,i,n,num; cout<<"Enter the size of an array "; cin>>n; cout<<"\nEnter the values in sorted order (asc or desc) \n"; for(i=0;i<n;i++) { cin>>arr[i]; } beg=0; end=n-1; cout<<"\nEnter a value to be searched in an array "; cin>>num; while(beg<=end) { mid=(beg+end)/2; if(arr[mid]==num) { cout<<"\nItem found at position "<<(mid+1); break; } else if(num>arr[mid]) { beg=mid+1; } else if(num<arr[mid]) { end=mid-1; } else { cout<<"Number does not found."; } } cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/"; return 0; } Output:

Write a program to find the location of a given element using Linear Search

Image
#include<iostream> using namespace std; int main() { int arr[10],n,i,o,e,count=0; cout<<"Enter the no. of elements to be enter:"; cin>>n; cout<<"Enter the elements:\n"; for(i=0;i<n;i++) { cin>>arr[i]; } cout<<"\nArray entered is :"; for(i=0;i<n;i++) cout<<arr[i]<<" "; cout<<"\nEnter the element to be Searched:";        cin>>e;        for(i=0;i<n;i++)   {        if(arr[i]==e)        count++;        }        if(count==0)        cout<<"\nElement not found";        else if(count==1)        cout<<"\nElement "<<e<<" exist "<<count<<" time in Array";        else        cout<<"\nElement "<<e<<" exist "<<count<<"...

Write a program to delete an element from a given whose value is given or whose position is given

Image
#include<iostream> using namespace std; int main() { int arr[10],n,i,o,p; cout<<"Enter the no.of elements to be enter:"; cin>>n; cout<<"Enter the elements:\n"; for(i=0;i<n;i++){ cin>>arr[i]; } cout<<"\nArray entered is :"; for(i=0;i<n;i++) { cout<<arr[i]<<" "; } cout<<"\nEnter the position of element to be deleted:";        cin>>p;        for(i=p;i<n;i++)        arr[i-1]=arr[i];        n--;   cout<<"\nThe Array after operation is:"; for(i=0;i<n;i++) cout<<arr[i]<<" "; cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/"; } Output:

Write a program to insert a new element at end as well as at a given position in an array.

Image
#include<iostream> using namespace std; int main() { int arr[50],size,insert,i,pos; cout<<"Enter the array size\n"; cin>>size; cout<<"Enter Array elements\n"; for(i=0;i<size;i++) { cin>>arr[i]; } cout<<"Enter The element to be inserted\n"; cin>>insert; cout<<"At which position(Enter the index number)\n"; cin>>pos; for(i=size;i>pos;i--) { arr[i]=arr[i-1]; } arr[pos]=insert; cout<<"Inserted Successfully!!\n"<<"Now the new array\n"; for(i=0;i<size+1;i++) { cout<<arr[i]<<" "; }    cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/"; } Output: