Friday, April 8, 2011

Doubly Linked List Program in C

 /* Doubly linked list */  
 /* This program seg,ent creates a doubly linked list then prints in forward and backword */  
 /* Double_l.c */  
 #include <stdio.h>  
 #include <malloc.h>  
 void main(void)  
 {  
      int i;  
      struct ListEntry {  
           int number;  
           struct ListEntry *next;  
           struct ListEntry *previous;  
      } start, *node;  
      start.next = NULL; /* Empty list */  
      start.previous = NULL;  
      node = &start;   /* Point to the start of the list */  
      for (i = 1; i <= 10; i++)  
      {  
           node->next = (struct ListEntry *) malloc(sizeof(struct ListEntry));  
           node->next->previous = node;  
           node = node->next;  
           node->number = 50+i;  
           node->next = NULL;  
      }  
      /* Display the list */  
      node = start.next;  
      do {  
           printf("%d ", node->number);  
           node = node->next;  
      } while (node->next); /* Show 60 only one time */  
      do {  
           printf("%d ", node->number);  
           node = node->previous;  
      } while (node->previous);  
 }  

Merging Two Doubly Linked Lists

 /* MERGING TWO DOUBLY LINKED LISTS (ASCENDING ORDER) */  
 /* DBL_MRG.C */  
 # include <stdio.h>  
 # include <malloc.h>  
 struct Double  
 {  
      int info;  
      struct Double *next;  
      struct Double *previous;  
 };  
 int i;  
 struct Double start, start1, *new1, *local;  
 void Doubly_insertion (struct Double * );  
 void Doubly_Create (struct Double * );  
 void Display (struct Double *);  
 /* Function create a list of five nodes */  
 void Doubly_Create (struct Double *node)  
 {  
      start.next = NULL; /* Empty list */  
      start.previous = NULL;  
      node = &start;   /* Point to the start of the list */  
      for (i = 1; i < 10; i += 2)  
      {  
           node->next = (struct Double *) malloc(sizeof(struct Double ));  
           node->next->previous = node;  
           node = node->next;  
           node->info = i;  
           node->next = NULL;  
      }  
 }  
 void Doubly_insertion (struct Double *node)  
 {  
      struct Double *new1;  
      start1.next = NULL; /* Empty list */  
      start1.previous = NULL;  
      new1 = &start1;   /* Point to the start of the list */  
      for (i = 2; i <= 10; i += 2)  
      {  
           new1->next = (struct Double *) malloc(sizeof(struct Double ));  
           new1->next->previous = new1;  
           new1= new1->next;  
           new1->info = i;  
           new1->next = NULL;  
      }  
      new1 = start1.next;  
      printf("\n Second list is as follows");  
      while(new1)  
      {  
           printf("\n 0x%x", new1);  
           printf(" %d", new1->info);  
           new1 = new1->next;  
      }  
      new1 = start1.next;  
      while(new1)  
      {  
           int found = 0;  
           local = (struct Double *) malloc(sizeof(struct Double));  
           local = new1;  
           new1 = new1->next;  
           node = start.next;  
           do  
           {  
                if ( node->info > local->info )  
                {  
                     local->next = node;  
                     local->previous = node->previous;  
                     node->previous->next = local;  
                     node->previous = local;  
                     found = 1;  
                     break;  
                }  
                else  
                     node = node->next;  
           } while ((node->next) && (! found));  
           if (! found)  
                if (node->info> local->info)  
                {  
                     local->next = node;  
                     local->previous = node->previous;  
                     node->previous->next = local;  
                     node->previous = local;  
                }  
                else  
                {  
                     local->next = NULL;  
                     local->previous = node;  
                     node->next = local;  
                }  
      }  
 }  
 /* 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_Create (node);  
      printf("\n First list is as follows\n");  
      Display (node);  
      Doubly_insertion (node);  
      printf("\n List after merging above two lists (Ascending order)\n");  
      Display (node);  
 }  

Creating Simple Doubly LInked List

 /* CREATING A SIMPLE DOUBLY LINKED LIST */  
 /* DBLINK.C */  
 # include <stdio.h>  
 # include <malloc.h>  
 struct Double  
 {  
      int info;  
      struct Double *next;  
      struct Double *previous;  
 };  
 int num ;  
 struct Double start;  
 void Doubly_link_list (struct Double *);  
 void display (struct Double *);  
 /* Function creates a simple doubly linked list */  
 void Doubly_link_list(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);  
 }  
 /* Display the list */  
 void display (struct Double *node)  
 {  
      node = start.next;  
      do {  
           printf("\n 0x%x", node);  
           printf(" %d", node->info);  
           node = node->next;  
      } while (node->next); /* Show value of last node only one time */  
      do {  
           printf("\n 0x%x", node );  
           printf(" %d", node->info);  
           node = node->previous;  
      } while (node->previous);  
 }  
 void main()  
 {  
      struct Double *node = (struct Double *) malloc(sizeof(struct Double));  
      Doubly_link_list(node);  
      printf("\n Created doubly linked list is as follows\n");  
      display(node);  
 }  
    

