Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Let’s look at the first question:
The node with the highest value in a single linked list
Enter a number of integers up to 100 to create a singly linked list, and then determine the node with the largest value in the singly linked list through a traversal. Prints the value of the node and its serial number.
Input format:
Enter an integer T to indicate the number of groups of test data, followed by T groups of test data. Enter the number of data n and n integers not exceeding 100 on one line for each group of test data.
Output format:
For each set of tests, output the value of the node with the largest value in the single linked list and the ordinal number of that node. The output format is as follows: “Max =dmax num=dnum”, where dmax indicates the value of the largest node, and dnum indicates the number of the node where the value of the largest node resides. If multiple maximum values are the same, the one that appears for the first time prevails.
Example Input:
1
30 85 97 43 70 69 29 77 22 64 25 55 39 95 69 99 61 97 69 59 12 88 55 75 66 13 75 36 85 67 69
Copy the code
Example output:
max=99 num=15
Copy the code
Answer:
They’re not completely clear, but the first number in the second row is actually the number of numbers in this set.
This problem asks us to create multiple linked lists and then find the maximum value and its value in each list separately.
I don’t know if anyone else understands this problem as looking for the maximum value in multiple lists, and then printing the maximum value and the position of the value in the list.
Here’s how I thought about it:
#include<stdio.h> #include<stdlib.h> typedef struct list{ int data; struct list *next; }node_s,* node_p; node_p create_head(){ node_p head = malloc(sizeof(node_s)); if(head ! = NULL){ head->next = NULL; } return head; } node_p creat_new_node(int n){ node_p new_node = malloc(sizeof(node_s)); if(new_node ! = NULL){ new_node->data = n; new_node->next = NULL; } return new_node; } void add_node_tail(node_p head,node_p new_node){ while(head->next ! = NULL){ head = head -> next; } head->next = new_node; } int main(){ node_p head = create_head(); int T; scanf("%d",&T); int num; int first_num = 0; int max=0; for(int i=0; i<T; i++){ node_p new_node = NULL; int t; scanf("%d",&t); for(int i=0; i<t; i++){ scanf("%d",&num); new_node = creat_new_node(num); add_node_tail(head,new_node); } int num1=0; while(head->next ! = NULL){ if(head->data > max){ max = head->data; first_num=num1; } head = head->next; num1++; } } printf("max=%d num=%d",max,first_num); return 0; }Copy the code
Interested friends can have a look, this article will not introduce the solution of this topic.
Here’s how to do it correctly.
Let’s look at the general structure of linked lists:
typedef struct list{
int data;
struct list *next;
}node_s,* node_p;
Copy the code
Data fields and pointer fields data and next.
Next comes the function that creates the head node.
node_p create_head(){ node_p head = malloc(sizeof(node_s)); if(head ! = NULL){ head->next = NULL; } return head; }Copy the code
Our head node is set to have no data, some people like to put data in the head node as well. I think it’s convenient to use a no-data header node when you need to consider processing a header node.
Next comes the function to create a new node.
node_p creat_new_node(int n){ node_p new_node = malloc(sizeof(node_s)); if(new_node ! = NULL){ new_node->data = n; new_node->next = NULL; } return new_node; }Copy the code
The process is simple, allocate a chunk of memory to the new node, and then fill in the data field. Since we don’t know whether to add nodes to the end of the list or to the head of the list, we assign the pointer field to be null here.
And then here we add nodes at the end of the list.
void add_node_tail(node_p head,node_p new_node){ while(head->next ! = NULL){ head = head -> next; } head->next = new_node; }Copy the code
Very simple implementation, point the head pointer to the tail, and then connect the nodes.
And then there’s the lookup function.
void find(node_p head){ int max=0; int num=0; int last_num=0; while(head ! = NULL){ if(head->data > max){ max = head->data; last_num=num; } head = head->next; num++; } printf("max=%d num=%d\n",max,last_num); }Copy the code
Walk through the list, find the maximum value and its coordinates and print it out.
Finally, take a look at the main function
int main(){ int T; scanf("%d",&T); int num; for(int i=0; i<T; i++){ node_p head = create_head(); node_p new_node = NULL; int t; scanf("%d",&t); for(int i=0; i<t; i++){ scanf("%d",&num); new_node = creat_new_node(num); add_node_tail(head,new_node); } find(head); } return 0; }Copy the code
Find large values and their coordinates in groups and print……