資料結構實驗之連結串列一:順序建立連結串列(SDUT 2116)
阿新 • • 發佈:2018-12-13
Problem Description
輸入N個整數,按照輸入的順序建立單鏈表儲存,並遍歷所建立的單鏈表,輸出這些資料。
Input
第一行輸入整數的個數N; 第二行依次輸入每個整數。
Output
輸出這組整數。
Sample Input
8
12 56 4 6 55 15 33 62
Sample Output
12 56 4 6 55 15 33 62
Hint
不得使用陣列!
#include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }; int main() { int n; struct node *head, *tail, *p; head = new node; head -> next = NULL; tail = head; scanf("%d",&n); for(int i = 0; i < n; i ++) { p = new node; p -> next = NULL; scanf("%d", &p -> data); tail -> next = p; tail = p; } for(p = head -> next ; p != NULL; p = p -> next) { if(p == head -> next) printf("%d",p->data); else printf(" %d",p->data); } printf("\n"); return 0; }