Ds Lab Assignment1 Disha Edited.docx

  • Uploaded by: Disha Goyal
  • 0
  • 0
  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Ds Lab Assignment1 Disha Edited.docx as PDF for free.

More details

  • Words: 7,263
  • Pages: 104
1) Find the max and min element in LL #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head; void create(); void maxmin(); int main(){ create(); maxmin(); return 0; } void maxmin(){ struct node *temp = head; int max=-2134567, min=2134567; while(temp!=NULL){ if(temp->data > max){ max = temp->data; } if(temp->data < min){ min = temp->data; } temp = temp->next; } printf("Max = %d\n", max); printf("Min = %d\n", min); } void create() { int c, n, i; struct node *temp, *rear; printf("Enter number of elements: "); scanf("%d",&n); for(i=0; idata = c; 1

} }

temp->next = NULL; if (head == NULL) { head = temp; } else { rear->next = temp; } rear = temp; printf("\n");

2

2) Find union/intersection of two LL #include<stdio.h> #include<stdlib.h> #include<stdbool.h> /* Link list node */ struct Node { int data; struct Node* next; }; void add(struct Node** head_ref, int new_data); bool isPresent(struct Node *head, int data); struct Node *getUnion(struct Node *head1, struct Node *head2) { struct Node *p = NULL; struct Node *t1 = head1, *t2 = head2; while (t1 != NULL) 3

{ add(&p, t1->data); t1 = t1->next; } while (t2 != NULL) { if (!isPresent(p, t2->data)) add(&p, t2->data); t2 = t2->next; } return p; } struct Node *getIntersection(struct Node *head1, struct Node *head2) { struct Node *result = NULL; struct Node *t1 = head1; while (t1 != NULL) { if (isPresent(head2, t1->data)) add (&result, t1->data); t1 = t1->next; } }

return result;

void add (struct node** head_ref, int new_data) { struct node* new_node = (struct node*) malloc(sizeof(struct node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } void displayList (struct node *node) { while (node != NULL) { printf ("%d ", node->data); node = node->next; } 4

printf("\n"); } bool isPresent (struct Node *head, int data) { struct Node *t = head; while (t != NULL) { if (t->data == data) return 1; t = t->next; } return 0; } int main() { struct Node* head1 = NULL; struct Node* head2 = NULL; struct Node* i = NULL; struct Node* u = NULL; add (&head1, 20); add (&head1, 4); add (&head1, 15); add (&head1, 10); add (&head2, 10); add (&head2, 2); add (&head2, 4); add (&head2, 8); i = getIntersection (head1, head2); u = getUnion (head1, head2); printf ("\n First list is \n"); printList (head1); printf ("\n Second list is \n"); printList (head2); printf ("\n Intersection list is \n"); printList (i); printf ("\n Union list is \n"); printList (u); 5

return 0; }

3) Write a program to delete a LL #include<stdio.h> #include<stdlib.h> #include struct node { int data; struct node* next; 6

}; struct Node *head; void deleteList(struct Node** head_ref) { struct node* current = *head_ref; struct node* next;

}

while (current != NULL) { next = current->next; free(current); current = next; } *head_ref = NULL;

void create() { int c, n, i; struct node *temp, *rear; printf("Enter number of elements: "); scanf("%d",&n); for(i=0; idata = c; temp->next = NULL; if (head == NULL) { head = temp; } else { rear->next = temp; } rear = temp; } printf("\n"); } int main() { 7

create(); printf("\nDeleting linked list"); deleteList(&head); }

printf("\nLinked list deleted");

8

4) Reverse a LL iteratively and recursively #include<stdio.h> #include<stdlib.h> #include struct node { int data; struct node* next; }; struct Node *head; void create(); void displayList(); void reverseList(); void create() { int c, n, i; struct node *temp, *rear; printf("Enter number of elements: "); scanf("%d",&n); for(i=0; idata = c; temp->next = NULL; if (head == NULL) { head = temp; } else { rear->next = temp; } rear = temp; } } void reverseList() 9

{ struct node *prevNode=NULL, *curNode=head, *nextNode; while(curNode != NULL) { nextNode = curNode->next; curNode->next = prevNode;

}

prevNode = curNode; curNode = nextNode;

head = prevNode; } void displayList() { struct node *temp; if(head == NULL) { printf("List is empty."); } else { temp = head; while(temp != NULL) { printf("%d ", temp->data); temp = temp->next; } } printf("\n"); } struct node* reverse_R(){ if(head==NULL || head->next == NULL){ return head; } struct node *finalHead = reverse_R(head->next); struct node *temp = finalHead; while(temp->next!=NULL){ temp = temp->next; } temp->next = head; 10

head->next = NULL; return finalHead; } int main(){ create(); displayList(); reverseList(); displayList(); return 0; } }

11

5) Merge two sorted LL #include<stdio.h> #include<stdlib.h> #include struct node { int data; struct node* next; }; void add (struct node** head_ref, int new_data) { struct node* new_node = (struct node*) malloc(sizeof(struct node));

}

new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node;

void displayList (struct node *node) { while (node != NULL) 12

{ printf ("%d ", node->data); node = node->next; } printf("\n"); } struct node* mergeTwoList(struct node *head1, struct node *head2) { struct node *head = NULL, *tail = NULL; if(head1->data <= head2->data) { head = head1; tail = head1; head1 = head1->next; } else { head = head2; tail = head2; head2 = head2->next; } while(head1 != NULL && head2 != NULL) { if(head1->data <= head2->data) { tail->next = head1; tail = head1; head1 = head1->next; } else { tail->next = head2; tail = head2; head2 = head2->next; } if(head1 != NULL) { tail->next = head1; } else { tail->next = head2; }

}

} return head;

int main(){ struct Node* head1 = NULL; struct Node* head2 = NULL; struct Node* i = NULL; struct Node* u = NULL; 13

add (&head1, 20); add (&head1, 8); add (&head1, 4); add (&head1, 1); printf("List1 is "); displayList(head1); add (&head2, 7); add (&head2, 6); add (&head2, 2); add (&head2, 5); printf("\nList2 is "); displayList(head2); struct node *newNode= mergeTwoList(head1, head2); printf("\nNew List is "); displayList(newNode); return 0; }

14

