Tuesday, October 1, 2013

Linked List Program in C


/*
LinkedList.c: this program is implementation of LinkedList in C. 

Date: 02/10/2013. 

*/


#include<stdio.h>
#include<string.h> 

void CreateNode(); 
void InsertElements(); 
void DisplayElements();
void DeleteElement(int a);

typedef struct 
{
 int data; 
 struct Node *next; 
}Node;

typedef Node *List;
List head,tail,temp; 

int main()
{
 int choice,data;  
 temp =NULL; head =NULL ; tail =head; 
 while(1){
 printf("==Linked List Menu==\n1.Insert Element\n2.Display\n3.Delete\n4.Exit\n");
 scanf("%d",&choice); 
 switch(choice)
 {
 case 1:
  InsertElements(); 
  break; 
 case 2:
  DisplayElements();
  break;
 case 3:
  printf("Enter the number that you want to delete\n");  
  scanf("%d",&data);
  DeleteElement(data); break; 
 default:
  printf("Thanks for using My programming ..\n");
  exit(0); 
 }
 }// end of infinite loop  


 return 0; 
}

void CreateNode()
{

 int data; 
 printf("Enter your element\n"); 
 scanf("%d",&data);
 temp = (List)malloc(sizeof(Node)); 
 temp->data = data; 
 temp->next =NULL; 
 
} 
void InsertElements()
{
 CreateNode(); 
 if(head == NULL)
 {
  head = temp; 
  tail =head; 
 }
 else 
 {
  tail->next = temp; 
  tail = tail->next;  
 }
}
void DisplayElements()
{
 if(head!=NULL)
 {
  printf("Elements===>\n");
 for(temp =head; temp!= NULL ; temp = temp->next)
  {
   printf("%d\t",temp->data);
  }
  printf("\n");
 }
 else
 {
  printf("Your Linked List Empty\n");
 }
}

void DeleteElement(int a)
{
 if(head!=NULL)
 {
  if(head->data == a)
  {
   head = head->next;
  }
  else{
   tail = head; 
   temp = head->next; 
   int found =0; 
  while(temp !=NULL)
   {
    if(temp->data ==a)
     {
     tail->next = temp->next;
 
          printf("element is found\\deleted\nPlease use option 2 to see remaining");
     found =1;     
     break;  
            }  
     else
     {
      tail = temp; 
      temp = temp->next;  
            }   
   }
   if(!found)
   printf("element is not present\n");   
      
      }
  
 
 }
 else
 {
  printf("Your Linked List Empty\n");
 }
}

Monday, March 11, 2013

SORTING ARRAY OF 0 AND 1 WITH ONE LOOP example in C


source code:

#include <stdio.h>

int main(int argc, char **argv)
{
   
    int a[]={1,1,1,1,0,0,0,0,1,1};  
      int j=0,i; 
      for(i=0;i<10;i++) 
      { 
           if(a[i]==0) 
           { 
             a[i]=1; 
            a[j++]=0;                
           } 
      } 
     for(i=0;i<10;i++)
      printf("%d\n",a[i]);
    return 0;
}

Sunday, March 10, 2013

Functions in C examples

 A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation. With properly designed functions, it is possible to ignore how a job is done; knowing what is done is sufficient. C makes the sue of functions easy, convenient and efficient; you will often see a short function defined and called only once, just because it clarifies some piece of code.


 A function definition Syntax:   
 return-type function-name(parameter declarations, if any)  
 {  
   declarations  
   statements  
 }  
 Example:   
 /*  
 power.c: This program will give you brief idea about functions.   
 Author: Afiz S   
 Date: 11/08/10  
 */  
 #include  
 int power(int a, int b); //prototype of the function.   
 main()  
 {  
 int a,b;  
 printf("Enter your 2 number base and exponent\n");  
 scanf("%d%d",&a,&b);  
 printf("%dpower %d is = %d\n",a,b,power(a,b)); // calling function.   
 }  
 int power(int a, int b) // function defination   
 {  
 int i,base=a;  
 for(i=1;i  
  {  
  //printf("%d\n",a);  
  a=a*base;  
  }  
  return a;  
 }  

C format specifiers


Format Specifiers in C
%i or %d int
%c char
%f float
%lf double
%s string
%hi short
%ld long int
%d,%hu unsinged short
%u unsinged int


  • \n (newline)
  • \t (tab)
  • \v (vertical tab)
  • \f (new page)
  • \b (backspace)
  • \r (carriage return)

Format Specifier for int and short in C example

 /*  
  * FormatSpecifiers.c  
  * Copyright 2013 Afiz <afiz@afiz-Extensa-4620>  
  */  
 #include <stdio.h>  
 int main(int argc, char **argv)  
 {  
      int a =109990909;   
      short b = 23;  
      printf("%d,%hi\n",a,b);  
      return 0;  
 }