在链表的头部插入后。现在在插入新节点之前是头部的那个节点的名称是什么
After inserting at head in linked list. Now what is name of that node which was head before inserting new node
// insert at head LL
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
}
};
void insertAtHead(node* &head, int value){
node* n= new node(value);
n->next=head;
head=n;
}
void display(node* head){
while(head!=NULL){
cout<<head->data<<"->";
head=head->next;
}
cout<<"NULL"<<endl;
}
int main(){
node* head=new node(1);
node* second=new node(2);
node* third=new node(3);
head->next=second;
second->next=third;
insertAtHead(head, 0);
display(head);
cout<<head->data; // accessing data of new head.
// how to access data of node which was previously head?
return 0;
}
现在插入数据为“0”的新节点后,它是我们的新头节点,但节点以前是头节点,我们如何访问它的数据以及该节点的名称是什么,因为它的名称肯定不是 'n'?
[H]ow to access data of node which was previously head?
前一个head
节点现在是当前head-nodes下一个节点:
std::cout << "Previous head value: " << head->next->data << '\n';
// insert at head LL
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
}
};
void insertAtHead(node* &head, int value){
node* n= new node(value);
n->next=head;
head=n;
}
void display(node* head){
while(head!=NULL){
cout<<head->data<<"->";
head=head->next;
}
cout<<"NULL"<<endl;
}
int main(){
node* head=new node(1);
node* second=new node(2);
node* third=new node(3);
head->next=second;
second->next=third;
insertAtHead(head, 0);
display(head);
cout<<head->data; // accessing data of new head.
// how to access data of node which was previously head?
return 0;
}
现在插入数据为“0”的新节点后,它是我们的新头节点,但节点以前是头节点,我们如何访问它的数据以及该节点的名称是什么,因为它的名称肯定不是 'n'?
[H]ow to access data of node which was previously head?
前一个head
节点现在是当前head-nodes下一个节点:
std::cout << "Previous head value: " << head->next->data << '\n';