Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Tuesday, November 9, 2010

c implementation order n square

#include    //Header File

#include      //Header File


int main()
{
 clrscr();

 int A[100];
  int tempnum ; 

 for(int i=0;i<100;i++)
 {
  A[i]=i;
 }

 A[10]=5;
 A[15]=50;



 for(int k=0; k<100;k++)
 {
   tempnum=A[k] ;
  for(int j=k+1;j<100;j++)
  {
   if(tempnum==A[j]) cout << A[j] << "\n" ;
  }

 }

getche();
return 0;
}

Example order n in c programming

#include    //Header File

#include      //Header File


int main()
{
 clrscr();

 int A[100];

 int Temp[100];




 for(int i=0;i<100;i++)
 {
  A[i]=i;
 }

 A[10]=5;
 A[15]=50;



 for(int k=0; k<100;k++)
 {
  Temp[k]=0;
 }

 for (int j=0;j<100;j++)
 {

  Temp[A[j]]=Temp[A[j]]+1;
 }



  for (int m=0;m<100;m++)
 {
   if(Temp[m]>=2) cout<< "\n\n" << A[m] ;
 }


getche();
return 0;
}

Radix sort using c pointer

#include 
#include 
#include 

void radix(int a[],int n,int m)
{
typedef struct node
{
int data;
struct node * next;
}NODE;

NODE * ptr,*start,*prev;
NODE *front[10], *rear[10];
int k=1,i,j,y,p;;
/*creating initial linked list*/
start=NULL;
for(i=0;i< n;++i)
{
ptr=(NODE *)malloc(sizeof(NODE));
ptr->data=a[i];
ptr->next=NULL;
if(start==NULL)
start=ptr;
else
prev->next=ptr;
prev=ptr;
}

/*radix sort*/
for(i=1;i<=m;i++)
{
for(j=0;j<10;j++)
front[j]=NULL;
/*placing elements into queues*/
ptr=start;
while(ptr!=NULL)
{y=ptr->data/k %10;/*y is the digit*/
if(front[y]==NULL)
{
front[y]=ptr;
rear[y]=ptr;
}
else
{
rear[y]->next=ptr;
rear[y]=ptr;
}

ptr=ptr->next;
}

start=NULL;
for(j=0;j< 10;++j)
if(front[j]!=NULL)
{
if(start==NULL)
start=front[j];
else rear[p]->next=front[j];
p=j;
}
rear[p]->next=NULL;
k=k*10;
}
/*copying back to array*/
ptr=start;
for(i=0;i< n;++i,ptr=ptr->next)
a[i]=ptr->data;

}

