简单链表实现c++实现
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 struct Student 6 { 7 int id; 8 string name; 9 Student* next; 10 }; 11 void printNodeInfo(Student* node); 12 void release(Student* node); 13 int main() 14 { 15 Student* head; 16 Student* p1 = new Student; 17 Student* p2 = new Student; 18 Student* p3 = new Student; 19 p1 ->id = 1001; 20 p1 ->name = "zhangsan"; 21 p2 ->id = 1002; 22 p2 ->name = "lisi"; 23 p3 ->id = 1003; 24 p3 ->name = "wangwu"; 25 head = p1; 26 p1 ->next = p2; 27 p2 ->next = p3; 28 p3 ->next = NULL; 29 30 p1 = p2 = p3 = NULL; 31 printNodeInfo(head); 32 //释放节点内存 33 release(head); 34 35 return 0; 36 } 37 void printNodeInfo(Student* node) 38 { 39 while(node != NULL) 40 { 41 cout << node ->id << " " << node ->name << endl; 42 node = node ->next; 43 } 44 } 45 void release(Student* node) 46 { 47 if(node == NULL) 48 { 49 cout << "链表为空" << endl; 50 } 51 while(node != NULL) 52 { 53 Student* temp = node; 54 node = node ->next; 55 delete temp; 56 cout << "节点内存清除成功" << endl; 57 } 58 }

更多精彩