首页 > 编程语言 >小学期C++实践

小学期C++实践

时间:2023-07-06 14:56:36浏览次数:39  
标签:node tmp head NULL struct 小学 实践 C++ now

一、链表

1、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head = NULL;  

int x; 

struct node *reverseList(struct node *head){
    struct node *head1 = NULL;
    struct node *now = NULL; 
    struct node* p = head; 
    vector <int> G; 
    while(p != NULL){
        G.push_back(p->val); 
        p = p->next; 
    }
    for(int i = G.size() - 1; i >= 0; i--){
        struct node *tmp = new node; 
        tmp->val = G[i]; 
        tmp->next = NULL; 
        if(head1 == NULL){
            head1 = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = now->next; 
        }
    }
    return head1; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(scanf("%d", &x) != EOF){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    struct node *head1 = reverseList(head); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

2、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *deleteDuplicates(struct node *head){
    struct node *head1 = NULL;
    struct node *now = NULL; 
    struct node* p = head; 
    int num = head->val; 
    insert(head1, now, num); 
    p = p->next; 
    while(p != NULL){
        if(p->val == num){
            p = p->next; 
            continue; 
        }
        num = p->val; 
        insert(head1, now, num); 
        p = p->next; 
    }
    return head1; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    struct node *head1 = deleteDuplicates(head); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

3、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *deleteDuplicates(struct node *head){
    struct node* p = head; 
    map<int, int> g; 
    while(p != NULL){
        g[p->val]++; 
        p = p->next; 
    }
    struct node *now = NULL; 
    p = head; 
    head = NULL; 
    while(p != NULL){
        if(g[p->val] == 1){
            insert(head, now, p->val); 
        }
        p = p->next; 
    }
    return head; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    struct node *head1 = deleteDuplicates(head); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

4、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head1 = NULL;  
struct node *head2 = NULL; 

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *addTwoNumbers(struct node *head1, struct node *head2){
    struct node *head = NULL; 
    struct node *now = NULL; 
    
    struct node *p1 = head1, *p2 = head2; 
    int last = 0; 
    while(p1 != NULL && p2 != NULL){
        int x = p1->val + p2->val + last; 
        last = 0; 
        if(x >= 10){
            x -= 10; 
            last += 1; 
        }
        insert(head, now, x); 
        p1 = p1->next; 
        p2 = p2->next; 
    }
    while(p1 != NULL){
        int x = p1->val + last; 
        last = 0; 
        if(x >= 10){
            last = 1; 
            x -= 10; 
        }
        insert(head, now, x); 
        p1 = p1->next; 
    }
    while(p2 != NULL){
        int x = p2->val + last; 
        last = 0; 
        if(x >= 10){
            last = 1; 
            x -= 10; 
        }
        insert(head, now, x); 
        p2 = p2->next; 
    }
    if(last){
        insert(head, now, 1); 
    }

    return head; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head1; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head1 == NULL){
            head1 = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    now = head2; 
    while(cin >> x && x != -1){
        insert(head2, now, x); 
    }

    struct node *head =  addTwoNumbers(head1, head2); 
    struct node *p = head; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

5、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *oddEvenList(struct node *head){
    struct node *head1 = NULL;
    struct node *now = NULL; 
    struct node* p = head; 
    
    int cnt = 0; 
    while(p != NULL){
        cnt++; 
        if(cnt % 2 == 1){
            insert(head1, now, p->val); 
        }
        p = p->next; 
    }
    cnt = 0; 
    p = head; 
    while(p != NULL){
        cnt++; 
        if(cnt % 2 == 0){
            insert(head1, now, p->val); 
        }
        p = p->next; 
    }

    return head1; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    struct node *head1 = oddEvenList(head); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

6、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *partition(struct node *head, int k){
    struct node *head1 = NULL;
    struct node *now = NULL; 
    struct node* p = head; 
    while(p != NULL){
        if(p->val < k){
            insert(head1, now, p->val); 
        }
        p = p->next; 
    }
    p = head;
    while (p != NULL){
        if(p->val >= k) insert(head1, now, p->val); 
        p = p->next; 
    }
    
    return head1; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }
    cin >> x; 

    struct node *head1 = partition(head, x); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

7、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    int sum; // 求前缀和,前缀和相等证明中间部分为 0
    struct node *next; 
    struct node *pre; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *DeleteZero(struct node *head){
    struct node *head1 = NULL;
    struct node *now = NULL; 
    struct node* p = head; 
    struct node *q = NULL; 
    
    head->sum = head->val; 
    while(233){
        p = head; 
        while(p != NULL){
            if(p->next != NULL){
              p->next->sum = p->next->val + p->sum; 
            } 
            p = p->next; 
        } 
        bool flag = 0; 
        p = head, q = head->next; 
        while(p != NULL){
            q = p->next; 
            while(q != NULL){
                if(p->sum == q->sum || q->sum == 0){
                    flag = 1; 
                    break; 
                }
                q = q->next; 
            }
            if(flag) break; 
            p = p->next; 
        }
        if(!flag) break; 
        p = p->next; 
        if(p == head || q->sum == 0) head = q->next; 
        else p->pre->next = q->next; 
    }

    return head; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        tmp->pre = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            tmp->pre = now; 
            now = tmp; 
        }
    }

    struct node *head1 = DeleteZero(head); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

8、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head1 = NULL;  
struct node *head2 = NULL; 

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *mergeTwoLists(struct node *head1, struct node *head2){
    struct node *head = NULL;
    struct node *now = NULL; 
    struct node* p1 = head1, *p2 = head2; 
    
    while(p1 != NULL && p2 != NULL){
        if(p1->val < p2->val){
            insert(head, now, p1->val); 
            p1 = p1->next; 
        }
        else{
            insert(head, now, p2->val); 
            p2 = p2->next; 
        }
    }
    while(p1 != NULL){
        insert(head, now, p1->val); 
        p1 = p1->next; 
    }
    while(p2 != NULL){
        insert(head, now, p2->val); 
        p2 = p2->next; 
    }

    return head; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head1; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head1 == NULL){
            head1 = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head2 == NULL){
            head2 = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    struct node *head = mergeTwoLists(head1, head2); 
    struct node *p = head; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}


## 9、

```cpp
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
    struct node *pre; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}


bool isPalindrome(struct node* head){
    struct node *p = head; 
    struct node *q = NULL; 
    while(p != NULL){
        if(p->next == NULL) q = p; 
        p = p->next; 
    }
    if(q == head) return 1; 
    p = head; 
    while(p != q && p->pre != q){
        // printf("val: %d %d\n", p->val, q->val); 
        if(p->val != q->val) return 0; 
        p = p->next; 
        q = q->pre; 
    }
    return 1; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        tmp->pre = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            tmp->pre = now; 
            now = tmp; 
        }
    }

    if(isPalindrome(head)) cout << "True" << endl; 
    else cout << "False" << endl; 



    return 0; 
}


二、位运算

1、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010


int main(){
//	freopen("hh.txt", "r", stdin); 
    int x = -23; 
    cout << bitset<64>(x) << endl; 
    x >>= 1; 
    cout << bitset<64>(x);      // 对于补码,Windows采取高位补1.
    cout << x << endl; 
    return 0; 
}

2、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010


int main(){
//	freopen("hh.txt", "r", stdin); 
    int a = 9; 
    int b = -2; 
    cout << (int)(a & b) << endl;   
    cout << bitset<65>(a) << endl; 
    cout << bitset<64> (b) << endl;     // 对负数高位补1,转成补码后 & 运算未消去原数字的高位
    return 0; 
}

3、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

string s; 
int a; 

string Int32toSixteen(ll a){
    if(a < 0){
        a = abs(a); 
        for(int i = 0; i < 32; i++)
            a ^= (1ll << i); 
        a += 1; // 取补码
    }
    
    
    string t = ""; 
    while(a){
        int tmp = (a >> 4) << 4; 
        int x = a ^ tmp; 
        // int num = 1 | (1 << 1) | (1 << 2) | (1 << 3); 
        // x ^= num; 
        if(x < 10) t.push_back(x + '0'); 
        else t.push_back(x + 'A' - 10); 
        a >>= 4;
    }
    reverse(t.begin(), t.end()); 
    return t; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
     cin >> a; 
     s = Int32toSixteen(a); 
     cout << s; 
    return 0; 
}

4、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

string s; 
int a; 

string Int32toTwo(ll a){
    if(a < 0){
        a = abs(a); 
        for(int i = 0; i < 32; i++)
            a ^= (1ll << i); 
        a += 1; // 取补码
    }
    
    string t = ""; 
    while(a){
        int tmp = (a >> 1) << 1; 
        int x = tmp ^ a; 
        t.push_back(x + '0'); 
        a >>= 1;
    }
    reverse(t.begin(), t.end()); 
    return t; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
     cin >> a; 
     s = Int32toTwo(a); 
     cout << s; 
    return 0; 
}

5、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

string s; 
int x = 0; 

int main(){
//	freopen("hh.txt", "r", stdin); 
    cin >> s; 
    if(s[0] == '0'){
        for(int i = 0; i < s.length(); i++){
            x = (x << 1) + (s[i] - '0'); 
        }
        printf("%x\n%d", x, x);  
    }
    else{
        for(int i = 1; i < s.length(); i++){
            x = (x << 1) + (s[i] - '0'); 
        }
        x -= 1; 
        for(int i = 0; i < 15; i++)
            x ^= (1 << i); 
        x *= -1; 
        printf("%x\n%d", x, x);
    }
    
    return 0; 
}

6、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int a:3;
    int b:5; 
    int c:6; 
    int d:9; 
} t;

int n; 

string Int32toSixteen(ll a){
    if(a < 0){
        a = abs(a); 
        for(int i = 0; i < 32; i++)
            a ^= (1ll << i); 
        a += 1; // 取补码
    }
    
    
    string t = ""; 
    while(a){
        int tmp = (a >> 4) << 4; 
        int x = a ^ tmp; 
        // int num = 1 | (1 << 1) | (1 << 2) | (1 << 3); 
        // x ^= num; 
        if(x < 10) t.push_back(x + '0'); 
        else t.push_back(x + 'A' - 10); 
        a >>= 4;
    }
    reverse(t.begin(), t.end()); 
    return t; 
}

string Int32toTwo(ll a){
    if(a < 0){
        a = abs(a); 
        for(int i = 0; i < 32; i++)
            a ^= (1ll << i); 
        a += 1; // 取补码
    }
    
    string t = ""; 
    while(a){
        int tmp = (a >> 1) << 1; 
        int x = tmp ^ a; 
        t.push_back(x + '0'); 
        a >>= 1;
    }
    reverse(t.begin(), t.end()); 
    return t; 
}


int main(){
//	freopen("hh.txt", "r", stdin); 
    cin >> n; 
    t.a = n, t.b = n, t.c = n, t.d = n; 
    string s1 = Int32toSixteen(t.a);  
    string s2 = Int32toSixteen(t.b); 
    string s3 = Int32toSixteen(t.c);
    string s4 = Int32toSixteen(t.d); 
    cout << s1 << " " << s2 << " " << s3 << " " << s4 << endl; 
    s1 = Int32toTwo(t.a); 
    s2 = Int32toTwo(t.b); 
    s3 = Int32toTwo(t.c);  
    s4 = Int32toTwo(t.d); 
    cout << s1 << " " << s2 << " " << s3 << " " << s4; 
    return 0; 
}

标签:node,tmp,head,NULL,struct,小学,实践,C++,now
From: https://www.cnblogs.com/wondering-world/p/17532127.html

相关文章

  • 【ChernoC++笔记】指针和引用
    指针【16】C++指针▶️指针的类型不影响指针的本质:任何type的指针都是保存着内存地址的整数(integer)。指针的type只用来使人更好理解。//一个最简单的void类型指针,储存内存地址0void*ptr=0;void*ptr=NULL;void*ptr=nullptr; //C++11//使ptr存储var的内存地......
  • C++程序课程设计任务书[2023-07-06]
    C++程序课程设计任务书[2023-07-06]C++程序课程设计任务书班级学号姓名一、实践目的该实践在系统学习《C++程序设计基础》课程后进行。通过本实践,培养学生使用C++解决实际问题的能力。二、实践任务与要求(任选一个任务,独立完成)任务一:(一)......
  • C/C++数据结构与算法课程设计[2023-07-03]
    C/C++数据结构与算法课程设计[2023-07-03]数据结构与算法课程设计一、课程设计的目的、要求和任务 本课程设计是为了配合《数据结构与算法》课程的开设,通过设计完整的程序,使学生掌握数据结构的应用、算法的编写等基本方法。1.课程的目的(1)使学生进一步理解和掌握课堂上所学......
  • 一个C++11的线程函数
    一个C++11的线程函数#include<iostream>#include<thread>#include<chrono>voidprintNumbers(){for(inti=1;i<=100;++i){std::cout<<i<<std::endl;std::this_thread::sleep_for(std::chrono::millis......
  • C++ 重载运算符和重载函数
     C++允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载。重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义(实现)不相同。当您调用一个重载函数或重载运算符时,编译器通过把您所使用的参数......
  • C/C++ Qt 数据库SqlRelationalTable关联表
    在上一篇博文中详细介绍了SqlTableModle组件是如何使用的,本篇博文将介绍SqlRelationalTable关联表组件,该组件其实是SqlTableModle组件的扩展类,SqlRelationalTable组件可以关联某个主表中的外键,例如将主表中的某个字段与附加表中的特定字段相关联起来,QSqlRelation(关联表名,关联ID,......
  • C/C++ HOOK 全局 API
    全局Hook不一定需要用到Dll,比如全局的鼠标钩子、键盘钩子都是不需要Dll的,但是要钩住API,就需要Dll的协助了,下面直接放上Dll的代码,注意这里使用的是MFCDLL。//Test_Dll(mfc).cpp:定义DLL的初始化例程。//#include"stdafx.h"#include"Test_Dll(mfc).h"#ifde......
  • c++ day1
    跟着数据结构,算法与应用c++语言描述来学习本书第一周计划回顾c++的基础知识本人只是记录自己认为要去记录的一些资料想要系统的学习还是需要去啃书1template<classT>是C++中用于定义模板的语法结构。它表明接下来的代码是一个模板,并且模板参数名为T,它可以是任意合法的C++......
  • 【嵌入式】keil5中使用C,C++混合编译
    5份文件:a.c和a.h和b.cpp和b.h和main.c其中main.c调用b.cpp,b.cpp调用a.c main.c#include"./b.h"intmain(void){ intt=funC(); while(1) { }} b.h#ifndef_B_#define_B_#ifdef__cplusplusextern"C"{#endifintfunC(void);#ifdef__cplusp......
  • Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用
    https://blog.csdn.net/qq_21438461/article/details/130764349Linux系统编程C/C++以及Qt中的零拷贝技术:从底层原理到高级应用一、零拷贝技术的概念与价值(Zero-CopyConceptandValue)1.1什么是零拷贝(WhatisZero-Copy)1.2为什么我们需要零拷贝(WhyWeNeedZero-C......