1 #include<iostream> 2 #include<string> 3 #define SIZE 3 4 using std::cout; 5 using std::endl; 6 using std::string; 7 8 9 class MyClass{ 10 public: 11 struct DataInfo{ 12 int age; 13 string name; 14 }; 15 private: 16 struct Queue{ 17 DataInfo dataInfo; 18 Queue *front; 19 Queue *next; 20 }; 21 private:
22 Queue *head, *tail; 23 24 public: 25 inline Queue* CreateNode() 26 { 27 Queue *node; 28 node = new Queue; 29 return node; 30 } 31 inline void InsertNode(DataInfo dataInfo)//採用頭插法 32 { 33 Queue *pNode; 34 pNode = CreateNode(); 35 pNode->dataInfo = dataInfo; 36 pNode->next = head->next; 37 head->next->front = pNode; 38 head->next = pNode; 39 pNode->front = head; 40 } 41 inline void DeleteNode()//採用頭刪法 42 { 43 Queue *pNode; 44 pNode = head->next; 45 head->next = pNode->next; 46 pNode->next->front = head; 47 48 } 49 inline void InitinalQueue() 50 { 51 head = CreateNode(); 52 tail = CreateNode(); 53 head->next = tail; 54 head->front = NULL; 55 tail->front = head; 56 tail->next = NULL; 57 } 58 inline void OutputQueue() 59 { 60 Queue *pNode; 61 pNode = head->next; 62 do{ 63 cout << pNode->dataInfo.age <<" "<< pNode->dataInfo.name << endl; 64 pNode = pNode->next; 65 } while (pNode!=tail); 66 } 67 inline void FreeSpace()//釋放空間 68 { 69 Queue *pNode; 70 pNode = head->next; 71 while (pNode != NULL) 72 { 73 delete pNode->front; 74 pNode = pNode->next; 75 } 76 delete tail; 77 } 78 }; 79 void main() 80 { 81 MyClass myClass; 82 myClass.InitinalQueue(); 83 MyClass::DataInfo dataInfo[SIZE]; 84 dataInfo[0].age = 21; 85 dataInfo[0].name = "436醬油哥"; 86 87 dataInfo[1].age = 22; 88 dataInfo[1].name = "436醬油哥"; 89 90 dataInfo[2].age = 23; 91 dataInfo[2].name = "436醬油哥"; 92 93 myClass.InsertNode(dataInfo[0]); 94 myClass.InsertNode(dataInfo[1]); 95 myClass.InsertNode(dataInfo[2]); 96 97 myClass.OutputQueue(); 98 myClass.FreeSpace();//釋放new 佔用的空間 99 100 system("pause"); 101 }
鏈表是很是重要的 咱們常常會用到,因此熟練的掌握有助實現!node
鏈表使用的自我理解概念:ios
鏈表在物理地址邏輯相鄰物理不相鄰,有單鏈表,循環鏈表,雙向鏈表,實現起來大同小異,主要是熟練掌握指針的使用。鏈表的節點數據部分能夠是變量,數組,結構體,容器等。數組