6) Middle of LL #include<stdio.h> #include<stdlib.h> #include struct node { int data; struct node* next; }; void add (struct node** head_ref, int new_data) { struct node* new_node = (struct node*) malloc(sizeof(struct node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } void displayList (struct node *node) 15

{ while (node != NULL) { printf ("%d ", node->data); node = node->next; } printf("\n"); } struct node* printMiddle(struct node *head) { struct node *slow = head; struct node *fast = head; int length = lengthRec(head); if(length%2 == 0){ while(fast->next->next != NULL){ slow = slow->next; fast = fast->next->next; } return slow; } else{

} }

while(fast->next!=NULL){ slow = slow->next; fast = fast->next->next; } return slow;

int lengthRec(struct node *h) { if(h==NULL) { return 0; } return 1 + lengthRec(h->next); } int main(){ struct node* head1 = NULL; add (&head1, 20); add (&head1, 8); add (&head1, 4); add (&head1, 1); add(&head1, 5); 16

printf("List1 is "); displayList(head1); struct node *mid = printMiddle(head1); printf("\nMid is %d", mid->data); return 0; }

7) Check if a LL is palindrome or not #include<stdio.h> #include<stdlib.h> #include struct node { char data; 17

struct node* next; }; void add (struct node** head_ref, char new_data) { struct node* new_node = (struct node*) malloc(sizeof(struct node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } void displayList (struct node *node) { while (node != NULL) { printf ("%c ", node->data); node = node->next; } printf("\n"); } struct node* Middle(struct node *head) { struct node *slow = head; struct node *fast = head; while(fast->next!=NULL){ slow = slow->next; fast = fast->next->next; } return slow; } struct node* reverse(struct node *head) { struct node *prev = NULL, *current = head, *next = NULL; while(current!=NULL){ next = current->next; current->next = prev; prev = current; current = next; } return prev; }

18

int isPalindrome(struct node *head) { struct node *mid = Middle(head); struct node *head1 = head; struct node *head2 = mid->next; mid->next = NULL; struct node *revHead2 = reverse(head2);

}

while(head1!=NULL && revHead2!=NULL){ if(head1->data != revHead2->data){ return 0; } head1 = head1->next; revHead2 = revHead2->next; } return 1;

int main(){ struct node* head1 = NULL; add (&head1, 'a'); add (&head1, 'b'); add (&head1,'c'); add (&head1, 'b'); add(&head1, 'a'); printf("List1 is "); displayList(head1);

}

int ans = isPalindrome(head1); if(ans==0){ printf("NO"); } else{ printf("YES"); } return 0;

19

8) Remove Duplicates from a LL #include<stdio.h> #include<stdlib.h> #include struct node { int data; struct node* next; }; void add (struct node** head_ref, int new_data) { struct node* new_node = (struct node*) malloc(sizeof(struct node)); new_node->data = new_data; new_node->next = (*head_ref); 20

(*head_ref) = new_node; } void displayList (struct node *node) { while (node != NULL) { printf ("%d ", node->data); node = node->next; } printf("\n"); } struct node* removeDuplicates(struct node *head) { struct node *temp = head, *nextNode = NULL; while(temp->next!=NULL){ if(temp->data == temp->next->data){ nextNode = temp->next->next; temp->next = NULL; temp->next = nextNode; } else temp = temp->next; } return head; } int main(){ struct node* head1 = NULL; add (&head1, 1); add (&head1, 1); add (&head1, 2); add (&head1, 3); add(&head1, 3); printf("List1 is "); displayList(head1); struct node *newHead = removeDuplicates(head1); printf("\nNew List is "); displayList(newHead); return 0; }

21

9) Delete n nodes after m nodes in a LL #include<stdio.h> #include<stdlib.h> #include

22

struct node { int data; struct node* next; }; struct Node *head; void create(); void displayList(); void create() { int c, n, i; struct node *temp, *rear; printf("Enter number of elements: "); scanf("%d",&n); for(i=0; idata = c; temp->next = NULL; if (head == NULL) { head = temp; } else { rear->next = temp; } rear = temp; } } void displayList() { struct node *temp; if(head == NULL) { printf("List is empty."); } else { temp = head; 23

while(temp != NULL) { printf("%d ", temp->data); temp = temp->next; } } printf("\n"); } struct node* skipMdeleteN(int m, int n){ struct node *curr=head, *t; int count; while(curr){ for(count=1; count<m;count++){ if(curr == NULL){ return head; } curr=curr->next; } t = curr->next; for(count=1; count<=n && t!=NULL; count++){ struct node *temp = t; t = t->next; free(temp); } curr->next = t; curr = t; } return head; } int main(){ create(); displayList(); int m,n; scanf("%d%d",&m,&n); struct node *newHead = skipMdeleteN(m, n); head = newHead; displayList(); return 0; }

24

9) Reverse nodes pairwise in a LL #include<stdio.h> #include<stdlib.h> #include struct node { int data; struct node* next; }; struct Node *head; void create(); void displayList(); 25

void create() { int c, n, i; struct node *temp, *rear; printf("Enter number of elements: "); scanf("%d",&n); for(i=0; idata = c; temp->next = NULL; if (head == NULL) { head = temp; } else { rear->next = temp; } rear = temp; } } void displayList() { struct node *temp; if(head == NULL) { printf("List is empty."); } else { temp = head; while(temp != NULL) { printf("%d ", temp->data); temp = temp->next; } } printf("\n"); } struct node* pairReverse(struct node *temp){ 26

if(temp == NULL){ return NULL; } struct node *tail1 = temp; int count = 1; while(count<2 && tail1->next!=NULL){ tail1 = tail1->next; count++; } struct node *head2 = tail1->next; tail1->next = NULL; struct node *prev = NULL, *current = temp, *next = NULL; while(current!=NULL){ next = current->next; current->next = prev; prev = current; current = next; } struct node *tail2 = prev; while(tail2->next!=NULL){ tail2 = tail2->next; } tail2->next = pairReverse(head2); return prev; } int main(){ create(); displayList(); struct node *newHead = pairReverse(head); head = newHead; displayList(); return 0; }

27

10) Reverse a LL after every k nodes #include<stdio.h> #include<stdlib.h> #include struct node { int data; struct node* next; }; struct Node *head; void create(); void displayList(); void create() { int c, n, i; struct node *temp, *rear; printf("Enter number of elements: "); scanf("%d",&n); for(i=0; i
} }

