gpt4 book ai didi

c - 在列表的开头插入节点

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:29 25 4
gpt4 key购买 nike

我需要在列表的开头插入一个节点,我该怎么做?

使用此代码:

while(tmp!=NULL){
printf("__________");
printf("\nCodigo: %d\n",tmp->code);
printf("Nombre: %s\n",tmp->name);
printf("Apellido: %s\n",tmp->last);
tmp = tmp->next;

};

我打印列表,这是我看到的:


科迪戈:3
姓名:第三名
阿佩利多:节点


科迪戈:2
名称:secc
阿佩利多:节点


科迪戈:1
名称:第一
阿佩利多:节点

所以,如果我在开头插入一些内容,我应该会看到


科迪戈:3
姓名:第三名
阿佩利多:节点


科迪戈:2
名称:secc
阿佩利多:节点


科迪戈:1
名称:第一
阿佩利多:节点


科迪戈:4
名号:第四名
阿佩利多:节点

我该怎么做?我试过这个:

    tmp_aux = lista;// creating an aux list
while(tmp_aux->next!=NULL){
tmp_aux->next = tmp_aux;
}; // i used this becouse the last printed (up) is the first node
new_aux = (struct nodo* ) malloc(1*sizeof(struct nodo));
printf("ingrese el codigo: ");
scanf("%d",&(*new_aux).code);
printf("ingrese el nombre: ");
scanf("%s",&(*new_aux).name);
printf("ingrese el apellido: ");
scanf("%s",&(*new_aux).last);



new_aux->next = tmp_aux;// then i put the aux on the next of my new node
lista = new_aux;// and make my list the new one

最佳答案

我个人认为应该首先打印第一个节点(引用评论),但我认为这只是语义。

在我使用链表的所有时间里,我都使用了headtail 指针。 head 指针指向列表中的第一项,tail 指针指向列表中的最后一项。每次从列表中添加和删除项目时,都需要一些额外的簿记来使这些保持最新,但我认为这是值得的。任何需要您遍历列表的操作(搜索特定节点、打印所有项目等)都可以更简单地完成,因为您从 head 开始并转到 tail。像下面这样的东西应该让你开始,这并不意味着是一个包罗万象的程序:

 static struct nodo *head = NULL, *tail = NULL;

struct nodo* insert_at_head(struct nodo* new_aux)
{
if (head == NULL && tail == NULL)
{
// our list is empty; any item inserted is both the beginning and end
head = new_aux;
tail = new_aux;
new_aux->next = NULL; // only 1 item in the list, there is no next element
}
else
{
// if maintained properly, this should be the only other possibility
new_aux->next = head; // new_aux is the new head of the list, so the previous head is now the 2nd item
head = new_aux; // make new_aux the new head of the list
}

// in fact, since head = new_aux happens in both branches, that should just go here

return head; // this could be a void function, but returning head and checking that it equals new_aux shows that new_aux is now the head of the list
}

struct nodo* remove_head()
{
if (head != NULL) // our list is not empty, so it does in fact have a head
{
struct nodo* temp = head
head = head->next; // even if there is one item in the list, head->next should be NULL, so now head is NULL
free(temp);
}
else
{
// this means our list is empty, optionally print an error message or warning "Trying to delete head from empty list!"
return NULL;
}

return head;
}

// now iterating over all the nodes is easy, you just have to go from head to tail.
void print_list()
{
struct nodo* cur_aux;
for (cur_aux=head; cur_aux!=NULL; cur_aux=cur_aux->next)
{
// print whatever you want here
}
}

// you can have several other functions, for manipulating the list. Their prototypes *might* look like the following:
// do not forget to maintain head and tail pointers for all of these!
struct nodo* insert_at_tail(stuct nodo* new_aux); // similar to insert_at_head(), except now you want to make the current last node the 2nd to last node
struct nodo* locate_aux(const struct nodo* aux); // iterate head to tail and return the node that matches all fields of struct nodo, return NULL if not found
void delete_aux(struct nodo* aux); // iterate through the list and delete aux if found
void clean_up_list(); // iterate from head to tail and delete all items
struct nodo* insert_aux_after(struct nodo* insert_after, struct nodo* new_aux); // this will insert new_aux after insert_after

int main(int argc, char* argv[])
{
// something like this
struct nodo* new_aux = malloc(sizeof(struct nodo));
struct nodo* new_aux2 = malloc(sizeof(struct nodo));
struct nodo* new_aux3 = malloc(sizeof(struct nodo));

// fill in the fields for each new_aux
if (insert_at_head(new_aux) != new_aux)
{
// some error happened on insertion,, handle it
}
insert_at_head(new_aux2);
insert_at_head(new_aux3);

print_list();
// the output should print new_aux3, then new_aux2, and finally new_aux

clean_up_list();
return 0;
}

您可以将 headtail 调整为列表中的第一项或最后一项,但一般约定将 head 标记为列表中的第一项列表。我可能会为其他原型(prototype)填写一些代码。事实上,你可以在没有 tail 指针的情况下实现上面的所有内容,只需在 head 处开始对列表的所有迭代,然后一直到 ->next == NULL。您还可以考虑维护一个 static size_t num_aux 来保持列表中项目数量的运行计数。当尝试从列表中删除项目时,这对于确定成功或失败特别有帮助。我怀疑如果你用 google 搜索链表教程,你会得到比我提供的代码好得多的代码,但我展示的至少应该是处理链表的一种合理方法。

关于c - 在列表的开头插入节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36169248/

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