展开全部
//简单写的代码,若符合要求,请采纳(在加些分最好)
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct _node_
{
int value;
_node_ *next;
}NODE,*P_NODE;
P_NODE head = NULL;
void init_list();
void create_list(int cnt);
void display_list();
void rotate_list();
int main()
{
init_list(); // 初始化头结点
create_list(5); // 创建5个元素的单链表
printf("==========================\n");
display_list(); // 显示元素
rotate_list(); // 逆置
printf("==========================\n");
display_list(); //显示元素
return 0;
}
void init_list()
{
if(head == NULL)
{
head = (P_NODE)malloc(sizeof(NODE));
if(!head)
{
printf("malloc memory failed.\n");
return;
}
head->next = NULL;
}
}
void create_list(int cnt)
{
P_NODE pt = NULL;
int num = 1;
while(cnt-- > 0)
{
P_NODE p = (P_NODE)malloc(sizeof(NODE));
if(p == NULL)
{
printf("malloc memory failed.\n");
return;
}
p->next = NULL;
printf("Please input %d node value:",num++);
scanf("%d",&(p->value));
if(head->next == NULL)
head->next = p;
else
pt->next = p;
pt = p;
}
}
void display_list()
{
int num = 1;
P_NODE pt = head->next;
while(pt)
{
printf("the %d node value:%d\n",num++,pt->value);
pt = pt->next;
}
}
void rotate_list()
{
P_NODE p1,p2,pHead;
p1 = head->next;
pHead = (P_NODE)malloc(sizeof(NODE));
pHead->next = NULL;
while(p1)
{
p2 = p1;
p1 = p1->next;
if(pHead->next == NULL)
{
pHead->next = p2;
p2->next = NULL;
}
else
{
p2->next = pHead->next;
pHead->next = p2;
}
}
head->next = pHead->next;
free(pHead);
}
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct _node_
{
int value;
_node_ *next;
}NODE,*P_NODE;
P_NODE head = NULL;
void init_list();
void create_list(int cnt);
void display_list();
void rotate_list();
int main()
{
init_list(); // 初始化头结点
create_list(5); // 创建5个元素的单链表
printf("==========================\n");
display_list(); // 显示元素
rotate_list(); // 逆置
printf("==========================\n");
display_list(); //显示元素
return 0;
}
void init_list()
{
if(head == NULL)
{
head = (P_NODE)malloc(sizeof(NODE));
if(!head)
{
printf("malloc memory failed.\n");
return;
}
head->next = NULL;
}
}
void create_list(int cnt)
{
P_NODE pt = NULL;
int num = 1;
while(cnt-- > 0)
{
P_NODE p = (P_NODE)malloc(sizeof(NODE));
if(p == NULL)
{
printf("malloc memory failed.\n");
return;
}
p->next = NULL;
printf("Please input %d node value:",num++);
scanf("%d",&(p->value));
if(head->next == NULL)
head->next = p;
else
pt->next = p;
pt = p;
}
}
void display_list()
{
int num = 1;
P_NODE pt = head->next;
while(pt)
{
printf("the %d node value:%d\n",num++,pt->value);
pt = pt->next;
}
}
void rotate_list()
{
P_NODE p1,p2,pHead;
p1 = head->next;
pHead = (P_NODE)malloc(sizeof(NODE));
pHead->next = NULL;
while(p1)
{
p2 = p1;
p1 = p1->next;
if(pHead->next == NULL)
{
pHead->next = p2;
p2->next = NULL;
}
else
{
p2->next = pHead->next;
pHead->next = p2;
}
}
head->next = pHead->next;
free(pHead);
}
追问
一样有错误
追答
什么错误,我运行结果Ok啊, 如图:
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询