0
点赞
收藏
分享

微信扫一扫

链表(线性表)


Problem C: 链表(线性表)

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 522  Solved: 376
[Submit][Status][Web Board]

Description

(线性表)设有一个正整数序列组成的有序单链表(按递增次序有序,且允许有相等的整数存在),试编写能实现下列功能的算法 :(要求用最少的时间和最小的空间)

(1)确定在序列中比正整数x大的数有几个(相同的数只计算一次);

(2) 在单链表将比正整数x小的数按递减次序排列;

Input

输入长度: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

 

[Submit][Status][Web Board]

한국어 中文 فارسی English ไทย Anything about the Problems, Please Contact Admin:admin
All Copyright Reserved 2010-2014 HUSTOJ TEAM
GPL2.0 2003-2014 HUSTOJ Project TEAM
Help Maunal

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
using namespace std ;

typedef struct node{
	int data ;
	struct node *next ;
	struct node *prior ;	
	
}Node;
 Node *createlist(Node *head,int n)                //头插法建立链表
{
    Node *p;
    int i ;
    p=head=(Node *)malloc(sizeof(Node));
    head=NULL;                                     
    p=(Node *)malloc(sizeof(Node));            //p建立新结点
   for(i=0 ;i<n ;i++)                      //将新结点插到开头的位置
    {
        /***************/
           // 添加代码
        /*****************/
     cin>>p->data ;
     	if(head==NULL)
     	{
     		 head = p ;
     		 head->next =NULL;
		 }
		
		 else
		 {
		 	p->next =head;
		}   
        head= p;
        
        
        p=(Node*)malloc(sizeof(Node));         //p每次建立新结点
    }
    return head;
}
Node *listsort(Node *head,int item,int &m,int n)
{
	Node *p =head;
	
	Node *pr =head->next;
	int num = 0 ;
	while(p->next &&pr->next)
	{
		if(p->data>item)
		{
			if(p->data !=pr->data)
			num++;
		}
		
		
		p=p->next ;
		pr=pr->next ;		
	}
	m = num ;
	p=head;
	while(p->data>item &&p->next)
	{
		if(p->next->data<item)
		{
			pr = p ;
			break ;
		}					
		p=p->next ;
		
	}
	pr->next =NULL;
	head =p->next;
	m = num;
	
	
	return head;
	
 } 

void showlist(Node *head)
{
	Node *p=head; 
	while(p)
	{
		cout<<p->data<<" ";
		p=p->next ;
	}
	
}
int main()
{
	int n ,m ;
	int item ;
	Node *head=NULL;
	cin>>n ;
head = createlist(head, n);
cin>>item ;
	head = listsort(head,item,m,n);
	cout<<m<<endl;
	showlist(head) ;
	
	
	
	return 0 ;
}

 

举报

相关推荐

0 条评论