设计一个算法,判断单链表L是否是递增的。实现这个算法,并完成测试。
head.h
#ifndef HEAD_H_INCLUDED
#define HEAD_H_INCLUDED
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LinkList;
void CreateListR(LinkList *&L,ElemType a[],int n);
void DispList(LinkList *L);
int LinkJudgement(LinkList *L);
#endif
fun.cpp
#include"head.h"
void CreateListR(LinkList *&L,ElemType a[],int n)
{
LinkList *s,*r;
int i;
L=(LinkList *)malloc(sizeof(LinkList));
r=L;
for(i=0;i<n;i++)
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void DispList(LinkList *L)
{
LinkList *p=L->next;
while(p!=NULL)
{
printf("%d “,p->data);
p=p->next;
}
printf(”\n");
}
int LinkJudgement(LinkList *L)
{
LinkList *p=L->next;
int flag=1;
while(p->next!=NULL)
{
if(p->datanext->data)
p=p->next;
else
{
flag=0;
break;
}
}
return flag;
}
main.cpp
#include
#include"head.h"
using namespace std;
int main()
{
ElemType a[1000];
LinkList *L;
int i,n;
printf("请输入单链表L长度:\n");
scanf("%d",&n);
printf("请输入单链表L各节点元素:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
CreateListR(L,a,n);
printf("单链表L各节点为:\n");
DispList(L);
if(LinkJudgement(L))
printf("该单链表是递增的!\n");
else
printf("该单链表不是递增的!\n");
return 0;
}