Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. SLL INSERTION:
- #include <stdio.h>
- #include <stdlib.h>
- // Define a structure for a singly linked list node
- struct node {
- int data;
- struct node* link;
- };
- int main(){
- int choice, value, position, i, n;
- struct node *head = NULL, *current, *new_node;
- // Get the number of elements and create the linked list
- printf("Enter the number of elements: ");
- scanf("%d", &n);
- // Create the first node of the linked list
- head = (struct node *)malloc(sizeof(struct node));
- printf("Enter element 1: ");
- scanf("%d", &head->data);
- head->link = NULL;
- // Create the remaining nodes of the linked list
- current = head;
- for(i = 2; i <= n; i++){
- new_node = (struct node *)malloc(sizeof(struct node));
- printf("Enter element %d: ", i);
- scanf("%d", &new_node->data);
- new_node->link = NULL;
- current->link = new_node;
- current = new_node;
- }
- // Print the linked list
- printf("\nThe linked list is: ");
- current = head;
- while(current != NULL){
- printf("%d ", current->data);
- current = current->link;
- }
- // Menu-driven code to insert new elements
- while(1){
- printf("\n\n1. Insert at the beginning");
- printf("\n2. Insert at the end");
- printf("\n3. Insert at a position");
- printf("\n4. Exit");
- printf("\nEnter your choice: ");
- scanf("%d", &choice);
- switch(choice){
- case 1:
- // Insert at the beginning
- printf("\nEnter the element to be inserted: ");
- scanf("%d", &value);
- new_node = (struct node *)malloc(sizeof(struct node));
- new_node->data = value;
- new_node->link = head;
- head = new_node;
- printf("\nThe linked list after insertion is: ");
- current = head;
- while(current != NULL){
- printf("%d ", current->data);
- current = current->link;
- }
- break;
- case 2:
- // Insert at the end
- printf("\nEnter the element to be inserted: ");
- scanf("%d", &value);
- new_node = (struct node *)malloc(sizeof(struct node));
- new_node->data = value;
- new_node->link = NULL;
- current = head;
- while(current->link != NULL){
- current = current->link;
- }
- current->link = new_node;
- printf("\nThe linked list after insertion is: ");
- current = head;
- while(current != NULL){
- printf("%d ", current->data);
- current = current->link;
- }
- break;
- case 3:
- // Insert at a position
- printf("\nEnter the element to be inserted: ");
- scanf("%d", &value);
- printf("Enter the position where the element should be inserted: ");
- scanf("%d", &position);
- new_node = (struct node *)malloc(sizeof(struct node));
- new_node->data = value;
- current = head;
- for(i = 1; i < position-1 && current != NULL; i++){
- current = current->link;
- }
- if(current == NULL){
- printf("\nInvalid position!\n");
- break;
- }
- new_node->link = current->link;
- current->link = new_node;
- printf("\nThe linked list after insertion is: ");
- current = head;
- while(current != NULL){
- printf("%d ", current->data);
- current = current->link;
- }
- break;
- case 4:
- // Exit
- printf("\nExiting...\n");
- exit(0);
- default:
- printf("\nInvalid choice!\n");
- }
- }
- return 0;
- }
- ----------------------------------------------------------------------------------------------------------------------
- 2. SLL DELETION:
- #include <stdio.h>
- #include <stdlib.h>
- // Define a structure for a singly linked list node
- struct node {
- int data;
- struct node* link;
- };
- int main() {
- int k, i, n;
- printf("Enter number of elements: ");
- scanf("%d", &n);
- // Create the linked list
- struct node* head = NULL;
- struct node* current = NULL;
- for (i = 0; i < n; i++) {
- printf("Enter element %d :", i+1);
- scanf("%d", &k);
- struct node* newNode = (struct node*)malloc(sizeof(struct node));
- newNode->data = k;
- newNode->link = NULL;
- if (head == NULL) {
- head = newNode;
- current = newNode;
- } else {
- current->link = newNode;
- current = newNode;
- }
- }
- current = head;
- printf("Linked list is: ");
- while (current != NULL) {
- printf("%d ", current->data);
- current = current->link;
- }
- printf("\n");
- // Delete elements at the start, end, and a position
- int choice, position;
- printf("Choose an operation to perform:\n");
- printf("1. Delete element at the start\n");
- printf("2. Delete element at the end\n");
- printf("3. Delete element at a position\n");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- // Delete element at the start
- current = head;
- head = current->link;
- free(current);
- break;
- case 2:
- // Delete element at the end
- current = head;
- while (current->link->link != NULL) {
- current = current->link;
- }
- free(current->link);
- current->link = NULL;
- break;
- case 3:
- // Delete element at a position
- printf("Enter position to delete: ");
- scanf("%d", &position);
- current = head;
- struct node* temp;
- for (i = 1; i < position - 1; i++) {
- current = current->link;
- }
- temp = current->link;
- current->link = temp->link;
- free(temp);
- break;
- default:
- printf("Invalid choice\n");
- break;
- }
- // Print the updated linked list
- current = head;
- printf("Updated linked list is: ");
- while (current != NULL) {
- printf("%d ", current->data);
- current = current->link;
- }
- printf("\n");
- return 0;
- }
- ------------------------------------------------------------------------------------------------------------
- 3. DOCTOR CIA:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // Define the structure for the Doctor node
- struct Doctor {
- int id;
- char name[50];
- float consultingFee;
- struct Doctor *next;
- };
- // Function to create a new Doctor node
- struct Doctor *createDoctor(int id, char name[], float consultingFee) {
- struct Doctor *newDoctor = (struct Doctor *) malloc(sizeof(struct Doctor));
- newDoctor->id = id;
- strcpy(newDoctor->name, name);
- newDoctor->consultingFee = consultingFee;
- newDoctor->next = NULL;
- return newDoctor;
- }
- // Function to display all the Doctor nodes in a neat format
- void displayDoctors(struct Doctor *head) {
- printf("\nDoctor ID\tDoctor Name\tConsulting Fee\n");
- printf("--------------------------------------------------\n");
- struct Doctor *temp = head;
- while (temp != NULL) {
- printf("%d\t\t%s\t\t%.2f\n", temp->id, temp->name, temp->consultingFee);
- temp = temp->next;
- }
- }
- // Function to insert a new Doctor node at a given position
- void insertDoctor(struct Doctor **head, int position, int id, char name[], float consultingFee) {
- struct Doctor *newDoctor = createDoctor(id, name, consultingFee);
- if (position == 1) {
- newDoctor->next = *head;
- *head = newDoctor;
- } else {
- struct Doctor *temp = *head;
- for (int i = 1; i < position - 1; i++) {
- temp = temp->next;
- if (temp == NULL) {
- printf("\nInvalid Position!\n");
- return;
- }
- }
- newDoctor->next = temp->next;
- temp->next = newDoctor;
- }
- printf("\nNew Doctor node inserted successfully!\n");
- }
- // Function to display all the Doctor nodes with consulting fee above a certain amount
- void displayDoctorsAboveFee(struct Doctor *head, float fee) {
- printf("\nDoctor ID\tDoctor Name\tConsulting Fee\n");
- printf("--------------------------------------------------\n");
- struct Doctor *temp = head;
- while (temp != NULL) {
- if (temp->consultingFee >= fee) {
- printf("%d\t\t%s\t\t%.2f\n", temp->id, temp->name, temp->consultingFee);
- }
- temp = temp->next;
- }
- }
- int main() {
- // Create the initial 4 Doctor nodes
- struct Doctor *head = createDoctor(101, "Dr. John", 500);
- struct Doctor *doctor2 = createDoctor(102, "Dr. Smith", 600);
- struct Doctor *doctor3 = createDoctor(103, "Dr. Jane", 400);
- struct Doctor *doctor4 = createDoctor(104, "Dr. Alex", 300);
- head->next = doctor2;
- doctor2->next = doctor3;
- doctor3->next = doctor4;
- // Display all the Doctor nodes
- displayDoctors(head);
- // Insert a new Doctor node at a given position
- insertDoctor(&head, 3, 105, "Dr. Sarah", 1200);
- // Display all the Doctor nodes with consulting fee above a certain amount
- displayDoctorsAboveFee(head, 750);
- return 0;
- }
- -------------------------------------------------------------------------------------------------------
- 4. STRINGS & PATTERN MATCHING:
- #include <stdio.h>
- #include <string.h>
- void compare_strings(char str1[], char str2[]) {
- int result = strcmp(str1, str2);
- if (result == 0) {
- printf("The two strings are equal.\n");
- } else if (result < 0) {
- printf("The first string is smaller than the second string.\n");
- } else {
- printf("The first string is larger than the second string.\n");
- }
- }
- void concatenate_strings(char dest[], char src[]) {
- strcat(dest, src);
- printf("The concatenated string is: %s\n", dest);
- }
- void copy_string(char dest[], char src[]) {
- strcpy(dest, src);
- printf("The copied string is: %s\n", dest);
- }
- void find_pattern(char str[], char pattern[]) {
- char *result = strstr(str, pattern);
- if (result == NULL) {
- printf("The pattern was not found in the string.\n");
- } else {
- int index = result - str;
- printf("The pattern was found at index %d in the string.\n", index);
- }
- }
- void extract_substring(char str[], int start, int length) {
- char result[100];
- strncpy(result, &str[start], length);
- result[length] = '\0';
- printf("The extracted sub-string is: %s\n", result);
- }
- int get_string_length(char str[]) {
- int length = strlen(str);
- printf("The length of the string is: %d\n", length);
- }
- int main() {
- int choice;
- char str1[1000], str2[1000], pattern[1000], result[3000];
- int start, length;
- printf("Enter a string: ");
- scanf("%[^\n]s", str1);
- while (1) {
- printf("\nMenu\n");
- printf("1. String Length\n");
- printf("2. String Concatenation\n");
- printf("3. Pattern Matching\n");
- printf("4. Compare strings\n");
- printf("5. Copy string\n");
- printf("6. Extract sub-string\n");
- printf("7. Exit\n");
- printf("\nEnter your choice: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- get_string_length(str1);
- break;
- case 2:
- printf("\nEnter another string: ");
- scanf("%s", str2);
- concatenate_strings(str1, str2);
- break;
- case 3:
- printf("\nEnter a pattern to match: ");
- scanf("%s", pattern);
- find_pattern(str1, pattern);
- break;
- case 4:
- printf("\nEnter the first string: ");
- scanf("%s", str1);
- printf("Enter the second string: ");
- scanf("%s", str2);
- compare_strings(str1, str2);
- break;
- case 5:
- printf("\nEnter the source string: ");
- scanf("%s", str2);
- copy_string(str1, str2);
- break;
- case 6:
- printf("\nEnter the start index of the sub-string: ");
- scanf("%d", &start);
- printf("Enter the length of the sub-string: ");
- scanf("%d", &length);
- extract_substring(str1, start, length);
- break;
- case 7:
- return 0;
- default:
- printf("\nInvalid Choice!\n");
- break;
- }
- }
- return 0;
- }
- -----------------------------------------------------------------------------------------------------------------------
- 5. DOCTOR EASY:
- #include <stdio.h>
- #include <stdlib.h>
- struct node{
- int docid;
- char docname[20];
- int fee;
- struct node *link;
- };
- int main(){
- int size;
- printf("Enter size of list: ");
- scanf("%d", &size);
- struct node * head = (struct node *)malloc(sizeof(struct node));
- printf("\nEnter Doc Id: ");
- scanf("%d", &(head->docid));
- printf("Enter doc name: ");
- scanf("%s", head->docname);
- printf("Enter doc fee: ");
- scanf("%d", &(head->fee));
- head->link= NULL;
- struct node * pointer;
- pointer = head;
- for(int i= 1; i<size; i++){
- struct node * newnode=(struct node *)malloc(sizeof(struct node));
- printf("\nEnter Doc Id: ");
- scanf("%d", &(newnode->docid));
- printf("Enter doc name: ");
- scanf("%s", newnode->docname);
- printf("Enter doc fee: ");
- scanf("%d", &(newnode->fee));
- newnode->link= NULL;
- pointer->link = newnode;
- pointer = pointer->link;
- }
- pointer= head;
- printf("List is:\n");
- while(pointer!= NULL){
- printf("ID: %d\tName: %s\tFees: %d\n", pointer->docid, pointer->docname, pointer->fee);
- pointer = pointer->link;
- }
- int choice;
- printf("1. for insertion\n2. for deletion\n");
- scanf("%d", &choice);
- switch(choice){
- case 1:
- struct node *temp = (struct node *) malloc(sizeof(struct node));
- printf("Enter pos. to insert data: ");
- int pos;
- scanf("%d", &pos);
- printf("Enter doc id: ");
- scanf("%d", &(temp -> docid));
- printf("Enter doc name: ");
- scanf("%s", &(temp -> docname));
- printf("Enter fee: ");
- scanf("%d", &(temp ->fee));
- if(pos ==1){
- temp ->link= head;
- head = temp;}
- else{
- pointer = head;
- for(int i=1; i<pos-1 && pointer != NULL; i++){
- pointer = pointer->link;
- }
- if(pointer== NULL){
- printf("Invalid position");
- }
- else{
- temp->link= pointer->link;
- pointer->link= temp;
- break;
- }
- }
- }
- pointer= head;
- printf("List after insertion is:\n");
- while(pointer!= NULL){
- printf("ID: %d\tName: %s\tFees: %d\n", pointer->docid, pointer->docname, pointer->fee);
- pointer = pointer->link;
- }
- }
- -----------------------------------------------------------------------------------------------------------
- LAB (CREATION / DELETION / INSERTION )
- #include <stdio.h>
- #include <stdlib.h>
- // Define a structure for a singly linked list node
- struct node {
- int data;
- struct node* link;
- };
- int main() {
- int k, i, n,value;
- printf("Enter number of elements: ");
- scanf("%d", &n);
- // Create the linked list
- struct node *head = NULL, *current, *new_node;
- for (i = 0; i < n; i++) {
- printf("Enter element %d :", i+1);
- scanf("%d", &k);
- struct node* newNode = (struct node*)malloc(sizeof(struct node));
- newNode->data = k;
- newNode->link = NULL;
- if (head == NULL) {
- head = newNode;
- current = newNode;
- } else {
- current->link = newNode;
- current = newNode;
- }
- }
- current = head;
- printf("Linked list is: ");
- while (current != NULL) {
- printf("%d ", current->data);
- current = current->link;
- }
- printf("\n");
- // Delete elements at the start, end, and a position
- int choice, position;
- printf("Choose an operation to perform:\n");
- printf("1. Delete element at the start\n");
- printf("2. Delete element at the end\n");
- printf("3. Delete element at a position\n");
- printf("\n\n4. Insert at the beginning");
- printf("\n5. Insert at the end");
- printf("\n6. Insert at a position");
- printf("\n7. Exit");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- // Delete element at the start
- current = head;
- head = current->link;
- free(current);
- break;
- case 2:
- // Delete element at the end
- current = head;
- while (current->link->link != NULL) {
- current = current->link;
- }
- free(current->link);
- current->link = NULL;
- break;
- case 3:
- // Delete element at a position
- printf("Enter position to delete: ");
- scanf("%d", &position);
- current = head;
- struct node* temp;
- for (i = 1; i < position - 1; i++) {
- current = current->link;
- }
- temp = current->link;
- current->link = temp->link;
- free(temp);
- break;
- case 4:
- // Insert at the beginning
- printf("\nEnter the element to be inserted: ");
- scanf("%d", &value);
- new_node = (struct node *)malloc(sizeof(struct node));
- new_node->data = value;
- new_node->link = head;
- head = new_node;
- printf("\nThe linked list after insertion is: ");
- current = head;
- while(current != NULL){
- printf("%d ", current->data);
- current = current->link;
- }
- break;
- case 5:
- // Insert at the end
- printf("\nEnter the element to be inserted: ");
- scanf("%d", &value);
- new_node = (struct node *)malloc(sizeof(struct node));
- new_node->data = value;
- new_node->link = NULL;
- current = head;
- while(current->link != NULL){
- current = current->link;
- }
- current->link = new_node;
- printf("\nThe linked list after insertion is: ");
- current = head;
- while(current != NULL){
- printf("%d ", current->data);
- current = current->link;
- }
- break;
- case 6:
- // Insert at a position
- printf("\nEnter the element to be inserted: ");
- scanf("%d", &value);
- printf("Enter the position where the element should be inserted: ");
- scanf("%d", &position);
- new_node = (struct node *)malloc(sizeof(struct node));
- new_node->data = value;
- current = head;
- for(i = 1; i < position-1 && current != NULL; i++){
- current = current->link;
- }
- if(current == NULL){
- printf("\nInvalid position!\n");
- break;
- }
- new_node->link = current->link;
- current->link = new_node;
- printf("\nThe linked list after insertion is: ");
- current = head;
- while(current != NULL){
- printf("%d ", current->data);
- current = current->link;
- }
- break;
- default:
- printf("Invalid choice\n");
- break;
- }
- // Print the updated linked list
- current = head;
- printf("Updated linked list is: ");
- while (current != NULL) {
- printf("%d ", current->data);
- current = current->link;
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement