有序链表的插入(PTA)

已知一个递增有序链表L(带头结点,元素为整数),编写程序将一个新的整数差入到L中,并保持
L的有序性。其中单链表的类型定义参考如下:
typedef struct elementType;
typedef struct Inode{
elementType data;
struct Inode *next;
}Lnode,*LinkList;
输入格式
输入分三行
第一行 元素个数
第二行 元素的值,元素间用空格分隔
第三行 待插入的元素值
输出格式
在一行中输出有序链表元素值,每个元素前输出一个空格以便于相邻元素分隔。
输出样例
5
1 3 5 7 9
输出样例
1 3 4 5 7 9

#include<iostream>
#include<stdlib>
using namespace std;
typedef int elemetType;
typedef struct Inode
{
	elemetType data;
	struct Inode *next;
}Lnode,*LinkList;
LinkList Init();
void Insert(LinkList L,int a);
void Attach(LinkList *p,int a);
int main()
{
	LinkList L,P;
	int n,a,b;
	cin>>n;
	L=Init();
	P=L;
	for(int i=0;i<n;i++)
	{
		cin>>a;
		Attach(&p,a);
	}
	cin>>a;
	Insert(L,b);
	while(L->next!=NULL)
	{
		cout<<L->next->data;
		L=L->next;
	}
}
LinkList Init()
{
	LinkList p;
	p=(LinkList)malloc(sizeof(Lnode));
	p->next=NULL;
	return p;
}
void Insert()
{
	LinkList p;
	while(L!=NULL)
	{
		if(a<L->next->data)
		{	
			p=(LinkList)malloc(sizeof(LinkList));
			p->data=a;
			p->next=L->next;
			L->next=p;
			return ;
		}
		L=L->next;
	}
}




猜你喜欢

转载自blog.csdn.net/weixin_43843978/article/details/88113877