Professor, take you to learn the linked list: getting to know the linked list 1

Title Description:

Input three integers from the keyboard to represent the scores of three students. Assuming that the score is between 0-100, output the scores of these three students. The data are separated by spaces, and there are spaces after the last data.

sample input

70 80 90

sample output

70 80 90

#include<iostream>
#include<stdlib. h> / / use the header file to be used by malloc 
//It can be divided into four parts: establishing structure, establishing linked list, demonstration and main function 
using namespace std;
//Establish structure 
struct node{
	int data;//Data content 
	node* next;//Point to the next node 
}; 
void build(node* head)
{
	node*p=head,*q;//First establish two structure pointers, and then assign the head address to p 
	for(int i=1;i<=3;++i)
	{
		q= new node;//Rebuild a new structure 
		scanf("%d",&q->data);//Store data or * (q) data q->next(*q). next
		p->next=q;//Connect so that the pointer of the head points to the first address of the next structure 
		p=q; //Assign the first address of q to p. in order to form a loop, it can continuously store data like a linked list 
	}//When i is 2, q opens up a new structure, inputs data repeatedly, and forms a cycle again 
	p->next=NULL;//The pointer to the last structure is null 
}
void show (node*head)
{
	while(head->next!=NULL)//Execute the following loop when the structure pointer is not null 
	{
		head=head->next;//Continuous pointing. After assigning the address of the next structure to the head, data1 --- > becomes data2 
		printf("%d ",head->data);
	}
	printf("\n");
}
int main()
{
	 node*head;//head is a structure pointer 
	 head=(node*)malloc(sizeof(node));//To allocate memory space to the pointer, call the header file #include < stdlib h> 
	 head=new node;//c++ 
	 build(head);
	 show(head);
 } 

 

 

If you want to input an N, then input n numbers, and then output these n numbers, you can make some small changes in the form of linked list.

#include<iostream>
#include<stdlib.h> 
using namespace std;
int n;
struct node{
	int data;//data
	node* next;//Point to the next node 
}; 
void build(node* head)
{
	node*p=head,*q;
	for(int i=1;i<=n;++i)
	{
		q= new node;
		scanf("%d",&q->data);//Or * (q) data q->next(*q). next
		p->next=q;//connect
		p=q; 
	}
	p->next=NULL;
}
void show (node*head)
{
	while(head->next!=NULL)
	{
		head=head->next;
		printf("%d ",head->data);
	}
	printf("\n");
}
int main()
{
	 node*head;
	 head=(node*)malloc(sizeof(node));//c language #include < stdlib h> 
	 head=new node;//c++ 
	 scanf("%d",&n);
	 build(head);
	 show(head);
 } 

Supplementary information:

1.malloc and new
new returns a pointer of the specified type, and can automatically calculate the required size.

int *p;
p = new int;// The return type is int *, and the allocated size is sizeof(int)
p = new int[100];// The return type is int * and the allocated size is sizeof(int)*100

malloc requires us to calculate the number of bytes by ourselves and forcibly turn it into a pointer of the specified type when returning.

int *p;
p = (int *)malloc(sizeof(int));

2. Return value of malloc function
If the allocation is successful, a pointer to the allocated memory is returned; otherwise, a NULL pointer is returned.  

Precautions for using malloc function
malloc function returns a typeless pointer, which must be cast to the required type when used.
**(knocking on the blackboard) key point: when using malloc to open up space, the space must be released after use. If it is not released, it will cause memory leakage.
In the space opened by malloc function, do not move the pointer, because once moved, there may be a mismatch between the applied space and the released space

malloc function usage form
About the type of space opened up by malloc: malloc only opens up space, does not check the type, and only forcibly turns the type when using it.
For example: 'I' open up the byte size space you need. How to use it is your business
The mallo function actually returns a typeless pointer, which must be preceded by a pointer type cast before it can be used
Pointer itself = (pointer type *) malloc (sizeof * number of data)

int *p = NULL;
int n = 10;
p = (int *)malloc(sizeof(int)*n);

If you don't understand the pointer, you can click this link to see:

(14 messages) understanding of pointer and double pointer_ Professor's blog - CSDN blog

Keywords: data structure linked list

Added by spighita on Sun, 30 Jan 2022 08:10:35 +0200