C++チェーンテーブルでデータを格納

8753 ワード

チェーンテーブルでデータを保存するのは、線形テーブルよりも挿入と削除操作が便利で、チェーンテーブルがそれらの位置のデータを移動する必要はありません.
これはチェーンテーブルで実現された簡単な通信録で、まずコードをつけます.
/*
*     :            
*   :BossMao
*   :2.0
*/


#include
#include

using namespace std;

struct student_Info{
	double stu_Num;				//    
	string stu_Name;			//    
	string stu_Parent_Name;			//      
	int stu_Age;				//    
	string stu_Sex;				//    
	string stu_Address;			//    
	char stu_PhoneNum[20];			//      
	char stu_Parent_PhoneNum[20];		//        

	student_Info *priority;			//   
	student_Info *next;			//   
};

class Student{
private:
	int current_stuNum;
	student_Info *head;
	student_Info *temp;
public:
	Student();
	~Student();
	void menu();
	void insert_student_Info();				//      
	void update_student_Info(string name);			//      
	void delete_student_Info(string name);			//      
	student_Info *search_student_Info(string name);		//      
	void show_student_Info();				//      

};

Student::Student()
{
	current_stuNum = 0;

	head = new student_Info;			//       
	head->next = NULL;
	head->priority = NULL;
	temp = head;
}

Student::~Student()
{
}

void Student::update_student_Info(string name)
{
	char choose;
	student_Info *s;

	s = search_student_Info(name);

	//      
	cout << "          ?(Y/N)" << endl;
	cin >> choose;
	if (choose == 'N'||choose == 'n')
	{
		return;
	}
	if (choose == 'Y'||choose == 'y')
	{
		cout << "======================================" << endl;
		cout << "  :";
		cin >> s->stu_Num;
		cout << "  :";
		cin >> s->stu_Name;
		cout << "    :";
		cin >> s->stu_Parent_Name;
		cout << "  :";
		cin >> s->stu_Age;
		cout << "  : ";
		cin >> s->stu_Sex;
		cout << "  :";
		cin >> s->stu_Address;
		cout << "      :";
		cin >> s->stu_PhoneNum;
		cout << "      :";
		cin >> s->stu_Parent_PhoneNum;

		cout << "======================================" << endl;
		cout << "    !!" << endl;
		cout << "======================================" << endl;
	}

}

void Student::delete_student_Info(string name)
{
	char choose;

	student_Info *s;

	s = search_student_Info(name);

	//      
	cout << "          ?(Y/N)" << endl;
	cin >> choose;
	if (choose == 'N'||choose == 'n')
	{
		return;
	}
	if (choose == 'Y'||choose == 'y')
	{
		if (s->next == NULL)
		{
			s->priority->next = s->next;			//                 
			temp = s->priority;
			delete s;
			cout << "======================================" << endl;
			cout << "    !!" << endl;
			cout << "======================================" << endl;
		}
		else{
			s->priority->next = s->next;			//                             
			s->next->priority = s->priority;		//                              
			delete s;
		}
	}
}

student_Info *Student::search_student_Info(string name)
{
	bool have = 0;
	student_Info *p, *found = NULL;
	for (p = head; p != NULL;p=p->next)
	{
		if (p->stu_Name == name)
		{
			have = 1;
			found = p;
			cout << "       :" << endl;
			cout << "======================================" << endl;
			cout << "  :";
			cout << p->stu_Num << endl;
			cout << "  :";
			cout << p->stu_Name << endl;
			cout << "    :";
			cout << p->stu_Parent_Name << endl;
			cout << "  :";
			cout << p->stu_Age << endl;
			cout << "  : ";
			cout << p->stu_Sex << endl;
			cout << "  :";
			cout << p->stu_Address << endl;
			cout << "      :";
			cout << p->stu_PhoneNum << endl;
			cout << "      :";
			cout << p->stu_Parent_PhoneNum << endl;
			cout << "======================================" << endl;

		}

	}

	if (have == 0)
	{
		cout << "======================================" << endl;
		cout << "    !!" << endl;
		cout << "======================================" << endl;
		menu();
	}
	return found;
}

