请用C语言 谢谢
答案:1 悬赏:60 手机版
解决时间 2021-12-04 12:41
- 提问者网友:棒棒糖
- 2021-12-03 21:39
请用C语言 谢谢
最佳答案
- 五星知识达人网友:笑迎怀羞
- 2021-12-03 22:59
#include #include
typedef struct tagNode
{
unsigned int data;
struct tagNode * pNext;
}Node, * pNode;
void InitData(pNode head, int num);
void ShowData(pNode head);
void DaozhiData(pNode head, int k);
int main(void)
{
// 分配头结点
pNode head = (pNode)malloc(sizeof(Node));
head->pNext = NULL;
// 模拟测试数据
InitData(head, 30);
// 显示为倒置的数据
ShowData(head);
// 倒置数据
DaozhiData(head, 17);
// 显示倒置后的数据
ShowData(head);
return 0;
}
void InitData(pNode head, int num)
{
for (int i=0; i {
pNode node = (pNode)malloc(sizeof(Node));
node->data = i+1;
node->pNext = NULL;
head->pNext = node;
head = node;
}
return;}
void ShowData(pNode head)
{
printf("\nThe data is :\n");
int num = 0;
while(head = head->pNext)
{
printf("%d\t", head->data);
num++;
if (num%5==0)
{
printf("\n");
}
}
printf("\n");
return;}
void DaozhiData(pNode head, int k)
{// k表示第k个
int cnt = 0;
while(cntpNext))
cnt++;
if (head == NULL)
{// 说明已经超出了链表的总数范围
printf("指定的K值有错误!\n");
}
// 这个时候head已经指向了不需逆转的最后一个元素,后面做倒置
// 倒置的时候需要3个变量,按照一定的顺序来改变三个变量的pNext指向实现倒置
pNode newtail = head->pNext;
if (newtail == NULL)
{//
return;
}
pNode newsec = newtail->pNext;
if (newsec == NULL)
{
return;
}
pNode newcurr = newsec->pNext;
if (newcurr == NULL)
{
newsec->pNext = newtail;
head->pNext = newsec;
newtail->pNext = NULL;
}
newtail->pNext = NULL;
// 这里有一个链表的pNext指向逻辑, 画个图看看就明白了
while(newcurr)
{
newsec->pNext = newtail;
newtail = newsec;
newsec = newcurr;
newcurr = newcurr->pNext;
newsec->pNext = newtail;
}
head->pNext = newsec;
return;}
typedef struct tagNode
{
unsigned int data;
struct tagNode * pNext;
}Node, * pNode;
void InitData(pNode head, int num);
void ShowData(pNode head);
void DaozhiData(pNode head, int k);
int main(void)
{
// 分配头结点
pNode head = (pNode)malloc(sizeof(Node));
head->pNext = NULL;
// 模拟测试数据
InitData(head, 30);
// 显示为倒置的数据
ShowData(head);
// 倒置数据
DaozhiData(head, 17);
// 显示倒置后的数据
ShowData(head);
return 0;
}
void InitData(pNode head, int num)
{
for (int i=0; i
pNode node = (pNode)malloc(sizeof(Node));
node->data = i+1;
node->pNext = NULL;
head->pNext = node;
head = node;
}
return;}
void ShowData(pNode head)
{
printf("\nThe data is :\n");
int num = 0;
while(head = head->pNext)
{
printf("%d\t", head->data);
num++;
if (num%5==0)
{
printf("\n");
}
}
printf("\n");
return;}
void DaozhiData(pNode head, int k)
{// k表示第k个
int cnt = 0;
while(cnt
cnt++;
if (head == NULL)
{// 说明已经超出了链表的总数范围
printf("指定的K值有错误!\n");
}
// 这个时候head已经指向了不需逆转的最后一个元素,后面做倒置
// 倒置的时候需要3个变量,按照一定的顺序来改变三个变量的pNext指向实现倒置
pNode newtail = head->pNext;
if (newtail == NULL)
{//
return;
}
pNode newsec = newtail->pNext;
if (newsec == NULL)
{
return;
}
pNode newcurr = newsec->pNext;
if (newcurr == NULL)
{
newsec->pNext = newtail;
head->pNext = newsec;
newtail->pNext = NULL;
}
newtail->pNext = NULL;
// 这里有一个链表的pNext指向逻辑, 画个图看看就明白了
while(newcurr)
{
newsec->pNext = newtail;
newtail = newsec;
newsec = newcurr;
newcurr = newcurr->pNext;
newsec->pNext = newtail;
}
head->pNext = newsec;
return;}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