一只老甲鱼 发表于 2020-12-23 17:57:33

求助,单链表c语言的题,

题目
用单链表存储键盘输入的整数。对输入的整数进行降序排序(冒泡),排序完成后输出链表中的所有数据。实现插入的功能。每次从键盘输入一一个整数,将这个整数插入链表中。要求插入以后仍然保持链表中的整数处于降序排列的状态,需要在插入完成后打印链表中的所有数据。

一世轻尘 发表于 2020-12-23 23:01:04

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>//头文件
#define LEN sizeof(struct data)//student结构的大小
struct data *creat();//创建链表
void output(struct data *head);//打印链表
void sort(struct data *head);//排序
struct data *insert(struct data *,struct data *);//插入结点
struct data *remove(struct data*head,int n);//删除节点
int m;
struct data{
        int n;
        struct data *next;
}; //定义结构数组
int N;//全局变量,用来记录存放了多少数据。
int main()//主函数
{
        int remove_n;//定义存放需要删除的数值的变量·
        struct data *stu,node;//声明结构指针变量
        stu=creat();//函数返回链表第一个节点的地址
        output(stu);//打印输出链表
        printf("\n");//换行
        system("pause");
        sort(stu);//排序
    printf("The result of ascending sorting:\n");
        output(stu);//升序输出
        printf("\nPlease enter the number to insert\n");
        scanf("%d",&node.n);
        stu=insert(stu,&node);
        output(stu);
        printf("\nPlease enter the number to delete\n");
        scanf("%d",&remove_n);
        stu=remove(stu,remove_n);
        output(stu);
        return 0;
}
struct data *creat()//创建链表函数
{
        struct data *head;
        struct data *p1,*p2;
        p1=p2=(struct data*)malloc(LEN);//开辟一个长度为LEN的内存区
    p1->n=0;
        head=NULL;//初始化头指针
        N=0;//初始化结点个数
        printf("Stop typing when -1 is entered\n");
        while(p1->n!=-1)
        {
                N++;
                if(1==N)
                {
                        head=p1;
                }
                else
                {
                        p2->next=p1;//把p1所指的结点连接在p2所指的结点后面
                }
                p2=p1;
                p1=(struct data*)malloc(LEN);
                printf("Please enter a number:");
                scanf("%d",&p1->n);
        }
        p2->next=NULL;//结束
        return head;//使函数返回头指针
}
void output(struct data *head)//定义打印输出链表函数
{
        struct data *p;//在函数中定义struct data类型的变量p
        p=head->next;//使p指向头结点后第一个结点
        if(NULL!=head)//若不是空表
        {
                do
                {
                        printf("%d ",p->n);//输出一个结点中的数值
                        p=p->next;//p指向下一个结点
                }while(p!=NULL);//当p不是空地址
        }
}
void sort(struct data *head)//定义排序函数
{
        struct data *p,*q,*r,*t;
        int flag = 1;
        while(flag){
                p = head;
                q = head->next;
                flag = 0;

                while(q != NULL){//循环终止条件
                        r = q->next;
                        if(r == NULL){//循环终止条件
                                break;
                        }else if(q->n > r->n){
                                t = r->next;
                                q->next = t;
                                r->next = q;
                                p->next = r;//重新排序
                                p = r;
                                q = p->next;
                                r = q->next;//指向整体后移
                                flag = 1;
                        }else{
                                p = q;
                                q = p->next;//指向整体后移便于接下来排序
                        }
                }
        }
}
struct data *insert(struct data *head,struct data *stud)//定义插入结点函数
{
        struct data *p0,*p1,*p2;
        p1=head;
        p0=stud;
        if(head==NULL)
        {
                head=p0;p0->next=NULL;
        }
        else
        {
                while((p0->n>p1->n)&&(p1->next!=NULL)){
                        p2=p1;
                        p1=p1->next;
                }
                if(p0->n<=p1->n)
                {
                        if(head==p1)
                        head=p0;
                        else
                        p2->next=p0;
                        p0->next=p1;
                }
                else
                {p1->next=p0;p0->next=NULL;
                }
        }
        m=m+1;
        return(head);
}
struct data *remove(struct data*head,int n)//定义删除结点函数
{
        struct data *p1,*p2;
        if(head==NULL)
        {
                printf("\nlist null! \n");
                return(head);
        }
        p1=head;
        while(n!=p1->n&&p1->next!=NULL)
        {
                p2=p1;p1=p1->next;
        }
        if(n==p1->n)
        {
                if(p1==head)head=p1->next;
                else p2->next=p1->next;
                printf("delete:%d\n",n);
                n=n-1;
        }
        else printf("%d not been found!\n",n);
        return(head);
}升序

一世轻尘 发表于 2020-12-23 23:01:34

结果

不知名的靓仔 发表于 2020-12-23 23:07:42

牛逼,大神真多
页: [1]
查看完整版本: 求助,单链表c语言的题,