Pointers on C——12 Using Structures and Pointers.1

本文深入探讨了链表这一数据结构,包括单链表的概念、节点构成及如何通过指针进行节点间的连接。此外,还讨论了链表遍历、排序等操作,并分析了其在不同应用场景下的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You can create powerful data structures by combining structures and pointers. In this chapter we take a closer look at some techniques for using structures and pointers.We spend a lot of time with a data structure called a linked list, not only because it is very useful but also because many of the techniques used to manipulate linked lists are applicable to other data structures.

你可以通过组合使用结构和指针创建强大的数据结构。本章我们将深入讨论些使用结构和指针的技巧。我们将花许多时间讨论种称为链表的数据结构,这不仅因为它非常有用,而且许多用于操纵链表的技巧也适用于其他数据结构

12.1 Linked Lists

For those of you who are not familiar with linked lists, here is a brief introduction. A linked list  is a collection of independent structures (often called nodes) that contain data.The individual nodes in the list are connected by links, or pointers. A program accesses the nodes in the list by following the pointers. Usually the nodes are dynamically allocated, though occasionally you will find linked lists constructed among elements of an array of nodes. Even in this case, though, a program traverses the list by following the pointers.

有些读者可能还不熟悉链表,这里对它作简单介绍链表(l inked list) 就些包含数据的独立数据结构〈通常称为节点〉的集合链表中的每个节点通过链或指针连接在。程序通过指针访问链表中的节点。通常节点是动态分配的,但有时你也能看到由节点数组构建的链表即使在这种情况下,程序也是通过指针来遍历链表的。


12.2 Singly Linked Lists

In a singly linked list, each node contains a pointer to the next node in the list. The pointer field of the last node in the list contains NULL to indicate that there are no more nodes in the list. After you have found the first node on a list, the pointers will lead you to the remaining nodes. To keep track of where the list begins, a root pointer is used. The root pointer points to the first node on the list. Notice that the root is a pointer and it does not contain any data.

在单链表中,每个节点包含一个指向链表下节点的指针链表最后一个节点的指针字段的值为NULL ,提示链表后面不再有其他节点在你找到链表的第1 个节点后,指针就可以带你访问剩余的所有节点。为了记住链表的起始位置,可以使用个根指针(root pointer) 根指针指向链表的第1 个节点。注意根指针只是一个指针,它不包含任何数据。


Here is a diagram of a singly linked list.

下面是张单链表的图。


The nodes in this example are structures created with the following declaration.

本例中的节点是用下面的声明创建的结构


typedef struct NODE {

struct NODE *link;

int value;

} Node;


The data stored in each node is an integer. This list contains three nodes. If you begin at the root and follow the pointer to the first node, you can access the data stored in that node. Following the pointer in the first node takes you to the second node, where you can get its data. Finally, the next pointer brings you to the last node. The value zero is used to indicate a NULL pointer; here it means that there are no more nodes in the list.

存储于每个节点的数据是个整型值这个链表包含个节点如果你从根指针开始,随着指针到达第1 个节点,你可以访问存储于那个节点的数据随着第1 个节点的指针可以到达第2 个节点,你可以访问存储在那里的数据最后,第2 个节点的指针带你来到最后个节点。零值提示它是一个NULL 指针,在这里它表示链表中不再有更多的节点


In the diagram the nodes are shown as adjacent to display the logical ordering that the links provide. In fact, the nodes might actually be scattered all through memory. It doesnʹt make any difference to a program processing such a list whether the nodes are physically adjacent or not, because the program always uses the links to get from one node to the next.

在上面的图中,这些节点相邻在一起,这是为了显示链表所提供的逻辑顺序事实上,链表中的节点可能分布于内存中的各个地方对于个处理链表的程序而,各节点在物理上是否相邻并没有什么区别,因为程序始终用链(指针〉从个节点移动到另个节点


A singly linked list can be traversed from start to end by following the links,but the list cannot be traversed backwards. In other words, once your program has reached the last node in the list, the only way to get back to any earlier node is to start again from the root pointer. Of course, the program could save a pointer to the current node before advancing to the next one, or it could even save pointers to the preceding fewʹ nodes. However, these linked lists are dynamic and can grow to hold hundreds or thousands of nodes, so it is not feasible to save pointers to all of the preceding nodes in a list.

单链表可以通过链从开始位置遍历链表直到结束位置,但链表无法从相反的方向进行遍历。换句话说, 你的程序到达链表的最后个节点时,如果你想回到其他任何节点,你只能从根指针从头开始。当然,程序在移动到下个节点前可以保存个指向前位置的指针,甚至可以保存指向前面几个位置的指针但是,链表是动态分配的,可能增长到几百或几千个节点,所以要保存所有指向前面位置的节点的指针是不可行的


The nodes in this particular list are linked so that the data values are in ascending order. This ordering is important for some applications, such as organizing appointments by time of day. It is also possible to create an unordered list for applications that do not require any ordering.

在这个特定的链表中,节点根据数据的值按升序链接在起。对于有些应用程序而言,这种顺序非常重要,比如根据天的时间安排约会对于那些不要求排序的应用程序, 然也可以创建无序的链表。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值