Friday, April 8, 2011

Delete Node from a Doubly Linked List

 /* DELETE A NODE FROM A SIMPLE DOUBLY LINKED LIST */ 
 # include <stdio.h>  
 # include <malloc.h>  
 struct Double  
 {  
      char info;  
      struct Double *next;  
      struct Double *previous;  
 };  
 int num ;  
 struct Double start;  
 void Doubly_Link_Del (struct Double *);  
 void Doubly_Link_Creat (struct Double *);  
 void display (struct Double *);  
 /* Function creates a doubly linked list */  
 void Doubly_Link_Creat (struct Double *node)  
 {  
      char ch;  
      start.next = NULL; /* Empty list */  
      start.previous = NULL;  
      node = &start;   /* Point to the start of the list */  
      num = 0;  
      printf("\n Input choice n for break: ");  
      ch = getchar();  
      while(ch != 'n')  
      {  
           node->next = (struct Double *) malloc(sizeof(struct Double));  
           node->next->previous = node;  
           node = node->next;  
           printf("\n Input the values of the node: %d:", (num+1));  
           scanf("%d", &node->info);  
           node->next = NULL;  
           fflush(stdin);  
           printf("\n Input choice n for break: ");  
           ch = getchar();  
           num ++;  
      }  
      printf("\n Total nodes = %d", num);  
 }  
 /* Function delete */  
 void Doubly_Link_Del (struct Double *node)  
 {  
      int delete_node;  
      int search_counter = 0;  
      printf("\n Input the node number to which you want delete: ");  
      scanf("%d", &delete_node);  
      node = start.next;  
      if ( node == NULL)  
      {  
           printf("\n Underflow\n");  
           printf("\n List is empty\n");  
      }  
      else  
           while(node)  
           {  
                if((search_counter + 1) == delete_node)  
                {  
                     node->previous->next = node->next ;  
                     node->next->previous = node->previous ;  
                     free(node);  
                }  
                else  
                {  
                     node = node->next;  
                }  
                search_counter++;  
           }  
 }  
 /* Display the list */  
 void display(struct Double *node)  
 {  
      node = start.next;  
      while (node)  
      {  
           printf("\n 0x%x", node);  
           printf(" %d", node->info);  
           node = node->next;  
      }  
 }  
 /* Function main */  
 void main()  
 {  
      struct Double *node = (struct Double *) malloc(sizeof(struct Double));  
      Doubly_Link_Creat(node);  
      printf("\n Created linked list is as follows\n");  
      display(node);  
      Doubly_Link_Del(node);  
      printf("\n After deletion of a node linked list is as follows\n");  
      display(node);  
 }   

CREATING CIRCULAR HEADER LINKED LIST in C

 /* CREATING CIRCULAR HEADER LINKED LIST */  
 /* CIRCLIST.C */  
 # include <stdio.h>  
 # include <malloc.h>  
 struct link  
 {  
      int info;  
      struct link *next;  
 };  
 int i; /* Represents number of nodes in the list */  
 int number;  
 struct link *start, *new1;  
 void insertion(struct link *);  
 void create_circular_list(struct link *);  
 void display(struct link *);  
 /* Function create a circular header linked list */  
 void create_circular_list( struct link *node)  
 {  
      char ch;  
      node = start;   /* Point to the header node in the list */  
      node->next = (struct link *) malloc(sizeof(struct link));  
      i = 0;  
      printf("\n Input choice n for break: ");  
      ch = getchar();  
      while(ch != 'n')  
      {  
           node->next = (struct link* ) malloc(sizeof(struct link));  
           node = node->next;  
           printf("\n Input the node: %d:", (i+1));  
           scanf("%d", &node->info);  
           fflush(stdin);  
           printf("\n Input choice n for break: ");  
           ch = getchar();  
           i++;  
      }  
      printf("\n Total nodes = %d", i);  
      node = start;  
      node->info = i; /* Assign total number of nodes to the header node */  
 }  
 /* Inserting a node in circular header linked */  
 void insertion(struct link *node)  
 {  
      int count = node->info;  
      int node_number = 0;  
      int insert_node;  
      node = start;  
      node = node->next;  
      printf("\n Input node number you want to insert: ");  
      printf("\n Value should be less are equal to the");  
      printf("\n number of nodes in the list: ");  
      scanf("%d", &insert_node);  
      while(count)  
      {  
           if((node_number+1) == insert_node)  
           {  
                new1 = (struct link* ) malloc(sizeof(struct link));  
                new1->next = node->next ;  
                node->next = new1;  
                printf("\n Input the node value: ");  
                scanf("%d", &new1->info);  
                node = node->next;  
                count--;  
           }  
           else  
           {  
                node = node->next;  
                count--;  
           }  
           node_number ++;  
      }  
      if (count == 0)  
      {  
           node = start; /* Points to header node */  
           node->info = node->info+1;  
      }  
 }  
 /* Display the list */  
 void display(struct link *node)  
 {  
      int count = node->info;  
      node = start;  
      node = node->next;  
      while (count-1)  
      {  
           printf("\n 0x%x", node);  
           printf(" %d ", node->info);  
           node = node->next;  
           count --;  
      }  
 }  
 /* Function main */  
 void main()  
 {  
      struct link *node = (struct link *) malloc(sizeof(struct link));  
      create_circular_list(node);  
      printf("\n Before inserting a node list is as follows:\n");  
      display(node);  
      insertion(node);  
      printf("\n After inserting a node list is as follows:\n");  
      display(node);  
 }  

