Найти сумму последнего и предпоследнего элементов списка, содержащего не менее двух элементов - C (СИ)

Узнай цену своей работы

Формулировка задачи:

Задача

Описать функцию или процедуру, которая находит сумму последнего и предпоследнего элементов списка, содержащего не менее двух элементов (тип=целочисленный).
Листинг программы
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. struct TNode {
  5. int n;
  6. TNode *next;
  7. } *start , *last ;
  8.  
  9. void funkt1 (int m ) {
  10. TNode *p;
  11. while (m!=0) {
  12. if (start==NULL) {
  13. start= new TNode;
  14. start->n=random(100);
  15. start->next=NULL;
  16. last=start;
  17. printf(" %d ",start->n);
  18. }
  19. else {
  20. p= new TNode;
  21. p->n=random(100);
  22. p->next=NULL;
  23. last->next=p;
  24. last=p;
  25. printf(" %d ",p->n);
  26. }
  27. m--;
  28. }
  29. return ;
  30. }
  31. void funkt2 () {
  32. int S=0;
  33. TNode *f;
  34. f=start;
  35. while ((f->next)->next!=NULL)
  36. f=f->next;
  37. S= f->n + (f->next)->n ;
  38. printf("\n S=%d ",S);
  39. return ;
  40. }
  41. int main() {
  42. randomize ();
  43. clrscr ();
  44. int L;
  45. printf("vvedute kol-vo elementov : ");
  46. scanf("%d",&L);
  47. funkt1 (L);
  48. funkt2 ();
  49. getch();
  50. return 0 ;
  51. }

Решение задачи: «Найти сумму последнего и предпоследнего элементов списка, содержащего не менее двух элементов»

textual
Листинг программы
  1. #include  <stdio.h>
  2. #include  <stdlib.h>
  3. #include  <stdbool.h>
  4. #include  <assert.h>
  5.  
  6. typedef struct Node {
  7.     int val;
  8.     struct Node * next;
  9. } Node;
  10.  
  11. typedef struct List {
  12.     Node *head;
  13. } List;
  14.  
  15. List * list_alloc() {
  16.     List *l = malloc(sizeof(List));
  17.     if (l != NULL)
  18.         l->head = NULL;
  19.     return l;
  20. }
  21.  
  22.  
  23. bool list_is_empty(List *l) {
  24.     return l->head == NULL;
  25. }
  26.  
  27.  
  28. void list_push_back(List *l, int v) {
  29.     Node *n = malloc(sizeof(Node));
  30.     assert(n != NULL);
  31.    
  32.     n->val = v;
  33.     n->next = NULL;
  34.    
  35.     if (l->head == NULL) {
  36.         l->head = n;
  37.     } else {
  38.         Node *current = l->head;
  39.         while (current->next != NULL)
  40.             current = current->next;
  41.         current->next = n;
  42.     }
  43. }
  44.  
  45. int list_pop_back(List *l) {
  46.     int v;
  47.     Node *prev = NULL;
  48.     Node *current = l->head;
  49.    
  50.     while (current->next != NULL) {
  51.         prev = current;
  52.         current = current->next;
  53.     }
  54.    
  55.     if (prev == NULL) {
  56.         v = current->val;
  57.         free(current);
  58.         current = NULL;
  59.     } else {
  60.         v = current->val;
  61.         free(current);
  62.         prev->next = NULL;
  63.     }
  64.    
  65.     return v;
  66. }
  67.  
  68. void list_print(List *l) {
  69.     Node *current = l->head;
  70.    
  71.     while (current != NULL) {
  72.         printf("%d ", current->val);
  73.         current = current->next;
  74.     }
  75.     printf("\n");
  76. }
  77.  
  78. void list_free(List *l) {
  79.     while (list_is_empty(l)) {
  80.         list_pop_back(l);
  81.     }
  82.     free(l);
  83. }
  84.  
  85. int main(void)
  86. {
  87.     int val;
  88.     List *list = list_alloc();
  89.    
  90.     list_push_back(list, 1);
  91.     list_push_back(list, 2);
  92.     list_push_back(list, 3);
  93.     list_push_back(list, 4);
  94.     list_push_back(list, 5);
  95.    
  96.     list_print(list);
  97.    
  98.     printf("%d\n", list_pop_back(list) + list_pop_back(list));
  99.    
  100.     list_free(list);
  101.    
  102.     return 0;
  103. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 3.9 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы