bool insert_list(struct list *head, int i) {
struct list *current = (struct list *)malloc(sizeof(struct list));
if (current==NULL) {
return false;
}
current->value = i;
struct list *next = head->next;
current->next = next;
head->next = current;
return true;
}
void print_list(struct list *head) {
while(head!=NULL) {
cout<<head->value<<" ";
head = head ->next;
}
cout<<endl;
}
int main() {
struct list *head = (struct list *)malloc(sizeof(struct list));
head->value = 3;
head->next = NULL;
insert_list(head,1);
insert_list(head,2);
print_list(head);
print_list(reverse_list(head));
system("pause");
}
面试常考题目:逆转一个链表
最新推荐文章于 2024-05-30 14:51:59 发布