用递归的方法创建单链表(整形数字)
并且判断单链表是否递增有序
求单链表的最大值
以上三个小问题都要求用递归算法实现,C(c++)高手帮个忙啊
单链表递归算法
答案:1 悬赏:70 手机版
解决时间 2021-04-05 14:12
- 提问者网友:练爱
- 2021-04-05 03:45
最佳答案
- 五星知识达人网友:傲气稳了全场
- 2020-06-02 18:08
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//结点
struct Node
{
int data;
Node *pNext;
};
//递归创建单链表
int MaxNodeNumber = 10;
void CreateList(Node **pWork)
{
static int Count = 0;
(*pWork) = new Node();
(*pWork)->data = rand();//使用随机数 判断非递增
//(*pWork)->data = Count;//使用序号 判断递增
(*pWork)->pNext = NULL;
Count++;
if ( Count < MaxNodeNumber )
{
CreateList(&((*pWork)->pNext));
}
return;
}
//输出链表
void PrintList(Node *pWork)
{
printf("Head->");
while ( pWork != NULL )
{
printf("[%d]->",pWork->data);
pWork = pWork->pNext;
}
printf("NULL\n");
}
//递归删除单链表
void DestroyList(Node *pWork)
{
if ( NULL != pWork )
{
if ( pWork->pNext != NULL )
{
DestroyList(pWork->pNext);
}
delete pWork;
pWork = NULL;
}
}
//递归判断单链表是否递增有序
bool TestIsAscending(Node *pWork)
{
//中间结点
if ( pWork != NULL && pWork->pNext != NULL)
{
bool Current = pWork->data < pWork->pNext->data;
return Current && TestIsAscending(pWork->pNext);
}
else
{
return true;
}
}
//递归求最大值
int GetMaxValue(Node *pWork)
{
if ( pWork != NULL && pWork->pNext != NULL )
{
int MaxNextValue = GetMaxValue(pWork->pNext);
if ( pWork->data > MaxNextValue )
{
return pWork->data;
}
else
{
return MaxNextValue;
}
}
else if ( pWork->pNext == NULL )
{
return pWork->data;
}
}
void main(void)
{
Node *pHead = NULL;
srand(time(NULL));
CreateList(&pHead);
PrintList(pHead);
if ( TestIsAscending(pHead) )
{
printf("是递增\n");
}
else
{
printf("不是递增\n");
}
printf("最大值为 = %d\n",GetMaxValue(pHead));
DestroyList(pHead);
fflush(stdin);
getchar();
}
#include <stdlib.h>
#include <time.h>
//结点
struct Node
{
int data;
Node *pNext;
};
//递归创建单链表
int MaxNodeNumber = 10;
void CreateList(Node **pWork)
{
static int Count = 0;
(*pWork) = new Node();
(*pWork)->data = rand();//使用随机数 判断非递增
//(*pWork)->data = Count;//使用序号 判断递增
(*pWork)->pNext = NULL;
Count++;
if ( Count < MaxNodeNumber )
{
CreateList(&((*pWork)->pNext));
}
return;
}
//输出链表
void PrintList(Node *pWork)
{
printf("Head->");
while ( pWork != NULL )
{
printf("[%d]->",pWork->data);
pWork = pWork->pNext;
}
printf("NULL\n");
}
//递归删除单链表
void DestroyList(Node *pWork)
{
if ( NULL != pWork )
{
if ( pWork->pNext != NULL )
{
DestroyList(pWork->pNext);
}
delete pWork;
pWork = NULL;
}
}
//递归判断单链表是否递增有序
bool TestIsAscending(Node *pWork)
{
//中间结点
if ( pWork != NULL && pWork->pNext != NULL)
{
bool Current = pWork->data < pWork->pNext->data;
return Current && TestIsAscending(pWork->pNext);
}
else
{
return true;
}
}
//递归求最大值
int GetMaxValue(Node *pWork)
{
if ( pWork != NULL && pWork->pNext != NULL )
{
int MaxNextValue = GetMaxValue(pWork->pNext);
if ( pWork->data > MaxNextValue )
{
return pWork->data;
}
else
{
return MaxNextValue;
}
}
else if ( pWork->pNext == NULL )
{
return pWork->data;
}
}
void main(void)
{
Node *pHead = NULL;
srand(time(NULL));
CreateList(&pHead);
PrintList(pHead);
if ( TestIsAscending(pHead) )
{
printf("是递增\n");
}
else
{
printf("不是递增\n");
}
printf("最大值为 = %d\n",GetMaxValue(pHead));
DestroyList(pHead);
fflush(stdin);
getchar();
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