Advertisement
fsoc131y

ssshh3

Apr 14th, 2023 (edited)
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.05 KB | None | 0 0
  1. 1. SLL INSERTION:
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // Define a structure for a singly linked list node
  6. struct node {
  7.     int data;
  8.     struct node* link;
  9. };
  10.  
  11. int main(){
  12.     int choice, value, position, i, n;
  13.     struct node *head = NULL, *current, *new_node;
  14.    
  15.     // Get the number of elements and create the linked list
  16.     printf("Enter the number of elements: ");
  17.     scanf("%d", &n);
  18.    
  19.     // Create the first node of the linked list
  20.     head = (struct node *)malloc(sizeof(struct node));
  21.     printf("Enter element 1: ");
  22.     scanf("%d", &head->data);
  23.     head->link = NULL;
  24.    
  25.     // Create the remaining nodes of the linked list
  26.     current = head;
  27.     for(i = 2; i <= n; i++){
  28.         new_node = (struct node *)malloc(sizeof(struct node));
  29.         printf("Enter element %d: ", i);
  30.         scanf("%d", &new_node->data);
  31.         new_node->link = NULL;
  32.        
  33.         current->link = new_node;
  34.         current = new_node;
  35.     }
  36.    
  37.     // Print the linked list
  38.     printf("\nThe linked list is: ");
  39.     current = head;
  40.     while(current != NULL){
  41.         printf("%d ", current->data);
  42.         current = current->link;
  43.     }
  44.    
  45.     // Menu-driven code to insert new elements
  46.     while(1){
  47.         printf("\n\n1. Insert at the beginning");
  48.         printf("\n2. Insert at the end");
  49.         printf("\n3. Insert at a position");
  50.         printf("\n4. Exit");
  51.         printf("\nEnter your choice: ");
  52.         scanf("%d", &choice);
  53.        
  54.         switch(choice){
  55.             case 1:
  56.                 // Insert at the beginning
  57.                 printf("\nEnter the element to be inserted: ");
  58.                 scanf("%d", &value);
  59.                
  60.                 new_node = (struct node *)malloc(sizeof(struct node));
  61.                 new_node->data = value;
  62.                 new_node->link = head;
  63.                 head = new_node;
  64.                
  65.                 printf("\nThe linked list after insertion is: ");
  66.                 current = head;
  67.                 while(current != NULL){
  68.                     printf("%d ", current->data);
  69.                     current = current->link;
  70.                 }
  71.                 break;
  72.                
  73.             case 2:
  74.                 // Insert at the end
  75.                 printf("\nEnter the element to be inserted: ");
  76.                 scanf("%d", &value);
  77.                
  78.                 new_node = (struct node *)malloc(sizeof(struct node));
  79.                 new_node->data = value;
  80.                 new_node->link = NULL;
  81.                
  82.                 current = head;
  83.                 while(current->link != NULL){
  84.                     current = current->link;
  85.                 }
  86.                 current->link = new_node;
  87.                
  88.                 printf("\nThe linked list after insertion is: ");
  89.                 current = head;
  90.                 while(current != NULL){
  91.                     printf("%d ", current->data);
  92.                     current = current->link;
  93.                 }
  94.                 break;
  95.                
  96.             case 3:
  97.                 // Insert at a position
  98.                 printf("\nEnter the element to be inserted: ");
  99.                 scanf("%d", &value);
  100.                
  101.                 printf("Enter the position where the element should be inserted: ");
  102.                 scanf("%d", &position);
  103.                
  104.                 new_node = (struct node *)malloc(sizeof(struct node));
  105.                 new_node->data = value;
  106.                
  107.                 current = head;
  108.                 for(i = 1; i < position-1 && current != NULL; i++){
  109.                     current = current->link;
  110.                 }
  111.                
  112.                 if(current == NULL){
  113.                     printf("\nInvalid position!\n");
  114.                     break;
  115.             }
  116.            
  117.             new_node->link = current->link;
  118.             current->link = new_node;
  119.            
  120.             printf("\nThe linked list after insertion is: ");
  121.             current = head;
  122.             while(current != NULL){
  123.                 printf("%d ", current->data);
  124.                 current = current->link;
  125.             }
  126.             break;
  127.            
  128.         case 4:
  129.             // Exit
  130.             printf("\nExiting...\n");
  131.             exit(0);
  132.            
  133.         default:
  134.             printf("\nInvalid choice!\n");
  135.     }
  136. }
  137.  
  138. return 0;
  139. }
  140.  
  141. ----------------------------------------------------------------------------------------------------------------------
  142. 2. SLL DELETION:
  143. #include <stdio.h>
  144. #include <stdlib.h>
  145.  
  146. // Define a structure for a singly linked list node
  147. struct node {
  148.     int data;
  149.     struct node* link;
  150. };
  151.  
  152. int main() {
  153.     int k, i, n;
  154.     printf("Enter number of elements: ");
  155.     scanf("%d", &n);
  156.  
  157.     // Create the linked list
  158.     struct node* head = NULL;
  159.     struct node* current = NULL;
  160.  
  161.     for (i = 0; i < n; i++) {
  162.         printf("Enter element %d :", i+1);
  163.         scanf("%d", &k);
  164.         struct node* newNode = (struct node*)malloc(sizeof(struct node));
  165.         newNode->data = k;
  166.         newNode->link = NULL;
  167.  
  168.         if (head == NULL) {
  169.             head = newNode;
  170.             current = newNode;
  171.         } else {
  172.             current->link = newNode;
  173.             current = newNode;
  174.         }
  175.     }
  176.  
  177.     current = head;
  178.     printf("Linked list is: ");
  179.     while (current != NULL) {
  180.         printf("%d ", current->data);
  181.         current = current->link;
  182.     }
  183.     printf("\n");
  184.  
  185.     // Delete elements at the start, end, and a position
  186.     int choice, position;
  187.     printf("Choose an operation to perform:\n");
  188.     printf("1. Delete element at the start\n");
  189.     printf("2. Delete element at the end\n");
  190.     printf("3. Delete element at a position\n");
  191.     scanf("%d", &choice);
  192.  
  193.     switch (choice) {
  194.         case 1:
  195.             // Delete element at the start
  196.             current = head;
  197.             head = current->link;
  198.             free(current);
  199.             break;
  200.         case 2:
  201.             // Delete element at the end
  202.             current = head;
  203.             while (current->link->link != NULL) {
  204.                 current = current->link;
  205.             }
  206.             free(current->link);
  207.             current->link = NULL;
  208.             break;
  209.         case 3:
  210.             // Delete element at a position
  211.             printf("Enter position to delete: ");
  212.             scanf("%d", &position);
  213.  
  214.             current = head;
  215.             struct node* temp;
  216.             for (i = 1; i < position - 1; i++) {
  217.                 current = current->link;
  218.             }
  219.             temp = current->link;
  220.             current->link = temp->link;
  221.             free(temp);
  222.             break;
  223.         default:
  224.             printf("Invalid choice\n");
  225.             break;
  226.     }
  227.  
  228.     // Print the updated linked list
  229.     current = head;
  230.     printf("Updated linked list is: ");
  231.     while (current != NULL) {
  232.         printf("%d ", current->data);
  233.         current = current->link;
  234.     }
  235.     printf("\n");
  236.  
  237.     return 0;
  238. }
  239. ------------------------------------------------------------------------------------------------------------
  240.    
  241. 3. DOCTOR CIA:
  242. #include <stdio.h>
  243. #include <stdlib.h>
  244. #include <string.h>
  245. // Define the structure for the Doctor node
  246. struct Doctor {
  247.     int id;
  248.     char name[50];
  249.     float consultingFee;
  250.     struct Doctor *next;
  251. };
  252. // Function to create a new Doctor node
  253. struct Doctor *createDoctor(int id, char name[], float consultingFee) {
  254.     struct Doctor *newDoctor = (struct Doctor *) malloc(sizeof(struct Doctor));
  255.     newDoctor->id = id;
  256.     strcpy(newDoctor->name, name);
  257.     newDoctor->consultingFee = consultingFee;
  258.     newDoctor->next = NULL;
  259.     return newDoctor;
  260. }
  261. // Function to display all the Doctor nodes in a neat format
  262. void displayDoctors(struct Doctor *head) {
  263.     printf("\nDoctor ID\tDoctor Name\tConsulting Fee\n");
  264.     printf("--------------------------------------------------\n");
  265.     struct Doctor *temp = head;
  266.     while (temp != NULL) {
  267.         printf("%d\t\t%s\t\t%.2f\n", temp->id, temp->name, temp->consultingFee);
  268.         temp = temp->next;
  269.     }
  270. }
  271. // Function to insert a new Doctor node at a given position
  272. void insertDoctor(struct Doctor **head, int position, int id, char name[], float consultingFee) {
  273.     struct Doctor *newDoctor = createDoctor(id, name, consultingFee);
  274.     if (position == 1) {
  275.         newDoctor->next = *head;
  276.         *head = newDoctor;
  277.     } else {
  278.         struct Doctor *temp = *head;
  279.         for (int i = 1; i < position - 1; i++) {
  280.             temp = temp->next;
  281.             if (temp == NULL) {
  282.                 printf("\nInvalid Position!\n");
  283.                 return;
  284.             }
  285.         }
  286.         newDoctor->next = temp->next;
  287.         temp->next = newDoctor;
  288.     }
  289.     printf("\nNew Doctor node inserted successfully!\n");
  290. }
  291. // Function to display all the Doctor nodes with consulting fee above a certain amount
  292. void displayDoctorsAboveFee(struct Doctor *head, float fee) {
  293.     printf("\nDoctor ID\tDoctor Name\tConsulting Fee\n");
  294.     printf("--------------------------------------------------\n");
  295.     struct Doctor *temp = head;
  296.     while (temp != NULL) {
  297.         if (temp->consultingFee >= fee) {
  298.             printf("%d\t\t%s\t\t%.2f\n", temp->id, temp->name, temp->consultingFee);
  299.         }
  300.         temp = temp->next;
  301.     }
  302. }
  303. int main() {
  304.     // Create the initial 4 Doctor nodes
  305.     struct Doctor *head = createDoctor(101, "Dr. John", 500);
  306.     struct Doctor *doctor2 = createDoctor(102, "Dr. Smith", 600);
  307.     struct Doctor *doctor3 = createDoctor(103, "Dr. Jane", 400);
  308.     struct Doctor *doctor4 = createDoctor(104, "Dr. Alex", 300);
  309.     head->next = doctor2;
  310.     doctor2->next = doctor3;
  311.     doctor3->next = doctor4;
  312.     // Display all the Doctor nodes
  313.     displayDoctors(head);
  314.     // Insert a new Doctor node at a given position
  315.     insertDoctor(&head, 3, 105, "Dr. Sarah", 1200);
  316.     // Display all the Doctor nodes with consulting fee above a certain amount
  317.     displayDoctorsAboveFee(head, 750);
  318.     return 0;
  319. }
  320.  
  321. -------------------------------------------------------------------------------------------------------
  322.    
  323. 4. STRINGS & PATTERN MATCHING:
  324. #include <stdio.h>
  325. #include <string.h>
  326.  
  327. void compare_strings(char str1[], char str2[]) {
  328.     int result = strcmp(str1, str2);
  329.     if (result == 0) {
  330.         printf("The two strings are equal.\n");
  331.     } else if (result < 0) {
  332.         printf("The first string is smaller than the second string.\n");
  333.     } else {
  334.         printf("The first string is larger than the second string.\n");
  335.     }
  336. }
  337.  
  338. void concatenate_strings(char dest[], char src[]) {
  339.     strcat(dest, src);
  340.     printf("The concatenated string is: %s\n", dest);
  341. }
  342.  
  343. void copy_string(char dest[], char src[]) {
  344.     strcpy(dest, src);
  345.     printf("The copied string is: %s\n", dest);
  346. }
  347.  
  348. void find_pattern(char str[], char pattern[]) {
  349.     char *result = strstr(str, pattern);
  350.     if (result == NULL) {
  351.         printf("The pattern was not found in the string.\n");
  352.     } else {
  353.         int index = result - str;
  354.         printf("The pattern was found at index %d in the string.\n", index);
  355.     }
  356. }
  357.  
  358. void extract_substring(char str[], int start, int length) {
  359.     char result[100];
  360.     strncpy(result, &str[start], length);
  361.     result[length] = '\0';
  362.     printf("The extracted sub-string is: %s\n", result);
  363. }
  364.  
  365. int get_string_length(char str[]) {
  366.     int length = strlen(str);
  367.     printf("The length of the string is: %d\n", length);
  368. }
  369.  
  370. int main() {
  371.     int choice;
  372.     char str1[1000], str2[1000], pattern[1000], result[3000];
  373.     int start, length;
  374.     printf("Enter a string: ");
  375.     scanf("%[^\n]s", str1);
  376.     while (1) {
  377.         printf("\nMenu\n");
  378.         printf("1. String Length\n");
  379.         printf("2. String Concatenation\n");
  380.         printf("3. Pattern Matching\n");
  381.         printf("4. Compare strings\n");
  382.         printf("5. Copy string\n");
  383.         printf("6. Extract sub-string\n");
  384.         printf("7. Exit\n");
  385.         printf("\nEnter your choice: ");
  386.         scanf("%d", &choice);
  387.         switch (choice) {
  388.             case 1:
  389.                 get_string_length(str1);
  390.                 break;
  391.             case 2:
  392.                 printf("\nEnter another string: ");
  393.                 scanf("%s", str2);
  394.                 concatenate_strings(str1, str2);
  395.                 break;
  396.             case 3:
  397.                 printf("\nEnter a pattern to match: ");
  398.                 scanf("%s", pattern);
  399.                 find_pattern(str1, pattern);
  400.                 break;
  401.             case 4:
  402.                 printf("\nEnter the first string: ");
  403.                 scanf("%s", str1);
  404.                 printf("Enter the second string: ");
  405.                 scanf("%s", str2);
  406.                 compare_strings(str1, str2);
  407.                 break;
  408.             case 5:
  409.                 printf("\nEnter the source string: ");
  410.                 scanf("%s", str2);
  411.                 copy_string(str1, str2);
  412.                 break;
  413.             case 6:
  414.                 printf("\nEnter the start index of the sub-string: ");
  415.                 scanf("%d", &start);
  416.                 printf("Enter the length of the sub-string: ");
  417.                 scanf("%d", &length);
  418.                 extract_substring(str1, start, length);
  419.                 break;
  420.             case 7:
  421.                 return 0;
  422.             default:
  423.                 printf("\nInvalid Choice!\n");
  424.                 break;
  425.         }
  426.     }
  427.     return 0;
  428. }
  429.  
  430. -----------------------------------------------------------------------------------------------------------------------
  431.  
  432. 5. DOCTOR EASY:
  433. #include <stdio.h>
  434. #include <stdlib.h>
  435.  
  436. struct node{
  437.     int docid;
  438.     char docname[20];
  439.     int fee;
  440.     struct node *link;
  441. };
  442.  
  443. int main(){
  444.     int size;
  445.     printf("Enter size of list: ");
  446.     scanf("%d", &size);
  447.     struct node * head = (struct node *)malloc(sizeof(struct node));
  448.     printf("\nEnter Doc Id: ");
  449.     scanf("%d", &(head->docid));
  450.     printf("Enter doc name: ");
  451.     scanf("%s", head->docname);
  452.     printf("Enter doc fee: ");
  453.     scanf("%d", &(head->fee));
  454.     head->link= NULL;
  455.     struct node * pointer;
  456.     pointer = head;
  457.     for(int i= 1; i<size; i++){
  458.         struct node * newnode=(struct node *)malloc(sizeof(struct node));
  459.         printf("\nEnter Doc Id: ");
  460.         scanf("%d", &(newnode->docid));
  461.         printf("Enter doc name: ");
  462.         scanf("%s", newnode->docname);
  463.         printf("Enter doc fee: ");
  464.         scanf("%d", &(newnode->fee));
  465.         newnode->link= NULL;
  466.         pointer->link = newnode;
  467.         pointer = pointer->link;        
  468.     }
  469.     pointer= head;
  470.     printf("List is:\n");
  471.     while(pointer!= NULL){
  472.         printf("ID: %d\tName: %s\tFees: %d\n", pointer->docid, pointer->docname, pointer->fee);
  473.         pointer = pointer->link;
  474.     }
  475.    
  476.     int choice;
  477.     printf("1. for insertion\n2. for deletion\n");
  478.     scanf("%d", &choice);
  479.     switch(choice){
  480.         case 1:
  481.             struct node *temp = (struct node *) malloc(sizeof(struct node));
  482.             printf("Enter pos. to insert data: ");
  483.             int pos;
  484.             scanf("%d", &pos);
  485.             printf("Enter doc id: ");
  486.             scanf("%d", &(temp -> docid));
  487.             printf("Enter doc name: ");
  488.             scanf("%s", &(temp -> docname));
  489.             printf("Enter fee: ");
  490.             scanf("%d", &(temp ->fee));
  491.        
  492.             if(pos ==1){
  493.             temp ->link= head;
  494.             head = temp;}
  495.            
  496.             else{
  497.                 pointer = head;
  498.                 for(int i=1; i<pos-1 && pointer != NULL; i++){
  499.                 pointer = pointer->link;               
  500.                 }
  501.                 if(pointer== NULL){
  502.                     printf("Invalid position");
  503.                 }  
  504.                 else{
  505.                 temp->link= pointer->link;
  506.                 pointer->link= temp;
  507.                 break; 
  508.                 }      
  509.                 }
  510.                    
  511.     }
  512.     pointer= head;
  513.     printf("List after insertion is:\n");
  514.     while(pointer!= NULL){
  515.         printf("ID: %d\tName: %s\tFees: %d\n", pointer->docid, pointer->docname, pointer->fee);
  516.         pointer = pointer->link;
  517.     }
  518.    
  519.     }
  520.  
  521.  
  522. -----------------------------------------------------------------------------------------------------------
  523.  
  524. LAB (CREATION / DELETION / INSERTION )
  525.  
  526. #include <stdio.h>
  527. #include <stdlib.h>
  528.  
  529. // Define a structure for a singly linked list node
  530. struct node {
  531.     int data;
  532.     struct node* link;
  533. };
  534.  
  535. int main() {
  536.     int k, i, n,value;
  537.     printf("Enter number of elements: ");
  538.     scanf("%d", &n);
  539.  
  540.     // Create the linked list
  541.     struct node *head = NULL, *current, *new_node;
  542.  
  543.     for (i = 0; i < n; i++) {
  544.         printf("Enter element %d :", i+1);
  545.         scanf("%d", &k);
  546.         struct node* newNode = (struct node*)malloc(sizeof(struct node));
  547.         newNode->data = k;
  548.         newNode->link = NULL;
  549.  
  550.         if (head == NULL) {
  551.             head = newNode;
  552.             current = newNode;
  553.         } else {
  554.             current->link = newNode;
  555.             current = newNode;
  556.         }
  557.     }
  558.  
  559.     current = head;
  560.     printf("Linked list is: ");
  561.     while (current != NULL) {
  562.         printf("%d ", current->data);
  563.         current = current->link;
  564.     }
  565.     printf("\n");
  566.  
  567.     // Delete elements at the start, end, and a position
  568.     int choice, position;
  569.     printf("Choose an operation to perform:\n");
  570.     printf("1. Delete element at the start\n");
  571.     printf("2. Delete element at the end\n");
  572.     printf("3. Delete element at a position\n");
  573.     printf("\n\n4. Insert at the beginning");
  574.     printf("\n5. Insert at the end");
  575.     printf("\n6. Insert at a position");
  576.     printf("\n7. Exit");
  577.  
  578.     scanf("%d", &choice);
  579.  
  580.     switch (choice) {
  581.         case 1:
  582.             // Delete element at the start
  583.             current = head;
  584.             head = current->link;
  585.             free(current);
  586.             break;
  587.         case 2:
  588.             // Delete element at the end
  589.             current = head;
  590.             while (current->link->link != NULL) {
  591.                 current = current->link;
  592.             }
  593.             free(current->link);
  594.             current->link = NULL;
  595.             break;
  596.         case 3:
  597.             // Delete element at a position
  598.             printf("Enter position to delete: ");
  599.             scanf("%d", &position);
  600.  
  601.             current = head;
  602.             struct node* temp;
  603.             for (i = 1; i < position - 1; i++) {
  604.                 current = current->link;
  605.             }
  606.             temp = current->link;
  607.             current->link = temp->link;
  608.             free(temp);
  609.             break;
  610.         case 4:
  611.                 // Insert at the beginning
  612.                 printf("\nEnter the element to be inserted: ");
  613.                 scanf("%d", &value);
  614.  
  615.                 new_node = (struct node *)malloc(sizeof(struct node));
  616.                 new_node->data = value;
  617.                 new_node->link = head;
  618.                 head = new_node;
  619.  
  620.                 printf("\nThe linked list after insertion is: ");
  621.                 current = head;
  622.                 while(current != NULL){
  623.                     printf("%d ", current->data);
  624.                     current = current->link;
  625.                 }
  626.                 break;
  627.  
  628.         case 5:
  629.                 // Insert at the end
  630.                 printf("\nEnter the element to be inserted: ");
  631.                 scanf("%d", &value);
  632.  
  633.                 new_node = (struct node *)malloc(sizeof(struct node));
  634.                 new_node->data = value;
  635.                 new_node->link = NULL;
  636.  
  637.                 current = head;
  638.                 while(current->link != NULL){
  639.                     current = current->link;
  640.                 }
  641.                 current->link = new_node;
  642.  
  643.                 printf("\nThe linked list after insertion is: ");
  644.                 current = head;
  645.                 while(current != NULL){
  646.                     printf("%d ", current->data);
  647.                     current = current->link;
  648.                 }
  649.                 break;
  650.  
  651.         case 6:
  652.                 // Insert at a position
  653.                 printf("\nEnter the element to be inserted: ");
  654.                 scanf("%d", &value);
  655.  
  656.                 printf("Enter the position where the element should be inserted: ");
  657.                 scanf("%d", &position);
  658.  
  659.                 new_node = (struct node *)malloc(sizeof(struct node));
  660.                 new_node->data = value;
  661.  
  662.                 current = head;
  663.                 for(i = 1; i < position-1 && current != NULL; i++){
  664.                     current = current->link;
  665.                 }
  666.  
  667.                 if(current == NULL){
  668.                     printf("\nInvalid position!\n");
  669.                     break;
  670.             }
  671.  
  672.             new_node->link = current->link;
  673.             current->link = new_node;
  674.  
  675.             printf("\nThe linked list after insertion is: ");
  676.             current = head;
  677.             while(current != NULL){
  678.                 printf("%d ", current->data);
  679.                 current = current->link;
  680.             }
  681.             break;
  682.         default:
  683.             printf("Invalid choice\n");
  684.             break;
  685.     }
  686.  
  687.     // Print the updated linked list
  688.     current = head;
  689.     printf("Updated linked list is: ");
  690.     while (current != NULL) {
  691.         printf("%d ", current->data);
  692.         current = current->link;
  693.     }
  694.     printf("\n");
  695.  
  696.     return 0;
  697. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement