CC9. 牛牛的单链表求和
描述
牛牛输入了一个长度为 n 的数组,他想把这个数组转换成链表,链表上每个节点的值对应数组中一个元素的值,然后遍历链表并求和各节点的值。输入描述
输出描述
把数组转换成链表然后对其求和并输出这个值。示例1
输入:
5 5 2 3 1 1
输出:
12
C 解法, 执行用时: 2ms, 内存消耗: 296KB, 提交时间: 2022-05-22
#include <stdio.h> #include <stdlib.h> typedef struct linklist { int num; struct linklist * link; }linklist; int main() { int n,i,j; scanf("%d",&n); getchar(); int * p = (int *)malloc(n*sizeof(int)); for(i=0;i<n;i++) { scanf("%d",p+i); getchar(); } linklist * head = NULL; linklist * ls = NULL; linklist * clean = NULL; linklist * list = (linklist *)malloc(sizeof(linklist)); ls = list; clean = list; head = list; for(j=0;j<n;j++) { list->num = p[j]; linklist * ls = (linklist *)malloc(sizeof(linklist)); ls->link = NULL; list->link = ls; list = ls; } int sum=0; while(head->link) { sum += head->num; head = head->link; } while(clean->link) { linklist * a; a = clean; clean = clean->link; free(a); } free(clean); free(p); printf("%d",sum); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 316KB, 提交时间: 2022-07-26
#include<stdio.h> #include<stdlib.h> struct node{ int data; struct node* next; }; int main(){ int i,n,sum=0; int a[100]; scanf("%d",&n); struct node* head=malloc(sizeof(struct node)); head->next=NULL; struct node* current=head; for(i=0;i<n;i++){ scanf("%d",&a[i]); struct node*node=malloc(sizeof(struct node)); node->data=a[i]; node->next=NULL; current->next=node; current=current->next; } current=head->next; while(current){ sum=sum+current->data; current=current->next; } printf("%d",sum); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 316KB, 提交时间: 2022-07-02
#include <stdio.h> #include <stdlib.h> typedef struct List{ int v; struct List* next; }List,*linkList; linkList createList(int n){ linkList L,p,q; L=(List*)malloc(sizeof(List)); if(!L)return 0; L->next=NULL; q=L; for(int i=0;i<n;i++){ p=(linkList)malloc(sizeof(List)); scanf("%d",&(p->v)); p->next=NULL; q->next=p; q=p; } return L; } void print(linkList h){ linkList p=h->next; int sum=0; while(p!=NULL){ // printf("%d ",p->v); sum=sum+(p->v); p=p->next; } printf("%d",sum); } int main(){ int n; linkList A=NULL; linkList B=NULL; scanf("%d\n",&n); A=createList(n); print(A); }
C 解法, 执行用时: 2ms, 内存消耗: 320KB, 提交时间: 2022-06-18
#include<stdio.h> typedef struct Node { int val; struct Node* next; }Node; int main(int argc, char* argv[]) { int n = 0; int k = 0; scanf("%d\n", &n); Node * phead = (Node*)malloc(sizeof(Node)); Node* ptail = phead; for (int i = 0; i < n; i++) { scanf("%d",&k); Node * pnode = (Node*)malloc(sizeof(Node)); pnode->next = NULL; pnode->val = k; ptail->next = pnode; ptail = pnode; } ptail = phead->next; int total = 0; while (ptail != NULL) { total = total + ptail->val; ptail = ptail->next; } printf("%d\n", total); while (phead != NULL) { Node * p = phead; phead = phead->next; free(p); } return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2022-06-27
#include <stdio.h> #include <stdlib.h> typedef struct node{ int val; struct node *next; }listed; int main(void){ int n; int sum=0,val; scanf("%d",&n); while(n--) scanf("%d",&val),sum+=val; printf("%d",sum); return 0; }