Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- struct Node
- {
- int data;
- struct Node* next;
- };
- void read_list(struct Node** head)
- {
- struct Node* p;
- int x;
- while (1 == scanf_s("%d", &x))
- {
- p = (struct Node*)malloc(sizeof(struct Node));
- p->data = x;
- p->next = *head;
- *head = p;
- p = NULL;
- }
- }
- void print_list(struct Node** head)
- {
- struct Node* p = *head;
- while (p)
- {
- printf("%d ", p->data);
- p = p->next;
- }
- printf("\n");
- }
- void reverse_list_in_range(struct Node** head, int k, int n)
- {
- struct Node* lastHead;
- lastHead = *head;
- while (--k)
- lastHead = lastHead->next;
- struct Node* prev = NULL;
- struct Node* current = lastHead->next;
- struct Node* lastTemp = lastHead->next;
- struct Node* next = NULL;
- while (current != NULL && --n != -1) {
- next = current->next;
- current->next = prev;
- prev = current;
- current = next;
- }
- lastHead->next = prev;
- struct Node* p = *head;
- while (p->next)
- p = p->next;
- p->next = next;
- }
- void print_random(struct Node** head)
- {
- struct Node* p = *head;
- struct Node* ls = *head;
- int listSize = 0;
- // преброяване на възлите
- while (ls)
- {
- ls = ls->next;
- listSize++;
- }
- // генериране на горна и долна граница за произволен интервал в списъка
- int lower = 0;
- int upper = listSize - 1;
- int numStart = (rand() % (upper - lower + 1)) + lower;
- lower = numStart + 1;
- upper = listSize;
- int numEnd = (rand() % (upper - lower + 1)) + lower;
- int iterationsToPrint = numEnd - numStart;
- // принтиране на произволния интервал
- while (numStart--)
- p = p->next;
- while (iterationsToPrint--)
- {
- printf("%d ", p->data);
- p = p->next;
- }
- }
- int main()
- {
- // Примерен вход: 8 9 10 20 30 40 50 60 70 a
- struct Node* head = NULL;
- read_list(&head); // четене на списъка от конзола
- print_list(&head); // принтиране на списъка
- int k = 0;
- int n = 4;
- reverse_list_in_range(&head, k, n); // Обръщане на n елемента от k елемент
- print_list(&head); // принтиране на обърнатия списък
- srand(time(NULL)); // 'захранване' на rand()
- print_random(&head); // принтиране на произволно избран участък на вече обърнатия списък
- }
- // working 11;25
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement