[150420][C++]数据结构复习——链表实现源码

来源:互联网 发布:正确看待网络新一代 编辑:程序博客网 时间:2024/06/09 14:18
#include <iostream>#include <string>#include <cstdlib>using namespace std;class List{private:struct ListNode{string value;ListNode* next;}*head;public:List(){head = new(ListNode);head->next = NULL;}int Add(string value){ListNode* p = new(ListNode);p->value = value;ListNode* h = head;for (h = head; h->next != NULL; h = h->next);p->next = h->next;h->next = p;return 1;}int Add(int pos, string value){ListNode* p = new(ListNode);p->value = value;ListNode* h = head;for (int count = 1; count < pos; count++){if (h->next != NULL)h = h->next;elsereturn 0;}p->next = h->next;h->next = p;return 1;}int Delete(int pos){ListNode* p = new(ListNode);ListNode* h = head;for (int count = 1; count < pos; count++){if (h->next != NULL)h = h->next;elsereturn 0;}p = h->next;h->next = p->next;delete(p);return 1;}int Search(string value){ListNode* h;int count = 1;for (h = head; h->next != NULL; h = h->next){if (h->next->value == value)return count;elsecount++;}return 0;}void Print(){ListNode* h = head;for (h = head; h->next != NULL; h = h->next){cout << h->next->value << endl;}}};int main(){List l;l.Add(1,"A");l.Add(2,"B");l.Add(3,"C");l.Add("D");l.Delete(2);cout << l.Search("A") << endl;l.Print();system("pause");return 0;}

0 0
原创粉丝点击