当前位置:   article > 正文

数据结构实验之链表专题_数据结构实验链表csdn

数据结构实验链表csdn

题目链接

说明:用函数的形式刷完数据结构链表的专题,整个专题整合成了一篇代码,只要在main函数里根据题目做相应变换即可,但是没有完全按照数据结构算法的要求严格书写(并非所有函数都有输出,偷了个懒)使用C++完成

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define LL long long
using namespace std;
const int maxn = 1e5 + 10;
const double PI = acos(-1.0);

struct node {
    int data;
    node *next;
};
struct farey {
    int a, b;
    farey *next;
};
struct shuang
{
    shuang *next;
    int data;
    shuang *front;
};

node *creat1(int n) {
    node *p, *tail, *head;
    head = new node;
    head->next = NULL;
    tail = head;
    for (int i = 0; i < n; i++) {
        p = new node;
        cin >> p->data;
        p->next = NULL;
        tail->next = p;
        tail = p;
    }
    return head;
}

node *creat2(int n) {
    node *p, *head;
    head = new node;
    head->next = NULL;
    for (int i = 0; i < n; ++i) {
        p = new node;
        cin >> p->data;
        p->next = head->next;
        head->next = p;
    }
    return head;
}

shuang *creat3(int n){
    shuang *p,*head, *tail;
    head = new shuang;
    head->front  = head->next = NULL;
    tail = head;
    for(int i = 0; i < n; i ++) {
        p = new shuang;
        cin >> p->data;
        p->next = NULL;
        tail->next = p;
        p->front = tail;
        tail = p;
    }
    return head;
}
void query(shuang *root, int x) {
    shuang *p = root->next;
    while (p->data != x)
    {
        p=p->next;
    }
    int flag = 0;
    if(p->front !=root) {
        flag = 1;
        cout << p->front->data;
    }
    if(flag == 1) cout << ' ';
    if(p->next != NULL)
    cout << p->next->data;
    cout << endl;
}
void pri(node *root) {
    node *p = root->next;
    while (p) {
        printf("%d%c", p->data, p->next == NULL ? '\n' : ' ');
        p = p->next;
    }
}

void reserve(node *root) {
    node *p = root->next;
    node *q = p->next;
    root->next = NULL;
    while (p) {
        p->next = root->next;
        root->next = p;
        p = q;
        if (q) {
            q = q->next;
        }
    }
}
node *marge(node *root1, node *root2) {
    node *root, *p, *q, *tail;
    p = root1->next;
    q = root2->next;
    root = new node;
    root->next = NULL;
    tail = root;
    while (p && q) {
        if (p->data < q->data) {
            tail->next = p;
            tail = p;
            p = p->next;
        } else {
            tail->next = q;
            tail = q;
            q = q->next;
        }
    }
    if (p) {
        tail->next = p;
        tail = p;
    }
    if (q) {
        tail->next = q;
        tail = q;
    }
    return root;
}
void Sort(node *root) {
    node *p, *q, *qq;
    p = root->next;
    qq = p->next;
    root->next = NULL;
    while (p) {
        q = root;
        while (q->next) {
            if (q->next->data > p->data) {
                break;
            }
            q = q->next;
        }
        p->next = q->next;
        q->next = p;
        p = qq;
        if (qq) qq = qq->next;
    }
}
node *chai(node *root, int &ou) {
    ou = 0;
    node *head, *p, *q, *tail1, *tail2;
    head = new node;
    head->next = NULL;
    tail1 = head;
    p = root->next;
    q = p->next;
    root->next = NULL;
    tail2 = root;
    while (p) {
        if (p->data % 2 == 0) {
            ou++;
            p->next = NULL;
            tail1->next = p;
            tail1 = p;
        } else {
            p->next = NULL;
            tail2->next = p;
            tail2 = p;
        }
        p = q;
        if (q) q = q->next;
    }
    return head;
}
void erase(node *root, int &n) {
    node *p, *q, *qq, *tail;
    p = root->next;
    qq = p->next;
    root->next = NULL;
    tail = root;
    while (p) {
        q = root->next;
        int flag = 0;
        while (q) {
            if (q->data == p->data) {
                n--;
                flag = 1;
                break;
            }
            q = q->next;
        }
        if (flag == 0) {
            p->next = NULL;
            tail->next = p;
            tail = p;
        }
        p = qq;
        if (qq) qq = qq->next;
    }
}
farey *Farey(int n) {
    farey *head, *p, *q;
    head = new farey;
    head->next = new farey;
    head->next->a = 0;
    head->next->b = 1;
    head->next->next = new farey;
    head->next->next->a = 1;
    head->next->next->b = 1;
    head->next->next->next = NULL;
    for (int i = 2; i <= n; i++) {
        p = head->next;
        while (p->next) {
            if (p->b + p->next->b <= i) {
                q = new farey;
                q->b = p->b + p->next->b;
                q->a = p->a + p->next->a;
                q->next = p->next;
                p->next = q;
            }
            p = p->next;
        }
    }
    return head;
}
void pri_farey(farey *root) {
    farey *p = root->next;
    int con = 0;
    while (p) {
        con++;
        printf("%d/%d", p->a, p->b);
        if(p->next != NULL && con%10 !=0) cout << '\t';
        if (con % 10 == 0) cout << '\n';
        p = p->next;
    }
}
int main(int argc, char const *argv[]) {
    int n, m;
    cin >> n >> m;
    shuang *root;
    root = creat3(n);
    while (m--)
    {
        int x;
        cin >> x;
        query(root,x);
    }
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/540800
推荐阅读
相关标签
  

闽ICP备14008679号