2117 資料結構實驗之連結串列二:逆序建立連結串列
阿新 • • 發佈:2018-12-22
資料結構實驗之連結串列二:逆序建立連結串列
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
輸入整數個數N,再輸入N個整數,按照這些整數輸入的相反順序建立單鏈表,並依次遍歷輸出單鏈表的資料。Input
第一行輸入整數N;;第二行依次輸入N個整數,逆序建立單鏈表。
Output
依次輸出單鏈表所存放的資料。Example Input
10 11 3 5 27 9 12 43 16 84 22
Example Output
22 84 16 43 12 9 27 5 3 11
Hint
不能使用陣列!#include <bits/stdc++.h> using namespace std; typedef struct node { int date; struct node *next; }Lnode; Lnode *creat(int n) { Lnode *p,*head; head = new node; head -> next = NULL; for(int i = 0; i < n; i++) { p = new node; p -> next = NULL; cin >> p->date; p->next = head ->next; head ->next = p; } return head; } void output( Lnode *head) { Lnode *q; q = head ->next; cout << q ->date; while(q->next) { cout << " " << q ->next ->date; q = q ->next; } cout << endl; } int main() { int n; Lnode *head; cin >> n; head = creat(n); output(head); return 0; }