Saturday, May 18, 2013

C Source Codes for Implementing Linked List

The linked list can be implemented using the pointer structure in C so that we can

  • add an element
  • delete an element
  • search for an element
  • concatenate two linked list
  • Invert a linked list
  • Display elements in the list


//Program to demonstrate linked list operations

# include<stdio.h>
# include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
};

Implement Infix to postfix conversion using Stack C source codes

We can implement a valid infix expression to a valid postfix expression using stacks in c as shown:





//C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX 20

char stack[MAX];
int istack[MAX];
int top = -1;
char pop();
void push(char item);

Code Generator in C

The following code generates the code itself. The code seems to be simple but it's little bit tricky and is FAQ for programmers in an interview.




#include"stdio.h"
#include"conio.h"
char *s="#include%c#include%cchar *s=%c%s%c;%cvoid main()%c{%cclrscr();-%cprintf(s,10,10,34,s,34,10,10,10,10,10,10);%cgetch();%c}";
void main(){
//clrscr();
printf(s,10,10,34,s,34,10,10,10,10,10,10);
getch();
}

Binary Tree Implementation in C with Tree Traversals

We can implement the binary tree using C so that we can add a node, delete a node, and can perform different tree traversals:
 1. Preorder tree traversal
 2. Inorder tree traversal
 3. Post order tree traversal




/// ..............................................BINARY TREE OPERATIONS AND TREE TRAVERSALS

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

///.................................................................DYNAMIC STRUCTURE FOR BINARY TREE

struct tree
{
     int info;
     struct tree *left_subtree;
     struct tree *right_subtree;
};

Saturday, May 11, 2013

Time & Space Complexity of Basic K-means Algorithm


The basic k-means clustering algorithm is a simple algorithm that separates the given data space into different clusters based on centroids calculation using some proximity function. Using this algorithm, we first choose the k- points as initial centroids and then each point is assigned to a cluster with the closest centroid. The algorithm is formally described as follows:
Input: A data set D containing m objects (points) with n attributes in an              Euclidean space
Output: Partitioning of m objects into k-clusters C1, C2, C3, …, Cki.e. Ci  D and Ci ∩ Cj = ᶲ  (for 1 ≤ i, j ≤ k)