【資料結構】單鏈表的逆序輸出
阿新 • • 發佈:2018-11-29
即:將一個已經建立好的單鏈表進行指標域的改變
今天突然被問到單鏈表逆序的問題,弄了好久才看出別人的程式有啥問題,就重新寫了一遍。
今天才在CSDN客戶端上看到美團的面試題是氣泡排序。
一個看似簡單的問題難倒很多人,所以簡單的不要因為他簡單就忽視它,人們在簡單的問題上越容易犯錯!!!!
#include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node *next; }N; N *p,*h,*q; void creat () { int x; scanf("%d",&x); while(x!=-1) { p=(N*)malloc(sizeof(N)); p->data=x; if(h==NULL) { h=p; q=p; } else { q->next=p; q=p; } p->next=NULL; scanf("%d",&x); } } void print() { p=h; while(p) { printf("%d ",p->data); p=p->next; } printf("\n"); return ; } void nixu () { N *n; q=h->next; n=NULL; h->next=n; while(q) { n=h; h=q; q=q->next; h->next=n; } return ; } void xiaohui() { while(h) { p=h; h=h->next; free(p); } return ; } int main () { int a; while(1) { h=NULL; creat(); print(); nixu(); print(); xiaohui(); printf("是否退出該程式?(1 or 0)"); scanf("%d",&a); if(a!=1) exit(0); } return 0; }