递增整数序列链表插入整数使有序性不变

使用的是带头结点的链表

#include
#include

typedef struct LNode *PtrToLNode;
struct LNode{
	int data;
	PtrToLNode Next;
}; 
typedef PtrToLNode List;

List Create( ){
	List head,tail,temp,L;
	L = (List)malloc(sizeof(struct LNode));
	int m,n;
	head = tail = NULL;
	scanf("%d",&n);
	while(n--){
		scanf("%d",&m);
		temp = (List)malloc(sizeof(struct LNode));
		temp->data = m;
		temp->Next = NULL;
		if(head==NULL)
			head = temp;
		else
			tail->Next = temp;
		tail = temp;
	}
	L->Next = head;
	return L;
}
int main(void){
	List L;
	L = Create( );
	List p = L,q = L;
	int x;
	p = p->Next;
	while(p){
		printf("%d ",p->data);
		p = p->Next;
	}
	printf("\n");
	scanf("%d",&x);
	List temp = (List)malloc(sizeof(struct LNode));
	temp->data = x;
	temp->Next = NULL;

	if(L->Next==NULL){
		L->Next = temp;
		return L;
	}
	while(q->Next&&q->Next->data<x){
		q = q->Next;
	}
	temp->Next = q->Next;
	q->Next = temp;
	L = L->Next;
	while(L){
		printf("%d ",L->data);
		L = L->Next;
	}
	return 0;
}

你可能感兴趣的:(C语言练习题,链表)