📄 333.txt
字号:
设计一算法,逆置带头结点的动态单链表L
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
int data;
struct node *next;
}Node;
typedef struct lnode
{
int length;
Node * head;
}Lnode;
void Invert(Lnode*);
void main()
{
Lnode* L;
Node *h,*p;
int i,n;
n=10;
i=2;
L=(Lnode*)malloc(sizeof(Lnode));
h=(Node*)malloc(sizeof(Node));
h->data=0;
p=h;
printf("逆置前:\n%d\n",p->data);
while(i<=n)
{
p->next=(Node*)malloc(sizeof(Node));
p=p->next;
p->data=i;
i++;
printf("%d\n",p->data);
}
L->head=h;
L->length=n;
Invert(L);
p=L->head;
printf("逆置后:\n");
while(p!=NULL)
{
printf("%d\n",p->data);
}
}
void Invert(Lnode *a)
{
Node *p,*q,*r;
if(a==NULL||a->next==NULL) return ;
q=a->head;
p=q->next;
while(p!=NULL)/*改了这里*/
{
r=p->next;
p->next=q;
q=p;
p=r;
}
a->head->next=NULL;
a->head=q;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -