Our Feeds

Sunday 27 December 2015

AJITH KP

Queue using Circular Linked List in C++

Hi GuyZ,
     Queue is another important data structure which follows FIFO (First In First Out) method. Implementing queue using Circular Linked List is best practicing method. Unlike implementing SLL, CLL is better choice because CLL allows store address of first node at last nodes link.

     By implementing Queue using CLL, we need to keep track of any of nodes, that is first or last to track full Queue. The better choice is to track last node because last node holds the link to first node. So by tracking last node we can access last and first nodes fast, ie, first_node = last_node->link. While tracking first node, we need to traverse until last node get the last node. ie,

tmp = first_node;
while(tmp->link!=first_node){
     tmp = tmp->link;
}
last_node = tmp;

If you are tracking first node you have to follow above steps to get last node. But while tracking last node, get the first node is very easy.


Source Code

#include <iostream>
using namespace std;
/*
   Queue Implementation using Circular Linked List (CLL)
 Coded By Ajith Kp    (C) http://www.terminalcoders.blogspot.com
*/
struct node
{
 int data;
 node *link = NULL;
};
class Queue{
 node *end;
public:
 Queue(){
  end = NULL;
 }
 void menu(){
  int op = 0;
  cout<<"\t\t\tMENU\n1. Enqueue\n2. Dequeue\n3. Show\n4. Exit";
  while(op!=4){
   cout<<"\nOption: ";
   cin>>op;
   switch(op){
    case 1:
     enqueue();
     break;
    case 2:
     dequeue();
     break;
    case 3:
     show();
     break;
    case 4:
     break;
    default:
     cout<<"Error: Try again..";
     break;
   }
  }
 }
 void enqueue(){
  node *nn = new node;
  cout<<"Enter data: ";
  cin>>nn->data;
  if(end==NULL){
   end = nn;
   end->link=nn;
  }
  else{
   nn->link = end->link;
   end->link = nn;
   end = nn;
  }
  cout<<"Data "<<nn->data<<" is added to queue...";
 }
 void dequeue(){
  node *nn;
  if(end==NULL){
   cout<<"Error: Queue is empty...";
  }
  else{
   nn=end->link;
   if(end->link==end){
    end=NULL;
   }
   else{
    end->link = nn->link;
   }
   cout<<"Data: "<<nn->data<<" is deleted from Queue...";
   delete(nn);
  }
 }
 void show(){
  node *tmp;
  cout<<"Queue: ";
  if(end){
   tmp = end->link;
   cout<<tmp->data<<" ";
   tmp = tmp->link;
   while(tmp!=end->link){
    cout<<tmp->data<<" ";
    tmp=tmp->link;
   }
  }
  else{
   cout<<"Empty...";
  }
 }
};
int main(){
 Queue q;
 q.menu();
 return 0;
}

Saturday 26 December 2015

AJITH KP

Polynomial Representation Using Singly Linked List: Addition and Evaluation of Polynomials in C++

Hi GuyZ,
    Another important application of Singly Linked List is representation of sparse matrix efficiently. Polynomial also can be represent using arrays. But linked list will be better choice than arrays.

Addition of Polynomial:

Image Source: http://www.mathwarehouse.com/algebra/polynomial/how-to-add-subtract-polynomials.php



Evaluation of Polynomial:

Polynomial: 2x2+1x+10
Value of x = 3
=> 2 x 32+ 1 x 3 + 10
=> 2 x 9  + 3 + 10
=> 18 + 3 + 10
=> 31


Solution

