链表专题复习

//本程序功能是从键盘上输入整数,直到输入的整数小于0时停止输入,然后反序输出这些整数
#include<stdio.h>
#include <stdlib.h>     //填空
struct node{
    int data;
    struct node *next;       //填空
}*p,*head;
void input(){
    int num;
    struct node *q;
    printf("Enter data:");
    scanf("%d",&num);
    if (num<0)
        return;          //填空
//        exit(0);  //填空
    q=(struct node *)malloc(sizeof(struct node));         //填空
    q->data=num;
    q->next=p;
    p=q;
    input(); //填空
}
void main(){
    printf("input data until <0 ");
    p=NULL;
    input();
    head=p;
    printf("OUTPUT:");
    while (p){   //填空
        printf("%d\n",p->data);
        p=p->next;   //填空
    }
}
//输入1 2 3 4 0 输出结果
#include<stdio.h>
#include <stdlib.h>     //填空
struct line{
    int num;
    struct line *next;       //填空
};
int main(){
    struct line *p1,*p2,*head;
    int j,k=0;
    p1=p2=head=(struct line *)malloc(sizeof(struct line));    //填空
    scanf("%d",&p1->num);
    while (p1->num!=0){
        p1=(struct line *)malloc(sizeof(struct line));
        scanf("%d",&p1->num);
        if (p1->num == 0)
            p2->next=NULL;
        else{
            p2->next=p1;
            p2=p1;
        }
        k++;
    }
    p2->next=head;
    p1=head->next;
    p1=p1->next;
    for (int j = 1; j <= k; ++j) {
        printf("-->%d",p1->num);
        p1=p1->next;
    }
    return 0;
}
//creatlist用来建立一个带头结点的学生数据的单向链表,新的结点总是差在链表的末尾。链表的头指针作为函数值返回,
// 链表最后一个结点的next域放入NULL,作为链表的结束标志,学号为0不存入链表。
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student {
    long num;
    float score;
    struct student *next;
};
int n;
struct student* creatlist() {      //填空
    struct student* head ,*p1,*p2;
    p1=p2=(struct student *)malloc(LEN);
    scanf("%ld %f",&p1->num,&p1->score);
    head=NULL;
    n=0;
    while (p1->num!=0){
        n=n+1;
        if (n==1)
            head=p1;    //填空
        else
            p2->next=p1;  //填空
//        if (head==NULL)
//            head=p1;
//        else
//            p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN); //填空
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next=NULL;     //填空
    free(p1);
    return head;
}
int main(){
    struct student *pt;
    pt=creatlist();
    printf("\nnum:%ld\nscore:%5.1f\n",pt->num,pt->score);
}
//删除重复元素
#include <stdio.h>
#include <stdlib.h>    //填空
#include "string.h"
typedef struct node{
    int n;
    struct node *next;   //填空
}Node;
void show(Node *head){
    while (head){
        printf("%d ",head->n);
        head = head->next;
    }
    printf("\n");
}
Node *newNode(int n,Node *next){
    Node *node=(Node *)malloc(sizeof(Node));  //填空
    node->n=n;
    node->next=next;  //填空
    return node;
}
Node *newList(int a[],int n){
    int i;
    Node *head = NULL,*tail;
    for (int i = 0; i < n; ++i) {
        Node *t = newNode(a[i],NULL);     //填空
        if (head == NULL)   //填空
            head=t;      //填空
        else
            tail->next=t;
        tail = t;
    }
    return head;
}
Node *delete(Node *head){
    Node *p=head,*q;
    while (p && p->next){
        if (p->n != p->next->n)
            p=p->next;
        else
            p->next = p->next->next;     //填空
    }
    return head;
}
int main(){
    int data[]={1,1,2,2,3,3,4,4,5,5,6,6};
    int n = sizeof(data) / sizeof(int);
    Node *head = newList(data,n);     //填空
    head = delete(head);
    show(head);
    return 0;
}
//反转链表
#include <stdio.h>
#include <stdlib.h>
#define N 3
struct ListNode{
    int val;
    struct ListNode* next;
};
//双指针法
struct ListNode* reverseList(struct ListNode* head){
    //保存cur的下一个结点
    struct ListNode* temp;
    //pre指针指向前一个当前结点的前一个结点
    struct ListNode* pre = NULL;
    //用head代替cur,也可以再定义一个cur结点指向head。
    while(head) {
        //保存下一个结点的位置
        temp = head->next;
        //翻转操作
        head->next = pre;
        //更新结点
        pre = head;
        head = temp;
    }
    return pre;
}
struct ListNode* reverse(struct ListNode* pre, struct ListNode* cur) {
    if(!cur)
        return pre;
    struct ListNode* temp = cur->next;
    cur->next = pre;
    //将cur作为pre传入下一层
    //将temp作为cur传入下一层,改变其指针指向当前cur
    return reverse(cur, temp);
}
//两数相加
#include <stdio.h>
#include <malloc.h>
struct ListNode {
    int val;
    struct ListNode *next;
};
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    struct ListNode*head=NULL,*tail=NULL;
    int carry=0;
    while(l1||l2){
        int n1=l1?l1->val:0;
        int n2=l2?l2->val:0;
        int sum=n1+n2+carry;
        if(!head){
            head=tail=malloc(sizeof(struct ListNode));
            tail->val=sum%10;
            tail->next=NULL;
        }
        else{
            tail->next=malloc(sizeof(struct ListNode));
            tail->next->val=sum%10;
            tail=tail->next;
            tail->next=NULL;
        }
        carry=sum/10;
        if(l1){
            l1=l1->next;
        }
        if(l2){
            l2=l2->next;
        }
    }
    if(carry>0){
        tail->next=malloc(sizeof(struct ListNode));
        tail->next->val=carry;
        tail->next->next=NULL;
    }
    return head;

}
int main() {
    struct ListNode* node1= malloc(sizeof (struct ListNode));
    struct ListNode* node2= malloc(sizeof (struct ListNode));
    struct ListNode* node3= malloc(sizeof (struct ListNode));
    struct ListNode* node4= malloc(sizeof (struct ListNode));
    struct ListNode* node5= malloc(sizeof (struct ListNode));
    struct ListNode* node6= malloc(sizeof (struct ListNode));
    node1->val=2;
    node2->val=4;
    node3->val=3;
    node4->val=5;
    node5->val=6;
    node6->val=4;
    node1->next=node2;
    node2->next=node3;
    node3->next=NULL;

    node4->next=node5;
    node5->next=node6;
    node6->next=NULL;
    struct ListNode* newhead= addTwoNumbers(node1,node4);
    while (newhead){
        printf("%d ",newhead->val);
        newhead=newhead->next;
    }

    return 0;
}

