SEx4-链表 参考思路及代码

6-1 建立学生信息链表

本题要求实现一个将输入的学生成绩组织成单向链表的简单函数。

参考代码:

void input() {
    int num,score;
    head = (struct stud_node*)malloc(sizeof(struct stud_node));
    struct stud_node *p = head;
    //注意分开读取数据
    while(scanf("%d",&num)!=EOF&&num!=0) {
        tail = (struct stud_node*)malloc(sizeof(struct stud_node));
        scanf("%s%d", tail->name, &tail->score);
        tail->num = num;
        p->next = tail;
        p = p->next;
    }
    head = head->next;
}

6-2 学生成绩链表处理

本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。

参考代码:

struct stud_node *createlist(){
    int num,score;

    struct stud_node *head = (struct stud_node*)malloc(sizeof(struct stud_node));
    struct stud_node *p = head, *tail;

    while(scanf("%d",&num)!=EOF&&num) {
        tail = (struct stud_node*)malloc(sizeof(struct stud_node));
        scanf("%s%d", tail->name, &tail->score);
        tail->num = num;
        tail->next = NULL;
        p->next = tail;
        p = p->next;
    }
    head = head->next;
    return head;
}
struct stud_node *deletelist(struct stud_node *head, int min_score) {
    struct stud_node *p, *q;
    p = head;
    while(head&&head->score<min_score) {
        p = head->next;
        free(head);
        head = p;
    }
    if(NULL == head) return NULL;
    while(p->next) {
        if(p->next->score<min_score) {
            q = p->next;
            p->next=q->next;
            free(q);
        }else {
            p = p->next;
        }
    }
    return head;
};

6-3 逆序数据建立链表

本题要求实现一个函数,按输入数据的逆序建立一个链表。

参考代码:

struct ListNode *createlist() {
    struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode)), *p, *tail=NULL;
    int num = 0;
    while(scanf("%d",&num)!=EOF&&num!=-1) {
		//每次将读取到的元素插入到头节点的下一位,如果下一位村
        p = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->data = num;
        if(NULL==tail) {
            tail = p;
            tail->next = NULL;
            head->next = tail;
        }
        else {
            head->next = p;
            p->next = tail;
            tail = p;
        }
    }
    return head->next;
};

6-4 链表拼接

本题要求实现一个合并两个有序链表的简单函数。

参考代码及思路

//可利用归并排序的merge操作思想,融合两个有序链表
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2){
    struct ListNode *p1=list1, *p2=list2, *q;
    struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode)), *head_p=head;
    while(p1&&p2) {
        if(p1->data<p2->data) {
            head_p->next = p1;
            head_p = head_p->next;
            p1 = p1->next;
        }
        else {
            head_p->next = p2;
            head_p = head_p->next;
            p2 = p2->next;
        }
    }
    while(p1) {
        head_p->next = p1;
        head_p = head_p->next;
        p1 = p1->next;
    }
    while(p2) {
        head_p->next = p2;
        head_p = head_p->next;
        p2 = p2->next;
    }
    return head->next;
}

6-5 奇数值结点链表

本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中奇数值的结点重新组成一个新的链表。

struct ListNode *readlist() {
    int data;
    struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode)), *q = head;
    while(scanf("%d",&data)!=EOF&&data!=-1) {
        struct ListNode *p = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->data = data;
        p->next = NULL;
        q->next = p;
        q = q->next;
    }
    return head->next;
}
struct ListNode *getodd( struct ListNode **L ) {
    struct ListNode *p = *L, *odd_head, *odd_p, *odd_q;
    odd_head = (struct ListNode*)malloc(sizeof(struct ListNode));
    odd_p = odd_head;
    while(*L&&(*L)->data&1) {
        odd_p->next = *L;
        odd_p = odd_p->next;
        *L = (*L)->next;
    }
    if((*L)==NULL) return odd_head->next;
    p = *L;
    while(p->next) {
        if(p->next->data&1) {
            odd_p->next = p->next;
            odd_p = odd_p->next;
            p->next = odd_p->next;
            odd_p->next = NULL;
        }
        else {
            p = p->next;
        }
    }
    return odd_head->next;
}

6-6 单链表结点删除

本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中所有存储了某给定值的结点删除。

struct ListNode *readlist() {
    int data;
    struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode)), *q = head;
    while(scanf("%d",&data)!=EOF&&data!=-1) {
        struct ListNode *p = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->data = data;
        p->next = NULL;
        q->next = p;
        q = q->next;
    }
    return head->next;
}
struct ListNode *deletem( struct ListNode *L, int m ) {
    struct ListNode *p = L,*q;
    while(p&&(p->data==m)) {
        q = p;
        p = q->next;
        free(q);
    }
    struct ListNode *head = p;
    if(head == NULL) return NULL;
    while(p->next) {
        if(p->next->data==m) {
            q = p->next;
            p->next = q->next;
            free(q);
        }
        else p = p->next;
    }
    return head;
}

6-7 链表逆置

本题要求实现一个函数,将给定单向链表逆置,即表头置为表尾,表尾置为表头。

struct ListNode *reverse( struct ListNode *head ) {
    struct ListNode *p,*q=NULL,*r=NULL;
    if(head==NULL) {
        return NULL;
    }
    if(head->next==NULL) {
        return head;
    }
    p=head;
    while(p) {
        q = p->next;	//用q指向p后面的一个节点
        p->next = r;	//把q指向的那个节点想在转换成指向它前面的那个节点,这个时候就实现了逆序,而且是就地逆序
        r = p;			//r向后移动到p的位置
        p = q;			//sp向后移动到q的位置,这时候完成了第一步的置序,后置程序依次循环,直到p到达结尾
    }
    return r;
}

6-8 统计专业人数

本题要求实现一个函数,统计学生学号链表中专业为计算机的学生人数。

int countcs( struct ListNode *head ){
    int cnt = 0;
    struct ListNode *p = head;
    while(p) {
        if(p->code[1]=='0'&&p->code[2]=='2')
            cnt++;
        p = p->next;
    }
    return cnt;
}

6-9 删除单链表偶数节点

本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。

struct ListNode *createlist() {
    int data;
    struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode)), *q = head;
    while(scanf("%d",&data)!=EOF&&data!=-1) {
        struct ListNode *p = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->data = data;
        p->next = NULL;
        q->next = p;
        q = q->next;
    }
    return head->next;
}
struct ListNode *deleteeven( struct ListNode *head ) {
    struct ListNode *p = head,*q;
    while(p&&!(p->data&1)) {
        q = p;
        p = q->next;
        free(q);
    }
    head = p;
    if(head == NULL) return NULL;
    while(p->next) {
        if(!(p->next->data&1)) {
            q = p->next;
            p->next = q->next;
            free(q);
        }
        else p = p->next;
    }
    return head;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值