printf("Enter number: "); scanf("%d", &c); temp = (struct node *)malloc(sizeof(struct node)); temp->data = c; temp->next = NULL; if (head == NULL) { head = temp; } else { rear->next = temp; } rear = temp;

void displayList() { struct node *temp; if(head == NULL) { printf("List is empty."); } else { temp = head; while(temp != NULL) { printf("%d ", temp->data); temp = temp->next; } } printf("\n"); } struct node* kReverse(struct node *temp, int k){ if(temp == NULL){ return NULL; } struct node *tail1 = temp; int count = 1; while(countnext!=NULL){ tail1 = tail1->next; count++; } 29

struct node *head2 = tail1->next; tail1->next = NULL; struct node *prev = NULL, *current = temp, *next = NULL; while(current!=NULL){ next = current->next; current->next = prev; prev = current; current = next; } struct node *tail2 = prev; while(tail2->next!=NULL){ tail2 = tail2->next; } tail2->next = kReverse(head2, k); return prev; } int main(){ create(); displayList(); int k; scanf("%d",&k); struct node *newHead = kReverse(head, k); head = newHead; displayList(); return 0; }

30

11) Find kth node from last in a LL #include<stdio.h> #include<stdlib.h> #include struct node { int data; struct node* next; }; struct Node *head; void create(); void displayList(); void create() { int c, n, i; struct node *temp, *rear; printf("Enter number of elements: "); scanf("%d",&n); for(i=0; i
scanf("%d", &c); temp = (struct node *)malloc(sizeof(struct node)); temp->data = c; temp->next = NULL; if (head == NULL) { head = temp; } else { rear->next = temp; } rear = temp; } } void displayList() { struct node *temp; if(head == NULL) { printf("List is empty."); } else { temp = head; while(temp != NULL) { printf("%d ", temp->data); temp = temp->next; } } printf("\n"); } struct node* nFromLast(struct node *temp, int n){ struct node *main = temp, *ref = temp; int count=1; while(count<=n){ if(ref==NULL){ return NULL; } ref = ref->next; count++; } 32

while(ref!=NULL){ main = main->next; ref = ref->next; } return main; } int main(){ create(); displayList(); int n; scanf("%d",&n); struct node *newNode= nFromLast(head, n); printf("%d",newNode->data); return 0; }

12) Find frequency of all data in LL #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; struct nodeOccur { int num; 33

};

int times; struct nodeOccur *next;

void create(struct node **); void occur(struct node *, struct node_occur **); void disp_occur(struct node_occur *); int main() { struct node *p = NULL; struct nodeOccur *head = NULL; int n;

}

printf("Enter data into the list\n"); create(&p); printf("Displaying the occurence of each node in the list:\n"); occur(p, &head); disp_occur(head); return 0;

void occur(struct node *head, struct node_occur **result) { struct node *p; struct node_occur *temp, *prev; p = head; while (p != NULL) { temp = *result; while (temp != NULL && temp->num != p->num) { prev = temp; temp = temp->next; } if (temp == NULL) { temp = (struct node_occur *)malloc(sizeof(struct node_occur)); temp->num = p->num; temp->times = 1; temp->next = NULL; if (*result != NULL) { prev->next = temp; } else { 34

*result = temp;

}

} } else { temp->times += 1; } p = p->next;

} void create(struct node **head) { int c, n, i; struct node *temp, *rear; printf("Enter number of elements: "); scanf("%d",&n); for(i=0; inum = c; temp->next = NULL; if (*head == NULL) { *head = temp; } else { rear->next = temp; } rear = temp; } printf("\n"); } void disp_occur(struct node_occur *p) { while (p != NULL) { printf("%d\t%d\n", p->num, p->times); p = p->next; } } 35

13) Print all codes #include <stdio.h> #include<string.h> void printCodes(); int main() { char input[20]; scanf("%[^\n]%*c", input); char output[500]; output[0] = '\0'; printCodes(input, 0, output); return 0; } void printCodes(char input[], int i, char output[]){ if(input[i] == '\0'){ printf("%s\n", output); return; } char c = input[i]-'0'+96; char substring[500]; int j = 0, k=0; 36

for(j=0; output[j]!='\0'; j++){ substring[j] = output[j]; } substring[j] = c; substring[j+1] = '\0'; printCodes(input, i+1, substring); if(input[i+1]!='\0'){ int p = (input[i]-'0')*10 + (input[i+1]-'0'); if(p>=10 && p<=26){ char substring2[500]; for(k=0; output[k]!='\0'; k++){ substring2[k] = output[k]; } substring2[k] = (char) (p+96); substring2[k+1] = '\0'; printCodes(input, i+2, substring2); } } }

37

14) Add variable number of arguments #include <stdarg.h> #include <stdio.h> int sumV(int count, ...) { va_list ap; va_start(ap, count); int sum = va_arg(ap, int); for (int i = 2; i <= count; i++) { sum = sum + va_arg(ap, int); } va_end(ap); }

return sum;

int main() { printf("Sum is %d", sumV(5, 'a', 2, 3, 4, 5)); return 0; }

38

15) Print all square matrices #include <stdio.h> int main() { int mtrx_size = 3; int mat[3][3] = { { 1, 2, 3}, { 9,10,11}, {17,18,19}, }; int i, j, ioff, joff, off_cnt; int sub_mtrx_size; for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--) { off_cnt = mtrx_size - sub_mtrx_size + 1; for (ioff = 0; ioff < off_cnt; ioff++) { for (joff = 0; joff < off_cnt; joff++) { for (i = 0; i < sub_mtrx_size; i++) { for (j = 0; j < sub_mtrx_size; j++) { printf("%3d ", mat[i+ioff][j+joff]); } printf("\n"); } printf("\n"); } } } return 0; 39

}

16) Run DOS through C #include <stdio.h> #include<process.h> int main() { int option = 0; printf("\n**********************\n"); printf("1. Open Notepad...\n"); printf("2. Get IP Address...\n"); printf("3. Shut down the computer...\n"); printf("Enter choice: "); scanf("%d",&option); switch(option){ case 1: system("notepad"); break; 40

}

case 2: system("ipconfig"); system("pause"); break; case 3: system("SHUTDOWN -S"); system("pause"); break; default: printf("\n Invalid choice");

return 0; }

17) Sparse matrix #include<stdio.h> #include void check() { 41

