Programmer's Path to cultivation-(2) linear table (top): Arrays and Linked lists

Source: Internet
Author: User

1two pieces of Cornerstone

Arrays and linked lists form the cornerstone of various data structures, and are essential elements for implementing all of the structure.

1.1Array

Arrays are typically built into programming languages and read and written directly by index . Indexes are generally numbers, and some languages even directly support other types of indexes such as strings. Arrays can be seen in many data structures, such as strings, dynamic arrays, heaps, stacks, and queues (with linked lists, but efficiently with arrays).

1.2Linked list

They are conceptually understandable, but there are many things that can go wrong in achieving it.

Implementation Details

? Header (header): Why do you want to add a table header? Because of the table header, it is convenient to add a node before the first node or delete the first node. Otherwise, you need to modify the value of the linked list pointer held by the client, which requires that both insert () and Delete pass in a level two pointer to modify the value of the client hold pointer.

   insert design : Deleting a node is typically the value that you want to delete the node from, with three designs inserted: 1 ) append (append) to the end of the list; 2 ) Insert (insert ) to the specified position by the index or node value to specify the position; 3 ) Insert (insert ) to the specified position, but the position is specified by a pointer. In fact, the latter two kinds of similar, the difference is that it is insert () call find () or the client first call find () lookup, and then call insert () .

   delete node : two-way linked list is not said, for a one-way list, delete the node to be traversed to delete the node before the first, will be this node next the domain crosses the node to be deleted, pointing directly to the next that you want to delete the node, and then releasing the memory space that is used to delete the node, even if it is complete.

? Find the node : The node value matching must be "= =" Ah!

CImplement

Interface Definition : struct Node is implemented internally, and exposed to the client is a List and Position two pointers. Functions include three blocks: Create and destroy, delete, Debug, and print. Because of the limitations of the C language, elements only support int. With regard to naming, all functions use the data structure name List as a prefix to avoid naming conflicts that occur when clients use them.


Node Structural Body : The structure is simple, that is, the pointer field next to a node and the range value.


Create and Destroy : Pay attention to the determination of malloc return value when creating. When destroying, pay attention to two points:1) destroy one node at a time and destroy the head node ;2) use level two pointer : modifies the value of the List pointer held by the client. Avoid client references to illegal memory space after destruction.


additions and Deletions check : Specific attention points in the previous has been mentioned, once again emphasize:1insert three design, here to achieve the 1 Append and 3 inserted into the pointer to specify the position of two ways;2) for a one-way linked list, Delete the node that you want to traverse to the previous node instead of the one you want to delete;3) Be sure to "= ="When looking for comparison values.


C + +Implement

template class Definition : Thanks to the C + + class encapsulation and templating capabilities, the code became more structured and finally returned to the familiar object-oriented world. Because Delete is a keyword, the Delete method is renamed Remove.


Node class : Nodes are also encapsulated as a class Node2, so that the work of allocating memory and initializing member variables for each node is uniformly placed in the Node2 , which can be seen in the constructor of the List2 .


Construction and destruction : Because of the encapsulation of the Node2 class, the constructor becomes very concise. The destructor doesn't change, but the free () function call becomes the delete.


additions and Deletions check : This time inserting and deleting the index subscript is designed so that both insertions and deletions are traversed to the previous node.


Note Wild pointer problem : If Node2 's next forgets to initialize to NULL, then the subsequent append () A variety of traversal operations will access an illegal memory location, causing the program to terminate the exit unexpectedly.


The Debug method in Visual Studio is : When the exception exits VS prompts for debugging, select yes. The console output is an illegal access to the memory location 0x000000043, from the next value of each node in the list, it is obvious that the next value of the first few nodes is the memory location at the beginning of 0x006ae , but the memory location of the last node is exactly the console output, which is obviously far from the program's allocated memory address, so this is the result of a wild pointer, to see if the code has a pointer that is not assigned a null value.

STLImplement

(To be added "STL Source Analysis")

Programmer's Path to cultivation-(2) linear table (top): Arrays and Linked lists

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.