链表(线性表)

Problem B: 链表(线性表)

 

Description

(线性表)设有一个正整数序列组成的有序单链表(按递增次序有序,且允许有相等的整数存在),试编写能实现下列功能的算法 :(要求用最少的时间和最小的空间)
(1) 确定在序列中比正整数x大的数有几个(相同的数只计算一次);
(2) 在单链表将比正整数x小的数按递减次序排列;

Input

Sample Output

输入长度:13

输入数据:4 5 7 7 8 10 11 15 15 16 17 20 20

输入x:10

 

Output

5

8 7 7 5 4

Sample Input

7
1 2 3 4 5 6 6
4

Sample Output

2
3 2 1 

HINT

#include <iostream>
#define NULL 0
using namespace std; 
struct student
{
	int num;
	student *next;
};
int n;


student *creat(int m)
{
	student *head,*p1,*p2;
	p1=p2=new student;
	cin>>p1->num;
	head=NULL;
	n=0;
	while(p1->num!=NULL)
	{
		n=n+1;
		if(n==1)head=p1;
		else p2->next=p1;
		p2=p1;
		p1=new student;
		if(n==m)
		{break;}
		else
			cin>>p1->num;
	

	}
	p2->next=NULL;
	return(head);
}


void print(student *p,student *stud)//递归输出
{
	student *p0;
	p0=stud;
	if(p==NULL)
		return ;
	print(p->next,stud);
	if(p0->num>p->num)//只有当p->num比p0->num小时才输出
	cout<<p->num<<" ";
	
}
student *intset(student *head,student *stud)
{
	int sum=0;
	student *p0,*p1,*p2;
	p1=head;p2=head;
	p0=stud;
	if(head!=NULL)
		do
		{
			if(p0->num<p1->num&&p1->num!=p2->num)
			{	
				sum++;
			}
			p1=p1->next;
			p2=p1->next;
		}while(p2!=NULL);//由p2判断结束位置
	
	cout<<sum+1<<endl;
	return (head);
}

int main()
{
	int m,num;
	cin>>m;
	student *head,*stud;
	student *creat(int);
	void print(student *,student *);
	student *intset(student *,student *);
	head=creat(m);
	stud=new student;
	cin>>stud->num;
	intset(head,stud);
	print(head,stud);
	return 0;
}





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值