#include <iostream>
#include <cmath>
using namespace std;
struct polynode{
 int coe, exp;
 polynode *link = NULL;
};
class polynomial{
 int i, n;
 polynode *tmp, *nn;
 polynode *poly;
public:
 polynomial(){
   poly = new polynode;
 }
 void read(){
  cout<<"Enter the number: ";
  cin>>n;
  cout<<"Enter items: ";
  nn = new polynode;
  cin>>nn->coe>>nn->exp;
  poly = nn;
  tmp = poly;
  for(i=1;i<n;i++){
   nn = new polynode;
   cin>>nn->coe>>nn->exp;
   tmp->link = nn;
   tmp = nn;
  }
 }
 void evaluation(){
  polynode *tmp = poly;
  int x, sum=0;
  cout<<"Enter value for `x`: ";
  cin>>x;
  while(tmp){
   sum+=(tmp->coe * pow(x, tmp->exp));
   tmp=tmp->link;
  }
  cout<<"Evaluation polynomial ";
  show();
  cout<<" result: "<<sum;
 }
 polynomial operator+(polynomial pol){
  polynomial sum;
  polynode *t1, *t2, *t;
  t1 = poly;
  t2 = pol.poly;
  nn = new polynode;
  if(t1->exp==t2->exp){
   nn->exp = t1->exp;
   nn->coe = t1->coe+t2->coe;
   t1 = t1->link;
   t2 = t2->link;
  }
  else if(t1->exp > t2->exp){
   nn->exp = t1->exp;
   nn->coe = t1->coe;
   t1 = t1->link;
  }
  else{
   nn->exp = t2->exp;
   nn->coe = t2->coe;
   t2 = t2->link;
  }
  sum.poly = nn;
  t = sum.poly;
  while(t1 && t2){
   nn = new polynode;
   if(t1->exp==t2->exp){
    nn->exp = t1->exp;
    nn->coe = t1->coe+t2->coe;
    t1 = t1->link;
    t2 = t2->link;
    t->link = nn;
    t = nn;
   }
   else if(t1->exp > t2->exp){
    nn->exp = t1->exp;
    nn->coe = t1->coe;
    t1 = t1->link;
    t->link = nn;
    t = nn;
   }
   else{
    nn->exp = t2->exp;
    nn->coe = t2->coe;
    t2 = t2->link;
    t->link = nn;
    t = nn;
   }
  }
  while(t1){
   nn = new polynode;
   nn->exp = t1->exp;
   nn->coe = t1->coe;
   t->link = nn;
   t = nn;
   t1 = t1->link;
  }
  while(t2){
   nn->exp = t2->exp;
   nn->coe = t2->coe;
   t->link = nn;
   t = nn;
   t2 = t2->link;
  }
  return sum;
 }
 void show(){
  tmp = poly;
  polynode *nl;
  char str;
  while(tmp->link){
   nl = tmp->link;
   str=(nl->coe<0)?' ':'+';
   cout<<tmp->coe<<"x^"<<tmp->exp<<str;
   tmp=tmp->link;
  }
  cout<<tmp->coe<<"x^"<<tmp->exp;
 }
};
class polyops{
public:
 void menu(){
  int op = 0;
  cout<<"\t\t\tMENU\n1. Add Polynomials\n2. Evaluate Polynomial\n3. Exit";
  while(op!=3){
   cout<<"\nOption: ";
   cin>>op;
   switch(op){
    case 1:  
     add(); 
     break;
    case 2:
     eval();
     break;
    case 3:
     break;
    default:
     cout<<"Error: Try again...";
     break;
   }
  }
 }
 void add(){
  polynomial p1, p2, sum;
  p1.read();
  cout<<"Polynomial 1: ";
  p1.show();
  cout<<endl;
  p2.read();
  cout<<"Polynomial 2: ";
  p2.show();
  cout<<endl;
  sum = p1+p2;  
  cout<<"Sum: ";
  sum.show();
 }
 void eval(){
  polynomial p;
  p.read();
  p.evaluation();
 }
};
int main(){
 polyops p;
 p.menu();
 return 0;
}
AJITH KP

Implementation of Stack using Singly Linked List in C++

Hi GuyZ,
     Implementation of stack is another important problem in Data Structures paper. Implementation of stack using Singly Linked List can be another problem you may have to solve.


Solution

