Dynamic Data Structure-linked list

Source: Internet
Author: User
Tags define null

A struct Member may contain data pointing to the pointer type, or a pointer pointing to the struct itself.

It is different from a common struct variable pointer. It is a member of a struct of the indicated type.

For example:

Struct student


Int num;

Float score;

Struct student * next;


Next refers to the data of the struct student type (this is the type of the struct where next is located)

With the concept of struct, we can implement dynamic data structures such as linked lists and trees.

The following is a simple process of establishing a linked list of student nodes (each student node contains data domains and pointer domains, and the data domains are student Student IDs and scores)


# Include <stdio. h>

# Include <stdlib. h>

# Define null 0

# Define Len sizeof (struct student)

// The Student node is represented by a struct.

Struct student


Long num;

Float score;

Struct student * next;


Int n = 0;


// This function creates a linked list, input data by the keyboard, and returns the pointer to the header of the linked list.

Struct student * creat ()


Float a = 1.1;

Struct student * head;

Struct student * P1, * P2; // P1 points to the new node, and P2 points to the last node of the linked list.

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; // if the newly opened node is the first node

Else P2-> next = p1; // if the newly opened node is not the first node

P2 = p1; // the newly opened node is always assigned to P2, and a new node is created.

P1 = (struct student *) malloc (LEN );

Scanf ("% LD % F", & P1-> num, & P1-> score );


P2-> next = NULL;

Return (head );


Int main ()


Creat ();

Return 0;


1. Dynamic Memory Allocation and recovery

Void * malloc (unsigned int size );

Void * calloc (unsigned int N, unsigned int size );

Void free (void * PTR );

Example: struct student * P1, * P2;

P1 = P2 = (struct student *) malloc (sizeof (struct student); to force type conversion, because the return value of malloc () is a void pointer type;

2, struct student * creat () returns the pointer Function

3, float a = 1.1; it is useless in the program.

Before this sentence is added, there is always a running error (the compilation is correct). After querying the cause, we will know:

C runtime error r6002 not loaded floating point Support

The required floating point library is not linked.

Fix the problem by checking the following possible causes:
  1. The program is compiled or linked through an option (such as/fpi87, which requires a coprocessor), but the program runs on a computer without a coprocessor installed.

  2. PrintfOrScanfThe Function Format String contains the floating point format specification, and this program does not contain any floating point value or variable.

  3. The compiler minimizes program size by loading floating point support only when necessary. The compiler cannot detect the floating point format specification in the format string, so the compiler does not load the necessary floating point routines.

  4. Use floating-point parameters to comply with floating-point format specifications, or perform floating-point assignments elsewhere in the program. This operation causes floating point loading support.

  5. In a program written in a hybrid language, C library is specified before the Fortran library when the program is linked. Reconnect and specify the C library.

Float a = 1.1; you can add floating point support before scanf. Of course, there are other solutions. Here, we just want to illustrate a small problem.

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.