int a[10][10],r,c,j,i,ctr; counter=0; printf("Enter number of rows and columns: "); scanf("%d %d",&r,&c); printf("Enter matrix: \n"); for(i=0;i(r*c)/2) printf("This is a sparse matrix"); else printf("This is not a sparse matrix");

} void gen() { int a[10][10],b[10][10],c[10][10],f,f1,r1,r2,c1,c2,i,j,k,counter,ch; counter=0; f=0; srand(time(NULL)); printf("Enter number of rows and columns: "); scanf("%d %d",&r1,&c1); for(i=0;i
scanf("%d",&ch); if(ch>=1&&ch<=3) { printf("Enter a second matrix: "); printf("Enter number of rows and columns: "); scanf("%d %d",&r2,&c2); if(ch==1||ch==2 && r1!=r2||c1!=c2) { f=1; } else if(ch==3 && c1!=r2) { f=1; } if(f==0) for(i=0;i
} printf("\n");

}

} break; case 3: printf("Difference is: \n"); for(i=0;i
} int main() { int ch; printf("1.Check whether matrix is sparse\n2.Generate sparse matrix\nEnter choice: "); scanf("%d",&ch); switch(ch) { case 1: check(); break; case 2: gen(); break; } return 0; }

44

18) Concatenate two strings #include<stdio.h> void strcat(char *s,char *t) { int i=0; for(; *s!='\0';i++,s++); for(;*t!='\0';i++,t++,s++) { *s=*t; } *s='\0'; s-=i; } 45

int main() { char s[80],t[80]; printf("Enter string: "); scanf("%s",&s); printf("Second string: "); scanf("%s",&t); strcat(s,t); printf("New string is: %s",s); return 0; }

19)Copy one string to other using pointers #include<stdio.h> #include char * strcat(char *s) { int i; char *t; for(i=0; *s!='\0';i++,s++,t++) { *t=*s; } *t='\0'; t-=i; return t; } Int main() { char s[80],*t; printf("Enter string: "); 46

}

scanf("%s",&s); t=strcat(s); printf("New string : %s",t); return 0;

20) Write a program to reverse a linked list using atmost two extra pointers. #include <stdio.h> #include struct Node { int data; struct Node* next; }; void reverse(struct Node** head_ref) { struct Node* current = *head_ref; struct Node* next; while (current->next != NULL) { next = current->next; current->next = next->next; next->next = (*head_ref); *head_ref = next; } } void push(struct Node** head_ref, int new_data) { struct Node* new_node = new Node; new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } 47

void printList(struct Node* head) { struct Node* temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } } void main() { clrscr(); struct Node* head = NULL; push(&head, 20); push(&head, 4); push(&head, 15); push(&head, 85); printf("Given linked list\n"); printList(head); reverse(&head); printf("\nReversed Linked list \n"); printList(head); getche(); }

48

21) Write a program given a linked list, add 1 to it and create a new linked list. #include<stdio.h> #include struct Node { int data; Node* next; }; Node *newNode(int data) { Node *new_node = new Node; new_node->data = data; new_node->next = NULL; return new_node; } int addWithCarry(Node *head) { if (head == NULL) return 1; int res = head->data + addWithCarry(head->next); 49

head->data = (res) % 10; return (res) / 10; } Node* addOne(Node *head) { int carry = addWithCarry(head); if (carry) { Node *newNode = new Node; newNode->data = carry; newNode->next = head; return newNode; } return head; } void printList(Node *node) { while (node != NULL) { printf("%d", node->data); node = node->next; } printf("\n"); } int main(void) { Node *head = newNode(1); head->next = newNode(9); head->next->next = newNode(9); head->next->next->next = newNode(9); printf("List is "); printList(head); head = addOne(head); printf("\nResultant list is "); printList(head); }

return 0;

50

22) DELETE THE NODES OF THE LINKED LIST WHOSE SUM IS ZERO. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> struct node { char data; struct node* next; 51

}; bool isPalindromeUtil(struct node **left, struct node *right) { if (right == NULL) return true; bool isp = isPalindromeUtil(left, right->next); if (isp == false) return false; bool isp1 = (right->data == (*left)->data); *left = (*left)->next; return isp1; } bool isPalindrome(struct node *head) { isPalindromeUtil(&head, head); } void push(struct node** head_ref, char new_data) { struct node* new_node = (struct node*) malloc(sizeof(struct node)); new_node->data = new_data; new_node->next = (*head_ref); }

(*head_ref) = new_node;

void printList(struct node *ptr) { while (ptr != NULL) { printf("%c->", ptr->data); ptr = ptr->next; } printf("NULL\n"); } int main() { struct node* head = NULL; char str[] = "abacaba"; 52

int i; for (i = 0; str[i] != '\0'; i++) { push(&head, str[i]); printList(head); isPalindrome(head)? printf("Is Palindrome\n\n"): printf("Not Palindrome\n\n"); } return 0; }

23) LARGEST NUMBER #include #include using namespace std; void sort(int A[], int size) { int temp=0; for(int i=0;i<size-1;i++) { for(int j=0;j<size-i-1;j++) { if(A[j]>A[j+1]) { temp= A[j]; A[j]=A[j+1]; A[j+1]=temp; } } } cout<<"Largest number:"; for(int i=size-1;i>=0;i--) { cout<>size; cout<<"Enter digits:"; 53

for(int i=0;i<size;i++) {cin>>A[i]; } sort(A,size); }

24) BOUNDARY OF SUBMATRIX SHOULD BE 1 #include using namespace std; bool calculate(int [][9],int ,int); int main() { int m,n,arr[9][9]; cout<<"Enter the number of rows and columns:"; cin>>m>>n; cout<<"Enter the elements of the array:"; for(int i=0;i<m;i++) { for(int j=0;j>arr[i][j]; } int size; if(m>n) { bool b=0; for(size=n;size>1;size--) { int x=n-size+1,y=m; for(int i=0;i<x+m-2;i++) { if(i<m-1){ 54

