Basic operations on a single-chain table (Article 2): basic operations on a single-chain table

Source: Internet
Author: User

Basic operations on a single-chain table (Article 2): basic operations on a single-chain table

Different from the first article, the creation method of a single-chain table is used to merge two ordered linked lists.

Knowledge Point introduction:

1. What is a wild pointer?

A wild pointer refers to a pointer pointing to a deleted object or a restricted memory area that has not been applied for access. Unlike NULL pointers, wild pointers cannot be avoided by simply determining whether they are NULL, but can only be minimized by developing good programming habits. Operations on the wild pointer can easily cause program errors.

2. There are two main causes of the wild pointer:
(1) pointer variables are not initialized. When a pointer variable is created, it does not automatically become a NULL pointer. Its default value is random, which means it is random. Therefore, the pointer variable should be initialized at the same time when it is created, either set the pointer to NULL or set it to direct to the legal memory.
(2) After the pointer p is free or deleted, It is not set to NULL, which makes people mistakenly think p is a valid pointer. Don't look at the bad names of free and delete (especially delete). They just release the memory indicated by the pointer, but didn't kill the pointer itself. Normally, if (p! = NULL. Unfortunately, the if statement does not prevent errors at this time, because even if p is not a NULL pointer, it does not point to a valid memory block. (Reference the explanations on others' blogs)

If the program defines a pointer, it must immediately point it to a space we set or set it to NULL. If this is not done, the content in this pointer is unpredictable, that is, it does not know which space it points to in the memory (that is, the wild pointer). It may point to a blank memory area, it may point to protected areas, or even to critical system memory, maybe we may accidentally operate the pointer later, which may cause system disorder and crash.

The following program encountered this situation when writing, so we introduced the wild pointer above and commented out it in the specific program.

The definition, initialization, and printing functions of a single-link table node are the same as those in the first article. They are not listed here.

Input values of n elements in sequence to create a single-chain table with table header nodes:

// Create a single-chain table void CreatlistR (LinkLNode * head, int n) {LinkLNode * node; head-> next = NULL; for (int I = 0; I <n; I ++) {node = (LinkLNode *) malloc (sizeof (LinkLNode); scanf ("% d", & node-> data); head-> next = node; head = node;} head-> next = NULL; // This statement is not added at the beginning, causing the exe to stop working.

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.