LinkList链表

来源:互联网 发布:重返十七岁电影知乎 编辑:程序博客网 时间:2024/05/16 02:44
#include<string>using namespace std;struct Bean { string str;int id;}; struct Node {//需要有下一个节点的指针,需要一个bean存储这个节点的数据Node* next;Bean bean;Node(Bean b) :bean(b),next(NULL) {} }; class LinkList01 {public:void insert(Bean b);LinkList01();void sprint();int find(Bean b);void remove(Bean b);private:Node* head;   

};

-------------------------------------------------------------------------

#include "LinkList.h"#include <iostream> LinkList01::LinkList01() {head = NULL;}  void LinkList01::insert(Bean b) {if (!head) {Node*temp = new Node(b);head = temp;}else {Node* temp01 =head;while (temp01->next) {temp01 = temp01->next;}Node* node = new Node(b);temp01->next = node;}/*while (head->next) {Node* node = new Node(b);head->next = node;}*/}void LinkList01::sprint() {Node*temp = head;if (temp != NULL)std::cout << temp->bean.id << temp->bean.str << std::endl;while (temp->next) {temp = temp->next;std::cout << temp->bean.id << temp->bean.str << std::endl;} }int LinkList01::find(Bean b) {Node*temp =head;int i = 0;while (temp->next) {i++;if (temp->next->bean.id == b.id&&temp->next->bean.str == b.str) {return i;}temp =temp->next;}return -1; } void LinkList01::remove(Bean b) {int a =find(b);Node*temp = head;int i = 0;while (i<a) {if (i==a-1) {temp->next =temp->next->next;return;}temp = temp->next;i++;}} 
------------------------------------------------------------------------------------------

#include "LinkList.h" void main() {Bean b1;b1.id = 1;b1.str = "1";Bean b2;b2.id = 2;b2.str = "2";Bean b3;b3.id = 3;b3.str = "3";LinkList01 list;list.insert(b1);list.insert(b2);list.insert(b3);list.sprint();int a =list.find(b2);if (a!=-1) {printf("%s","find");}list.remove(b3);list.sprint();getchar(); }

原创粉丝点击