Created
June 23, 2016 15:12
-
-
Save thomas861205/259ef0bea4ccdae8a3cc2ecea9b3cbe5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
#include <string> | |
using namespace std; | |
void add_node(void); | |
void print_node(void); | |
int count_node = 0; | |
struct node { | |
string name; | |
int age; | |
struct node *next; | |
}; | |
struct node *head, *ptr, *newptr; | |
int main() { | |
int choose; | |
cout << "=========Linked List 鏈結串鍊========="<<endl; | |
cout << " 按 1 新增節點"<<endl; | |
cout << " 按 2 顯示當前節點" << endl; | |
cout << " 按 0 結束" << endl; | |
cout << "======================================" << endl; | |
cout << " -> "; | |
while (cin >> choose) { | |
switch (choose) { | |
case 1: | |
add_node(); | |
break; | |
case 2: | |
print_node(); | |
break; | |
case 0: | |
exit(0); | |
} | |
cout << "=========Linked List 鏈結串鍊=========" << endl; | |
cout << " 按 1 新增節點" << endl; | |
cout << " 按 2 顯示當前節點" << endl; | |
cout << " 按 0 結束" << endl; | |
cout << "======================================" << endl; | |
cout << " -> "; | |
} | |
return 0; | |
} | |
void add_node(void) { | |
if (count_node == 0) { | |
newptr = new node; | |
cout << " 輸入姓名 : "; | |
cin >> (newptr->name); | |
cout << " 輸入年齡 : "; | |
cin >> (newptr->age); | |
newptr->next = NULL; | |
head = ptr = newptr; | |
count_node++; | |
} | |
else { | |
newptr = new node; | |
cout << " 輸入姓名 : "; | |
cin >> (newptr->name); | |
cout << " 輸入年齡 : "; | |
cin >> (newptr->age); | |
newptr->next = NULL; | |
ptr->next = newptr; | |
ptr = newptr; | |
count_node++; | |
} | |
} | |
void print_node(void) { | |
int i = 1; | |
struct node* gg = head; | |
while (gg != NULL) { | |
cout << endl << "node " << i++; | |
cout<<" ==> 姓名 : " << gg->name; | |
cout << "\t 年齡 : " << gg->age << endl; | |
gg = gg->next; | |
} | |
cout << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment