C语言中的单链表:pushBack
我必须创建一个pushBack方法,将元素添加到列表的末尾,但是它实现的pushBack方法不起作用。下面我留下代码:C语言中的单链表:pushBack,c,C,我必须创建一个pushBack方法,将元素添加到列表的末尾,但是它实现的pushBack方法不起作用。下面我留下代码: #include <stdio.h> #include <stdlib.h> typedef struct { int *data; struct Node *next; }Node; typedef struct { // Puntero al primer nodo Node *head; // Canti
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int *data;
struct Node *next;
}Node;
typedef struct
{
// Puntero al primer nodo
Node *head;
// Cantidad de datos
unsigned short size;
// Puntero al ultimo dato accedido.
Node *current;
}List;
List *createList();
Node *createNode(int *);
void pushBack(List *, int *);
int main(void)
{
List *lista = createList();
pushBack(&lista, 4);
return 0;
}
List *createList()
{
List *list = (List *) malloc(sizeof(List));
list -> head = NULL;
list -> size = 0;
list -> current = NULL;
return list;
}
Node *createNode(int *data)
{
Node *node = (Node *) malloc(sizeof(Node));
node -> data = data;
node -> next = NULL;
return node;
}
// Función para insertar un nuevo nodo al final de la lista
void pushBack(List *list, int *data)
{
Node *node = createNode(data);
while ( list -> head )
list -> head = list -> head -> next;
list -> head = node;
list -> size ++;
}
#包括
#包括
类型定义结构
{
int*数据;
结构节点*下一步;
}节点;
类型定义结构
{
//蓬特罗铝底漆
节点*头;
//达托斯角酒店
无符号短尺寸;
//阿塞迪多最后一站。
节点*电流;
}名单;
List*createList();
节点*创建节点(int*);
无效回推(列表*,整数*);
内部主(空)
{
List*lista=createList();
推回(&lista,4);
返回0;
}
List*createList()
{
List*List=(List*)malloc(sizeof(List));
list->head=NULL;
列表->大小=0;
列表->当前=空;
退货清单;
}
节点*创建节点(int*数据)
{
Node*Node=(Node*)malloc(sizeof(Node));
节点->数据=数据;
节点->下一步=空;
返回节点;
}
//最后一轮选举的新成员职能
无效回推(列表*列表,整数*数据)
{
Node*Node=createNode(数据);
while(列表->标题)
列表->标题=列表->标题->下一步;
列表->头部=节点;
列表->大小++;
}
如果对pushBack方法进行任何更正,我们将不胜感激。关于您当前的代码:
void pushBack(List *list, int *data)
{
Node *node = createNode(data);
while ( list -> head )
list -> head = list -> head -> next;
list -> head = node;
list -> size ++;
}
如果列表以前是空的,将某个内容推到列表的后面只会改变列表的标题。您所做的实际上是通过将列表的标题移到不应该的位置来破坏列表
您可能打算使用current
而不是head
(当然,在首次将其设置为head
之后),但在列表本身中为其指定一个字段是不寻常的-您通常只使用一个临时变量
你最好也保存尾巴,这样你就不必每次都搜索它了(a)。但是,假设这不是一个选项,下面的伪代码应该可以帮助您:
def push_back(list, item):
# Force tail constraints, new tail must be end of list.
item.next = null
# If list empty, simply set head.
if list.head == null:
list.head = item
return
# Find last element in list:
curr = list.head
while curr.next != null:
curr = curr.next
# Now have last, just attach new item to it.
curr.next = item
将其转换为C,我将留给读者作为练习。你至少应该做一些家庭作业:-)
(a) 如果您对如何做到这一点感兴趣,请参阅以下内容:
def push_back(list, item):
# Force tail constraints, new tail must be end of list.
item.next = null
# If list empty, simply set head/tail.
if list.head == null:
list.head = item
list.tail = item
return
# In non-empty list, tail will always be last element.
curr.tail.next = item # Point current tail to new tail.
curr.tail = item # Update tail
关于您当前的代码:
void pushBack(List *list, int *data)
{
Node *node = createNode(data);
while ( list -> head )
list -> head = list -> head -> next;
list -> head = node;
list -> size ++;
}
如果列表以前是空的,将某个内容推到列表的后面只会改变列表的标题。您所做的实际上是通过将列表的标题移到不应该的位置来破坏列表
您可能打算使用current
而不是head
(当然,在首次将其设置为head
之后),但在列表本身中为其指定一个字段是不寻常的-您通常只使用一个临时变量
你最好也保存尾巴,这样你就不必每次都搜索它了(a)。但是,假设这不是一个选项,下面的伪代码应该可以帮助您:
def push_back(list, item):
# Force tail constraints, new tail must be end of list.
item.next = null
# If list empty, simply set head.
if list.head == null:
list.head = item
return
# Find last element in list:
curr = list.head
while curr.next != null:
curr = curr.next
# Now have last, just attach new item to it.
curr.next = item
将其转换为C,我将留给读者作为练习。你至少应该做一些家庭作业:-)
(a) 如果您对如何做到这一点感兴趣,请参阅以下内容:
def push_back(list, item):
# Force tail constraints, new tail must be end of list.
item.next = null
# If list empty, simply set head/tail.
if list.head == null:
list.head = item
list.tail = item
return
# In non-empty list, tail will always be last element.
curr.tail.next = item # Point current tail to new tail.
curr.tail = item # Update tail
为什么要更改列表的头
指针?为什么要更改列表的头
指针?