首页 > 解决方案 > 理解一些基本链表函数的问题

问题描述

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;

}

标签: cpointersdata-structureslinked-list

解决方案


这个程序有几行我看不懂

好的 - 让我们来看看那些线条......

这是一条线new->next=l->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

推荐阅读