计蒜客课程数据结构(链表)

1.链表是一种与火车非常相似的数据结构,在链表里,我们叫火车头为表头,每节车厢就是链表的元素,车厢里载的人和物就是元素的数据域,连接车厢的部件就是元素的指针。从火车的结构我们可以发现链表的一个特点,元素之间前后依赖,串联而成。

2.性质:

  • 元素相互依赖,串联而成(除了火车头,每节车厢都只链接到前一节车厢)
  • 链表只有一个表头(火车只有一个火车头)
  • 元素不能随机访问(不能随机到达某节车厢)

3.链表的创建,插入,遍历,删除和翻转

 1 #include<iostream>
 2 using namespace std;
 3 class Node {
 4 public:
 5     int data;                           //数据域
 6     Node* next;                         //指针域
 7     Node(int _data) {                   //类Node的构造函数
 8         data = _data;
 9         next = NULL;
10     }
11 };
12 class LinkList {
13 private:
14     Node* head;                        //头指针
15 public:
16     LinkList() {                       //类LinkList构造函数
17         head = NULL;
18     }
19     void insert(Node *node, int index) {     //链表的插入
20         if (head == NULL) {                  //特殊情况,头指针为空
21             head = node;
22             return;
23         }
24         if (index == 0) {                   //特殊情况,插入节点后的位置是链表首位
25             node->next = head;              //先让node的指针指向当前表头,完成node的插入
26             head = node;                    //让node成为头结点,完成表头的更新
27             return;
28         }
29         Node *current_node = head;          //一般情况
30         int count = 0;
31         while (current_node->next != NULL && count < index - 1) {
32             current_node = current_node->next;
33             count++;
34         }
35         if (count == index - 1) {
36             node->next = current_node->next;
37             current_node->next = node;
38         }
39     }
40     void output() {                         //链表的遍历
41         if (head == NULL) {
42             return;
43         }
44         Node *current_node = head;
45         while (current_node != NULL) {
46             cout << current_node->data << " ";
47             current_node = current_node->next;
48         }
49         cout << endl;
50     }
51     void delete_node(int index) {          //链表的删除
52         if (head == NULL) {
53             return;
54         }
55         Node *current_node = head;
56         int count = 0;
57         if (index == 0) {                //特殊情况,被删除节点为头结点
58             head = head->next;
59             delete current_node;
60             return;
61         }
62         while (current_node->next != NULL && count < index -1) { //一般情况
63             current_node = current_node->next;
64             count++;
65         }
66         if (count == index - 1 && current_node->next != NULL) {
67             Node *delete_node = current_node->next;
68             current_node->next = delete_node->next;
69             delete delete_node;
70         }
71     }
72    void reverse(){                                   //链表的反转
73        if(head==NULL){
74            return;
75        }
76        Node *next_node,*current_node;
77        current_node=head->next;
78        head->next=NULL;
79        while(current_node!=NULL){
80            next_node=current_node->next;
81            current_node->next=head;
82            head=current_node;
83            current_node=next_node;
84        }
85    }
86 };
87 int main() {
88     LinkList linklist;
89     for (int i = 1; i <= 10; i++) {
90         Node *node = new Node(i);
91         linklist.insert(node, i - 1);
92     }
93     linklist.output();
94     linklist.delete_node(3);
95     linklist.output();
96     linklist.reverse();
97     linklist.output();
98     return 0;
99 }
原文地址:https://www.cnblogs.com/Reindeer/p/5626385.html