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