第二课(模拟银行系统--------链表)

头文件Node.h

#ifndef HEADER_NODE
#define HEADER_NODE
#include "Record.h"
#include<iostream>
using namespace std;

class Node
{
private:
	Record *record;
	Node *next;
public:
	Node();
	~Node();
	void set_record(Record *record);
	void set_next(Node *next);
	Record * get_record();
	Node * get_next();
	void display_Node();
};
#endif

源文件Node.cpp

#include"Node.h"
#include<iostream>
using namespace std;

Node::Node()
{
	this->record = NULL;
	this->next = NULL;
}

Node::~Node()
{
	delete this->record;
	//delete this->next;下一个的地址
	this->record = NULL;
	this->next = NULL;
}

void Node::set_record(Record * record)
{
	this->record = record;
}

void Node::set_next(Node * next)
{
	this->next = next;
}

Record * Node::get_record()
{
	return this->record;
}

Node * Node::get_next()
{
	return this->next;
}

void Node::display_Node()
{
	cout << "Print Node Elements......" << endl;
	//cout << "Record * :" << this->record << endl;
	if(this->record != NULL)
	{
		this->record->display_Record();//相当于Record * r = this->record;  r->display_Record();
	}
	else
	{
		cout << "Record is NULL......" << endl;
	}
	cout << "Next * :" << this->next << endl;
	cout << "End of Node......" << endl;
}

测试文件 TestNode.cpp

#include"Node.h"
#include<iostream>
using namespace std;

int main()
{
	Node * node = new Node();
	node->display_Node();

	Record *record = new Record();
	record ->set_number(10001);
	record ->set_userName("zhangsan");
	record ->set_passWord("123");
	record ->set_balance(10000);
	record ->set_flag(1);

	node->set_record(record);
	node->display_Node();

	return 0;
}


第二课(模拟银行系统--------链表)_第1张图片

头文件 LinkList.h

#ifndef HEADER_LINKLIST
#define HEADER_LINKLIST
#include"Node.h"

class LinkList
{
private:
	Node * head;
	int len;
public:
	LinkList();
	~LinkList();
	void set_head(Node * head);
	void set_len(int len);
	Node * get_head();
	int get_len();

	Node * make_node(Record * record);
	void insert_node(Node * node);
	Node * find_node(int number);
	void display_LinkList();
};
#endif


源文件 LinkList.cpp

#include "LinkList.h"
#include<iostream>
using namespace std;

LinkList::LinkList()
{
	this->head = NULL;
	this->len = 0;
}

LinkList::~LinkList()
{
	Node *p, *q;
	p = this->head;
	while(p != NULL)
	{
		q = p;
		p = p->get_next();
		delete q;
	}
	this->head = NULL;
	this->len = 0;
}

void LinkList::set_head(Node * head)
{
	this->head = head;
}

void LinkList::set_len(int len)
{
	this->len = len;
}

Node * LinkList::get_head()
{
	return this->head;
}

int LinkList::get_len()
{
	return this->len;
}

Node * LinkList::make_node(Record * record)
{
	Node * node = new Node();
	node->set_record(record);
	node->set_next(NULL);
	return node;
}

void LinkList::insert_node(Node * node)
{
	Node * p = this->head;
	if(p == NULL)
	{
		this->head = node;
		this->len++;
	    return;
	}
	while(p->get_next() != NULL)
	{
		p = p->get_next();
	}
	p->set_next(node);
	this->len++;
}

Node * LinkList::find_node(int number)
{
	Node * p = this->head;
	while(p != NULL)
	{
		if(p->get_record()->get_number() == number)
		{
			return p;
		}
		else
		{
			p = p->get_next();
		}
	}
	return p;
}

void LinkList::display_LinkList()
{
	cout << "Print LinkList Elements......" << endl;
	Node * p = this->head;
	if(p = NULL)
	{
		cout << "LinkList is NULL......." << endl;
		cout << "Len: " << this->len << endl;
		cout << "End of LinkList......" << endl;

		return;
	}
	while ( p != NULL)
	{
		p->display_Node();
		p = p->get_next();
	}
	cout << "Len: " << this->len << endl;
	cout << "End of LinkList..." << endl;
}

测试文件 TestLinkList.cpp

#include "LinkList.h"
#include <iostream>
using namespace std;

int main ()
{
	LinkList * list = new LinkList();
	list->display_LinkList();
	cout <<endl;
	
	Record * r1 = new Record();
	r1->set_number(10001);
	r1->set_userName("zhangsan");
	r1->set_passWord("123456");
	r1->set_balance(10000);
	r1->set_flag(1);
	
	Node * n1 = list->make_node(r1);
	list->insert_node(n1);
	list->display_LinkList();
	cout <<endl;
	
	Record * r2 = new Record();
	r2->set_number(10002);
	r2->set_userName("lisi");
	r2->set_passWord("654321");
	r2->set_balance(20000);
	r2->set_flag(1);
	
	Node * n2 = list->make_node(r2);
	list->insert_node(n2);
	list->display_LinkList();

	Node * n3 = list->find_node (10001);
	if( n3 == NULL )
	{
		cout << "Not Found..." << endl;
	}
	else
	{
	n3->display_Node();
	}
	
	return 0;
}


第二课(模拟银行系统--------链表)_第2张图片

上机感言:今天的内容有点多,一下午好累啊。

你可能感兴趣的:(第二课(模拟银行系统--------链表))