#include <iostream>
using namespace std;
struct node{
 int data;
 node *link = NULL;
};
class stack{
 node *root;
public:
 stack(){
  root = NULL;
 }
 void push(){
  node *nn = new node;
  cout<<"Enter data: ";
  cin>>nn->data;
  nn->link = root;
  root = nn;
  cout<<"Data "<<nn->data<<" pushed to stack...";
 }
 void pop(){
  node *nn;
  if(root){
   nn = root;
   root = root->link;
   cout<<"Data "<<nn->data<<" is popped from stack...";
   delete(nn);
  }
  else{
   cout<<"Error: Stack is empty...";
  }
 }
 void empty(){
  if(root){
   cout<<"Stack is Not Empty...";
  }
  else{
   cout<<"Stack is Empty...";
  }
 }
 void show(){
  node *tmp = root;
  cout<<"Stack: ";
  if(root){
   while(tmp){
    cout<<tmp->data<<" ";
    tmp = tmp->link;
   }
  }
  else{
   cout<<"Empty...";
  }
 }
};
class stackops{
 stack s;
public:
 void menu(){
  int op = 0;
  cout<<"\t\t\tMENU\n1. PUSH\n2. POP\n3. Status\n4. Show\n5. Exit";
  while(op!=5){
   cout<<"\nOption: ";
   cin>>op;
   switch(op){
    case 1:
     s.push();
     break;
    case 2:
     s.pop();
     break;
    case 3:
     s.empty();
     break;
    case 4:
     s.show();
     break;
    case 5:
     break;
    default:
     cout<<"Error: Try again...";
     break;
   }
  }
 }
};
int main()
{
 stackops s;
 s.menu();
 return 0;
}

Thursday 24 December 2015

AJITH KP

Queue Implementation using Singly Linked List (SLL) in C++/CPP

Hi GuyZ,
     Queue implementation using Singly Linked List is most efficient because we can add unlimited items to queue unlike using arrays statically. It is another problem you may to solve during your MCA/BTech/BCA graduation years.



Solution

#include <iostream>
using namespace std;
/*
   Queue Implementation using Singly Linked List (SLL)
 Coded By Ajith Kp    (C) http://www.terminalcoders.blogspot.com
*/
struct node
{
 int data;
 node *link = NULL;
};
class Queue{
 node *start, *end;
public:
 Queue(){
  start = end = NULL;
 }
 void menu(){
  int op = 0;
  cout<<"\t\t\tMENU\n1. Enqueue\n2. Dequeue\n3. Show\n4. Exit";
  while(op!=4){
   cout<<"\nOption: ";
   cin>>op;
   switch(op){
    case 1:
     enqueue();
     break;
    case 2:
     dequeue();
     break;
    case 3:
     show();
     break;
    case 4:
     break;
    default:
     cout<<"Error: Try again..";
     break;
   }
  }
 }
 void enqueue(){
  node *nn = new node;
  cout<<"Enter data: ";
  cin>>nn->data;
  if(start==NULL){
   start = nn;
   end = nn;
  }
  else{
   end->link = nn;
   end = nn;
  }
  cout<<"Data "<<nn->data<<" is added to queue...";
 }
 void dequeue(){
  node *nn;
  if(start==NULL){
   cout<<"Error: Queue is empty...";
  }
  else{
   nn=start;
   if(start->link==NULL){
    start=NULL;
    end=NULL;
   }
   else{
    start=start->link;
   }
   cout<<"Data: "<<nn->data<<" is deleted from Queue...";
   delete(nn);
  }
 }
 void show(){
  node *tmp=start;
  cout<<"Queue: ";
  if(tmp){
   while(tmp){
    cout<<tmp->data<<" ";
    tmp=tmp->link;
   }
  }
  else{
   cout<<"Empty...";
  }
 }
};
int main(){
 Queue q;
 q.menu();
 return 0;
}
AJITH KP

Merge Sort in C++/CPP

