链表相关

  • 用尾插法建立链表
  • 用头插法建立链表
  • 用有序插入建立链表
  • 删除指定的某个结点
  • 将两个有序链表合并成一个有序链表
  • 将一个链表逆置。如: 1->2 ->3 ->4 ->5 ->NULL, 输出: 5 -> 4 -> 3 ->2 ->1 -> NULL ;
  • 找出链表的倒数第四个节点
  • 找出链表的中间节点
  • 判断单链表是否有环    //操场跑圈
  • 判断两个链表是否相交, 如果相交, 计算交点
  • 删除单链表中重复的元素
  • 将一个链表拆分(将链表奇数位置上的节点构成一个链表,偶数位置上的节点构成另一个链表)
typedef struct node{
    int score;
    struct node *next;
}*link, Node;

typedef struct node{
    int score;
    struct node *next;
}*link, Node;

link linklist_end(){
    Node *p = NULL, *tail, *head;
    tail = head = NULL;
    int tmp;
    printf("please input linklist:\n");
    while(scanf("%d", &tmp) != EOF){
        p = (Node *)malloc(sizeof(Node));
        p->score = tmp;
        if(head == NULL){
            p->next = NULL;
            head = tail = p;
        }
        else{
            p->next = NULL;
            tail->next = p;
            tail = p;
        }
    }
    print_link(head);
    return head;
}

link linklist_front(){
    Node *p = NULL, *head = NULL;
    int tmp;
    printf("please input linklist:\n");
    while(scanf("%d", &tmp) != EOF){
        p = (Node *)malloc(sizeof(Node));
        p->score = tmp;
        if(head == NULL){
            p->next = NULL;
            head = p;
        }
        else{
            p->next = head;
            head = p;
        }
    }
    print_link(head);
    return head;
}

link linklist_insert(){
    Node *p = NULL, *head = NULL, *cur;
    int tmp;
    printf("please input linklist:\n");
    while(scanf("%d", &tmp) != EOF){
        p = (Node *)malloc(sizeof(Node));
        p->score = tmp;

        if(head == NULL){
            p->next = NULL;
            head = p;
        }
        else{
            cur = head;
            if(tmp < cur->score){
                    p->next = head;
                    head = p;
                }
            else if(cur->next == NULL){
                    p->next = NULL;
                    head->next = p;
            }
            else{
                while(cur->next->score <= tmp){
                    cur = cur->next;
                    if(cur->next == NULL){
                        break;
                    }
                }
                if(cur->next == NULL){
                    cur->next = p;
                    p->next = NULL;
                }
                else{
                    p->next = cur->next;
                    cur->next = p;
                }
            }
        }
    }
    print_link(head);
    return head;
}

void print_link(link head){
    while(head != NULL){
        printf("%d ", head->score);
        head = head->next;
    }
    printf("\n");
}

link del_node(Node *head, int i){
    Node *p, *pre = head;
    if(head == NULL){
        printf("NULL linklist\n");
    }
    else{
        p = head->next;
        if(head->score == i || head->next == NULL){
            if(head->score == i){
                head = head->next;
                free(pre);
                print_link(head);
            }
            else{
                printf("fail to find\n");
            }
        }
        else{
            while(p->score != i){
                pre = p;
                p = p->next;
                if(p == NULL){
                    break;
                }
            }
            if(p == NULL){
                printf("fail to find!\n");
            }
            else{
                pre->next = p->next;
                free(p);
                print_link(head);
            }
        }
    }
    return head;
}

link merge_link(link L1, link L2){
    Node *p1 = L1, *p2 = L2, *head, *p;
    if(p1 == NULL || p2 == NULL){
        if(p1 != NULL){
            print_link(p1);
            return p1;
        }
        else if(p2 != NULL){
            print_link(p2);
            return p2;
        }
        else
            return p2;
    }
    else{
        if(L1->score < L2->score){
            head = L1;
            p = head;
            p1 = p1->next;
        }
        else{
            head = L2;
            p = head;
            p2 = p2->next;
        }
        while(p1 != NULL && p2 != NULL){
            if(p1->score < p2->score){
                p->next = p1;
                p = p1;
                p1 = p1->next;
            }
            else{
                p->next = p2;
                p = p2;
                p2 = p2->next;
            }
        }
        if(p2 == NULL){
            p->next = p1;
        }
        else{
            p->next = p2;
        }
        print_link(head);
        return head;
    }
}

