创建三个空结点,通过next的指针,将三个结点的地址串联在一起,变成一个单链表
#include <iostream> using namespace std; struct Node { int data; Node* next; }; void printList(Node* n) { while (n != NULL) { cout << n->data << " "; n = n->next; } } int main() { Node* head = new Node(); Node* second = new Node(); Node* third = new Node(); head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = NULL;
Node* newnode = new Node();
newnode->data = -1;
newnode->next = head;
head = newnode;
printList(head); return 0; }