/*
LinkedList
*/
#include<iostream>
using namespace std;
#define size 50
struct Node
{
int id;
char name[size];
Node *next;
};
int main()
{
int n;
Node *head = new Node[size];
Node *ptr,*ptr2;
head->next = NULL;
ptr = head;
cout<<"輸入n筆資料: ";
cin>>n;
for(int i = 1;i<=n;i++)
{
cout<<endl<<"輸入第"<<i<<"個資料編號: ";
cin>>ptr->id;
cout<<"輸入第"<<i<<"個資料內容: ";
cin>>ptr->name;
ptr->next = new Node;
ptr = ptr->next;
ptr->next = NULL;
}
ptr2 = head;
cout<<endl;
while( ptr2->next != NULL )
{
cout<<ptr2->id<<" - "<<ptr2->name<<endl;
ptr2 = ptr2->next;
}
return 0;
}
執行結果:
文章標籤
全站熱搜
