描述
-
構造有序(升序)的單鏈表
並實現單鏈表的逆置
(可以采用結構化的程序設計方法實現,即不必定義類)
輸入輸入鏈表中的數據。(用0表示輸入的結束,0不能添加到鏈表中)輸出按順序輸出有序鏈表中的數據樣例輸入
4 1 6 8 2 0
樣例輸出
1 2 4 6 8 8 6 4 2 1
代碼:方法略顯無恥
#include<bits/stdc++.h> using namespace std; int a[1000]; struct Node { int data; Node *next; }; int main() { int x=0,j=0; while(scanf("%d",&x)) { if(x==0) break; a[++j]=x; } sort(a+1,a+1+j); for(int i=1;i<=j;i++) { printf("%d ",a[i]); } printf("\n"); Node *head,*s,*p,*q; s=new Node; s->data=a[j]; head=s; for(int i=1;i<=j-1;i++) { p=s; s=new Node; s->data=a[j-i]; p->next=s; } s->next=NULL; p=head; while(p) { printf("%d ",p->data); p=p->next; } return 0; }