-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path链表反转(尾插法)
78 lines (68 loc) · 1.27 KB
/
链表反转(尾插法)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// linkedreverse.c
// ex.1
//
// Created by 毛遵杰 on 2019/11/7.
// Copyright © 2019 毛遵杰. All rights reserved.
//
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
}*List;
List Create(List first,int n) //tail insertion create the list
{
int i;
List rear,s;
first=(List)malloc(sizeof(List)); //head node
rear=first; //tail node
for( i=0;i<n;i++)
{
s=(List)malloc(sizeof(List)); //create new node
scanf("%d",&s->data);
rear->next=s;
rear=rear->next;
}
rear->next=NULL;
return first;
}
void Printlist(List L) //output
{
L=L->next; //jump across the head node
while(L!=NULL)
{
printf("%d\n",L->data);
L=L->next;
}
return ;
}
//given function type on the PPT,as follows
List Reverse(List L) //reverse
{
List p = NULL;
List r = L;
List Next=NULL;
while (r!=NULL)
{
Next = r->next;
r->next = p;
p = r;
r = Next;
}
return p;
}
int main()
{
List i;
int n;
printf("please enter");
scanf("%d",&n);
i=Create(i,n);
Printlist(i);
Reverse(i);
Printlist(i);
return 0;
}