Untitled
unknown
plain_text
a year ago
1.8 kB
1
Indexable
Never
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; } Node; // Function to compute the maximum element in a singly linked list int maxElement(Node *head) { int max = head->data; while (head != NULL) { if (head->data > max) { max = head->data; } head = head->next; } return max; } // Function to delete the element preceded by the last element in a singly linked list void deleteSecondLastElement(Node **head) { Node *temp = *head; Node *prev = NULL; while (temp->next->next != NULL) { prev = temp; temp = temp->next; } prev->next = temp->next; free(temp); } // Function to print a singly linked list void printList(Node *head) { while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { int n; printf("Enter the size of the list: "); scanf("%d", &n); Node *head = NULL; for (int i = 0; i < n; i++) { int data; printf("Enter the element at index %d: ", i); scanf("%d", &data); Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if (head == NULL) { head = newNode; } else { Node *temp = head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } } // Compute the maximum element in the list int maxElement = maxElement(head); // Delete the element preceded by the last element in the list deleteSecondLastElement(&head); // Print the list after deleting the element printf("The list after deleting the element is: "); printList(head); // Print the maximum element in the list printf("The maximum element in the list is: %d\n", maxElement); return 0; }