【问题标题】:Problem in understanding some basic linked list function理解一些基本链表函数的问题
【发布时间】:2020-01-07 18:36:24
【问题描述】:
void
LInsert (LIST * l, int x, int pos)
{  
    struct Node *new, *p;   // p: previous node
    // create a new node
    new = (struct Node *) malloc (sizeof (struct Node));
    new->val = x;
    if (pos == 0)
    {               // insert to start
        new->next = l->head;
        l->head = new;
    }
    else
    {               
        // insert after p
        p = l->head;

        while (p != NULL && pos > 1)
        {
            p = p->next;
            --pos;
        }

        if (p == NULL)
        {
            printf ("LInsert: Position not possible\n");
            return;
        }
        new->next = p->next;
        p->next = new;
    }
    l->size++;
}

这是一个将节点插入链表的功能。我不明白这个程序中的几行。 在第一个 if 条件中有一行 new->next=l->head; 我认为这意味着在新节点的“下一个”部分中,它将存储头节点中的内容(可能是一个地址),但为什么呢?它使链表成为循环链表,但这只是一个简单的链表。 同样接近尾声new->next=p->next 这是什么意思。它使链表再次循环。 希望缩进是正确的 我总是让人们因为错误的缩进而对我大喊大叫 这是包含结构声明和内容的完整代码

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

struct Node {
    int  val;
    struct Node *next;
};

struct List {
    struct Node *head;
    int size;
};

// LIST is new name for "struct List"
typedef struct List LIST;

void LInit(LIST *l){ // Initialize list to empty
    l->head = NULL;  // pointer to first node
    l->size = 0;     // number of nodes
}

int LGetPos(LIST *l, int x) {
    struct Node *p;
    int i=0;
    // go through all nodes
    for (p=l->head; p!=NULL; p=p->next)
        if (p->val == x) return i;   // found
        else i++;                    // next
    return -1;   // not found in the list
}

int LGetAt(LIST *l, int pos) {
    struct Node *p=l->head;
    int i;
    // go to position
    for(i=0; p!=NULL && i<pos; i++) p = p->next;
    if(p) return p->val;  // if exists, return it
    else { printf("LDelete: Position not exist\n"); return -99; }
}

void LInsert(LIST *l, int x, int pos) {
    struct Node *new, *p;  // p: previous node
    // create a new node
    new = (struct Node *) malloc(sizeof(struct Node));
    new->val = x;
    if(pos==0) {  // insert to start
        new->next = l->head;
        l->head = new;
    }
    else {  // insert after p
        p = l->head;
        while(p!=NULL && pos>1) { p = p->next; --pos; }
        if(p==NULL) { printf("LInsert: Position not possible\n"); return; }
        new->next = p->next;
        p->next = new;
    }
    l->size++;
}

void LDelete(LIST *l, int pos) {
    struct Node *p, *d;  // p: previous
    if(l->head == NULL) return;
    if(pos==0) {  // delete first node
        d = l->head;
        l->head = d->next;
    }
    else {  // delete after p
        p = l->head;
        while(pos>1 && p) { p = p->next; --pos; }
        if(p==NULL) { printf("LDelete: Position not exist\n"); return; }
        d = p->next;
        p->next = p->next->next;
    }
    l->size--;
    free(d);
}

int LIsEmpty(LIST * l){
    return (l->size == 0);
}

int LSize(LIST * l){
     return (l->size);
}

void LDisplay(LIST *l) {
    struct Node *p;
    printf("List: ");
    for(p=l->head; p!=NULL; p=p->next)
        printf("--> %d ", p->val);
    printf("\n");
}

int LHeadOf(LIST *l) {
    if (!LIsEmpty(l)) return l->head->val;
    else {
        printf("LHeadOf: Linked list empty\n");
        return -99;
    }
}


int main() {
    LIST list;

    LInit(&list);
    LDisplay(&list);

    LInsert(&list, 3, 0);
    LInsert(&list, 4, 0);
    LInsert(&list, 5, 2);
    LDisplay(&list);

    printf("Value at 1: %d\n", LGetAt(&list, 1));

    printf("Location of 4: %d\n", LGetPos(&list, 4));

    LDelete(&list, 1);
    LDisplay(&list);

    return 0;

}

【问题讨论】:

  • Re "从我的想法",向后。右侧分配给左侧。指向头节点 (l-&gt;head) 的指针分配给新节点的 next 字段 (new-&gt;next)。
  • new->next=l->head 不存储“头节点中的内容”,它存储您观察到的地址。您需要清楚地了解指针。我建议画图。 val 显然存储了一个数字,next 是另一个节点的地址。您还没有显示结构有什么,但如果 head 指向第一项,它确实会创建“循环”链接。没问题。
  • @john 如果我删除它会不会一样。我有点明白,但我不确定。是用于while循环吗?因为有一个条件 p!=NULL
  • 我会用整个程序更新我的问题,因为它只是它的一部分

标签: c pointers data-structures linked-list


【解决方案1】:

这个程序有几行没看懂

好的 - 让我们看看那些行...

这里有一行new-&gt;next=l-&gt;head; 我认为这意味着在新节点的“下一个”部分中,它将存储头节点中的内容(可能是一个地址),但为什么呢?

该行用于在当前头部元素的前面插入新元素。所以

new->next=l->head;  // Make the new element point to current head
l->head = new;      // Change head to point to the new element as it is now the front element

也接近尾声new-&gt;next=p-&gt;next这是什么意思。它使链表再次循环。

嗯,它不会使列表循环。它只是将新元素插入到列表中间的某个位置。

    new->next = p->next;  // Make new point to the element after p
    p->next = new;        // Make p point to new
                          // In this way new has between inserted after p and
                          // before the element that folloed p

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2012-05-18
    • 1970-01-01
    相关资源
    最近更新 更多