将节点添加到链表
Adding nodes to linked list
struct ll
{
int data;
ll *next;
};
void display(ll **head) {
ll *t = *head;
while(t){
cout<<t->data<<"-->";
t = t->next;
}
}
void add(ll **head, int d) {
ll *c = *head;
ll temp;
temp.data = d;
temp.next = NULL;
if(*head == NULL) {
*head = &temp;
} else {
while(c->next) {
c = c->next;
}
c->next = &temp;
}
}
int main() {
ll *head = NULL;
add(&head,1);
add(&head,2);
//add(&head,3);
//add(&head,4);
//add(&head,10);
//display(&head);
getchar();
}
为什么在 add() 中使用 ll temp 不起作用。如果我将其转换为 ll *temp= new ll;所有 w
您的 Add() 函数创建了一个本地 ll 结构。
访问它会导致未定义的行为。
您需要在内存的堆部分分配您的节点。
void add(ll **head, int d) {
ll *c = *head;
ll *temp = new ll;
temp->data = d;
temp->next = NULL;
if(*head == NULL) {
*head = temp;
} else {
while(c->next) {
c = c->next;
}
c->next = temp;
}
}
您在函数中创建了一个超出范围的局部结构(所有内存都在函数结束时释放)。但是,当您将节点指向它时:*head = &temp;
,它将导致未定义的行为,因为它会指向函数末尾被破坏的内存(一旦 temp
超出范围),即取消引用不安全。
然而,如果temp
分配给new
,这意味着temp
不会被销毁,直到你调用delete
关键字,允许它安全有效地指向函数中创建的变量。
试试这个:
struct ll
{
int data;
ll *next;
};
void display(ll** head)
{
ll* cur = *head;
while(cur != NULL){
cout<< cur->data << "-->";
cur = cur->next;
}
}
void add(ll** head, int d)
{
ll* cur = *head, *temp = new ll;
temp->data = d;
temp->next = NULL;
if(*head == NULL) {
*head = temp;
}
else {
while(cur->next != NULL) {
cur = cur->next;
}
c->next = temp;
}
}
void free_list(ll** head)
{
ll* cur = *head, *next = NULL;
while(cur != NULL){
next = cur->next;
delete cur;
cur = next;
}
}
int main()
{
ll *head = NULL;
add(&head,1);
add(&head,2);
display(&head);
free_list(&head);
getchar();
}
struct ll
{
int data;
ll *next;
};
void display(ll **head) {
ll *t = *head;
while(t){
cout<<t->data<<"-->";
t = t->next;
}
}
void add(ll **head, int d) {
ll *c = *head;
ll temp;
temp.data = d;
temp.next = NULL;
if(*head == NULL) {
*head = &temp;
} else {
while(c->next) {
c = c->next;
}
c->next = &temp;
}
}
int main() {
ll *head = NULL;
add(&head,1);
add(&head,2);
//add(&head,3);
//add(&head,4);
//add(&head,10);
//display(&head);
getchar();
}
为什么在 add() 中使用 ll temp 不起作用。如果我将其转换为 ll *temp= new ll;所有 w
您的 Add() 函数创建了一个本地 ll 结构。 访问它会导致未定义的行为。 您需要在内存的堆部分分配您的节点。
void add(ll **head, int d) {
ll *c = *head;
ll *temp = new ll;
temp->data = d;
temp->next = NULL;
if(*head == NULL) {
*head = temp;
} else {
while(c->next) {
c = c->next;
}
c->next = temp;
}
}
您在函数中创建了一个超出范围的局部结构(所有内存都在函数结束时释放)。但是,当您将节点指向它时:*head = &temp;
,它将导致未定义的行为,因为它会指向函数末尾被破坏的内存(一旦 temp
超出范围),即取消引用不安全。
然而,如果temp
分配给new
,这意味着temp
不会被销毁,直到你调用delete
关键字,允许它安全有效地指向函数中创建的变量。
试试这个:
struct ll
{
int data;
ll *next;
};
void display(ll** head)
{
ll* cur = *head;
while(cur != NULL){
cout<< cur->data << "-->";
cur = cur->next;
}
}
void add(ll** head, int d)
{
ll* cur = *head, *temp = new ll;
temp->data = d;
temp->next = NULL;
if(*head == NULL) {
*head = temp;
}
else {
while(cur->next != NULL) {
cur = cur->next;
}
c->next = temp;
}
}
void free_list(ll** head)
{
ll* cur = *head, *next = NULL;
while(cur != NULL){
next = cur->next;
delete cur;
cur = next;
}
}
int main()
{
ll *head = NULL;
add(&head,1);
add(&head,2);
display(&head);
free_list(&head);
getchar();
}