for(int j=0;j<x;j++) { int out1[9][9]={0}; for(int k=0;k2) { for(int z=0;z<2;z++) { for(int l=0;l<size;l++) out2[z+i][l+j]=arr[z+i][l+j]; } b=calculate(out2,2,size); if(b==1) break; } } if(b==1) break; y--;

} } if(b==1) break; }

} else { bool b=0; for(size=n;size>1;size--) { int x=n-size+1,y=m; for(int i=0;i<x+1;i++) { if(i<m-1) { for(int j=0;j<x;j++) { int out1[9][9]={0}; for(int k=0;k
if(b==1) break; int out2[9][9]={0}; if(y>2) { for(int z=0;z
} if(b==1) break; y--;

} if(b==1) break; }

if(b==1) break;

} } return 0; } bool calculate(int input[][9],int row,int col) { int a=0,r=row,cs=0,c=col,flag=0; for(int i=cs;i
} } c--; for(int i=c-1;i>=cs;i--) { if(input[r-1][i]==0) { flag=1; break; } } for(int i=r-1;i>=a;i--) { if(input[i][cs]==0) { flag=1; break; } } if(flag==1) return 0; else { for(int i=0;i
57

25) CONVERT LOWERCASE TO UPPERCASE #include #include #include<stdio.h> #include<string.h> #include using namespace std; int main() { int len; char a[50]; cout<<"Enter the string: "; cout<<endl; cout<<endl; gets(a); len=strlen(a); cout<<"The new string is:"; cout<<endl; for(int i=0;i='A')&& (a[i]<='Z'))?a[i]=(tolower(a[i])):(a[i]=toupper(a[i])); } cout< #include #include int main() { char text[100]; std::ifstream.infile; infile.open("ABC.txt",ios::in); infile>>text; std::cout<
} (B) #include #include #include int main() { char text[100]; std::ifstream infile; infile.open("ABCD.txt",ios::in); infile>>text; std:: cout< int main(int argc, char **argv) { remove(argv[0]); return 0; }

28) SEGREGATE ODD EVEN TERMS IN A LL #include<stdio.h> #include #include using namespace std; struct node { int info; node *next; }*start,*newptr,*save,*ptr; 59

node *create(int); void insert(node* ); void display(node*); void segregate(struct node** head) { node* end = *head; node* prev=NULL; node* curr=*head; while(end->next!=NULL) end = end->next; node* new_end=end; while(curr->info%2!=0 && curr!=end) { new_end->next=curr; curr=curr->next; new_end->next->next=NULL; new_end= new_end->next; } if((curr->info)%2==0) { *head=curr; while(curr!=end) { if(curr->info%2==0) { prev=curr; curr=curr->next; } else { prev->next=curr->next; curr->next=NULL; new_end->next=curr; new_end=curr; curr=prev->next; } } } else prev=curr; if(new_end!=end && (end->info)%2!=0) { prev->next=end->next; end->next=NULL; new_end->next=end; } return; } 60

int main() { start=NULL; int inf; char ch='y'; while(ch=='y'||ch=='Y') { cout<<"Enter information for the new node\n:"; cin>>inf; newptr=create(inf); insert(newptr); cout<<"\nNow the list is\n:"; display(start); cout<<"\nPress Y for continuing, N for exiting\n:"; cin>>ch; } if(ch=='n'||ch=='N') { cout<<"After deleting duplicates in a linked list: \n"; segregate(&start); display(start); } return 0; } struct node * create(int n) { ptr=new node; ptr->info=n; ptr->next=NULL; return ptr; } void insert(node* np) { if(start==NULL) start=np; else { save=start; start=np; np->next=save; } } void display(struct node* np) 61

{ while(np!=NULL) { cout<info<<"->"; np=np->next; } cout<<"!!"; }

29) DELETE THE LAST OCCURRENCE #include<stdio.h> #include #include using namespace std; struct node { int info; node *next; }*start,*newptr,*save,*ptr; node *create(int); void insert(node* ); void display(node*); void delLast(struct node* head, int m) { node *temp = head; node *ptr = NULL; while (temp) 62

{ if (temp->info== m) ptr = temp; temp = temp->next;

} if (ptr != NULL && ptr->next == NULL) { temp = head; while (temp->next != ptr) temp = temp->next; temp->next = NULL; } if (ptr != NULL && ptr->next != NULL) { ptr->info = ptr->next->info; temp = ptr->next; ptr->next = ptr->next->next; delete(temp); }

} int main() { start=NULL; int inf; char ch='y'; while(ch=='y'||ch=='Y') { cout<<"Enter information for the new node\n:"; cin>>inf; newptr=create(inf); insert(newptr); cout<<"\nNow the list is\n:"; display(start); cout<<"\nPress Y for continuing, N for exiting\n:"; cin>>ch; } if(ch=='n'||ch=='N') { delLast(start, 4); cout<<"List after deletion of 4:\n "; display(start); return 0; } return 0; } struct node * create(int n) { ptr=new node; 63

ptr->info=n; ptr->next=NULL; return ptr; } void insert(node* np) { if(start==NULL) start=np; else { save=start; start=np; np->next=save; } } void display(struct node* np) { while(np!=NULL) { cout<info<<"->"; np=np->next; } cout<<"!!"; }

64

30) DETECTION OF A LOOP #include<stdio.h> #include #include using namespace std; struct node { int info; node *next; }*start,*newptr,*save,*ptr; node *create(int); void insert(node* ); void display(node*); int detectLoop(struct node *list) { 65

node *slow= list, *fast= list; while(slow && fast && fast->next) { slow = slow->next; fast = fast->next->next; if (slow == fast) { cout<<"There is a cycle in the linked list."; } else cout<<"There is no cycle in the Linked list."; } return 0; } int main() { start=NULL; int inf; char ch='y'; while(ch=='y'||ch=='Y') { cout<<"Enter information for the new node\n:"; cin>>inf; newptr=create(inf); insert(newptr); cout<<"\nNow the list is\n:"; display(start); cout<<"\nPress Y for continuing, N for exiting\n:"; cin>>ch; } if(ch=='n'||ch=='N') { detectLoop(start); return 0; } return 0; } struct node * create(int n) { ptr=new node; ptr->info=n; ptr->next=NULL; return ptr; } void insert(node* np) 66

{ if(start==NULL) start=np; else { save=start; start=np; np->next=save; } } void display(struct node* np) { while(np!=NULL) { cout<info<<"->"; np=np->next; } cout<<"!!"; }

31) Stack: Array and LL implementation 67

