关于#c语言#的问题:单链表基本操作

单链表基本操作
#include
#include
struct Node {
    int data;
    struct Node* next;
};
int main() {
    int n, m, k, d, a;
    scanf("%d", &n);
    struct Node* L, *L3,*q,*head,*p,*T;
    head = (struct Node*)malloc(sizeof(struct Node));
    q = head;
    while (n > 0) {
        p = (struct Node*)malloc(sizeof(struct Node));
        scanf("%d", &p->data);
        q->next = p;
        q = p;
        n--;
    }
    q->next = NULL;
    scanf("%d", &m);
    while(m>0){
    scanf("%d", &a);
    T = head;
    if (a == 0) {
        scanf("%d %d", &k, &d);
        L = (struct Node*)malloc(sizeof(struct Node));
        L->data = d;
        while (k >0&&T!=NULL ) {
            T = T->next;
            k--;
        }if(T!=NULL){
        L->next = T->next;
        T->next = L;}
    }
    if (a == 1) {
        scanf("%d",&k);
            if(k>0&&T!=NULL){
        while (k-1>0&&T!=NULL) {
            k--;
            T = T->next;
        }
        struct Node*tmp=T->next;
        T->next = tmp->next;
        free(tmp);}
        }
    m--;

    }
    L3 = head->next;
    while (L3 != NULL) {
        printf("%d ", L3->data);
        L3 = L3->next;
    }
    printf("\n");
    return 0;
}

运行结果及报错内容

img

我的解答思路和尝试过的方法
#include
#include
struct Node {
    int data;
    struct Node* next;
};
int main() {
    int n, m, k, d, a;
    scanf("%d", &n);
    struct Node* L, *L3,*q,*head,*p,*T;
    head = (struct Node*)malloc(sizeof(struct Node));
    q = head;
    while (n > 0) {
        p = (struct Node*)malloc(sizeof(struct Node));
        scanf("%d", &p->data);
        q->next = p;
        q = p;
        n--;
    }
    q->next = NULL;
    scanf("%d", &m);
    while(m>0){
    scanf("%d", &a);
    T = head;
    if (a == 0) {
        scanf("%d %d", &k, &d);
        L = (struct Node*)malloc(sizeof(struct Node));
        L->data = d;
        while (k >0&&T!=NULL ) {
            T = T->next;
            k--;
        }if(T!=NULL){
        L->next = T->next;
        T->next = L;}
    }
    if (a == 1) {
        scanf("%d",&k);
            
        while (k-1>0&&T!=NULL) {
            k--;
            T = T->next;
        }if(k>0&&T!=NULL){                          //只是把这边移动了一下
        struct Node*tmp=T->next;
        T->next = tmp->next;
        free(tmp);}
        }
    m--;

    }
    L3 = head->next;
    while (L3 != NULL) {
        printf("%d ", L3->data);
        L3 = L3->next;
    }
    printf("\n");
    return 0;


}

我想要达到的结果

img

不懂为什么移动一下就可以对,谁能解释一下

是这样的,没有移动的话这个T通过while可能变为NULL了,下面在取去tmp=T->next就是指针非法访问了
tmp那一段,tmp可能变为NULL,也有可能有问题

if(k>0&&T!=NULL){
        while (k-1>0&&T!=NULL) {
            k--;
            T = T->next;
        } /*while结束的时候 T可能为NULL */
        struct Node*tmp=T->next; /*TNULL的时候,就是指针非法访问了*/
        T->next = tmp->next;/*这里还得注意下tmp是否为NULL,如果tmp为NULL也会指针非法访问了*/
        free(tmp);}
        }

参考一下

链表的基本操作(C/C++)_风间琉璃•的博客-CSDN博客_c++链表的基本操作 文章目录前言一、单链表定义二、单链表的基本操作1.遍历操作2.链表长度3.链表查找4.链表插入5.头插入法(创建链表)6.尾插法(创建链表)7.删除结点8.链表释放三、循环链表四、双向链表总结前言提示:以下是本篇文章正文内容一、单链表定义当需要建立一个学生信息表,学生的人数无法确定而且人数经常变化,此时若用顺序表来实现将会很麻烦单链表:线性表的链接存储结构, 用一组任意(不联系, 零散分布) 的存储单元存放线性表的元素.存储特点:1.逻辑次序和物理次序不一定相同2.元素之.. https://blog.csdn.net/qq_53144843/article/details/120749247#:~:text=%E4%BA%8C%E3%80%81%E5%8D%95%E9%93%BE%E8%A1%A8%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C%201.%E9%81%8D%E5%8E%86%E6%93%8D%E4%BD%9C%20%E6%8E%A5%E5%8F%A3%E6%93%8D%E4%BD%9C%3A%20void%20displayNode%20%28Link%20head%29%3B%20void,%2F%2F%E8%BE%93%E5%87%BA%E6%95%B0%E6%8D%AE%20p%20%3D%20p-%3Enext%3B%20%2F%2F%E7%A7%BB%E5%90%91%E4%B8%8B%E4%B8%80%E4%B8%AA%E7%BB%93%E7%82%B9%20%7D%20%7D%201