The document contains detailed explanations and comments for five different C programs: a singly linked list for insertion and deletion, an ordered singly linked list, a stack implemented using a linked list, string operations including concatenation and substring extraction, and the creation and display of an adjacency matrix for a graph. Each program includes functions for creating nodes, inserting and deleting elements, and printing results. The comments clarify the purpose and functionality of each line of code.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views9 pages
Program Explaination
The document contains detailed explanations and comments for five different C programs: a singly linked list for insertion and deletion, an ordered singly linked list, a stack implemented using a linked list, string operations including concatenation and substring extraction, and the creation and display of an adjacency matrix for a graph. Each program includes functions for creating nodes, inserting and deleting elements, and printing results. The comments clarify the purpose and functionality of each line of code.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9
Okay, let's break down programs 4, 7, 10, 16, and 17 with detailed comments for each line to
explain its function.
Program 4: Singly Linked List Insertion and Deletion #include <stdio.h> #include <stdlib.h> // Define the structure for a node in the linked list struct Node { int data; // Data stored in the node struct Node* next; // Pointer to the next node in the list }; // Function to create a new node struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Allocate memory for a new node newNode->data = data; // Assign data to the new node newNode->next = NULL; // Initialize the next pointer to NULL return newNode; // Return the newly created node } // Function to insert a node at the end of the linked list void insertEnd(struct Node** head, int data) { struct Node* temp; struct Node* newNode = createNode(data); // Create a new node with the given data if (*head == NULL) { // If the list is empty *head = newNode; // Set the new node as the head return; // Exit the function } temp = *head; // Start from the head of the list while (temp->next != NULL) { // Traverse to the last node temp = temp->next; } temp->next = newNode; // Link the new node at the end } // Function to delete a node with a specific value void deleteNode(struct Node** head, int key) { struct Node* temp = *head; // Temporary pointer to traverse the list struct Node* prev = NULL; // Pointer to the previous node // If head node itself holds the key to be deleted if (temp != NULL && temp->data == key) { *head = temp->next; // Change head free(temp); // Free old head return; // Exit the function } // Search for the key to be deleted, keep track of the previous node while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // If key was not present in linked list if (temp == NULL) return; // Unlink the node from linked list prev->next = temp->next; free(temp); // Free memory } // Function to print the linked list void printList(struct Node* node) { while (node != NULL) { printf("%d -> ", node->data); // Print the data of the current node node = node->next; // Move to the next node } printf("NULL\n"); // Print NULL to indicate the end of the list } int main() { struct Node* head = NULL; // Initialize an empty linked list int elements[] = {61, 16, 8, 27}; int i; int keysToDelete[] = {8, 61, 27}; // Inserting elements into the linked list for (i = 0; i < 4; i++) { insertEnd(&head, elements[i]); printf("List after inserting %d: ", elements[i]); printList(head); } // Deleting elements from the linked list for (i = 0; i < 3; i++) { deleteNode(&head, keysToDelete[i]); printf("List after deleting %d: ", keysToDelete[i]); printList(head); } return 0; }
Program 7: Ordered Singly Linked List Insertion and Deletion
#include <stdio.h> #include <stdlib.h> // Define the structure for a node in the linked list struct Node { int data; struct Node* next; }; // Function to create a new node struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; } // Function to insert a node in an ordered manner void insertOrdered(struct Node** head, int data) { struct Node* newNode = createNode(data); struct Node* current; // Case for inserting at the beginning or as the first element if (*head == NULL || (*head)->data >= data) { newNode->next = *head; *head = newNode; return; } // Locate the node before the point of insertion current = *head; while (current->next != NULL && current->next->data < data) { current = current->next; } // Insert the new node newNode->next = current->next; current->next = newNode; } // Function to delete a node with a specific value void deleteNode(struct Node** head, int key) { struct Node* temp = *head; struct Node* prev = NULL; // If head node itself holds the key to be deleted if (temp != NULL && temp->data == key) { *head = temp->next; free(temp); return; } // Search for the key to be deleted, keep track of the previous node while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // If key was not present in linked list if (temp == NULL) return; // Unlink the node from linked list prev->next = temp->next; free(temp); } // Function to print the linked list void printList(struct Node* node) { if (node == NULL) { printf("List is empty\n"); return; } printf("List: "); while (node != NULL) { printf("%d -> ", node->data); node = node->next; } printf("NULL\n"); } int main() { struct Node* head = NULL; // Initialize an empty linked list int elements[] = {61, 16, 8, 27}; int keysToDelete[] = {8, 61, 27}; int i; // Inserting elements into the ordered linked list for (i = 0; i < 4; i++) { insertOrdered(&head, elements[i]); printf("After inserting %d: ", elements[i]); printList(head); } // Deleting elements from the linked list for (i = 0; i < 3; i++) { deleteNode(&head, keysToDelete[i]); printf("After deleting %d: ", keysToDelete[i]); printList(head); } return 0; }
Program 10: Stack using Linked List
#include <stdio.h> #include <stdlib.h> // Define the structure for a node in the stack struct Node { int data; struct Node* next; }; // Function to create a new node struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; } // Function to push an element onto the stack void push(struct Node** top, int data) { struct Node* newNode = createNode(data); newNode->next = *top; // Link the old top to the new node *top = newNode; // Update the top to point to the new node printf("Pushed %d onto the stack.\n", data); } // Function to pop an element from the stack int pop(struct Node** top) { struct Node* temp; int poppedValue; if (*top == NULL) { printf("Stack is empty! Cannot pop.\n"); return -1; // Return -1 if stack is empty } temp = *top; // Store the current top node poppedValue = temp->data; // Get the data from the top node *top = (*top)->next; // Move the top pointer to the next node free(temp); // Free memory of the popped node printf("Popped %d from the stack.\n", poppedValue); return poppedValue; // Return the popped value } // Function to display the elements in the stack void display(struct Node* top) { struct Node* temp; if (top == NULL) { printf("Stack is empty.\n"); return; } printf("Stack elements: "); temp = top; // Temporary pointer for traversal while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); } int main() { struct Node* stack = NULL; // Initialize an empty stack // Perform stack operations push(&stack, 10); push(&stack, 20); push(&stack, 30); display(stack); // Display current stack pop(&stack); // Pop an element from the stack display(stack); // Display current stack after pop pop(&stack); // Pop another element from the stack display(stack); pop(&stack); // Pop last element from the stack display(stack); pop(&stack); // Attempt to pop from an empty stack return 0; }
Program 16: String Operations
#include <stdio.h> #include <string.h> int main() { // Initialize the strings char S1[] = "Flowers"; char S2[] = "are beautiful"; char result[100]; // To store concatenated string char *substr; // To store extracted substring char *pos; // I. Find the length of S1 int length_S1 = strlen(S1); printf("Length of S1: %d\n", length_S1); // II. Concatenate S1 and S2 strcpy(result, S1); // Copy S1 to result strcat(result, " "); // Add a space between the two strings strcat(result, S2); // Concatenate S2 to result printf("Concatenated string (S1 + S2): %s\n", result); // III. Extract the substring "low" from S1 substr = strstr(S1, "low"); // Find substring "low" if (substr) { printf("Extracted substring: %.*s\n", 3, substr); // Print first 3 characters of found substring } else { printf("Substring 'low' not found in S1.\n"); } // IV. Find "are" in S2 and replace it with "is" pos = strstr(S2, "are"); // Find position of "are" if (pos) { strncpy(pos, "is", 2); // Replace "are" with "is" strcpy(pos + 2, pos + 4); // Shift remaining part of string left printf("Modified string S2: %s\n", S2); } else { printf("\"are\" not found in S2.\n"); } return 0; }
Program 17: Adjacency Matrix of a Graph
#include <stdio.h> #include <stdlib.h> #define MAX 20 // Maximum number of vertices int adj[MAX][MAX]; // Adjacency matrix int n; // Number of vertices // Function to create the adjacency matrix void createGraph() { int origin, destin, max_edges; int i, j; printf("Enter the number of vertices: "); scanf("%d", &n); max_edges = n * (n - 1); // Maximum edges in a directed graph // Initialize adjacency matrix to 0 for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { adj[i][j] = 0; } } // Input edges for (i = 0; i < max_edges; i++) { printf("Enter edge %d (or -1 -1 to stop): ", i + 1); scanf("%d %d", &origin, &destin); if (origin == -1 && destin == -1) { break; // Exit if input is -1 -1 } if (origin >= n || destin >= n || origin < 0 || destin < 0) { printf("Invalid edge!\n"); i--; // Decrement i to repeat this iteration } else { adj[origin][destin] = 1; // Add edge to adjacency matrix } } } // Function to display the adjacency matrix void displayGraph() { int i, j; printf("Adjacency Matrix:\n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%d ", adj[i][j]); } printf("\n"); } } int main() { createGraph(); // Create the graph displayGraph(); // Display the adjacency matrix return 0; }
These comments should give you a very clear understanding of what each line of code is doing!