#include <stdio.h> #include <stdlib.h> struct Stack { int top; int a; int* array; }; struct Stack* createStack(int a) { struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack)); stack->a = a; stack->top = -1; stack->array = (int*) malloc(stack->capacity * sizeof(int)); return stack; } int isFull(struct Stack* stack) { return stack->top == stack->a-1; } int isEmpty(struct Stack* stack) { return stack->top == -1; } void push(struct Stack* stack, int item) { if (isFull(stack)) return; stack->array[++stack->top] = item; printf("%d pushed to stack\n", item); } int pop(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack->array[stack->top--]; } int main() {struct Stack* stack = createStack(100); push(stack, 10); push(stack, 20); push(stack, 30); printf("%d popped from stack\n", pop(stack)); }

return 0;

68

#include <stdio.h> #include <stdlib.h> struct StackNode { int data; struct StackNode* next; }; struct StackNode* newNode(int data) 69

{

}

struct StackNode* stackNode = (struct StackNode*) malloc(sizeof(struct StackNode)); stackNode->data = data; stackNode->next = NULL; return stackNode;

int isEmpty(struct StackNode *root) { return !root; } void push(struct StackNode** root, int data) { struct StackNode* stackNode = newNode(data); stackNode->next = *root; *root = stackNode; printf("%d pushed to stack\n", data); } int pop(struct StackNode** root) { if (isEmpty(*root)) return INT_MIN; struct StackNode* temp = *root; *root = (*root)->next; int popped = temp->data; free(temp); return popped; } int peek(struct StackNode* root) { if (isEmpty(root)) return INT_MIN; return root->data; } int main() { struct StackNode* root = NULL; push(&root, 10); push(&root, 20); push(&root, 30); printf("%d popped from stack\n", pop(&root)); printf("Top element is %d\n", peek(root)); 70

return 0; }

32) Queue: Array and LL implementation #include <stdio.h> #include <stdlib.h> struct Queue { int front, rear, size; unsigned capacity; int* array; }; struct Queue* createQueue(unsigned capacity) { struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue)); queue->capacity = capacity; queue->front = queue->size = 0; queue->rear = capacity - 1; // This is important, see the enqueue queue->array = (int*) malloc(queue->capacity * sizeof(int)); return queue; 71

} int isFull(struct Queue* queue) { return (queue->size == queue->capacity); } int isEmpty(struct Queue* queue) { return (queue->size == 0); } void enqueue(struct Queue* queue, int item) { if (isFull(queue)) return; queue->rear = (queue->rear + 1)%queue->capacity; queue->array[queue->rear] = item; queue->size = queue->size + 1; printf("%d enqueued to queue\n", item); } int dequeue(struct Queue* queue) { if (isEmpty(queue)) return INT_MIN; int item = queue->array[queue->front]; queue->front = (queue->front + 1)%queue->capacity; queue->size = queue->size - 1; return item; } int front(struct Queue* queue) { if (isEmpty(queue)) return INT_MIN; return queue->array[queue->front]; } int rear(struct Queue* queue) { if (isEmpty(queue)) return INT_MIN; return queue->array[queue->rear]; } int main() { struct Queue* queue = createQueue(1000); enqueue(queue, 10); enqueue(queue, 20); enqueue(queue, 30); enqueue(queue, 40); printf("%d dequeued from queue\n\n", dequeue(queue)); printf("Front item is %d\n", front(queue)); printf("Rear item is %d\n", rear(queue)); return 0; } 72

73

#include <stdlib.h> #include <stdio.h> struct QNode { int key; struct QNode *next; }; struct Queue { struct QNode *front, *rear; }; struct QNode* newNode(int k) { struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode)); temp->key = k; temp->next = NULL; return temp; } struct Queue *createQueue() { struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue)); q->front = q->rear = NULL; return q; } void enQueue(struct Queue *q, int k) { struct QNode *temp = newNode(k); if (q->rear == NULL) { q->front = q->rear = temp; return; } q->rear->next = temp; q->rear = temp; } struct QNode *deQueue(struct Queue *q) { if (q->front == NULL) return NULL; struct QNode *temp = q->front; q->front = q->front->next; if (q->front == NULL) q->rear = NULL; return temp; } int main() { struct Queue *q = createQueue(); 74

enQueue(q, 10); enQueue(q, 20); deQueue(q); deQueue(q); enQueue(q, 30); enQueue(q, 40); enQueue(q, 50); struct QNode *n = deQueue(q); if (n != NULL) printf("Dequeued item is %d", n->key); return 0; }

33) Implement multiple stack in common memory #include<stdio.h> #include #define MAX_X 5 #define MAX_Y 5 75

int topx=-1; int topy=10;

/*Begin of push_x*/ void push_x(int *stack) { int info;

if(topx>=(MAX_X-1)) {

printf("\n\nStack OverFlow"); return;

} else {

printf("\n\nEnter The info To Push"); scanf("%d",&info); topx++; stack[topx]=info;

}} /*End of push_x*/ /*Begin of push_y*/ void push_y(int *stack) { int info;

if(topy<=(MAX_Y))

76

{ printf("\n\nStack OverFlow"); return; } else { printf("\n\nEnter The info To Push"); scanf("%d",&info); topy--; stack[topy]=info; } } /*End of push_y*/ /*Begin of pop_x*/ void pop_x(int *stack) {

if(topx==-1) { printf("Stack X is Underflow"); return; } else { printf("Item Poped from stack X is:%d\n",stack[topx]);

topx--;

77

} } /*End of pop_x*/ /*Begin of pop_y*/ void pop_y(int *stack) {

if(topy==10) {printf("Stack y is Underflow"); return; } else { printf("Item Poped from stack Y is:%d\n",stack[topy]); topy++; }}

/*End of pop_y*/ /*Begin of display_x*/ void display_x(int *stack) { int i; if(topx==-1) { printf("Stack X is Empty"); return; } else { for(i=topx;i>=0;i--)

78

{printf("%d,",stack[i]);} printf("\n"); }} /*End of display_x*/ /*Begin of display_y*/ void display_y(int *stack) { int i; if(topy==10) {printf("Stack Y is Empty"); return;} else {for(i=topy;i<=9;i++) { printf("%d,",stack[i]); } printf("\n"); }

}

