簡體   English   中英

鏈接的字符串列表C / Struct

[英]Linked string list C / Struct

我要創建一個具有在其中添加和打印字符串的功能的LinkedList。

在下面附加我的代碼。

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

struct Line {
       char tekst[20];
       struct Line *next;
};

void print(const struct Line* n);
void add(struct Line* n, char t[20], struct Line* next);

int main(void) {
    struct Line lin, *tekst;
    tekst = NULL;
    add(&lin, "ExampleString1", tekst);
    add(&lin, "ExampleString2", tekst);
    print(&lin);

    getch();
    return 0;  
};

void print(const struct Line* n) {
     for ( ; n; n = n->next )
         printf("%s", n->tekst);
     printf("\n");
}

void add(struct Line* n, char t[20], struct Line* next) {
     strcpy(n->tekst,t); // before: n->tekst[20] = t[20];
     n->next = next;
}

它將一些隨機數寫入標准輸出,然后使命令行崩潰。 我不知道tekst2[20]是否應該在這里(我不確定如何在這里調用函數參數)。

我的目標是制作一個字符串列表,然后可以添加和打印它們。

我幾乎可以確定這是您要執行的操作:

void add(struct Line** pp, const char *t)
{
    struct Line *p = malloc(sizeof(*p));
    strcpy(p->tekst, t);
    p->tekst2[0] = 0;
    p->next = NULL;

    while (*pp)
        pp = &(*pp)->next;
    *pp = p;
}

您的print()函數使用錯誤的格式說明符來打印字符串:

void print(const struct Line* n) 
{
     for ( ; n; n = n->next )
         printf("%s\n", n->tekst);
     printf("\n");
}

將其放到main()

int main(void)
{
    struct Line *lst = NULL;

    add(&lst, "SomeTxt");
    add(&lst, "SomeMoreTxt");
    add(&lst, "YetMoreTxt");

    print(lst);

    getch();
    return 0;  
};

產量

SomeTxt
SomeMoreTxt
YetMoreTxt

我將適當的列表清除代碼以及正確的錯誤檢查作為任務留給您。

這個怎么運作

此功能使用“指針到指針”的成語。 main()傳遞&lst ,您正在傳遞指針變量的地址 指針不過是保存東西地址的變量(當然,它是聲明的指針的類型)。 例:

int a;
int *b = &a;

聲明一個指向int的指針,並分配要存儲在其中的int地址 另一方面,這是:

int a;
int *b = &a;
int **c = &b;

聲明我們以前擁有的東西,但是c聲明為pointer-to-pointer-to-int 就像我們將int的地址存儲在b ,請注意我們如何將int*的地址存儲在c 這是該語言非常強大的功能。

也就是說,代碼的工作方式如下:

void add(struct Line** pp, const char *t)
{
    // node allocation stuff. nothing special here
    struct Line *p = malloc(sizeof(*p));
    strcpy(p->tekst, t);
    p->tekst2[0] = 0;
    p->next = NULL;

    // look at the pointer addressed by our pointer-to-pointer pp
    //  while it is not null, store the address of the `next` pointer 
    //  of that node in pp and loop.
    while (*pp)
        pp = &(*pp)->next;

    // pp now holds the address of the pointer we want to set with our new node
    //  it could be the address of the original pointer passed in (if the list was
    //  empty). or the address of some `next` member in the list. We really don't 
    //  care which. All we care about it is it addresses the pointer we need to 
    //  assign our new allocation to, so that is what we do.
    *pp = p;
}

花一些時間在網上研究C和指向指針的指針。 它們將改變您對列表管理之類的想法。 要記住的最重要的事情是一個指針,指針指向某個“節點”; 它指向一個指向節點的指針 這是一個令人頭疼的聲明,但要做一些功課就可以了。

編輯:使用下面的行(或strncpystrcpy(n->tekst,t); 而不是n->tekst[20] = t[20];

初始化* test為null

struct Line lin, *tekst=NULL;

使用%s在程序中打印文本。

printf("%s", n->tekst);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM