1 //
2 //6_21线性表逆置
3 //
4 #include <stdio.h>
5 #include <stdlib.h>
6 typedef int datatype; //数据域中的数据类型
7 #define MAXSIZE 1024
8 #define LAST 50 //数组的最大存储量
9 typedef struct
10 {
11 datatype data[MAXSIZE];
12 int last;
13 }sequenlist;
14 typedef char datatype2;
15 typedef struct node
16 {
17 datatype2 data;
18 struct node *next;
19 }linklist;
20 void SetNull(sequenlist *L); //置空表
21 linklist *CreatFromE(); //尾插法建链表
22 sequenlist *CreatS(sequenlist *L); //建立顺序表
23 void ConvertS(sequenlist *L); //顺序表逆置
24 void ConvertL(linklist *L); //链表逆置
25
26 void main()
27 {
28 //线性表
29 sequenlist *E = NULL;
30 E = CreatS(E);
31 ConvertS(E);
32 //单链表
33 linklist *A = NULL;
34 A = CreatFromE();
35 ConvertL(A);
36 free(E);
37 free(A);
38 }
39 void SetNull(sequenlist *L)
40 {
41 for (int i=0;i<=L->last;i++)
42 {
43 L->data[i] = 0;
44 }
45 }
46 linklist *CreatFromE()
47 {
48 char ch;
49 linklist *head,*s,*r;
50 head = NULL;
51 r =NULL;
52 printf("请输入字符:(按回车结束)/n");
53 ch = getchar();
54
55 while (ch != '/n')
56 {
57 s = (linklist *)malloc(sizeof(linklist));
58 s->data = ch;
59 if (head == NULL)
60 {
61 head = s;
62 }
63 else r->next = s;
64 r = s;
65 ch = getchar();
66 }
67 if (r!=NULL)
68 {
69 r->next = NULL;
70 }
71 return head;
72 }
73 sequenlist *CreatS(sequenlist *L)
74 {
75 L = (sequenlist *)malloc(sizeof(sequenlist));
76 L->last = LAST;
77
78 SetNull(L);
79 for(int i=0;i<L->last;i++)
80 {
81 L->data[i] = i;
82 }
83 for(i=0;i<L->last;i++)
84 {
85 printf("%d/t",L->data[i]);
86 }
87 printf("/n");
88 return L;
89 }
90 void ConvertS(sequenlist *L)
91 {
92 int i,j;
93 datatype temp;
94 i = L->last/2;
95 for(j=0;j<=i;j++)
96 {
97 temp = L->data[j];
98 L->data[j] = L->data[L->last-j];
99 L->data[L->last-j] = temp;
100 }
101 printf("After Convert:/n");
102 for(j=0;j<L->last;j++)
103 printf("%d/t",L->data[j]);
104 printf("/n");
105 }
106 void ConvertL(linklist *L)
107 {
108 linklist *a,*b;
109 if(L->next && L->next->next)
110 {
111 a = L;
112 b = a->next;
113 a->next = NULL;
114 while(b)
115 {
116 a = b;
117 b = b->next;
118 a->next = L;
119 L = a;
120 }
121 }
122 printf("After Convert:/n");
123 linklist *temp = L;
124 while(temp)
125 {
126 printf("%c",temp->data);
127 temp = temp->next;
128 }
129 printf("/n");
130 }