//删除链表倒数第n个节点
#include <stdio.h>
#include <malloc.h>
struct ListNode {
    int val;
    struct ListNode *next;
};
 
int getLength(struct ListNode* head) {
    int length = 0;
    while (head) {
        ++length;
        head = head->next;
    }
    return length;
}
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    struct ListNode* dummy = malloc(sizeof(struct ListNode));
    dummy->val = 0, dummy->next = head; //虚拟头节点
    int length = getLength(head);
    struct ListNode* cur = dummy;
    for (int i = 1; i < length - n + 1; ++i) {
        cur = cur->next;
    }
    cur->next = cur->next->next;
    struct ListNode* ans = dummy->next;
    free(dummy);        //填空
    return ans;
}
 
int main() {
    struct ListNode* node1= malloc(sizeof (struct ListNode));
    struct ListNode* node2= malloc(sizeof (struct ListNode));
    struct ListNode* node3= malloc(sizeof (struct ListNode));
    struct ListNode* node4= malloc(sizeof (struct ListNode));
    struct ListNode* node5= malloc(sizeof (struct ListNode));
    node1->val=1;
    node2->val=2;
    node3->val=3;
    node4->val=4;
    node5->val=5;
    node1->next=node2;
    node2->next=node3;
    node3->next=node4;
    node4->next=node5;
    node5->next=NULL;
    struct ListNode* newhead= removeNthFromEnd(node1,2);
    while (newhead){
        printf("%d ",newhead->val);
        newhead=newhead->next;
    }
    return 0;
}
//合并两个有序链表
#include <stdio.h>
#include <malloc.h>
struct ListNode {
    int val;
    struct ListNode *next;
};
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    if(list1==NULL){
        return list2;
    }
    else if(list2==NULL){
        return list1;
    }
    struct ListNode* ps=(struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* temp=ps;
    while(list1!=NULL && list2!=NULL){
        if(list1->val <= list2->val){
            temp->next=list1;    //填空
            list1=list1->next;
        }
        else{
            temp->next=list2;
            list2=list2->next;  //填空
        }
        temp=temp->next;
    }
    if(list1!=NULL){
        temp->next=list1;
    }
    if(list2!=NULL){
        temp->next=list2;
    }
    return ps->next;
}
int main() {
    struct ListNode* node1= malloc(sizeof (struct ListNode));
    struct ListNode* node2= malloc(sizeof (struct ListNode));
    struct ListNode* node3= malloc(sizeof (struct ListNode));
    struct ListNode* node4= malloc(sizeof (struct ListNode));
    struct ListNode* node5= malloc(sizeof (struct ListNode));
    struct ListNode* node6= malloc(sizeof (struct ListNode));
    node1->val=1;
    node2->val=2;
    node3->val=4;

    node4->val=1;
    node5->val=3;
    node6->val=4;

    node1->next=node2;
    node2->next=node3;
    node3->next=NULL;

    node4->next=node5;
    node5->next=node6;
    node6->next=NULL;

    struct ListNode* newhead= mergeTwoLists(node1,node4);
    while (newhead){
        printf("%d ",newhead->val);
        newhead=newhead->next;
    }
    return 0;
}
//环形链表 快慢指针
#include <stdio.h>
#include <stdbool.h>
#include <malloc.h>
struct ListNode {
    int val;
    struct ListNode *next;
};
bool hasCycle(struct ListNode *head) {
    if(head==NULL || head->next==NULL){    //填空
        return false;
    }
    struct ListNode* slow=head;
    struct ListNode* fast=head->next;   //填空
    while(slow!=fast){
        if(fast==NULL || fast->next==NULL){
            return false;
        }
        slow=slow->next;
        fast=fast->next->next;      //填空
    }
    return true;
}
int main() {
    struct ListNode* node1= malloc(sizeof (struct ListNode));
    struct ListNode* node2= malloc(sizeof (struct ListNode));
    struct ListNode* node3= malloc(sizeof (struct ListNode));
    struct ListNode* node4= malloc(sizeof (struct ListNode));
    node1->val=3;
    node2->val=2;
    node3->val=0;
    node4->val=-4;
    node1->next=node2;
    node2->next=node3;
    node3->next=node4;
    node4->next=node2;
    printf("shifou you huan:%d\n", hasCycle(node1));
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值