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; int findMax(Node* head) { int max = head->data; Node* current = head->next; while (current != NULL) { if (current->data > max) { max = current->data; } current = current->next; } return max; } void deletePrecedingLast(Node** head) { if (*head == NULL || (*head)->next == NULL || (*head)->next->next == NULL) { return; } Node* current = *head; while (current->next->next->next != NULL) { current = current->next; } Node* temp = current->next; current->next = current->next->next; free(temp); } void printList(Node* head) { Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } void freeList(Node** head) { Node* current = *head; while (current != NULL) { Node* temp = current; current = current->next; free(temp); } } int main() { int n; scanf("%d", &n); Node* head = NULL; Node* tail = NULL; for (int i = 0; i < n; i++) { int element; scanf("%d", &element); Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = element; newNode->next = NULL; if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } int max = findMax(head); deletePrecedingLast(&head); printf("%d\n", max); printList(head); freeList(&head); return 0; }