Hi GuyZ,
     Merge sort is an efficient sorting algorithm which have a complexity of O(n log n). The merge sort algorithm sorts an array by spliting it into two equally sized subarrays, and sort the subarrays to merge them into sorted larger arrays until the full array is sorted.


Solution

#include <iostream>
using namespace std;
/*
         Merge Sort in C++/CPP
  Coded By Ajith Kp   (C) http://www.terminalcoders.blogspot.com
*/
class merge
{
 int a[1024], sort[1024], n, i, j, tmp, k;
public:
 void read(){
  cout<<"Enter limit: ";
  cin>>n;
  cout<<"Enter "<<n<<" items: ";
  for(i=0;i<n;i++){
   cin>>a[i];
  }
  divide(0, n-1);
 }
 void divide(int beg, int end){
  if((end-beg)>=1){
   int mid = (beg+end)/2;
   divide(beg, mid);
   divide(mid+1, end);
   arrange(beg, mid, end);
  }
 }
 void arrange(int beg, int mid, int end){
  i=k=beg;
  j=mid+1;
  while(i<=mid && j<=end){
   if(a[i]<a[j]){
    sort[k++]=a[i++];
   }
   else{
    sort[k++]=a[j++];
   }
  }
  while(i<=mid){
   sort[k++]=a[i++];
  }
  while(j<=end){
   sort[k++]=a[j++];
  }
  for(i=beg;i<=end;i++){
   a[i]=sort[i];
  }
 }
 void show(){
  cout<<"Sorted List: ";
  for(i=0;i<n;i++){
   cout<<a[i]<<" ";
  }
  cout<<endl;
 }
};
int main(){
 merge m;
 m.read();
 m.show();
 return 0;
}

Wednesday 23 December 2015

AJITH KP

Singly Linked List (SLL) Basic Operations Implementation in C++

Hello GuyZ,
     Singly Linked List is important data structure which is used for several purposes like storing sparse matrix, sparse polynomial, etc. Also it is used to create circular queues, stacks, etc. The basic operations performed on SLL are,

  1. Insert data
  2. Delete data
     Data can be inserted at any position in Linked List, also the deletion can be take place at any position.







Source Code

