数据结构(一):链表逆序输出
题目描述:创建一个链表,并将链表逆序输出,链表中以输入0作为结束
关键代码详解:
附:全部代码
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>typedef struct node{int n;struct node *next;
}linklist;linklist *CreatList(){int a;linklist *head,*s,*r,*w;head = (linklist*)malloc(sizeof(linklist));r = head;while(1){s = (linklist*)malloc(sizeof(linklist));printf("输入数组,遇0结束\n"); scanf("%d",&a);if(a==0){break;}else{s->n = a;r->next = s;r = s;}} r->next = NULL;w = head;w = w->next;printf("输出正向数组\n");while(w->next!=NULL){printf("%d",&w);printf("\n");w = w->next;}return head;
}void OutPut(linklist *L){linklist *p;p = L;p = L->next;printf("输出逆向数组\n"); while(p!=NULL){printf("%d",&p->n);p = p->next;}
}linklist *upend(linklist *L){linklist *p,*q,*r;p = L;p = p->next;r = NULL;while(p->next!=NULL){q = p->next;p->next = r;r = p;p = q;}p->next = r;L->next = p;return L;
}int main(){linklist *head;head = CreatList();upend(head);OutPut(head);return 0;
}