void Student::show_student_Info()
{
	student_Info *p = head->next;
	cout << "     :" << endl;

	if (p == NULL)
	{
		cout << "======================================" << endl;
		cout << "     !!" << endl;
		cout << "======================================" << endl;
		return;
	}

	for (; p != NULL; p = p->next){
		cout << "======================================" << endl;
		cout << "  :";
		cout << p->stu_Num << endl;
		cout << "  :";
		cout << p->stu_Name << endl;
		cout << "    :";
		cout << p->stu_Parent_Name << endl;
		cout << "  :";
		cout << p->stu_Age << endl;
		cout << "  : ";
		cout << p->stu_Sex << endl;
		cout << "  :";
		cout << p->stu_Address << endl;
		cout << "      :";
		cout << p->stu_PhoneNum << endl;
		cout << "      :";
		cout << p->stu_Parent_PhoneNum << endl;
		cout << "======================================" << endl;
	}
}

void Student::insert_student_Info()
{
	char s;

	while (1)
	{
		cout << "         (Y/N)?" << endl;
		while (1){
			cin >> s;
			if (s == 'Y' || s == 'y' || s == 'N' || s == 'n')
			{
				break;
			}
			else{
				cout << "    ,     !!" << endl;
				cout << "         (Y/N)?" << endl;
			}
		}
		if (s == 'N' || s == 'n')
		{
			break;
		}
		else{
			student_Info *tail = new student_Info;		//     
			temp->next = tail;		//             
			tail->priority = temp;
			tail->next = NULL;				//         
			temp = tail;

			cout << "======================================" << endl;
			cout << "  :";
			cin >> tail->stu_Num;
			cout << "  :";
			cin >> tail->stu_Name;
			cout << "    :";
			cin >> tail->stu_Parent_Name;
			cout << "  :";
			cin >> tail->stu_Age;
			cout << "  : ";
			cin >> tail->stu_Sex;
			cout << "  :";
			cin >> tail->stu_Address;
			cout << "      :";
			cin >> tail->stu_PhoneNum;
			cout << "      :";
			cin >> tail->stu_Parent_PhoneNum;
			cout << "======================================" << endl;
		}
	}

	
}

void Student::menu()
{
	int choose;
	string name;

	cout << "1.      " << endl;
	cout << "2.      " << endl;
	cout << "3.      " << endl;
	cout << "4.      " << endl;
	cout << "5.      " << endl;
	cout << "0.  " << endl;
	cout << endl;
	cout << "              :" << endl;
	cin >> choose;
	if (choose >= 0 && choose < 6)
	{
		switch (choose)
		{
		case 1:
		{
			insert_student_Info();
			break;
		}
		case 2:
		{
			cout << "            :" << endl;
			cin >> name;
			update_student_Info(name);
			break;
		}
		case 3:
		{
			 cout << "            :" << endl;
			 cin >> name;
			 delete_student_Info(name);
			 break;
		}
		case 4:
		{
			cout << "            :" << endl;
			cin >> name;
			search_student_Info(name);
			break;
		}
		case 5:
		{
			show_student_Info();
			 break;
		}
		case 0:
			exit(0);
		}
	}
	else{
		cout << "======================================" << endl;
		cout << "    !!     " << endl;
		cout << "======================================" << endl;
	}

}

void main()
{
	Student stu;

	while (1){
		stu.menu();
	}

}

この簡単なプログラムを実現するとき、私は二重チェーンテーブルのデータ構造を使います.
プログラムを見ると、この小さなシステムを作るのにチェーン時計が本当に便利だと気づきます.関数の呼び出しはすべてメニュー関数で対応する機能関数を呼び出すので簡単です.
ここで注意すべきは、最後のノードを削除する操作をする場合は、最後から2番目のノードのnextポインタをtempポインタに保存することを覚えておいてください.そうしないと、チェーンテーブルの最後に新しいノードを再び挿入する場合、tempポインタで新しいノードを元のノードにリンクすることはできません.