链表 单链表和双链表
链表的特点是只能从头开始访问
struct ListNode
{
double value;
ListNode *next;
};
链表修改和遍历
//声明链表
ListNode* head;
ListNode* new_head;
head = new ListNode;
head->value = 1;
head->next = nullptr;
ListNode *secondPtr = new ListNode;
secondPtr->value = 2;
secondPtr->next = nullptr;
head->next = secondPtr;
ListNode *thirdPtr = new ListNode;
thirdPtr->value = 3;
thirdPtr->next = nullptr;
secondPtr->next = thirdPtr;
ListNode *fourthPtr = new ListNode;
fourthPtr->value = 4;
fourthPtr->next = nullptr;
thirdPtr->next = fourthPtr;
ListNode *ptr = head;
//遍历链表
while (ptr != nullptr)
{
cout << ptr->value << " ";
ptr = ptr->next;
}
cout << "\n";