C: Linked List Operation (1). How to Create a linked list

Source: Internet
Author: User
Tags define null

A linked list is a common and important data structure that dynamically allocates memory.
 
When using arrays to store data, you must define a fixed length (that is, the number of elements) in advance. However, if it is difficult to determine the number of elements in advance, you must define an array large enough, to ensure success.
 
Undoubtedly, this will cause a waste of memory. However, the linked list does not have this disadvantage. It can dynamically open up memory units as needed.
 
Each element in the linked list can not be stored continuously in the memory. However, to find an element, you must know its address. In this case, the linked list must have a head pointer ).
 
Today, I will introduce a series of linked list operations, including creating a linked list, outputting a linked list, deleting a linked list, and inserting a linked list.
 
This process is presented by student ID and score.
 
Create a linked list:
 
# Include "stdlib. h"
# Include "stdio. h"
# Define NULL 0
# Define LEN sizeof (struct student)
Struct student
{
Long num;
Float score;
Struct student * next;
};
 
Int n;
Struct student * creat (void)
{
Struct student * head;
Struct student * p1, * p2;
N = 0;
P1 = p2 = (struct student *) malloc (LEN );
Scanf ("% ld, % f", & p1-> num, & p1-> score );
Head = NULL;
While (p1-> num! = 0)
{
N = n + 1;
If (n = 1)
Head = p1;
Else
P2-> next = p1;
P2 = p1;
P1 = (struct student *) malloc (LEN );
Scanf ("% ld, % f", & p1-> num, & p1-> score );
}
P2-> next = NULL;
Return (head );
}
 
Void main ()
{
Creat ();
}
 
In this way, you can create a linked list,
 
 
 
At this point, you can create a linked list.
 
Please support Li Mu Space

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.