C语言程序设计第四版(何钦铭、颜晖)第十一章指针进阶之奇数值结点链表

  1. 奇数值结点链表:输入若干个正整数(输入-1为结束标志)建立一个单向链表,头指针为L,将链表L中奇数值的结点重新组成一个新的链表NEW,并输出新建链表的信息。试编写相应程序。

第一种方案:

复制代码
#include<stdio.h>
#include<stdlib.h>

struct num_node {
    int x;
    struct num_node *next;
};

int main() {
    int a;
    struct num_node *L = NULL, *NEW = NULL, *p = NULL, *s = NULL, *q = NULL;
    
    // 输入链表
    scanf("%d", &a);
    while(a != -1) {
        p = (struct num_node*)malloc(sizeof(struct num_node));
        p->x = a;
        p->next = NULL;
        if(L == NULL) {
            L = p;
            s = L;
        } else {
            s->next = p;
            s = p;
        }
        scanf("%d", &a);
    }
    
    // 从原链表中提取奇数节点,组成新链表
    p = L;
    q = NULL;  // q 用于追踪新链表的尾节点
    while(p != NULL) {
        if(p->x % 2 != 0) {  // 如果是奇数
            s = (struct num_node*)malloc(sizeof(struct num_node));
            s->x = p->x;
            s->next = NULL;
            
            if(NEW == NULL) {
                NEW = s;
                q = NEW;
            } else {
                q->next = s;
                q = s;
            }
        }
        p = p->next;  // 关键:移动原链表指针
    }
    
    // 输出新链表(只含奇数)
    q = NEW;
    while(q != NULL) {
        printf("%d ", q->x);
        q = q->next;
    }
    printf("\n");
    
    // 释放内存(可选,但好习惯)
    // 释放原链表
    p = L;
    while(p != NULL) {
        struct num_node* temp = p;
        p = p->next;
        free(temp);
    }
    // 释放新链表
    q = NEW;
    while(q != NULL) {
        struct num_node* temp = q;
        q = q->next;
        free(temp);
    }
    
    return 0;
}

输入样例:1 2 3 4 5 6 7 8 9 -1

输出结果:1 3 5 7 9

第二种方案:

复制代码
#include<stdio.h>
#include<stdlib.h> 

struct num_node{
    int x;
    struct num_node *next;
};

struct num_node *createList(struct num_node *head);
struct num_node *Insert_node(struct num_node *head,struct num_node *p);
struct num_node *Select_node(struct num_node *head);
void print(struct num_node *head);
void free_node(struct num_node *head);


int main()
{
    struct num_node *L=createList(NULL),*NEW=Select_node(L);
    print(NEW);
    free_node(L);
    free_node(NEW);
	return 0;
}


//创建链表
struct num_node *createList(struct num_node *head){
    int a;
    struct num_node *p=NULL;
    scanf("%d",&a);
    while(a!=-1){
        p=(struct num_node*)malloc(sizeof(struct num_node));
        p->x=a;
        p->next=NULL;
        head=Insert_node(head,p);
        scanf("%d",&a);
    }
    return head;
}


//插入结点
struct num_node *Insert_node(struct num_node *head,struct num_node *p){
    struct num_node *s=head;
    if(head==NULL){
        head=p;
        s=head;
    }
    else{
        while(s->next!=NULL){
            s=s->next;
        }
        s->next=p;
        s=p;
    }
    return head;
}


//摘取奇数结点形成新的链表
struct num_node *Select_node(struct num_node *head){
    struct num_node *NEW=NULL,*p=head,*s=NULL,*q=NULL;
    while(p!=NULL){
        if(p->x%2!=0){
            s=(struct num_node *)malloc(sizeof(struct num_node));
            s->x=p->x;
            s->next=NULL;
            if(NEW==NULL){
                NEW=s;
                q=NEW;
            }
            else{
                q->next=s;
                q=s;
            }
        }
        p=p->next;
    }
    return NEW;
}


//打印链表
void print(struct num_node *head){
    struct num_node *q=head;
    while(q!=NULL){
        printf("%d ",q->x);
        q=q->next;
    }
}


//释放结点
void free_node(struct num_node *head){
    struct num_node *q=head,*temp=NULL;
    while(q!=NULL){
        temp=q;
        q=q->next;
        free(temp);
    }
}

输入样例:1 2 3 4 5 6 7 8 9 11 13 -1

输出结果:1 3 5 7 9 11 13

相关推荐
红宝村村长38 分钟前
windows笔记本双系统ubuntu设置
开发语言
菜鸟~noob2332 小时前
【电子战】第15篇:混合定位——TDOA/FDOA/AOA 联合【含matlab代码】
开发语言·matlab
云泽8083 小时前
STM32 USART 详解(八):C I/O 串口重定向原理与实现
c语言·stm32·嵌入式硬件
ThornArmor3 小时前
重铸1996|肉体渲染:关节的裂缝:分段模型积木拼装与未成熟的 RSP 矩阵骨骼动画形变
c语言·汇编·c++·python·硬件架构·游戏机
T1mzhou3 小时前
ARM64 Linux 6.10 内核驱动(12):dd.c:probe/ remove、延迟 probe
linux·服务器·c语言·arm开发·arm
Dreams_l3 小时前
死信队列和延迟队列介绍
java·开发语言
Zenova EdgeOS4 小时前
Python 工业边缘 redis-py 异步实战
开发语言·redis·python
君顾14 小时前
西安 24 小时自助健身房系统开发实战指南
java·开发语言·健身房
白色的北极熊4 小时前
c语言 scanf 输入流有空格,逗号 说明
c语言
Logic1015 小时前
C语言/数据结构位运算题解:异或XOR找出货船中的“独特载货量“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质