/*
 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;
}


執行結果:

 

文章標籤
全站熱搜
創作者介紹
創作者 flyinsky76 的頭像
flyinsky76

Deja Vu

flyinsky76 發表在 痞客邦 留言(0) 人氣(123)