了解一些基本的链表功能时遇到问题

时间:2020-01-07 18:36:24

标签: c pointers data-structures linked-list

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的含义是什么。它使链接列表再次循环。 希望缩进是正确的,我总是让人们对我的缩进大喊大叫 这是完整的代码,其中包括struc声明和内容

#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;

}

1 个答案:

答案 0 :(得分:0)

我在程序中看不懂几行

好的-让我们看一下这些线条...

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

该行用于将新元素插入到当前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->next=p->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