算法题,删除特定值,逆置的三种方法,原地逆置,链表排序,求链表的公共结点,链表归并,链表匹配算法

#include <stdbool.h>
#include "stdio.h"
#include "stdlib.h"
/*学习心得,代码尽量自己想,没思路再去看答案,但是代码还是自己写,只是借鉴思路而已。做完,如果答案有不同的思路,就按照答案再做一遍*/
struct ListNode {
    int val;
    struct ListNode *next;
};
/*工具类,栈*/
int top=0;
void push(int a[],int val){
    a[top]=val;
    top++;
}
void pop(int a[]){
    top--;
}
int getTop(int a[]){
    return a[top-1];
}
//链表长度
int length(struct ListNode *L){
    struct ListNode *p=L;
    int len=0;
    while (p->next!=NULL){
        p=p->next;
        len++;
    }
    return len;
}

//生成一个链表,用来做测试用,n是你要填入的数据个数
struct ListNode* produceList(int n){
    struct ListNode *s=(struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *first=s;
    FILE *fp=fopen("test.txt", "r");//文件放在exe文件所在的目录下
    for (int i = 0; i < n; ++i) {
        struct ListNode *SNext=(struct ListNode *)malloc(sizeof(struct ListNode));
        s->next=SNext;
        s=SNext;
        fscanf(fp,"%d",&SNext->val);
    }
    s->next=NULL;
    return first->next;//返回了一个不带头链表
}
struct ListNode* produceList2(int n){
    struct ListNode *s=(struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *first=s;
    FILE *fp=fopen("test1.txt", "r");//文件放在exe文件所在的目录下
    for (int i = 0; i < n; ++i) {
        struct ListNode *SNext=(struct ListNode *)malloc(sizeof(struct ListNode));
        s->next=SNext;
        s=SNext;
        fscanf(fp,"%d",&SNext->val);
    }
    s->next=NULL;
    return first->next;//返回了一个不带头链表
}
struct ListNode* produceList_withHead(int n){
    struct ListNode *s=(struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *first=s;
    FILE *fp=fopen("test.txt", "r");//文件放在exe文件所在的目录下
    for (int i = 0; i < n; ++i) {
        struct ListNode *SNext=(struct ListNode *)malloc(sizeof(struct ListNode));
        s->next=SNext;
        s=SNext;
        fscanf(fp,"%d",&SNext->val);
    }
    s->next=NULL;
    return first;//返回了一个带头链表
}
void delete_x_withoutHead(struct ListNode *Node){

}
/*2*///单指针就够了,注意连续都是9的情况,删除节点后指针不能马上后移,这样有可能会导致跳过符合条件的元素
void delete_x_withHead(struct ListNode *Head){
    struct ListNode *p;
    struct ListNode *scan_pointer=Head;//扫描指针
    while (scan_pointer->next!=NULL){
        if (scan_pointer->next->val==9){
            p=scan_pointer->next;
            scan_pointer->next=p->next;
            free(p);
        } else//删除节点后指针不能马上后移,这样有可能会导致跳过符合条件的元素(连续是9的情况下就会跳过)
        scan_pointer=scan_pointer->next;
    }

}

/*3*/
//逆置链表,头插法逆置
void reverse(struct ListNode *Head){
//    struct ListNode *NewHead=(struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *p=Head->next;//扫描指针
    Head->next=NULL;//初始化它的next为null
    while (p!=NULL){
        struct ListNode *s=(struct ListNode *)malloc(sizeof(struct ListNode));//不能直接操作p,采取的方法是把p复制一份,s是放进去的块
        *s=*p;
        s->next=Head->next;
        Head->next=s;
        p=p->next;
    }
}
//原地逆置无头链表
//由于要改变头结点,也就是要改变头结点的指针,因此需要传入头结点指针的地址
void reverse_withoutHead(struct ListNode **L){
    struct ListNode *p=*L,*Head=(struct ListNode *)malloc(sizeof(struct ListNode)),*temp;
    Head->next=NULL;//初始化它的next为null
    while (p!=NULL){
        temp=p;
        p=p->next;
        temp->next=Head->next;
        Head->next=temp;
    }
    *L=Head->next;
}

//栈
void stack(struct ListNode *Head){
    int a[10];
    struct ListNode *p=Head->next;//扫描指针
    while (p!=NULL){
        push(a,p->val);
        p=p->next;
    }
    while (top!=0){
        printf("%d",getTop(a));
        pop(a);
    }
}
//递归
void R_print(struct ListNode *Node){
    if (Node==NULL)
        return;
    R_print(Node->next);
    printf("%d",Node->val);
}

/*6*/
void subject_6(struct ListNode *Head){
    struct ListNode *p=Head->next;//扫描指针
    while (p->next!=NULL){
        struct ListNode *s=Head->next;//用于比较大小的扫描指针
        struct ListNode *temp;//暂存
        if (p->val > p->next->val){
            temp=p->next;
            p->next=p->next->next;//删除节点p->next
            while (s!=p){
                if (s->val < temp->val && s->next->val > temp->val){//找到插入位置
                    temp->next=s->next;
                    s->next=temp;
                    break;
                }
                s=s->next;//没有找到插入位置,后移
            }
        } else//完成一次比较并插入后,要暂留一下
        p=p->next;
    }
}

//算法不熟练,练习一遍
void subject_61(struct ListNode *Head){
    struct ListNode *p=Head->next;
    while (p->next != NULL){
        struct ListNode *s=Head->next;
        struct ListNode *temp;
        if (p->val > p->next->val){
            temp=p->next;
            p->next=p->next->next;
            while (s!=p){
                if (s->val < temp->val && s->next->val >temp->val){
                    temp->next=s->next;
                    s->next=temp;
                    break;
                }
                s=s->next;
            }
        } else
            p=p->next;
    }
}

//空间换时间策略,快速排序
void quickSort(struct ListNode *Head){

}
/*8*///逻辑检查,算法单步调试
struct ListNode * search_common(struct ListNode *L1,struct ListNode *L2){
    int len1=length(L1),len2=length(L2);
    struct ListNode *p1=L1,*p2=L2;
    if (len1<len2){
        int dist=len2-len1;
        for (int i = 0; i < dist; ++i) {
            p2=p2->next;
        }
        while (p1!=p2){
            p1=p1->next;
            p2=p2->next;
        }
        return p1;
    } else{
        int dist=len1-len2;
        for (int i = 0; i < dist; ++i) {
            p1=p1->next;
        }
        while (p1!=p2){
            p1=p1->next;
            p2=p2->next;
        }
        return p2;

    }
}
/*13*/
void Union(struct ListNode *L1,struct ListNode *L2){
    //和归并排序一样,有三个指针
    struct ListNode *p1=L1,*p2=L2;
    struct ListNode *Head,*p3;
    if (p1->val < p2->val){
        Head=p1;
        p1=p1->next;
    } else{
        Head=p2;
        p2=p2->next;
    }
    p3=Head;
    while (p1!=NULL && p2!=NULL){
        if (p1->val < p2->val){
            p3->next=p1;
            p1=p1->next;
        } else{
            p3->next=p2;
            p2=p2->next;
        }
        p3=p3->next;
    }
    if (p1==NULL){
        p3->next=p2;
    } else{
        p3->next=p1;
    }
}
/*14*/
struct ListNode * get_common(struct ListNode *L1,struct ListNode *L2){
    struct ListNode *p1=L1,*p2=L2;
    struct ListNode *p3=(struct ListNode *)malloc(sizeof(struct ListNode)),*Head=p3;
    while (p1!=NULL && p2!=NULL){
        if (p1->val < p2->val){
            p1=p1->next;
        } else if (p1->val > p2->val){
            p2=p2->next;
        } else{
            struct ListNode *s=(struct ListNode *)malloc(sizeof(struct ListNode));
            s->val=p1->val;
            p3->next=s;
            p1=p1->next;
            p2=p2->next;
            p3=p3->next;
        }
    }
    return Head;
}
/*16*///判断L2是不是子串
//朴素匹配模式
bool pattern(struct ListNode *L1,struct ListNode *L2){
    struct ListNode *p1=L1,*p2=L2;
    while (p2!=NULL && p1!=NULL){
        if(p1->val==p2->val){
            p1=p1->next;
            p2=p2->next;
        } else{
            p2=L2;
            p1=p1->next;
        }
    }
    if (p1==NULL)
        return false;
    else
        return true;

}
/*21*///创新的使用了两个指针在同一链表上扫描
int get_k(struct ListNode *L){
    struct ListNode *p=L,*p_k=NULL;
    int k=3;
    for (int i = 0; i < 3; ++i) {
        p=p->next;
    }
    p_k=L;
    while (p!=NULL){
        p=p->next;
        p_k=p_k->next;
    }
    if (p_k==NULL)
        return 0;
    else {
        printf("%d",p_k->val);
        return 1;
    }
}
/*25*/
void changeList(struct ListNode *L,int n){
    struct ListNode *p=L;//扫描指针,用于分割链表
    for (int i = 0; i < n/2; ++i) {
        p=p->next;
    }
    struct ListNode *newHead;//新的链表头
    if (n%2==0)
        newHead=p;
    else
        newHead=p->next;
    struct ListNode *s=(struct ListNode *)malloc(sizeof(struct ListNode));
    reverse_withoutHead(&newHead);
    struct ListNode *p1=L,*p2=newHead,*temp;//链表1指针,链表2指针,暂存变量
    for (int i = 0; i < n/2; i++) {
            temp=p2;//暂存p2指针
            p2=p2->next;//移动p2指针
            //插入p2
            temp->next=p1->next;
            p1->next=temp;
            p1=p1->next->next;//移动p1指针
    }
}
/*63页思考题*/
//先用基数排序
int get_elem(int n,int pos){//这个函数用来得到第几位数,如n=539,pos=2,返回5
    int a[3];
    a[0]=n%100;
    a[1]=(n%10)/10;
    a[2]=n/100;
    return a[n];
}

void twoSum(int a[],int len){
    //直接利用链表结构体
    struct ListNode b[10];
    for (int k = 0; k < 3; ++k) {
        for (int i = 0; i < len; ++i) {
            b[get_elem(a[i],k)].next->val=get_elem(a[i],k);
        }
    }

}
int main(){
    struct ListNode *firstNode=produceList(6);
    struct ListNode *head=produceList_withHead(6);
    //创建一个交叉链表
    struct ListNode *head1=produceList_withHead(8);
    struct ListNode *s=head->next;
    struct ListNode *p=head1->next;
    for (int i = 0; i < 4; ++i) {
       p=p->next;
    }
    for (int i = 0; i < 2; ++i) {
        s=s->next;
    }
    p->next=s;
    search_common(head1,head);
    //创建两个无头链表
    struct ListNode *L1=produceList(6);
    struct ListNode *L2=produceList2(3);
    struct ListNode *L3=get_common(L1,L2);
    changeList(L1,6);
    int a[20]={7,20,13,19,14,20,27,22,20,40,998,653,45,13,654,216,77,112,336,444};

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值