#include <iostream>
using namespace std;
struct node
{
 int data;
 node *link = NULL;
};
class llops
{
public:
 node *root;
 llops(){
  root = NULL;
 }
 void menu(){
  int op = 0;
  while(op!=8){
   cout<<"\n\t\t\tMENU\n1. Add @ Beg\n2. Add @ End\n3. Add at i th node\n4. Del from Beg\n5. Del from End\n6. Del ith node\n7. Traversal\n8. Exit\nOption: ";
   cin>>op;
   switch(op){
    case 1:
    addbeg();
    break;
    case 2:
    addend();
    break;
    case 3:
    addith();
    break;
    case 4:
    delbeg();
    break;
    case 5:
    delend();
    break;
    case 6:
    delith();
    break;
    case 7:
    show();
    break;
    case 8:
    break;
    default:
    cout<<"Wrong option... Try again...";
    break;
   }
  }
 }
 void addbeg(){
  node *nn;
  nn = new node;
  cout<<"Enter data: ";
  cin>>nn->data;
  nn->link = root;
  root = nn;
 }
 void addend(){
  node *nn, *tmp = root;
  cout<<"Enter data: ";
  nn = new node;
  cin>>nn->data;
  if(root==NULL){
   root = nn;
  }
  else{
   while(tmp->link!=NULL){
    tmp=tmp->link;
   }
   tmp->link = nn;
  }
 }
 void addith(){
  int i, j=0;
  node *nn, *pn, *tmp = root;
  cout<<"Enter i th position: ";
  cin>>i;
  i--;
  if(i==0){
   nn = new node;
   cout<<"Enter data: ";
   cin>>nn->data;
   nn->link = root;
   root = nn;
   return;
  }
  while(j<i && tmp!=NULL){
   pn = tmp;
   tmp=tmp->link;
   j++;
  }
  if(i==j){
   nn = new node;
   cout<<"Enter data: ";
   cin>>nn->data;
   nn->link = pn->link;
   pn->link = nn;
  }
  else{
   cout<<"Error: Cannot add item at position "<<i+1<<"\n";
  }
 }
 void delbeg(){
  node *cn;
  if(root){
   cn = root;
   root = root->link;
   cout<<cn->data<<" Deleted from beginning...\n";
   delete(cn);
  }
  else cout<<"Error: List is empty...\n";
 }
 void delend(){
  node *pn=NULL, *tmp = root;
  if(!root){
   cout<<"Error: List is empty...\n";
  }
  else{
   if(root->link==NULL){
    cout<<root->data<<" Deleted from end...\n";
    delete(root);
    root=NULL;
   }
   else{ 
    while(tmp->link){
     pn = tmp;
     tmp=tmp->link;
    }
    pn->link = NULL;
    cout<<tmp->data<<" Deleted from end...\n";
    delete(tmp);
   }
  }
 }
 void delith(){
  int i, j=0;
  node *nn, *pn, *tmp=root;
  cout<<"Enter i th position: ";
  cin>>i;
  i--;
  if(i==0 && root){
   cout<<root->data<<" Deleted from position "<<i+1<<"...\n";
   root = root->link;
  }
  else{
   while(j<i && tmp){
    pn = tmp;
    tmp=tmp->link;
    j++;
   }
   if(j==i && tmp){
    pn->link=tmp->link;
    cout<<tmp->data<<" Deleted from position "<<i+1<<"...\n";
    delete(tmp);
   }
   else{
    cout<<"Error: No nodes at position "<<i+1<<"...\n";
   }
  }
 }
 void show(){
  node *tmp = root;
  if(root){
   cout<<"\nList: ";
   while(tmp){
    cout<<tmp->data<<" ";
    tmp=tmp->link;
   }
   cout<<endl<<endl;
  }
  else{
   cout<<"\nList Empty\n\n";
  }
 }
};
int main(){
 llops l;
 l.menu();
 return 0;
}
AJITH KP

Evaluation of Expression for Bracket Matching in C++

Hi GuyZ,
     Evaluation of Expression for Bracket Matching using C++ is another application of `STACK` data structure. This is another question you may have to solve during your BE/B.Tech/MCA graduation.



Solution

#include <iostream>
#include <vector>
using namespace std;
/*
      Bracket Matching in C++
 Coded By Ajith Kp (C)  http://www.terminalcoders.blogspot.com
*/
template <typename T>
class stack{
 vector<T> v;
public:
 void push(T c){
  v.push_back(c);
 }
 T pop(){
  T top = v.back();
  v.pop_back();
  return top;
 }
 int empty(){
  if(v.empty()){
   return 1;
  }
  return 0;
 }
};
class bracketMatch{
 char str[1024], *ptr;
 stack<char> s;
public:
 void read(){
  cout<<"Enter expression: ";
  cin.getline(str, 1024);
 }
 void check(){
  ptr = str;
  int err = 0;
  char top;
  while(*ptr!='\0'){
   switch(*ptr){
    case '{':
    case '(':
    case '[':
     s.push(*ptr);
     break;
    case '}':
     if(s.empty()){
      err = 2;
     }
     else if((top=s.pop())!='{'){
      err = 1;
     }
     break;
    case ')':
     if(s.empty()){
      err = 2;
     }
     else if((top=s.pop())!='('){
      err = 1;
     }
     break;
    case ']':
     if(s.empty()){
      err = 2;
     }
     else if((top=s.pop())!='['){
      err = 1;
     }
     break;
   }
   if(err==1){
    cout<<"Error: You haven't closed "<<top<<" before "<<*ptr<<endl;
    return;
   }
   else if(err==2){
    cout<<"Error: You haven't opened ";
    if(*ptr==']'){
     cout<<"[";
    }
    else if(*ptr=='}'){
     cout<<"{";
    }
    else if(*ptr==')'){
     cout<<"(";
    }
    cout<<" to close"<<endl;
    return;
   }
   ptr++;
  }
  if(!s.empty()){
   cout<<"Error: You haven't closed ";
   while(!s.empty()){
    cout<<s.pop()<<" ";
   }
   cout<<" correctly"<<endl;
  }
  else{
   cout<<"Success: Your expression is correct"<<endl;
  }
 }
};
int main(){
 bracketMatch bm;
 bm.read();
 bm.check();
}

