链表课后练习题(基础)

//13人围成一圈,报到3退出,找到留在圈子中的人原来的序号
/*
#include
#include
#include
using namespace std;

#define len sizeof(struct student)

struct student
{
    int num;
    struct student *next;
};

struct student * creat(int n)
{
    struct student *head;
    struct student *p1, *p2;        //p1指向循环链表的尾部, p2指向将要插入的新操作点
    head = p1 = p2 = (struct student *)malloc(len);
    p1->num = 1;
    p1->next = head;
    while(p1->num < n){
        p2 = (struct student *)malloc(len);
        p2->num = p1->num+1;
        p1->next = p2;
        p2->next = head;
        p1 = p2;
    }
    return p1;      //返回尾指针
}


void print(struct student *p1)
{
    struct student *p;
    p = p1->next;       //p指向头指针
    while(p != p1){
        printf("%d ", p->num);
        p = p->next;
    }
    printf("%d\n", p1->num);
}


void del(struct student *p)
{
    struct student *tmp;
    tmp = p->next;
    while(tmp->next != p)
    {
        tmp = tmp->next;
    }
    tmp->next = p->next;
    free(p);
}

int main()
{
    int n, x;
    printf("请输入一共多少人:");
    scanf("%d", &n);
    printf("请输入报到序号几退出圈子:");
    scanf("%d", &x);
    struct student *l = creat(n);
    struct student *l1 = l->next;
    int i = 1;
    while(l1 != l1->next){
        struct student *l2 = l1;
        l1 = l1->next;
        if(i == 3){
            del(l2);
            i = 0;
        }
        else{
            l2 = NULL;
        }
        i++;
    }
    printf("剩余的人的序号为:%d\n", l1->num);
}
*/
/*
//实现链表的建立,输出,插入,删除
#include
#include
#include
using namespace std;

#define len sizeof(struct student)

struct student
{
    int num;
    int score;
    struct student *next;
};

struct student *creat()
{
    struct student *head;
    struct student *p1, *p2;
    head = p1 = p2 = (struct student *)malloc(len);
    scanf("%d %d", &p1->num, &p1->score);
    head = p1;
    while(p1->num != 0){
        p2 = (struct student *)malloc(len);
        scanf("%d %d", &p2->num, &p2->score);
        p1->next = p2;
        p2->next = NULL;
        p1 = p2;
    }
    return head;
}

void print(struct student *head)
{
    struct student *p;
    p = head;
    while(p->next != NULL){
        printf("学号:%d 成绩:%d\n", p->num, p->score);
        p = p->next;
    }
}

void del(struct student *p, struct student *head)
{
    struct student *tmp;
    tmp = head;
    while(tmp->next != p){
        tmp = tmp->next;
    }
    tmp->next = p->next;
    free(p);
}

void in(int n, struct student *head)
{
    struct student *p = head;
    struct student *tmp;
    tmp = (struct student *)malloc(len);
    scanf("%d %d", &tmp->num, &tmp->score);
    for(int i=2; inext;
    }
    tmp->next = p->next;
    p->next = tmp;

}

int main()
{
    int n, x;
    struct student *l = creat();
    struct student *l1 = l;
    print(l1);
    printf("请输入插入节点几:");
    scanf("%d", &n);
    in(n, l1);
    print(l1);
    printf("请输入删除节点几:");
    scanf("%d", &x);
    if(x == 1)
        print(l1->next);
    else{
        while(x--){
            l1 = l1->next;
        }
        del(l1, l);
        print(l);
    }
    return 0;
}
*/

//合并两个链表,按学号排序
/*
#include
#include
#include
using namespace std;

#define len sizeof(struct student)

struct student
{
    int num;
    int score;
    struct student *next;
};

struct student *creat()
{
    struct student *head;
    struct student *p1, *p2;
    head = p1 = p2 = (struct student *)malloc(len);
    scanf("%d %d", &p1->num, &p1->score);
    head = p1;
    while(p1->num != 0){
        p2 = (struct student *)malloc(len);
        scanf("%d %d", &p2->num, &p2->score);
        //cout<num<if (p2->num==0)
            break;
        p1->next = p2;
        p2->next = NULL;
        p1 = p2;
    }
    return head;
}

void print(struct student *head)
{
    struct student *p;
    p = head;
    while(p != NULL){
        printf("学号:%d 成绩:%d\n", p->num, p->score);
        p = p->next;
    }
}

struct student* join(struct student *head1, struct student *head2)
{
    struct student *t = head1;
    struct student *l = head1;
    struct student *g = head2;
    while(t->next != NULL){
        t = t->next;
    }
    t->next = g;            //链接两个链表
    int n = 1;
    while(l->next !=  NULL){
        n++;
        l = l->next;
    }
    //print(head1);
    for(int i=0; i*m = head1;
        struct student *n = m->next;
        if(m->num > n->num){
            m->next = n->next;
            n->next = m;
            head1 = n;
        }
        m = head1;
        while(m->next->next != NULL)
        {
            struct student *x = m->next;
            struct student *y = x->next;
            if(x->num > y->num){
                m->next = x->next;
                x->next = y->next;
                y->next = x;
            }
            m = m->next;
        }
    }
    return head1;
}

int main()
{
    struct student *p1 = creat();
    struct student *p2 = creat();
    struct student *p3 = join(p1, p2);
    print(p3);
    return 0;
}
2 3
4 5
5 55
7 9
0 0
1 9
2 6
111 93
0 0
*/

//删除a链表中和b相同学号的节点
#include
#include
#include
using namespace std;

#define len sizeof(struct student)

struct student
{
    int num;
    int score;
    struct student *next;
};

struct student *creat()
{
    struct student *head;
    struct student *p1, *p2;
    head = p1 = p2 = (struct student *)malloc(len);
    scanf("%d %d", &p1->num, &p1->score);
    head = p1;
    while(p1->num != 0){
        p2 = (struct student *)malloc(len);
        scanf("%d %d", &p2->num, &p2->score);
        //cout<num<if (p2->num==0)
            break;
        p1->next = p2;
        p2->next = NULL;
        p1 = p2;
    }
    return head;
}

void print(struct student *head)
{
    struct student *p;
    p = head;
    while(p != NULL){
        printf("学号:%d 成绩:%d\n", p->num, p->score);
        p = p->next;
    }
}

struct student* del(struct student *head1, struct student *head2)
{
    struct student *m = head1;
    while(m != NULL)
    {
        bool flag=false;
        struct student *n = head2;
        while(n != NULL)
        {
            if(m->num == n->num)
            {
                flag=true;
                if(m==head1)
                {
                    head1 = m->next;
                }
                else
                {
                    struct student *tmp;
                    tmp = head1;
                    while(tmp->next != m){
                        tmp = tmp->next;
                    }
                    tmp->next = m->next;
                }
            }
            n = n->next;
        }
        struct student * p1=m;
        m = m->next;
        if(flag)
        {
            free(p1);
        }
    }
    return head1;
}

int main()
{
    struct student *p1 = creat();
    struct student *p2 = creat();
    struct student *p3 = del(p1, p2);
    print(p3);
    return 0;
}

你可能感兴趣的:(c程序设计课后作业)