Posts

Assignment 8 Question 1

#include <stdio.h> #include <stdlib.h> struct DoublyLinkedList{ int data; struct DoublyLinkedList *prev; struct DoublyLinkedList *next; }; void removeNode ( struct DoublyLinkedList **headptr, struct DoublyLinkedList **tailptr, struct DoublyLinkedList *ptr) { if ( ptr == *headptr ){ *headptr = ptr->next; (*headptr)->prev = NULL; }else{ if ( ptr == *tailptr ) { *tailptr = ptr->prev; (*tailptr)->next=NULL; }else{ ptr->prev->next = ptr->next; ptr->next->prev = ptr->prev; } } free ( ptr ); ptr = NULL; return; } void removeData ( struct DoublyLinkedList **headptr, struct DoublyLinkedList **tailptr, int n) { struct DoublyLinkedList *curr; curr = *headptr; while ( curr != NULL && curr->data != n ){ curr = curr->next; } if ( curr != NULL ){ removeNode(headptr, tailptr, curr); } return; } struct DoublyLinkedList *createNode(int n) {

Assignment 8 Question 2

#include <stdio.h> int main() { int n; scanf("%d", &n); int a[4]; int count =0; a[0]=n%1000; n=n/1000; if(a[0]){ count=1; } a[1]=n%100; n=n/100; if(a[1]){ count=2; } a[2]=n%100; n=n/100; if(a[2]){ count=3; } a[3]=n%100; n=n/100; if(a[3]){ count=4; } int i; for(i=0;i<count;i++) { printf("%d\n", a[i]); } return 0; }

Assignment 7 Question 2

#include <stdio.h> #include <stdlib.h> struct node {     int data;     struct node *next; }; typedef struct node NODE; int main() {     NODE *head=NULL;     NODE *tail=NULL;     int a,n;     scanf("%d",&a);     while(a!=-1)     {         NODE *s;         s=(NODE *)malloc(sizeof(NODE));         s->data=a;         s->next=NULL;         if(head==NULL)         {              head=s;              tail=s;         }         else         {             tail->next=s;             tail=s;         }         scanf("%d",&a);     }     scanf("%d",&n);     struct node *temp=head;     int count=0;     while(temp!=NULL)     {         count++;         temp=temp->next;     }     int l=count-n;     if(l<0)     {         printf("-1");     }     else     {         while(l!=0)         {             head=head->next;             l--;         }          temp=head;  

Assignment 7 Question 1

#include <stdio.h> struct details{ char name[128]; int p,c,m; }; int main() { int i,j,n; scanf("%d", &n); struct details a[n]; for(i=0;i<n;i++) { scanf("%s %d %d %d", a[i].name,&a[i].p,&a[i].c,&a[i].m); } struct details temp; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(j+1==n){ break; } if(a[j].p>a[j+1].p) { temp=a[j+1]; a[j+1]=a[j]; a[j]=temp; }else if(a[j].p==a[j+1].p) { if(a[j].c>a[j+1].c) { temp=a[j+1]; a[j+1]=a[j]; a[j]=temp; }else if(a[j].c==a[j+1].c) { if(a[j].m>a[j+1].m) { temp=a[j+1]; a[j+1]=a[j]; a[j]=temp; } } } } } for(i=0;i<n;i++) { printf("%s-%d-%d-%d\n", a[i].name,a[i].p,a[i].c,a[i].m); } return 0; }

Assignment 6 Question 3

#include <stdio.h> int main() { int n,m; scanf("%d %d", &m ,&n ); int s[n]; s[0]=m; int i=1; int j=0; int flag=0; int count=1; for(i=1;i<n;i++) { s[i]=(2*s[i-1])%n; for(j=0;j<i;j++) { if(i==j) { continue; } if(s[i]==s[j]) { flag=1; } } if(flag) { break; } count++; } printf("%d", count); return 0; }

Assignment 6 Question 2

#include <stdio.h> #define MAX 100 int main() { char str1[MAX]; scanf("%s", &*str1); int i=0; int count1=0; int high=1; for(i=0;i<MAX;i++)   {    if(str1[i]==str1[i+1])    {     count1++;    }else{     if(count1>high)     {    high=count1; }     count1=1;        }    if(str1[i]=='\0')    {     break;    }   }     printf("%d", high); return 0; }

Assignment 6 Question 1 (probably a difficult question)

// C program to check if two strings // are anagrams of each other #include <stdio.h> #define NO_OF_CHARS 256   /* function to check whether two strings are anagram of     each other */ int areAnagram(char* str1, char* str2) {     // Create 2 count arrays and initialize all values as 0     int count1[NO_OF_CHARS] = { 0 };     int count2[NO_OF_CHARS] = { 0 };     int i;       // For each character in input strings, increment count in     // the corresponding count array     for (i = 0; str1[i] && str2[i]; i++) {         count1[str1[i]]++;         count2[str2[i]]++;     }       // If both strings are of different length. Removing this     // condition will make the program fail for strings like     // "aaca" and "aca"     if (str1[i] || str2[i])         return 0;       // Compare count arrays     for (i = 0; i < NO_OF_CHARS; i++)         if (count1[i] != count2[i])             return 0;       return 1; }   /* Driver