qq_我多想把你给忘了_03210628
2016-12-26 13:27
/*Person.h*/ #ifndef PERSON_H #define PERSON_H #include<string> #include<ostream> using namespace std; class Person { friend ostream &operator<<(ostream &out, Person &person);//友元函数输出运算符的重载 public: string name; string phone; Person &operator=(Person &person); bool operator==(Person &person); }; #endif /*Person.cpp*/ /*List.h*/ #ifndef LIST_H #define LIST_H #include"Node.h" class List { public: List(); ~List(); void ClearList(); bool ListEmpty(); int ListLength(); bool GetElem(int i, Node *pNode); int LocateElem(Node *pNode); void ListTraverse(); bool ListInsert(int i, Node *pNode); bool ListDelete(int i, Node *pNode); bool ListInsertHead(Node *pNode); bool ListInsertTail(Node *pNode); private: Node *m_plist; int m_ilength; }; #endif /*List.cpp*/ #include"List.h" #include<iostream> using namespace std; List::List() { m_plist = new Node; //m_plist->data=0; m_plist->next = NULL; m_ilength = 0; } List::~List() { ClearList(); delete m_plist; m_plist = NULL; } void List::ClearList()//清空 { Node *currentNode = m_plist->next; while (currentNode != NULL) { Node *temp = currentNode->next; delete currentNode; currentNode = temp; } m_plist->next = NULL; } bool List::ListEmpty() { if (m_ilength == 0) { return true; } else { return false; } //return m_ilength==0?true:false; } int List::ListLength() { return m_ilength; } bool List::GetElem(int i, Node *pNode)//获取指定元素 { if (i<0 || i >= m_ilength) { return false; } Node *currentNode = m_plist; Node *currentNodeBefore = NULL; for (int k=0; k <= i; k++) { currentNodeBefore = currentNode; currentNode = currentNode->next; } pNode->data = currentNode->data; return true; } int List::LocateElem(Node *pNode)//寻找第一个满足e的数据元素的位序 { Node *currentNode = m_plist; int count = 0; while (currentNode->next != NULL) { currentNode = currentNode->next; if (currentNode->data == pNode->data) { return count; } count++; } return -1; } void List::ListTraverse()//遍历 { Node *currentNode = m_plist; while (currentNode->next != NULL) { currentNode = currentNode->next; currentNode->printNode(); } } bool List::ListInsert(int i, Node *pNode)//在第i个位置插入元素 { if (i<0 || i>m_ilength) { return false; } Node *currentNode = m_plist; for (int k = 0; k<i; k++) { currentNode = currentNode->next; } Node *newNode = new Node; if (newNode == NULL) { return false; } newNode->data = pNode->data; newNode->next = currentNode->next; currentNode->next = newNode; return true; } bool List::ListDelete(int i, Node *pNode)//删除第i个位置的元素 { if (i<0 || i >= m_ilength) { return false; } Node *currentNode = m_plist; Node *currentNodeBefore = NULL; for (int k=0; k <= i; k++) { currentNodeBefore = currentNode; currentNode = currentNode->next; } currentNodeBefore->next = currentNode->next; pNode->data = currentNode->data; delete currentNode; currentNode = NULL; m_ilength--; return true; } bool List::ListInsertHead(Node *pNode) { Node *temp = m_plist->next; Node *newNode = new Node; if (newNode == NULL) { return false; } newNode->data = pNode->data; m_plist->next = newNode; newNode->next = temp; m_ilength++; return true; } bool List::ListInsertTail(Node *pNode) { Node *currentNode = m_plist; while (currentNode->next != NULL) { currentNode = currentNode->next; } Node *newNode = new Node; if (newNode == NULL) { return false; } newNode->data = pNode->data; newNode->next = NULL; currentNode->next = new Node; m_ilength++; return true; } /*Node.h*/ #ifndef NODE_H #define NODE_H #include"Person.h" class Node { public: Person data; Node *next; void printNode(); }; #endif /*Node.cpp*/ #include"Node.h" #include<iostream> using namespace std; void Node::printNode() { cout << data << endl; } /*demo.cpp*/ #include<stdlib.h> #include"List.h" #include<iostream> using namespace std; int main(void) { Node node1; node1.data.name = "weqrwq"; node1.data.phone = "1312556"; Node node2; node2.data.name = "wq"; node2.data.phone = "1356"; List *pList = new List(); pList->ListInsertTail(&node1); pList->ListInsertTail(&node2); pList->ListTraverse(); delete pList; pList = NULL; system("pause"); return 0; }
把你的错误提示发一下
数据结构探险之线性表篇
57564 学习 · 257 问题
相似问题