逆置单链表
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct LNode 5 { 6 char data; 7 struct LNode *next; 8 }LinkList; 9 10 void CreateList(LinkList *& L, int l) 11 { 12 L =(LinkList*)malloc(sizeof(LinkList)); 13 L->next = NULL; 14 LinkList * p; 15 for (int i = 0; i < l; ++i) 16 { 17 p =(LinkList*)malloc(sizeof(LinkList)); 18 scanf(" %c",&p->data); 19 p->next = L->next; 20 L->next = p; 21 } 22 } 23 24 void Dis(LinkList* L) 25 { 26 LinkList* r; 27 r = L->next; 28 while(r!=NULL) 29 { 30 31 printf("%c ",r->data); 32 r = r->next; 33 } 34 } 35 36 int main(int argc, char const *argv[]) 37 { 38 LinkList *L; 39 int n; 40 scanf("%d",&n); 41 CreateList(L,n); 42 Dis(L); 43 return 0; 44 }
住:单链表采用头插法逐个插入元素,从而单链表元素的输出顺序就是逆置好的;