#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}Node;
// 请在此添加你的代码
/********** Begin *********/
Node *CreatList()
{
Node *p,*s,*phead;
phead=(Node *)malloc(sizeof(Node));
int i,x;
s=phead;
printf("输入数据个数:");
scanf("%d",&x);
for(i=0;i<x;i++)
{
int data;
p=(Node *)malloc(sizeof(Node));
if(p==NULL)
exit(0);
scanf("%d",&data);
p->data=data;
s->next=p;
s=p;}
p->next=NULL;
return phead;
}
void ShowList(Node *phead)
{
Node *p=phead;
while(p!=NULL)
{
if(p!=phead)
printf("%d ",p->data);
p=p->next;
}
}
Node *ReverseList(Node *phead)//头插法
{
Node *p,*q;
p=phead->next;
phead->next=NULL;
while(p!=NULL)
{
q=p;
p=p->next;
q->next=phead->next;
phead->next=q;
}
return phead;
}
/********** End **********/
int main(void)
{
Node *phead;
phead = CreatList();
printf("链表逆置前的数据:\n");
ShowList(phead);
phead = ReverseList(phead);
printf("\n");
printf("链表逆置后的数据:\n");
ShowList(phead);
return 0;
}
链表插入之头插法
最新推荐文章于 2024-06-25 07:11:52 发布