纠错,求大佬
  • 板块学术版
  • 楼主月下纯春
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/10/10 21:29
  • 上次更新2023/11/2 14:34:50
查看原帖
纠错,求大佬
52153
月下纯春楼主2023/10/10 21:29

题目 从文件读入N个int型整数,将它们按照从小到大的顺序存放在一个链表中,并显示。要求每输入一个数据,即将它插入到链表中,并维护链表的有序性。建立链表、插入链表,显示链表等操作应使用单独的函数完成。 【输入形式】 第一行一个整数n(n<100) 第二行n个整数 【输出形式】n行,展现每次插入后链表的状态,如样例所示 【样例输入】 5 1 3 9 4 3 【样例输出】 1 1 3 1 3 9 1 3 4 9 1 3 3 4 9

代码


#include<stdio.h>

#include<string.h>

#include<math.h>

#include<stdlib.h>

int s;

typedef struct node{
	int x;
	struct node* next;
}Node;

void creat(Node *head, int n);
void print(Node *p);
void delet(Node *c);
Node* insort(Node *head);

int main()
{
	Node *head=new Node;
	head->next=NULL; 
	scanf("%d",&s);
    creat(head,s);
    insort(head);
    print(head);
    delet(head);
    return 0;
}

void creat(Node *head,int n)
{
	Node *c=head;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&c->x);
		c->next=new Node;
		c=c->next;
		c->next=NULL;
		
	}
}


void print(Node *p)
{
	while(p->next!=NULL)
	{
		printf("%d ",p->x);
		p=p->next;
	}
}



void delet(Node *c)
{
	Node *t=NULL;
	while(c!=NULL)
	{
		t=c;
		c=c->next;
		delete t;
	}
}


Node* insort(Node *head)
{
	/*if(head==NULL||head->next==NULL)
	{
		return head;
	}*/
	
	Node *c=head;
	Node *fh=new Node;
	fh->x=0;
	fh->next=head;
	Node *pc=fh;
	Node *t=NULL;
	while(c->next!=NULL)
	{
		if(pc->x<=c->x)
        {
            pc=c ;
            c=c->next ;
        }
        else
        {
        	t=c;
        	pc->next=c->next;
        	c=fh;
        	while(t->x>=c->next->x)
        	{
        		c=c->next;
			}
			t->next=c->next;
			c->next=t;
			c=pc->next;
		}
	}
	head=fh->next;
	delete fh;
	return head;
}
2023/10/10 21:29
加载中...