link invert_link(link L){
    Node *head = L, *p, *tmp;
    if(head == NULL){
        return head;
    }
    else{
        p = head->next;
        head->next = NULL;
        while(p != NULL){
            tmp = p->next;
            p->next = head;
            head = p;
            p = tmp;
        }
        print_link(head);
        return head;
    }
}

void findFinalFour(link L){
    Node *p = L, *pre = L;
    int i, n;
    scanf("%d", &n);
    if(p == NULL){
        printf("NULL\n");
        return 0;
    }
    for(i = 1; i < n; i++){
        p = p->next;
        if(p == NULL){
            break;
        }
    }
    if(p == NULL){
        printf("num is not enough\n");
        return 0;
    }
    while(p->next != NULL){
        p = p->next;
        pre = pre->next;
    }
    printf("The No.%d final is:%d\n", n, pre->score);
    return 0;
}

void findMidList(link L){
    Node *p = L, *mid = L;
    int cnt = 0, flag = 0;
    if(L == NULL){
        printf("NULL\n");
        return 0;
    }
    while(p != NULL){
        cnt++;
        flag = cnt % 2;
        if(flag == 1 && cnt != 1){
            mid = mid->next;
        }
        p = p->next;
    }
    printf("The mid node is:");
    if(cnt % 2 == 0){
        printf("%d and %d\n", mid->score, mid->next->score);
    }
    else{
        printf("%d\n", mid->score);
    }
}

void isLoop(link L){
    Node *p = L, *pre = NULL;
    int flag = 0;
    if(L == NULL){
        printf("NULL\n");
        return 0;
    }
    while(p != NULL && p != pre){
        if(flag == 0){
            flag = 1;
            if(p == L){
                pre = L;
            }
            else{
                pre = pre->next;
            }
        }
        else{
            flag = 0;
        }
        p = p->next;
    }
    if(pre == p){
        printf("find loop\n");
    }
    else{
        printf("not find\n");
    }
}

void isInter(link L, link L1){
    int len, len1, tmp;
    len = len1 = tmp = 0; //inital!!!!
    Node *p = L, *p1 = L1, *last, *last1;
    if(L == NULL || L1 == NULL){
        printf("NULL\n");
        return 0;
    }
    while(p != NULL){
        len++;
        if(p->next == NULL){
            last = p;
        }
        p = p->next;
    }
    while(p1 != NULL){
        len1++;
        if(p1->next == NULL){
            last1 = p1;
        }
        p1 = p1->next;
    }

    if(last == last1){
        tmp = len-len1;
        p = L; p1 = L1;
        if(tmp > 0){
            while(tmp){
                p = p->next;
                tmp--;
            }
        }
        else if(tmp < 0){
            while(tmp){
                p1 = p1->next;
                tmp++;
            }
        }
        while(p1 != p){
            p1 = p1->next;
            p = p->next;
        }
        printf("The point is %d\n", p->score);
    }
    else{
        printf("no point\n");
    }
}

void delRepeat(link L){
    Node *p = L, *cur, *pre;
    while(p){
        pre = p;
        cur = p->next;
        while(cur){
            if(p->score == cur->score){
                pre->next = cur->next;
                free(cur);
                cur = pre->next;
            }
            else{
                pre = cur;
                cur = cur->next;
            }
        }
        p = p->next;
    }
    print_link(L);
}

link* linkAart(link L){
    Node *p = L, *tail, *tail2, *pre, *pre2;
    link l1 = NULL, l2 = NULL, *arr;
    tail = tail2 = pre = pre2 = NULL;
    int cnt = 0;
    if(p == NULL){
        printf("NULL\n");
        return NULL;
    }
    while(p){
        cnt++;
        if(cnt % 2){
            if(tail == NULL){
                l1 = tail = p;
                pre = p;
            }
            else{
                pre->next = p;
                pre = p;
            }
        }
        else{
            if(tail2 == NULL){
                l2 = tail2 = p;
                pre2 = p;
            }
            else{
                pre2->next = p;
                pre2 = p;
            }
        }
        p = p->next;
    }
    if(pre2 != NULL)
        pre->next = pre2->next = NULL;
    else{
        pre->next = NULL;
    }
    arr = (link*)malloc(2 * sizeof(link));
    arr[0] = l1;
    arr[1] = l2;
    return arr;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值