链表
结构体变量和结构体变量连接在一起
struct ListNode { int data; ListNode* next; ListNode(int data):data(data),next(NULL){} };
//创建链表
ListNode* createList(int n) { ListNode* head = new ListNode(0); //创建一个ListNode对象head,head将作为头节点 ListNode* curr = head; int num; for (int i = 0; i < n; i++) { cin >> num; ListNode* newNode = new ListNode(num); curr->next = newNode; curr = newNode; } return head; }
// 打印链表 void printList(ListNode* head) { ListNode* curr = head->next; while (curr!= NULL) { cout << curr->data << " "; curr = curr->next; } cout << endl; }