/*End of display_y*/ /*Begin of main*/ main() {

int choice; char ch; int stack[MAX_X+MAX_Y];

do

79

{ printf("1.Push_X\n2.Push_Y\n"); printf("\n3.Pop_X\n4.Pop_Y\n"); printf("\n5.Display_X\n6.Display_Y\n"); printf("\n7.Exit"); printf("\n\nEnter Choice"); scanf("%d",&choice); switch(choice) { case 1: push_x(stack);break; case 2: push_y(stack);break;

case 3: pop_x(stack);break; case 4: pop_y(stack);break;

case 5: display_x(stack);break; case 6: display_y(stack);break; case 7: break; default: printf("Wrong Option..."); } }while(choice!=7); } /*End of main*/

80

34) Implement binary tree with LL #include <stdio.h> #include <malloc.h> typedef struct node { struct node * left; char data; 81

struct node * right; }node; node* constructTree(void) { int index; node *temp = NULL; printf("Enter data "); scanf("%d",&index); if (index != -1) { temp = (node *)malloc( sizeof (node ) ); temp->left = constructTree(); temp->data = index; temp->right = constructTree(); } return temp; } void inorder( node *root ) { if (root != NULL) { inorder(root->left); printf("%d\t", root->data); inorder(root->right); } } void main() { node *root; root = constructTree(); printf("In-order Traversal: \n"); inorder(root); }

82

35) Mirror of a given tree #include <stdio.h> #include <malloc.h> typedef struct node { struct node * left; char data; struct node * right; }node; node* constructTree(void) { int index; node *temp = NULL; printf("Enter data "); scanf("%d",&index); if (index != -1) { temp = (node *)malloc( sizeof (node ) ); temp->left = constructTree(); temp->data = index; temp->right = constructTree(); } return temp; } void inorder( node *root ) { if (root != NULL) { inorder(root->left); printf("%d\t", root->data); inorder(root->right); 83

} } void convertToMirror(node *root) { if(root == NULL) { return; } convertToMirror(root->left); convertToMirror(root->right); node *t = root->left; root->left = root->right; root->right = t; } void main() { node *root; root = constructTree(); printf("In-order Traversal of original tree: \n"); inorder(root); convertToMirror(root); printf("\nIn-order Traversal of mirror tree: \n"); inorder(root); }

84

36) Delete a node in a tree #include <stdio.h> #include <malloc.h> #include typedef struct node { struct node * left; int data; struct node * right; }node; node* constructTree(void) { int index; node *temp = NULL; printf("Enter data "); 85

scanf("%d",&index); if (index != -1) { temp = (node *)malloc( sizeof (node ) ); temp->left = constructTree(); temp->data = index; temp->right = constructTree(); } return temp;

} void inorder( node *root ) { if (root != NULL) { inorder(root->left); printf("%d\t", root->data); inorder(root->right); } } int FindMin(node *root) { if (root == NULL) { return INT_MAX; } if (root->left != NULL) { return FindMin(root->left); } return root->data; }

node* Delete(node *root, int data) { if (root == NULL) { return NULL; } if (data < root->data) { root->left = Delete(root->left, data); } else if (data > root->data) { root->right = Delete(root->right, data); } else { if (root->left == NULL && root->right == NULL) { //free(root); root = NULL; } else if (root->left == NULL) { node *temp = root; root = root->right; temp->right = NULL; 86

free(temp); } else if (root->right == NULL) { node *temp = root; root = root->left; temp->left = NULL; free(temp); } else { node *temp = FindMin(root->right); root->data = temp->data; root->right = Delete(root->right, temp->data); free(temp); }

}

} return root;

void main() { node *root; int index; root = constructTree(); printf("In-order Traversal of original tree: \n"); inorder(root); printf("\nEnter data to be deleted "); scanf("%d",&index); root = Delete(root,index); printf("\nIn-order Traversal after deletion of node: \n"); inorder(root); }

87

37) Height of a tree #include <stdio.h> #include <malloc.h> typedef struct node { struct node * left; char data; struct node * right; }node; node* constructTree(void) { int index; node *temp = NULL; printf("Enter data "); scanf("%d",&index); if (index != -1) { temp = (node *)malloc( sizeof (node ) ); temp->left = constructTree(); temp->data = index; temp->right = constructTree(); 88

}

} return temp;

int height(node* node) { if (node==NULL) return 0; else { int lheight = height(node->left); int rheight = height(node->right);

}

if (lheight > rheight) return(lheight+1); else return(rheight+1);

} void inorder(node *root) { node* temp = root; if (root != NULL) { inorder(temp->left); printf("%d\t", temp->data); inorder(temp->right); } } void main() { node *root; root = constructTree(); printf("In-order Traversal: \n"); int h = height(root); inorder(root); printf("\nHeight of tree is: %d",h); }

89

38) Number of nodes in a tree #include <stdio.h> #include <malloc.h> typedef struct node { struct node * left; char data; struct node * right; }node; node* constructTree(void) { int index; node *temp = NULL; printf("Enter data "); scanf("%d",&index); if (index != -1) { temp = (node *)malloc( sizeof (node ) ); temp->left = constructTree(); temp->data = index; temp->right = constructTree(); } return temp; }

90

int height(node* node) { if (node==NULL) return 0; else { int lheight = height(node->left); int rheight = height(node->right);

}

if (lheight > rheight) return(lheight+1); else return(rheight+1);

} int inorder(node *root) { node* temp = root; int count = 1; if (root != NULL) { count += inorder(temp->left); printf("%d\t", temp->data); count += inorder(temp->right); } else{ return 0; } return count; } void main() { node *root; root = constructTree(); printf("In-order Traversal: \n"); int h = inorder(root); printf("\nNumber of nodes in tree is: %d",h); }

91

39) Number of nodes in left and right branch #include <stdio.h> #include <malloc.h> typedef struct node { struct node * left; char data; struct node * right; }node; node* constructTree(void) { int index; node *temp = NULL; printf("Enter data "); scanf("%d",&index); if (index != -1) { temp = (node *)malloc( sizeof (node ) ); temp->left = constructTree(); temp->data = index; temp->right = constructTree(); } return temp; } 92

int height(node* node) { if (node==NULL) return 0; else { int lheight = height(node->left); int rheight = height(node->right);

}

if (lheight > rheight) return(lheight+1); else return(rheight+1);

} void inorder( node *root ) { if (root != NULL) { inorder(root->left); printf("%d\t", root->data); inorder(root->right); } } int c(node *root) { node* temp = root; int count = 1; if (root != NULL) { count += c(temp->left); count += c(temp->right); } else{ return 0; } return count; } void main() { node *root; root = constructTree(); printf("In-order Traversal: \n"); inorder(root); int l = c(root->left); int r = c(root->right); printf("\nNumber of nodes in left branch tree is: %d and right branch of tree is: %d",l,r); }