void main()
{
int a[100],n,i,m;
char temp;
do
{
clrscr();
printf("===========================RADIX SORT=
==========================================\n");
printf("ENTER NUMBER OF NUMBERS AND NUMBER OF DIGITS\n");
scanf("%d%d",&n,&m);
printf("ENTER ELEMENTS\n");
for(i=0;i< n;++i)
scanf("%d",&a[i]);
radix(a,n,m);
printf("SORTED LIST\n");
for(i=0;i< n;++i)
printf("%d ",a[i]);
printf("\nDO YOU wish to continue?[y/n]\n");
scanf("%c",&temp);


}while(temp=='y'|| temp=='Y');
printf("\n-------------------------------------
--------------------------------------------\n");
getch();
}

Radix sort algorithm implementation in c using counting sort

/* radix sort */

#include 
#include 
#include  

#define LEN  72 /* maximum string length */
#define K  256 /* number of possible characters */
//#define MAX_STRINGS 50



void counting_sort (unsigned char *A[], 
unsigned char *B[], int n, int h) {
 int  C[K];
 int  i, j;

 for (i=0; i
 /* all counts are now zero */

 for (j=0; j

 /* C[i] is number of times character i occurs */

 for (i=1; i
 /* C[i] is number of times i or less occurs */

 for (j=n-1; j>=0; j--) {

  /* place elements in the array from largest to smallest */
  /* -1 is because arrays start out at 0 in C */

  B[C[A[j][h]]-1] = A[j];
  C[A[j][h]]--;
 }
}

/* this radix sort sorts the n pointers to strings 
of size d in the array A
 * by the strings they point to.
 */
void radix_sort (unsigned char *A[], int n, int d) {
 int  i, j;
 unsigned char *B[50];

 /* we're assuming here that digit 0 is the highest order digit,
  * like in real life, not like in the book
  */
 for (i=d-1; i>=0; i--) {

  /* stable sort A into B */

  counting_sort (A, B, n, i);

  /* copy the results back into A */

  for (j=0; j }
}


/* main program */

int main () {
 unsigned char A[50][5+1],
   *Ap[50],s[50];
 int  i, n;

        clrscr() ; 

 /* get a bunch of strings into the array A */

 for (n=0;n<8;n++) {
  gets (s);
  if (feof (stdin)) break;

  /* make sure the string is no longer than LEN */

  s[LEN] = 0;

  /* make sure the string is no shorter than LEN */

  while (strlen(s)

  /* put the string into the next position in A */

  strcpy (A[n++],s);
 }

 /* make Ap an array of pointers to the strings in A */

 for (i=0; i
 /* sort the pointers */

 radix_sort (Ap,n,LEN);

 /* print them out */

 for (i=0; i

 getch() ; 

 return 0 ;
}

Marge sort algorithgm c implementation

#include
#include
#include

int *a;

merge(int p,int q,int r)
{
  int n1,n2,i,j,k;
  int *L,*R;
  n1=q-p+1;
  n2=r-q;
  L=(int*)malloc(sizeof(int)*(n1+2));
  R=(int*)malloc(sizeof(int)*(n2+2));
  for(i=1;i<=n1;i++)
  L[i]=a[p+i-1];
  for(j=1;j<=n2;j++)
  R[j]=a[q+j];
  L[n1+1]=-1;
  R[n2+1]=-1;
  i=1;
  j=1;
  for(k=p;k<=r;k++)
  {
  if(L[i]<=R[j]&&L[i]!=-1)
  {
   a[k]=L[i];
   i++;
  }
  else
  {
   if(R[j]!=-1)
   {
    a[k]=R[j];
    j++;
   }
   else
   {
    while(L[i]!=-1)
    {
     a[k]=L[i];
     i++;
     k++;

    }
   }
  }

  }
  free(L);
  free(R);
}

merge_sort(int p,int r)
{
  int q;
  if(p
  {
  q=(p+r)/2;
  if((p+r)%2==0)
   q=q-1;
  merge_sort(p,q);
  merge_sort(q+1,r);
  merge(p,q,r);
  }
}

void main()
{
 clrscr();
 int i,j,k,p,r;
 printf("How many numbers : ");
 scanf("%d",&i);
 a=(int*)malloc(sizeof(int)*(i+1));
 printf("\nEnter the numbers : ");
 for(k=1;k<=i;k++)
 {
  scanf("%d",&j);
  a[k]=j;
 }
 p=1;
 r=i;
 merge_sort(p,r);
 printf("\nSorted list is :   ");

 for(k=1;k<=i;k++)
  printf("   %d",a[k]);

 free(a);
 getch();
}

Wednesday, March 10, 2010

the basic structures and techniques for building linked lists

This article introduces the basic structures and techniques for building linked lists with a mixture of explanations, drawings, sample code, and exercises. The material is useful if you want to understand linked lists or if you want to see a realistic, applied example of pointer-intensive code. Even if you never really need a linked list, they are an excellent way to learn pointers and pointer algorithms.


Why Linked Lists?
Linked lists and arrays are similar since they both store collections of data. The
terminology is that arrays and linked lists store "elements" on behalf of "client" code. The specific type of element is not important since essentially the same structure works to store elements of any type. One way to think about linked lists is to look at how arrays work and think about alternate approaches.


Array Review
Arrays are probably the most common data structure used to store collections of
elements. In most languages, arrays are convenient to declare and the provide the handy
[ ] syntax to access any element by its index number. The following example shows some
typical array code and a drawing of how the array might look in memory. The code
allocates an array int scores[100], sets the first three elements set to contain the
numbers 1, 2, 3 and leaves the rest of the array uninitialized...

void ArrayTest() {
int scores[100];
// operate on the
elements of the scores array...
scores[0] = 1;
scores[1] =
2;
scores[2]
= 3;
}


What Linked Lists Look Like?
An array allocates memory for all its elements lumped together as one block of memory.In contrast, a linked list allocates space for each element separately in its own block of memory called a "linked list element" or "node". The list gets is overall structure by using pointers to connect all its nodes together like the links in a chain. Each node contains two fields: a "data" field to store whatever element type the list holds for its client, and a "next" field which is a pointer used to link one node to the next node. Each node is allocated in the heap with a call to malloc(), so the node memory continues to exist until it is explicitly deallocated with a call to free(). The front of the list is a 5 pointer to the first node. Here is what a list containing the numbers 1, 2, and 3 might look like...


The Empty List — NULL
The above is a list pointed to by head is described as being of "length three" since it is made of three nodes with the .next field of the last node set to NULL. There needs to be some representation of the empty list — the list with zero nodes. The most common representation chosen for the empty list is a NULL head pointer. The empty list case is the one common weird "boundary case" for linked list code. All of the code presented in this article works correctly for the empty list case, but that was not without some effort. When working on linked list code, it's a good habit to remember to check the empty list case to verify that it works too. Sometimes the empty list case works the same as all the cases, but sometimes it requires some special case code. No matter what, it's a good case to at least think about.


Linked List Types: Node and Pointer
Before writing the code to build the above list, we need two data types...
• Node: The type for the nodes which will make up the body of the list.
These are allocated in the heap. Each node contains a single client data
element and a pointer to the next node in the list. Type: struct node

struct node {
int data;
struct node* next;
};

• Node Pointer: The type for pointers to nodes. This will be the type of the
head pointer and the .next fields inside each node. In C and C++, no
separate type declaration is required since the pointer type is just the node
type followed by a '*'. Type: struct node*

Example :

struct node* AppendNode(struct node** headRef, int num) {
struct node*
current = *headRef;
struct node* newNode;
newNode = malloc(sizeof(struct
node));
newNode->data = num;
newNode->next = NULL;
// special
case for length 0
if (current == NULL) {
*headRef = newNode;
}
else
{
// Locate the last node
while (current->next != NULL) {
current =
current->next;
}
current->next = newNode;
}
}

To know more about basic linklist please Download LinkedListBasics.pdf