鱼C论坛

 找回密码
 立即注册
查看: 2117|回复: 2

单链表

[复制链接]
发表于 2020-4-11 11:06:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
建立一个单链表,并将其输出?
出现的问题是:只能输入一个节点的数据,不能全部输入
#include<stdio.h>
#include<stdlib.h>
#define n 5

struct student{
        int num;     
        char name[10];
        char sex[10];
        int  age;
        struct student *next;
};
struct student *jianli(struct student *head)
{        
        struct student *p,*q;
        head=(struct student *)malloc(sizeof(struct student));
        p=head=NULL;
        int i;
        
        for(i=0;i<n;i++)
        {
                q=(struct student *)malloc(sizeof(struct student));
                q->next=NULL;
                printf("请输入学号,姓名,性别,年龄\n");
                scanf("%d %s %s %d",&(q->num),q->name,q->sex,&(q->age));
                q->next=p->next;
                p->next=q;
                p=q;
        }
        p->next=NULL;
        return head;
}

struct student *shuchu(struct student *head)
{
        struct student *p,*q;
        int x;
        p=head;
        q=p->next;
        printf("请输入一个年龄");
        scanf("%d",&x); 
        while(q!=NULL)
        {
                if(q->age==x)
                        p->next=q->next;
                else 
                        {
                                p=q;
                                q=q->next;
                        }        
        }
        
        return head;
}


int main()
{        struct student *head,*p,*q;
        jianli(head);
        
        shuchu(head);
        q=head->next;
        while(q!=NULL)
        {
                printf("%d %10s %10s %d\n",q->num,q->name,q->sex,q->age);
                q=q->next;
        }
        
} 
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-16 08:46:55 | 显示全部楼层
你这个是循环输入,每次当然只能输入一组。还有一个问题你没有释放内存
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-16 16:54:50 | 显示全部楼层
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#define n 5

struct student{
        int num;     
        struct student *next;
};
struct student *jianli(struct student *head)
{        
        struct student *p,*q;
        for(int i=0;i<n;i++)
        {
                q=(struct student *)malloc(sizeof(struct student));
                q->next=NULL;
                printf("请输入学号,姓名,性别,年龄\n");
                scanf("%d",&(q->num));
               
                if (head == NULL)
                {
                        head  = q;
                        p = head;                //用作数据链接
                }
                else
                {
                        p->next = q;
                        p = q;
                }
        }
        return head;
}

void shuchu(struct student *head)
{
        /*struct student *p,*q;
        int x;
        p=head;
        q=p->next;
        printf("请输入一个年龄");
        scanf("%d",&x);
        while(q!=NULL)
        {
                if(q->age==x)
                        p->next=q->next;
                else
                {
                        p=q;
                        q=q->next;
                }        
        }*/
        struct student *p;
        while(head!=NULL)
        {
                p = head;
                printf("%d\n",head->num);
                head = head->next;
                free(p);        //
                p = nullptr;
        }
}


int main()
{      
        struct student *head = NULL,*q = NULL;
        head = jianli(head);

        shuchu(head);
       
        system("pause");

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 12:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表