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

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

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

Задача

Описать функцию или процедуру, которая находит сумму последнего и предпоследнего элементов списка, содержащего не менее двух элементов (тип=целочисленный).
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
 
  struct TNode {                
 
    int n;
 
    TNode *next;
 
  }  *start , *last ;

 void funkt1 (int m ) {         
 
   TNode *p;
 
   while (m!=0) {
 
     if (start==NULL) {     
 
       start= new TNode;
       start->n=random(100);
       start->next=NULL;
       last=start;
       printf(" %d ",start->n);
 
     }
 
     else {                
 
       p= new TNode;
       p->n=random(100);
       p->next=NULL;
       last->next=p;
       last=p;
       printf(" %d ",p->n);
 
     }
 
      m--;
 
    }
 
  return  ;
 
 }
 
 void funkt2 () {       
 
 int S=0;
 
 TNode *f;
 
 f=start;
 
 while ((f->next)->next!=NULL) 
 
  f=f->next;
 
 S= f->n + (f->next)->n ;     
 
printf("\n S=%d ",S);
 
return ;
 
}
 
int main() {
 
 randomize ();
 
 clrscr ();
 
 int L;
 
 printf("vvedute kol-vo elementov : ");
 scanf("%d",&L);
 
 funkt1 (L);
 
 funkt2 ();
 
 getch();
 
return 0 ;
 
}

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

textual
Листинг программы
#include  <stdio.h>
#include  <stdlib.h>
#include  <stdbool.h>
#include  <assert.h>
 
typedef struct Node {
    int val;
    struct Node * next;
} Node;
 
typedef struct List {
    Node *head;
} List;
 
List * list_alloc() {
    List *l = malloc(sizeof(List));
    if (l != NULL)
        l->head = NULL;
    return l;
}
 
 
bool list_is_empty(List *l) {
    return l->head == NULL;
}
 
 
void list_push_back(List *l, int v) {
    Node *n = malloc(sizeof(Node));
    assert(n != NULL);
    
    n->val = v;
    n->next = NULL;
    
    if (l->head == NULL) {
        l->head = n;
    } else {
        Node *current = l->head;
        while (current->next != NULL)
            current = current->next;
        current->next = n;
    }
}
 
int list_pop_back(List *l) {
    int v;
    Node *prev = NULL;
    Node *current = l->head;
    
    while (current->next != NULL) {
        prev = current;
        current = current->next;
    }
    
    if (prev == NULL) {
        v = current->val;
        free(current);
        current = NULL;
    } else {
        v = current->val;
        free(current);
        prev->next = NULL;
    }
    
    return v;
}
 
void list_print(List *l) {
    Node *current = l->head;
    
    while (current != NULL) {
        printf("%d ", current->val);
        current = current->next;
    }
    printf("\n");
}
 
void list_free(List *l) {
    while (list_is_empty(l)) {
        list_pop_back(l);
    }
    free(l);
}
 
int main(void)
{
    int val;
    List *list = list_alloc();
    
    list_push_back(list, 1);
    list_push_back(list, 2);
    list_push_back(list, 3);
    list_push_back(list, 4);
    list_push_back(list, 5);
    
    list_print(list);
    
    printf("%d\n", list_pop_back(list) + list_pop_back(list));
    
    list_free(list);
    
    return 0;
}

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

10   голосов , оценка 3.9 из 5
Похожие ответы