创建链表
#include <iostream>
using namespace std;
struct ListNode {
int val; //存储元素的数据域 [val,next]->[val,next]...
ListNode* next; //存储下一个节点的指针域
};
void test01() {
ListNode a,b,c,d,e; //创建节点
a.val = 10; //赋值
b.val = 20;
c.val = 30;
d.val = 40;
e.val = 50;
a.next = &b; //建立连接
b.next = &c;
c.next = &d;
d.next = &e;
e.next = NULL; //链表尾端
ListNode* head = &a; //头指针
while (head) {
cout << head->val << endl;
head = head->next;
}
}
int main() {
test01();
system("pause");
return 0;
}