Skip to main content

Posts

Showing posts from September, 2011

C program to find size of text file

/*  *Program to find size of text file  */ #include<stdio.h> #define LINE 80 int main(int c, char *argv[]) { /* fp is file pointer, once we open file, it is pointer to opened file */  FILE *fp;  char ch;  unsigned int size=0; /* Open file for reading   * argv[0] is executable program name and argv[1] is first   * argument given after executable file  */  fp=fopen(argv[1],"r");  /* if we get NULL as return value from fopen()   * it means, program is not able to open file or   * file may not existed ? or given file path will be wrong   */  if(fp == NULL){     printf("Can't open file\n");     return 0;  }  /*   * read characters from file and check size   */  while((ch = fgetc(fp)) != EOF){  /* we can add 1 to size every time but better to use sizeof to avoid issues */        size=size+sizeof(ch); ...

Program to Swap Adjecent nodes in single linked list

shiv@ubuntu:~/ds/list$ cat   altxchangenode.c /*  * program to Xchange or swap all the adjecent nodes(not node data) in the single linked list  */ #include<stdio.h> #include<stdlib.h> #define MAX 10 struct list {  int d;  struct list *next; }; /*  * function to swap 2 adjecent nodes(linkes) in the list  */ void xchangenodes(struct list **head, struct list **ne, struct list **tm) {   struct list *temp, *x1, *x2;   if(*tm == NULL){      return;   }   if((*tm)->next && (*tm)->next->next){       /*next node when more than 3 nodes present in the list */      temp=(*tm)->next->next;   } else if((*tm)->next){       if(*head != NULL) {   /* only two nodes present in the list */          (*ne)->next = (*tm)->next;       ...

C program to delete node from single linked list when just address of the previous node to be deleted is given

shiv@ubuntu:~/ds/list$ cat deln.c /*  * C Program to delete node in the single linked list, when address of the  *previous  node to be deleted is given  */ #include <stdio.h> #include <stdlib.h> struct list {     int d;     struct list *next; }; /*  * function to delete node when just address of the node to be  * deleted is given in the single linked list  */ void deletenode(void **ptr) {   struct list **temp, *del;   if(*ptr == NULL) {      printf("Invalid pointer\n");      return;   }   temp= (struct list **)ptr;   printf("The address of previous node of the node to be deleted is %p data %d\n", *temp, (*temp)->d);   if((*temp)->next && (*temp)->next->next){      /* any node apart from node before last or last node in the list */      (*tem...

C Program to create sorted single linked list from given unsorted array

shiv@ubuntu:~/ds/list$ cat  tarr_to_sortlist.c  /*  * Program to create sorted list from given unsorted array  */ #include<stdio.h> #include<stdlib.h> #include<malloc.h> #define MAX 10 struct list { int d; struct list *next; }; int main() {  /* example array to test program */  int a[MAX]={4,1,5,6,10,9,3,2,7,8};  int  i;  struct list *head=NULL, *temp, *insert; /*loop to get all array elements*/ for(i=0;i<MAX;i++) { temp= malloc(sizeof(struct list)); if (temp == NULL) {         printf("Can't allocate memory\n");         exit(0); } temp->d = a[i]; temp->next = NULL;    /* first node in list */    if( head == NULL) {       head = temp;    } else {        / * second node in the list */        if( head->next == NULL){   ...

Simple program to create Zombie @ Linux

shiv@ubuntu:~/ds/unix$ cat zmb.c /*  * simple program to create a Zombie  * ps command display Zombie with Z under flags in ps output  */ #include<stdio.h> #include<unistd.h> #include<stdlib.h> int main() {  pid_t pid; /* create child process */  pid=fork();   if(pid == 0){    /* child process exits and becomes Zombie     */    exit(0);  } sleep(20); printf("Done ....\n"); return 0; }

simple program to create Orphan process