Thursday 17 December 2015

AJITH KP

Shell Sort Solution - C++

Hi GuyZ,
     Shell sort is an alternate version of insertion sort. The algorithm can be read from Wikipedia[^].



Source Code

#include <iostream>
using namespace std;
/*
 Shell Sort Solution - http://www.terminalcoders.blogspot.com
 Ajith Kp [@ajithkp560]
*/
void arrange(int *a, int start, int x, int l){
 int i = start+x;
 while(i<l){
  int val = a[i];
  int pos = i;
  while(pos>=x && a[pos-x]>val){
   a[pos] = a[pos-x];
   pos = pos-x;
  }
  a[pos] = val;
  i++;
 }
}
void shells(int *a, int l){
 int start = l/2;
 while(start>0){
  for(int i=0;i<start;i++){
   arrange(a, i, start, l);
  }
  cout<<"At the size "<<start<<": ";
  for(int i=0;i<l;i++)
   cout<<a[i]<<" ";
  cout<<endl;
  start=start/2;
 }
}
int main(){
 int n, x, a[1024];
 cout<<"Enter limit: ";
 cin>>n;
 cout<<"Enter "<<n<<" items: ";
 for(int i=0;i<n;i++)
  cin>>a[i];
 shells(a, n);
 cout<<"After Sort: ";
 for(int i=0;i<n;i++)
  cout<<a[i]<<" ";
 cout<<endl;
}

Monday 14 December 2015

Ajith KP

NFS Most Wanted in Higher Resolution

          I hope most of you will be a fan of NFS Game Series. The NFS Most Wanted have a bug, that it will not run in higher resolution monitors in correct screen resolution. I have experienced this problem on my new Laptop Dell Inspiron.



           There is a ultimate solution for this. Download This application: http://downloads.ziddu.com/download/24133764/NFS-Resolution.zip.html and extract it to NFS root folder. Then run nfsmwres.exe, and enter your screen's width and height at the input boxes. The screenshot is bellow,


          After open it, just click the button `Launch` to launch the NFS Most Wanted in correct resolution.

Friday 4 December 2015

AJITH KP

Implementation of Radix Sort in C++

Hi guyz,
     According to Wikipedia[^] radix sort is described as,
``In computer science, radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. A positional notation is required, but because integers can represent strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers. Radix sort dates back as far as 1887 to the work of Herman Hollerith on tabulating machines.```.



     The implementation of radix sort in c++

#include <iostream>
#include <queue>
/*
 * 
 * Coded By Ajith Kp [@ajithkp560]
 * http://www.terminalcoders.blogspot.com
 * 
 */
using namespace std;
int main(){
 int n, flg, i, nw, d, t;
 cout<<"Enter limit: ";
 cin>>n;
 int *arr = new int[n];
 cout<<"Enter "<<n<<" items: ";
 for(i=0;i<n;i++){
  cin>>arr[i];
 }
 flg = 0;
 d = 1;
 while(!flg){
  flg = 1;
  queue<int> Q[10];
  for(i=0;i<n;i++){
   nw = (arr[i]/d);
   if(nw>0){
    flg = 0;
   }
   Q[nw%10].push(arr[i]);
  }
  t = 0;
  for(i=0;i<10;i++){
   while(!Q[i].empty()){
    arr[t++] = Q[i].front();
    Q[i].pop();
   }
  }
  d = d*10;
 }
 cout<<"Sorted List: ";
 for(i=0;i<n;i++)cout<<arr[i]<<" ";cout<<endl;
}