Написать программу на СИ. Структуры Создать «список» при помощи полей структуры, заполнить его элементами с клавиатуры, создать меню действий со списком.
Меню 1.Вывести список на экран 2. Удаление первого элемента 3. Проверка списка на пустоту 4. Изменение порядка элементов на обратный 5. Выйти из программы
void printList(struct List list) { struct Node *current = list.head; if (current == NULL) { printf("List is empty\n"); } else { printf("List elements: "); while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } }
void deleteFirst(struct List list) { if (list->head == NULL) { printf("List is empty\n"); } else { struct Node temp = list->head; list->head = list->head->next; free(temp); printf("First element deleted\n"); } }
int isEmpty(struct List list) { if (list.head == NULL) { return 1; } else { return 0; } }
void reverseList(struct List list) { struct Node prev = NULL; struct Node current = list->head; struct Node next = NULL;
while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } list->head = prev; printf("List reversed\n");
}
int main() { struct List list; initList(&list); int choice, value;
do { printf("\nMenu\n"); printf("1. Print list\n"); printf("2. Delete first element\n"); printf("3. Check if list is empty\n"); printf("4. Reverse list\n"); printf("5. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch(choice) { case 1: printList(list); break; case 2: deleteFirst(&list); break; case 3: if (isEmpty(list)) { printf("List is empty\n"); } else { printf("List is not empty\n"); } break; case 4: reverseList(&list); break; case 5: printf("Exiting program\n"); break; default: printf("Invalid choice\n"); } } while (choice != 5); return 0;
struct Node {
int data;
struct Node *next;
};
struct List {
struct Node *head;
};
void initList(struct List *list) {
list->head = NULL;
}
void insert(struct List list, int value) {
struct Node newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = list->head;
list->head = newNode;
}
void printList(struct List list) {
struct Node *current = list.head;
if (current == NULL) {
printf("List is empty\n");
} else {
printf("List elements: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
}
void deleteFirst(struct List list) {
if (list->head == NULL) {
printf("List is empty\n");
} else {
struct Node temp = list->head;
list->head = list->head->next;
free(temp);
printf("First element deleted\n");
}
}
int isEmpty(struct List list) {
if (list.head == NULL) {
return 1;
} else {
return 0;
}
}
void reverseList(struct List list) {
while (current != NULL) {struct Node prev = NULL;
struct Node current = list->head;
struct Node next = NULL;
next = current->next;
current->next = prev;
prev = current;
current = next;
}
list->head = prev;
printf("List reversed\n");
}
int main() {
do {struct List list;
initList(&list);
int choice, value;
printf("\nMenu\n");
printf("1. Print list\n");
printf("2. Delete first element\n");
printf("3. Check if list is empty\n");
printf("4. Reverse list\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printList(list);
break;
case 2:
deleteFirst(&list);
break;
case 3:
if (isEmpty(list)) {
printf("List is empty\n");
} else {
printf("List is not empty\n");
}
break;
case 4:
reverseList(&list);
break;
case 5:
printf("Exiting program\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);
return 0;
}