A. experiment 11  4  preliminary list

Title Description

Problem Description: you know a sequence of positive integers, the number of which is unknown, but there is at least one element. Your task is to establish a single chain table, and use the chain table to store the sequence of positive integers, then count the maximum and minimum values of the elements in the sequence, and calculate the sum of all the elements in the sequence. The input of positive integer uses - 1 as the end flag. Note that - 1 does not count the elements in the positive integer sequence (do not count - 1).
Input and output requirements: input a positive integer sequence. The number of positive integer sequence elements is unknown, but it ends with "- 1". Input at least one positive integer before "- 1". Elements in the sequence range from 1 to 9999999. Output three positive integers, that is, the maximum value, the minimum value, and the sum of all elements.
The maximum number of test case nodes is 1000. All integers can be stored in int type.
Note the input and output formats.

 

sample input

1 4 99 21 50 61 32 4 -1

sample output

The maximum,minmum and the total are:99 1 272
#include<stdio.h>
#include<stdlib.h>
struct node//Linked list structure
{
	int data;
	struct node *next;
};
typedef struct node NODE;//rename
NODE * createNode();//Create linked list function
void freeList(NODE * head);//Release linked list function
int findMax(NODE * head);//Maximum function, return integer
int findMin(NODE * head);//Minimum function, return integer
int sum = 0;//and
int main()
{
	NODE *head=createNode();
	
	printf("The maximum,minmum and the total are:%d %d %d\n", findMax(head), findMin(head), sum);
	
	freeList(head);
	head = NULL;
	return 0;
}
NODE * createNode()
{
	int num;
	NODE * current = NULL;
	NODE * last = NULL;
	NODE * head = NULL;
	scanf("%d", &num);
	while (num != -1)
	{
		current = malloc(sizeof(NODE));
		if (current != NULL)
		{
			current->data = num;
			sum += num;
			if (head == NULL)
			{
				head = current;
				last = current;
			}
			else
			{
				last->next = current;
				last = current;
			}
		}
		scanf("%d", &num);
	}
	last->next = NULL;
	return head;
}

void freeList(NODE * head)
{
	NODE * temp;
	while (head != NULL)
	{
		temp = head;
		head = head->next;
		free(temp);
	}
}

int findMax(NODE *head)
{
	NODE * sptr = head;
	int max=sptr->data;
	while (sptr!=NULL)
	{
		if (sptr->data > max)
			max = sptr->data;
		sptr = sptr->next;
	}
	return max;
}

int findMin(NODE *head)
{
	NODE * sptr = head;
	int min = sptr->data;
	while (sptr != NULL)
	{
		if (sptr->data < min)
			min = sptr->data;
		sptr = sptr->next;
	}
	return min;
}

 

Posted on Mon, 02 Dec 2019 14:02:33 -0800 by _DarkLink_