Monday, February 21, 2011

How to Excute System Commands in C

 /* system example : DIR */  
 #include <stdio.h>  
 #include <stdlib.h>  
 int main ()  
 {  
  int i;  
  //printf ("Checking if processor is available...");  
  if (system(NULL)) puts ("Ok");  
  else exit (1);  
  printf ("Executing command dir...\n");  
  i=system ("dir"); // put ls if you are using linux  
  printf ("The value returned was: %d.\n",i);  
  return 0;  
 }  

Thursday, February 10, 2011

QUICK SORT IN C

STRUCTURE IN STRUCTURE EXAMPLE PROGRAM IN C

 #include<stdio.h>  
 struct date  
 {  
      int year;   
      int month;  
      int day;  
 };  
 struct student   
 {  
      char name[20];  
      struct date dob;   
 };  
 main()  
 {  
      struct student s1;   
      printf("Enter student Name\n");  
      scanf("%s",s1.name);  
      printf("Enter Date of Birth\n");  
      scanf("%d%d%d",&(s1.dob.year),&(s1.dob.month),&(s1.dob.day));  
      printf("%s\n%d-%d-%d\n",s1.name,s1.dob.day,s1.dob.month,s1.dob.year);       
 }  

POINTER STRUCTURES EXAMPLE PROGRAM IN C

 #include <stdio.h>  
 #include <stdlib.h>   
 #include <memory.h>  
 struct mystruct {  
      int a;  
 };  
 typedef struct mystruct * pmystruct;  
 pmystruct getpstruct()  
 {  
      pmystruct temp=(pmystruct)malloc(sizeof(pmystruct*));  
      return temp;  
 }  
 int main(int argc, char* argv[])  
 {    
      int * fred;  
      pmystruct b= getpstruct();  
      b->a = 8;  
      ++b->a;  
      printf ("The value of b->a is %i \n",b->a);  
      free(b);  
      fred = (int *) malloc(sizeof(int));  
      memset(fred,0x100,sizeof(int));  
      printf("Value of *fred = %i\n",*fred);  
      free(fred);  
      return 0;  
 }  

STRUCTURES EXAMPLE PROGRAM IN C

 /*  
 */  
 #include<stdio.h>  
 struct student  
 {  
      char name[20];  
      char id[10];  
      char class[5];   
      float marks;   
 };   
 main( )  
 {  
   struct student std[10];   
   int i;   
   for(i=0;i<10;i++)  
        {  
             scanf("%s%s%s",std[i].name,std[i].id,std[i].class);  
             scanf("%f",&std[i].marks);  
        }  
  printf("Name\tId\tclass\tmarks\n");  
   for(i=0;i<10;i++)  
        {  
             printf("%s\t%s\t%s",std[i].name,std[i].id,std[i].class);  
             printf("\t%0.2f\n",std[i].marks);  
        }  
   printf("Students Details of More than 5 Marks:\n");  
   printf("Name\tId\tclass\tmarks\n");  
   for(i=0;i<10;i++)  
        {  
             if(std[i].marks>=5){  
             printf("%s\t%s\t%s",std[i].name,std[i].id,std[i].class);  
             printf("\t%0.2f\n",std[i].marks);}  
        }  
 }