gpt4 book ai didi

c - 理解一些基本链表函数时遇到的问题

转载 作者:行者123 更新时间:2023-11-30 16:07:24 25 4
gpt4 key购买 nike

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; 根据我的想法,这意味着在新节点的“下一个”部分中它将存储 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;

}

最佳答案

I don't understand a few lines in this program

好吧 - 让我们看看这些行...

here is a line new->next=l->head; From my thinking it means that in the "next" part of the new node it will store the what's in the head node(an address probably), but why?

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

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

Also near the end new->next=p->next what does this mean.It makes the linked list circular again.

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

    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

关于c - 理解一些基本链表函数时遇到的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59634200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com