Posts

Slip 30_2 : Write a program that merges two ordered linked lists into third new list. When two lists are merged the data in the resulting list are also ordered. The two original lists should be left unchanged. That is merged list should be new one. Use linked implementation.

 Solution : #include<stdio.h> struct node {  int data;  struct node *next; }; struct node* create(); void display(struct node*); struct node* create() { int n,i;  struct node *s,*f;  printf("Enter how many nodes ");  scanf("%d",&n);  f= (struct node *)malloc(sizeof(struct node));  printf("Enter data ");  scanf("%d",&f->data);  s=f;  for(i=1;i<n;i++)  {  s->next=(struct node*)malloc(sizeof(struct node));  s=s->next;  printf("Enter data ");  scanf("%d",&s->data);  }  s->next=NULL;  return f; } void display(struct node *f) { struct node *s;  for(s=f;s!=NULL;s=s->next)  {  printf("| %d |-> ",s->data);  } } struct node* merge(struct node *f1,struct node *f2) { struct node *s;  for(s=f1;s->next!=NULL;s=s->next)  {  }  s->next=f2;  return f1; } main() { ...

Slip 27_2 : Write a program that adds two single variable polynomials. Each polynomial should be represented as a list with linked list implementation.

  Solution : #include<stdio.h> #include<stdlib.h> struct node { int coeff,exp;  struct node *next; }; struct node* create(struct node *f) {  int i,n;  struct node *s;  printf("\nEnter no of terms ");  scanf("%d",&n);  printf("Enter term in descending order of power ");  f=(struct node *)malloc(sizeof(struct node));  printf("\n Enter coeff");  scanf("%d",&f->coeff);  printf("\n Enter power ");  scanf("%d",&f->exp);  s=f;  for(i=1;i<n;i++)  {  s->next=(struct node *)malloc(sizeof(struct node));  s=s->next;  printf("\n Enter coeff");  scanf("%d",&s->coeff);  printf("\n Enter power ");  scanf("%d",&s->exp);  }  s->next=NULL;  return f; } void display(struct node *f) { struct node *s;  for(s=f;s!=NULL;s=s->next)  { printf("%dx^%d ->",s-...

Slip27_1 : Read the data from the file and sort on names in alphabetical order (use strcmp) using Merge sort and write the sorted data to another file 'sortedemponname.txt'

 Solution : #include<stdio.h> #include<stdlib.h> #include<string.h> struct employee {  char name[20];  int age; }emp[10]; int readFile(struct employee a[]) {  int i=0;  FILE *fp;  if((fp=fopen("emp.txt","r"))!=NULL)  {  while(!feof(fp))  {  fscanf(fp,"%s%d",&a[i].name,&a[i].age);  i++;  }  }  return i-1; } mergesort(struct employee a[10],int lb,int ub) { int mid;  if(lb<ub)  {  mid=(lb+ub)/2;  mergesort(a,lb,mid);  mergesort(a,mid+1,ub);  merge(a,lb,mid,ub);  } } merge(struct employee a[10],int lb,int mid,int ub) {  struct employee b[20];  int k,i,j;  k=0;  i=lb;  j=mid+1;  while(i<=mid && j<=ub)  {  //if(a[i]<=a[j])  if(strcmp(a[i].name,a[j].name)<0)  { b[k]=a[i];  i++;  k++;  }  else  { b[k]=a[j];  j++; ...

Slip27_1 : Read the data from the file and sort on names in alphabetical order (use strcmp) using Merge sort and write the sorted data to another file 'sortedemponname.txt'

 Solution : #include<stdio.h> #include<stdlib.h> #include<string.h> struct city {  char name[20];  int code; }ct[10]; int readFile(struct city a[]) {  int i=0;  FILE *fp;  if((fp=fopen("sortedfile.txt","r"))!=NULL)  {  while(!feof(fp))  {  fscanf(fp,"%s%d",&a[i].name,&a[i].code);  i++;  } }  return i-1; } int binarysearch(struct city a[10],int lb,int ub,char sr[20]) {  int mid=0;  while(lb<=ub)  {  mid=(lb+ub)/2;  if(strcmp(a[mid].name,sr)==0)  return mid;  else if(strcmp(sr,a[mid].name)<0)  ub=mid-1;  else  lb=mid+1;  }  return -1; } main() { int n,p;  char sr[20];  n=readFile(ct);  if(n==-1)  printf("File not found ");  else  {  printf("Enter city name to search ");  scanf("%s",sr);  p=binarysearch(ct,0,n,sr);  if(p>=0)  prin...

Slip 23_2: Read the data from file ‘sortedcities.txt’ containing sorted names of cities and their STD codes. Accept a name of the city from user and use binary search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”.

 Solution : #include<stdio.h> #include<stdlib.h> #include<string.h> struct city {  char name[20];  int code; }ct[10]; int readFile(struct city a[]) {  int i=0;  FILE *fp;  if((fp=fopen("sortedfile.txt","r"))!=NULL)  {  while(!feof(fp))  {  fscanf(fp,"%s%d",&a[i].name,&a[i].code);  i++;  } }  return i-1; } int binarysearch(struct city a[10],int lb,int ub,char sr[20]) {  int mid=0;  while(lb<=ub)  {  mid=(lb+ub)/2;  if(strcmp(a[mid].name,sr)==0)  return mid;  else if(strcmp(sr,a[mid].name)<0)  ub=mid-1;  else  lb=mid+1;  }  return -1; } main() { int n,p;  char sr[20];  n=readFile(ct);  if(n==-1)  printf("File not found ");  else  {  printf("Enter city name to search ");  scanf("%s",sr);  p=binarysearch(ct,0,n,sr);  if(p>=0)  prin...

Slip 22_2: Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use sentinel linear search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”. Solution :

 Solution : #include<stdio.h> #include<stdlib.h> #include<string.h> struct city {  char name[20];  int code; }ct[10]; int readFile(struct city a[]) {  int i=0;  FILE *fp;  if((fp=fopen("city.txt","r"))!=NULL)  {  while(!feof(fp))  {  fscanf(fp,"%s%d",&a[i].name,&a[i].code);  i++;  }  }  return i-1; } void sentinelsearch(struct city a[10],int n,int sr) { int i,cnt=0;  a[n]=sr;  while(strcmp(sr,a[i].name)!=0) { i++;  }  if(i<n)  printf("city is found and STD code is %d ",a[i].code);  else  printf("city is not found "); } main() { int n;  char sr[20];  n=readFile(ct);  if(n==-1)  printf("File not found ");  else  {  printf("Enter city name to search");  scanf("%s",sr);  SentinelSearch(ct,n,sr);  } }

Slip 21_1: Write a program that reverses a string of characters. The function should use a stack library (cststack.h). Use a static implementation of the stack.

 Solution Header File : cststack.h #include<stdio.h> char s[20]; int top; void init() {  top==-1; } int isempty() {  if(top==-1)  return 1;  else  return 0; } int isfull() {  if(top==19)  return 1;  else  return 0; } void push(char ch) {  if(isfull()==1)  printf("Stack is full");  else  {  top++;  s[top]=ch;  } } char pop() {  char ch;  if(isempty()==1)  printf("Stack is empty");  else  {  ch=s[top];  top--;  return ch;  } } Program File : #include<stdio.h> #include"stack.h" int main() {  init();  char str[20];  int i;  printf("Enter String: ");  scanf("%s",&str);  for(i=0;str[i]!='\0';i++)  {  push(str[i]);  }  printf("Reversed string: ");  while(!isempty())  {  printf("%c",pop());  } }