shiv@ubuntu:~/ds/unix$ cat  orp.c /*  * Program to create orphan process @ Linux  * getpid() gives process PID and   * getppid() gives process's parent ID   * here main() process ID is parent id is current shells PID  * once process becomes orphan it is adopted by init process(it's PID is 1)  */   #include<stdio.h> #include<unistd.h> int main() {  pid_t p; /* create child process */  p=fork();  if(p==0) {     /* fork() returns Zero to child */     sleep(10);  }  printf("The child process pid is %d parent pid %d\n", getpid(), getppid()); /*parent/child waits for 20 secs and exits*/  sleep(20);  printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());  return 0; } O/p shiv@ubuntu:~/ds/unix$ ./a.out The child process pid is 2575 parent pid 1922 The child process pid is 2576 parent pid 2575 Process 2575 is done it...

few ways Program to Find the machine is little endian or Big endian

following are the ways to find machine is little endian or big endian , and how to verify my program is working fine? # 1 /* program to find machine is little endian  or little endian */ #include<stdio.h> int main() {  int x=0x12345678;  char *p;  p=(char *)&x;  if(*p == 0x78) {     printf("Little endian \n");  } else {    printf("Big endian \n");  } return 0; } #2 /* another correct way is using unions */ int main() { union e {        int x;        char y; }; union e p1; p1.x=0x12345678; if(p1.y == 0x78) {     printf("Little endian \n");  } else {    printf("Big endian \n"); } return 0; } #3 below is same as #1 with more simplified shiv@ubuntu:~/ds/misc$ /* following is not correct way to find the machine */  #include<stdio.h> int main() { int num=1; if(*(char *)...

Interactive C program to Simulate Queue operations on Circular Queue using arrays

shiv@ubuntu:~/ds/queue$ cat  circularq.c /*  * Interactive program to simulate Circular Queue using arrays  */ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_SIZE 5 #define FALSE 0 #define TRUE 1 /* Insert element into Queue */ int insert(int cq[], int e, int head, int *tail) {   if((*tail + 1)%MAX_SIZE == head){       printf("Queue is full\n");       return FALSE;   }   cq[*tail]=e;  *tail = (*tail +1)% MAX_SIZE;   return TRUE; } /* Delete element from queue */ int  del(int cq[], int *head, int tail) {  if(tail == *head) {     printf("Queue is empty");     return FALSE;  }  printf("deleted %d\n", cq[*head]);  *head = (*head + 1)%MAX_SIZE;  return TRUE;      } /* Display elements in the queue */ void display(int cq[], int h, int t...

Interactive C program to demonstrate Circular single linked list

/* Program to Demonstrate Circular single linked list */ #include <stdio.h> #include <stdlib.h> /* Circular single linked list */ struct clist { int data; struct clist *next; }; typedef struct clist *CL; /* inserting elements into Circular list just after first(head) node */ void insert(CL *head) {  CL temp;  int e;  temp=malloc(sizeof(struct clist));  if(temp == NULL){     printf("Can't allocate memory\n");     return;  }  printf("Enter element\n");  scanf("%d", &e);  temp->data = e;  if(*head == NULL){      *head = temp;      (*head)->next = temp;  } else {     temp->next = (*head)->next;    (*head)->next =temp;     *head =temp;      } }   /* Displaying elements in list */ void display(CL head) {  CL temp;  if(head == N...

program to different ways to swap values of two variables without using temporary variables

shiv@ubuntu:~/ds/list$ cat swap.c /*  * Program to swap values of two variables  * without using temporary variables  */ #include <stdio.h> /*  * swaping values two variables using arithmetic  * operators  */ void swap(int *x, int *y) {   *x = *x + *y;   *y = *x - *y;   *x = *x - *y; } /*  * swaping values of variables using  * XOR oprations  */ void swap_xor(int *x, int *y) {   *x = *x ^ *y;   *y = *x ^ *y;   *x = *x ^ *y; } int main() {  int x, y, a, b;  x=20;  y=30;  swap(&x, &y);  printf("\nafter swap x=%d and y=%d", x, y);  a=50;  b=100;  swap_xor(&a, &b);  printf("\nafter swap a=%d and b=%d\n", a, b); }