我可以在c中创建这种链表吗?

时间:2015-12-18 08:33:42

标签: c linked-list

我正在学习链接列表,我写了这段代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct linkedList
{
    char name[100];
    struct linkedList *next;
};

struct linkedList *head = NULL;


void main()
{
    struct linkedList *node1,*node2;

        node1 = (struct linkedList*)malloc(sizeof(struct linkedList));
        strcpy(node1->name, "aaa");
        node1->next = NULL;
        head = node1;

        node2 = (struct linkedList*)malloc(sizeof(struct linkedList));
        strcpy(node2->name, "bbb");
        node1->next = node2;
        node2->next = NULL;
}

我无法理解这是一个有效的链表吗? 我是否必须创建相同的节点&#34;名称 ?或者我可以使用&#34; node1&#34; &#34;节点2&#34 ;?
在我看来,我写的是等于:
头----&GT; |&#34; AAA&#34; | addr_to_node2 | ----&GT; |&#34; BBB&#34; | NULL |
这是真的吗? 如何打印此链表?

谢谢!

2 个答案:

答案 0 :(得分:2)

此处如何打印链接列表

   Protected Sub mainLoop()
    Dim Counting As Integer = 0
    Try
        While (Not stopping)
            While Counting <> 2

                If Today.Minute >= 0 Then
                    If Counting = 1 Then

                    ElseIf Counting = 0 Or Counting = 2 Then
                        PingServers()
                        Counting = Counting + 1

                ElseIf Today.Minute >= 31 Then
                    If Counting = 1 Then
                        PingServers()
                        Counting = Counting + 1

                    ElseIf Counting = 0 Then

                    End If

                End If

            End While

            If Counting = 2 Then
               DoFunction()
            End If
        End While
    Catch ex As Exception

    End Try

End Sub

答案 1 :(得分:0)

这是有效的。

  

我是否必须创建相同的“节点”名称?或者我可以使用“node1”“node2”?

您可以使用任何名称,因为它是常规变量。

实际上,您创建的struct不是链表,而是链表节点。节点包含指向下一个节点的信息和指针,而列表本身应包含指向头节点和尾节点的指针(用于快速插入) 另外,请考虑添加用于在此列表中添加和删除项目的功能。

相关问题