93

40) BST implementation #include<stdio.h> #include<stdlib.h> struct node { int key; struct node *left, *right; }; struct node *newNode(int item) { struct node *temp = (struct node *)malloc(sizeof(struct node)); 94

temp->key = item; temp->left = temp->right = NULL; return temp; } void inorder(struct node *root) { if (root != NULL) { inorder(root->left); printf("%d ", root->key); inorder(root->right); } } struct node* insert(struct node* node, int key) { if (node == NULL) return newNode(key); if (key < node->key) node->left = insert(node->left, key); else if (key > node->key) node->right = insert(node->right, key); return node; } int main() { struct node *root = NULL; root = insert(root, 50); insert(root, 30); insert(root, 20); insert(root, 40); insert(root, 70); insert(root, 60); insert(root, 80); inorder(root); return 0; }

95

41) Different traversal in Binary Tree #include using namespace std; struct Node { int data; struct Node* left, *right; Node(int data) { this->data = data; left = right = NULL; } }; Post Order Traversal void printPost(struct Node* node) { if (node == NULL) return;

}

printPost(node->left); printPost(node->right); cout << node->data << " ";

In Order Traversal void printIn(struct Node* node) { if (node == NULL) return; printIn(node->left); cout << node->data << " "; printIn(node->right); } Pre Order Traversal void printPre(struct Node* node) { if (node == NULL) return;

}

cout << node->data << " "; printPre(node->left); printPre(node->right);

96

Level wise Traversal void printGivenLevel(struct Node* node, int level) { if (node == NULL) return; if (level == 1) cout << node->data << " "; else if (level > 1) { printGivenLevel(node->left, level-1); printGivenLevel(node->right, level-1); } } int height(struct Node* node) { if (node==NULL) return 0; else { int lheight = height(node->left); int rheight = height(node->right); if (lheight > rheight) return(lheight+1); else return(rheight+1); } } void printLevel(struct Node* root) { int h = height(root); int i; for (i=1; i<=h; i++) printGivenLevel(root, i); } int main() { struct Node *root = new Node(10); root->left = new Node(20); root->right = new Node(30); root->left->left = new Node(40); root->left->right = new Node(50); std::cout << "\nPreorder traversal of binary tree is \n"; printPre(root); 97

std::cout << "\nInorder traversal of binary tree is \n"; printIn(root); std::cout << "\nPostorder traversal of binary tree is \n"; printPost(root); std::cout<<"\nLevelorder traversal of binary tree is \n"; printLevel(root); }

return 0;

42) Merge sort #include <stdio.h> #define max 10 int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 }; int b[10]; void merging(int low, int mid, int high) { int l1, l2, i; for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) { if(a[l1] <= a[l2]) 98

b[i] = a[l1++]; else b[i] = a[l2++]; } while(l1 <= mid) b[i++] = a[l1++]; while(l2 <= high) b[i++] = a[l2++]; for(i = low; i <= high; i++) a[i] = b[i]; } void sort(int low, int high) { int mid;

}

if(low < high) { mid = (low + high) / 2; sort(low, mid); sort(mid+1, high); merging(low, mid, high); } else { return; }

int main() { int i; printf("List before sorting\n"); for(i = 0; i <= max; i++) printf("%d ", a[i]); sort(0, max); printf("\nList after sorting\n");

}

for(i = 0; i <= max; i++) printf("%d ", a[i]);

99

43) Quick sort #include <stdio.h> #include <stdbool.h> #define MAX 7 int intArray[MAX] = {4,6,3,2,1,9,7}; void printline(int count) { int i; for(i = 0;i < count-1;i++) { printf("="); } printf("=\n"); } void display() { int i; printf("[");

100

// navigate through all items for(i = 0;i < MAX;i++) { printf("%d ",intArray[i]); } }

printf("]\n");

void swap(int num1, int num2) { int temp = intArray[num1]; intArray[num1] = intArray[num2]; intArray[num2] = temp; } int partition(int left, int right, int pivot) { int leftPointer = left -1; int rightPointer = right; while(true) { while(intArray[++leftPointer] < pivot) { //do nothing } while(rightPointer > 0 && intArray[--rightPointer] > pivot) { //do nothing } if(leftPointer >= rightPointer) { break; } else { printf(" item swapped :%d,%d\n", intArray[leftPointer],intArray[rightPointer]); swap(leftPointer,rightPointer); } }

}

printf(" pivot swapped :%d,%d\n", intArray[leftPointer],intArray[right]); swap(leftPointer,right); printf("Updated Array: "); display(); return leftPointer;

void quickSort(int left, int right) { if(right-left <= 0) { return; } else { int pivot = intArray[right]; int partitionPoint = partition(left, right, pivot); 101

}

quickSort(left,partitionPoint-1); quickSort(partitionPoint+1,right);

} int main() { printf("Input Array: "); display(); printline(50); quickSort(0,MAX-1); printf("Output Array: "); display(); printline(50); }

44) Heap sort #include <stdio.h> void main() { int heap[10], no, i, j, c, root, temp; printf("\n Enter no of elements :"); scanf("%d", &no); 102

printf("\n Enter the nos : "); for (i = 0; i < no; i++) scanf("%d", &heap[i]); for (i = 1; i < no; i++) { c = i; do { root = (c - 1) / 2; if (heap[root] < heap[c]) /* to create MAX heap array */ { temp = heap[root]; heap[root] = heap[c]; heap[c] = temp; } c = root; } while (c != 0); } printf("Heap array : "); for (i = 0; i < no; i++) printf("%d\t ", heap[i]); for (j = no - 1; j >= 0; j--) { temp = heap[0]; heap[0] = heap[j]; /* swap max element with rightmost leaf element */ heap[j] = temp; root = 0; do { c = 2 * root + 1; /* left node of root element */ if ((heap[c] < heap[c + 1]) && c < j-1) c++; if (heap[root]
103

104

Related Documents

Assignment1
June 2020 9
Assignment1
June 2020 12
Assignment1.docx
December 2019 23
Disha Editorial
December 2019 8
Disha-lu
June 2020 7

More Documents from ""

Analysis.docx
April 2020 18
Delf.pdf
December 2019 22
Bharti Airtel
October 2019 39