如何以单链表的存储结构实现线性表的就地逆置
1个回答
展开全部
#include <游袭stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}LinkNode, *LinkList;
LinkList Creat(LinkList head);
void Inverse(LinkList head);
void Output(LinkList head);
LinkList Creat(LinkList head)//建表滚判
{
LinkList p = NULL;
LinkList q = NULL;
head = (LinkList) malloc (sizeof(LinkNode));
head->大磨改next = NULL;
q = head;
while (1)
{
p = (LinkList) malloc (sizeof(LinkNode));
p->next = NULL;
scanf("%d", &p->data);
if (0 == p->data)
{
free(p);
break;
}
q->next = p;
q = p;
}
return head;
}
void Inverse(LinkList head)//逆置
{
LinkList p = head->next;
LinkList tmp = NULL;
head->next = NULL;
while (NULL != p)
{
tmp = p->next;
p->next = head->next;
head->next = p;
p = tmp;
}
}
void Output(LinkList head)//输出
{
LinkList p = head->next;
while (NULL != p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main(void)
{
LinkList head = NULL;
head = Creat(head);
Output(head);
Inverse(head);
Output(head);
return 0;
}
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}LinkNode, *LinkList;
LinkList Creat(LinkList head);
void Inverse(LinkList head);
void Output(LinkList head);
LinkList Creat(LinkList head)//建表滚判
{
LinkList p = NULL;
LinkList q = NULL;
head = (LinkList) malloc (sizeof(LinkNode));
head->大磨改next = NULL;
q = head;
while (1)
{
p = (LinkList) malloc (sizeof(LinkNode));
p->next = NULL;
scanf("%d", &p->data);
if (0 == p->data)
{
free(p);
break;
}
q->next = p;
q = p;
}
return head;
}
void Inverse(LinkList head)//逆置
{
LinkList p = head->next;
LinkList tmp = NULL;
head->next = NULL;
while (NULL != p)
{
tmp = p->next;
p->next = head->next;
head->next = p;
p = tmp;
}
}
void Output(LinkList head)//输出
{
LinkList p = head->next;
while (NULL != p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main(void)
{
LinkList head = NULL;
head = Creat(head);
Output(head);
Inverse(head);
Output(head);
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询