Inserting Nodes in the Doubly Linked List

 /* INSERTING SOME NODES IN THE DOUBLY LINKED LIST */  
 /* DBL_IL.C */  
 # include <stdio.h>  
 # include <malloc.h>  
 struct Double {  
      char info;  
      struct Double *next;  
      struct Double *previous;  
 };  
 int i;  
 struct Double start, *new1;  
 void Doubly_insertion_Last (struct Double *);  
 void Doubly_Create_Last (struct Double *);  
 void Display (struct Double *);  
 /* Function create a list of five nodes */  
 void Doubly_Create_Last (struct Double *node)  
 {  
      int i = 0;  
      char ch;  
      start.next = NULL; /* Empty list */  
      start.previous = NULL;  
      node = &start;   /* Point to the start of the list */  
      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 value for: %d: ", i+1);  
           scanf("%c", &node->info);  
           node->next = NULL;  
           i++;  
           fflush(stdin);  
           printf("\n Input choice n for break: ");  
           ch = getchar();  
      }  
 }  
 void Doubly_insertion_Last (struct Double *node)  
 {  
      node = start.next;  
      new1 = (struct Double *) malloc(sizeof(struct Double ));  
      fflush(stdin);  
      printf("\n Input the last node value: ");  
      scanf("%c", &new1->info);  
      if (node == NULL)  
      {  
           printf("\n List is empty\n");  
           printf("\n Insert as last node\n");  
      }  
      else  
           while(node)  
           {  
                node = node->next;  
           }  
      new1->next = node;  
      new1->previous = node->previous;  
      node->previous->next = new1;  
      node->next = new1;  
 }  
 /* Display the list */  
 void Display (struct Double *node)  
 {  
      node = start.next;  
      while (node)  
      {  
           printf("\n 0x%x", node);  
           printf(" %c", node->info);  
           node = node->next;  
      }  
 }  
 /* Function main */  
 void main()  
 {  
      struct Double *node = (struct Double *) malloc(sizeof(struct Double));  
      Doubly_Create_Last (node);  
      printf("\n Created list is as follows\n");  
      Display(node);  
      Doubly_insertion_Last (node);  
      printf("\n List after insertion of last node \n");  
      Display (node);  
 }  
    

Inserting First Node in Doubly Linked List

 /* INSERTING FIRST NODE IN THE DOUBLY LINKED LIST */  
 /* DBL_IF.C */  
 # include <stdio.h>  
 # include <malloc.h>  
 struct Double  
 {  
      char info[20];  
      struct Double *next;  
      struct Double *previous;  
 };  
 int i;  
 struct Double start, *new1;  
 void Doubly_insertion_First (struct Double *);  
 void Doubly_Create_First (struct Double *);  
 void Display (struct Double *);  
 /* Function create a list of five nodes */  
 void Doubly_Create_First (struct Double *node)  
 {  
      int i = 0;  
      char ch;  
      start.next = NULL; /* Empty list */  
      start.previous = NULL;  
      node = &start;   /* Point to the start of the list */  
      fflush(stdin);  
      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 value for: %d: ", i+1);  
           gets(node->info);  
           node->next = NULL;  
           i++;  
           fflush(stdin);  
           printf("\n Input choice n for break: ");  
           ch = getchar();  
      }  
 }  
 void Doubly_insertion_First (struct Double *node)  
 {  
      node = start.next;  
      new1 = (struct Double *) malloc(sizeof(struct Double ));  
      fflush(stdin);  
      printf("\n Input the first node value: ");  
      gets(new1->info);  
      printf("\n new1 address: 0x%x", new1);  
      printf("\n Node address: 0x%x", node);  
      new1->next = node;  
      printf("\n New1 next address: 0x%x", new1->next);  
      printf("\n Node previous address: 0x%x", node->previous);  
      new1->previous = node->previous;  
      node->previous->next = new1;  
      printf("\n node->previous.next: 0x%x", node->previous->next);  
      printf("\n Node previous address: 0x%x", node->previous);  
      printf("\n Node next address: 0x%x", node->next);  
      printf("\n new1 address: 0x%x", new1);  
      node->previous = new1;  
      printf("\n node->previous: 0x%x", node->previous);  
 }  
 /* Display the list */  
 void Display (struct Double *node)  
 {  
      node = start.next;  
      while (node)  
      {  
           printf("\n 0x%x", node);  
           printf(" %s", node->info);  
           node = node->next;  
      }  
 }  
 /* main function */  
 void main()  
 {  
      struct Double *node = (struct Double *) malloc(sizeof(struct Double));  
      Doubly_Create_First (node);  
      printf("\n Created list is as follows\n");  
      Display(node);  
      Doubly_insertion_First (node);  
      printf("\n List after insertion of first node \n");  
      Display (node);  
 }  

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);}  
        }  
 }  

Friday, January 28, 2011

Double Linked List Program in C

 /*  
      dlinkedlist.c: This programs is implementation of doubly linked list.   
      @uthor: itsafiz@gmail.com   
      Date: 11Oct2010.   
 */  
 #include<stdio.h>   
 typedef struct   
      {  
           int data;   
           struct dnode *next;   
           struct dnode *prev;   
      }dnode;  
 typedef dnode *list;   
 list head, tail;   
 main()  
 {  
      list temp,paste;   
      head=NULL;   
      int ch, n;   
      while(1)  
      {  
      printf("\t##Double Linked list ##\n\t1.Add element\n\t2.Display\n\t3.Delete\n\t4.Exit\n");  
      scanf("%d",&ch);  
      switch(ch)  
           {  
           case 1:   
           temp=(list)malloc(sizeof(dnode));  
           printf("Enter Data = ");  
           scanf("%d",&n);  
           temp->data=n;  
           if(head==NULL)  
           {  
           temp->prev=NULL;   
           head=temp;   
           }  
           else  
           {  
           temp->prev=tail;  
           tail->next=temp;  
           }  
           temp->next=NULL;  
           tail=temp;   
           break;   
           case 2:   
                paste=head;  
                for(;paste!=NULL;paste=paste->next)  
                     {  
                          printf("%d\t",paste->data);  
                     }  
                printf("\n");  
           break;   
           case 3: break;   
           case 4: exit(1);   
           }  
      }  
 }