题解 | #【模板】链表#

【模板】链表

https://www.nowcoder.com/practice/97dc1ac2311046618fd19960041e3c6f

#include <cstring>
#include <iostream>
using namespace std;

// 链表结构
typedef struct LNode {
    int data;
    struct LNode* next;
} LNode, *LinkedList;

// 链表初始化 带头结点的单链表
bool initList(LinkedList& L) {
    L = (LNode*)malloc(sizeof(LNode));
    L -> next = NULL;
    L -> data = -1;
    if (L == NULL) return false;
    return true;
}

// 在当前结点之后插入新结点
bool insertNextNode(LNode* q, int b) {
    if (q == NULL) return false;
    LNode* s = (LNode*)malloc(sizeof(LNode));
    s -> data = b;
    s -> next = q -> next;
    q -> next = s;
    return true;
}

// 链表插入
bool insert(LinkedList& L, int a, int b) {
    LNode* p = L;
    // 找到插入值结点的前一个结点
    while (p -> next != NULL && p -> next -> data != a) {
        p = p->next;
    }
    insertNextNode(p, b);
    return true;
}

// 链表删除
bool deleteNode(LinkedList& L, int a) {
    LNode* p = L;
    LNode* q = L;
    while (p != NULL && p -> data != a) {
        q = p;
        p = p -> next;
    }
    if (p == NULL) return true;
    q -> next = p -> next;
    free(p);
    return true;
}

// 打印当前链表的所有data
void printfAllNode(LinkedList L) {
    LNode* p = L;
    if (L -> next == NULL)
        cout << "NULL";
    while (p -> next != NULL) {
        p = p -> next;
        cout << p -> data << " ";
    }
}

int main() {
    int a, b;
    int count = 0;
    string str;
    string inserta = "insert";
    string deletea = "delete";
    cin >> count;
    LinkedList L;
    initList(L);
    for (int i = 0; i < count; i++) {
        // cin >> str >> a >> b;
	    // 注意输入情况 delete 和 insert 结构不同
        cin >> str;
        if(inserta ==  str) {
            cin >> a;
            cin >> b;
        } else {
            cin >> a;
        }
        if (inserta ==  str) {
            insert(L, a, b);
        } else if (deletea ==  str) {
            deleteNode(L, a);
        }
    }
    printfAllNode(L);
    return 0;
}

全部评论

相关推荐

東大沒有派對:这是好事啊(峰哥脸
我的秋招日记
点赞 评论 收藏
分享
亲爱滴达瓦里氏:又有嵌软又有FPGA又有硬件的,三个方向显得你的简历太杂糅了,展示不出你在某一个方向有哪些技术栈。基本上也不会有企业会去招一个本科的全栈工程师。HR一般是看你的技术栈是否和招聘要求匹配才发面试的。如果你想三个方向都试试的话还是建议你分成三份不同的简历根据不同岗位来投。多看看****或牛客上面的招聘信息,根据上面要求的技术栈来写简历。看了你的帖子经历这一块还是过关的,不过看的每个方向都会一点,但每个方向优势都不突出。 还有就是要海投,你这才几十份算不了啥。去年投了一两百份的大有人在。多去跑跑线下招聘吧,有机会继续搞份实习,后面转正机会大些。有条件就多往北上广深投吧。武汉竞争还是太激烈了。
我的秋招日记
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务