第二十七课:数据结构入门 - 数组与链表
学习目标:
学习内容:
数组(Array)
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5}; // 声明并初始化一个整型数组
// 访问并打印数组元素
for (int i = 0; i < 5; i++) {
cout << "Element at index " << i << ": " << arr[i] << endl;
}
return 0;
}
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
链表(LinkedList)
#include <iostream>
using namespace std;
// 定义链表节点结构体
struct Node {
int data;
Node* next;
};
// 打印链表函数
void printList(Node* n) {
while (n != nullptr) {
cout << n->data << " ";
n = n->next;
}
cout << endl;
}
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 = nullptr;
printList(head); // 打印链表
return 0;
}
1 2 3
链表的遍历
链表中的数据插入
链表的节点定义示例(C++):
class ListNode {
public:
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr)};
遍历链表示例(C++):
void traverseList(ListNode* head) {
ListNode* temp = head;
while(temp != nullptr) {
cout << temp->val << " ";
temp = temp->next;
}
cout << endl;
}
链表中数据插入示例(C++):
ListNode* insertAtHead(ListNode* head, int x) {
ListNode* newNode = new ListNode(x);
newNode->next = head;
head = newNode;
return head;
}
ListNode* insertAtTail(ListNode* head, int x) {
ListNode* newNode = new ListNode(x);
if(head == nullptr) {
return newNode;
}
ListNode* temp = head;
while(temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
return head;
}
void insertAfterNode(ListNode* prevNode, int x) {
if(prevNode == nullptr) {
cout << "The given previous node cannot be null." << endl;
return;
}
ListNode* newNode = new ListNode(x);
newNode->next = prevNode->next;
prevNode->next = newNode;
}
练习题:
#include <iostream>
class ListNode {
public:
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr)};
class SinglyLinkedList {
public:
ListNode *head;
SinglyLinkedList() : head(nullptr) // 遍历链表
void traverseList() {
ListNode *temp = head;
while (temp != nullptr) {
std::cout << temp->val << " ";
temp = temp->next;
}
std::cout << std::endl;
}
// 在头部插入节点
void insertAtHead(int x) {
ListNode *newNode = new ListNode(x);
newNode->next = head;
head = newNode;
}
// 在尾部插入节点
void insertAtTail(int x) {
ListNode *newNode = new ListNode(x);
if (head == nullptr) {
head = newNode;
} else {
ListNode *temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}
// 在某个节点后插入新节点
void insertAfterNode(ListNode *prevNode, int x) {
if (prevNode == nullptr) {
std::cout << "The given previous node cannot be null." << std::endl;
return;
}
ListNode *newNode = new ListNode(x);
newNode->next = prevNode->next;
prevNode->next = newNode;
}
};
int main() {
SinglyLinkedList list;
// 在链表头部插入节点
list.insertAtHead(5);
list.insertAtHead(3);
list.insertAtHead(1);
// 在链表尾部插入节点
list.insertAtTail(7);
list.insertAtTail(9);
// 遍历链表
list.traverseList();
// 在链表中第一个节点后插入新节点
list.insertAfterNode(list.head, 2);
// 再次遍历链表
list.traverseList();
return 0;
}