⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shuangxianglianbiao.cpp

📁 该程序为双向链表
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*第2题	双向链表--源代码及关键源代码注解如下:*/ 

#include <iostream.h>
#include <conio.h>
#include<fstream.h>
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
#include<stdlib.h>
#include<Double Link List.h>

void main(void)
{
	int x;
	cout<<"对双向链表操作输入1,对通讯录操作输入2.";
	cin>>x;
	if(x==1)
	{
		DoubleLinkList< int > List;
		int Value = 0 ,Option = 0;
		do
		{
			cout<<"\n\t\t Driver To Test  Link List "
				<<"\n\t\t\t\t Menu ."
			    <<"\n\t\t\t\t______ "
		    	<<"\n 1.) Insert At Front Of A Double Linked List . "
		    	<<"\n 2.) Insert At Rear Of A Double Linked List . "
		    	<<"\n 3.) Insert In Middle Of A Double Linked List . "
		    	<<"\n 4.) Remove From Front Of A Double Linked List. "
		    	<<"\n 5.) Remove From Rear Of A Double Linked List. "
		    	<<"\n 6.) Remove From Middle Of The Double Linked List . "
		    	<<"\n 7.) Traverse Forward A Double Linked List ."
				<<"\n 8.) Traverse Backwards A Double Linked List . "
				<<"\n 9.) Lenght Of The Double Linked List ."
				<<"\n 10.) insert a Node."
			    <<"\n 11.) search a Node."
			    <<"\n 12.) delete a Node."
			    <<"\n 13.) sort."
				<<"\n 14.) End Double Linked List Processing & Exit . "
		    	<<"\n     Enter Your Choice : \t";
			cin>>Option;
			switch(Option)
			{
			case 1 :
				{
					cout<<"\n Enter Data : ";
					cin>>Value;
					List.InsertAtFront(Value);
					break;
				}
			case 2 :
				{
					cout<<"\n Enter Data : ";
					cin>>Value;
					List.InsertAtRear(Value);
					break;
				}
			case 3:
				{
					cout<<"\n Enter Data : ";
					cin>>Value;
					List.InsertAtMiddle(Value);
					break;
				}
			case 4 :
				{
					List.RemoveFromFront();
					break;
				}
			case 5 :
				{
					List.RemoveFromRear();
					break;
				}
			case 6:
				{
					List.RemoveFromMiddle();
					break;
				}

			case 7 :
				{
					List.TraverseForward();
					break;
				}
			case 8 :
				{
					List.TraverseBackwards();
					break;
				}
			case 9 :
				{
					cout<<"\n The Lenght Of A Double Linked List "
						<<"\n (ie Number Of Nodes In A Double Linked List ) : "
						<<List.LenghtOfDoubleLinkList();
					break;
				}
			case 10 :
				{
					cout<<"\n Enter Data:";
					cin>>Value;
					List.InsertNode(Value);
					break;
				}
			case 11:
				{
					cout<<"\n Mission:Search a Node.\n";
					cin>>Value;
					List.Search(Value);
					break;
				}
			case 12:
				{
					cout<<"\n Enter Data:";
					cin>>Value;
					List.deleteNode(Value);
					break;
				}
			case 13:
				{
					cout<<"\n Sort the List.\n";
					List.Sort();
					break;
				}
			case 14 :
				{
					break;
				}
			default :
				{
					Option = 14;
					break;
				}
			}
		}
	while( Option != 14 );
	}
	if(x==2)
	{
		cout << "欢迎使用双向链表功能!\n";
		int choice;
        List list;      // 初始化链表头指针
        list.load_list_from_file(); // 从文件中提取以存记录
		do
		{             //  主菜单显示 
			cout<<"主菜单:\n";
			cout << "    1 - 显示所有联系人\n";
	        cout << "    2 - 添加联系人\n";
            cout << "    3 - 计算联系人的个数\n";
            cout << "    4 - 删除联系人\n";
        	cout << "    5 - 查找联系人的信息\n";
        	cout << "    6 - 修改联系人的信息\n";
        	cout << "    7 - 退出并且保存联系人\n";
        	cout << "请您输入数字进行选择 :";
            cin >> choice;
			//cin.ignore(20,'\n');
			list.handle_choice(choice); // 根据选择调用不同功能
			system("cls");
		} 
		while(choice != 7);  // 重复显示选择菜单直至选择退出
	}
}

#include <iostream.h>
#include <conio.h>
#include <assert.h>
#include "Double Link List.H"
template< class NodeType >
Node< NodeType >::Node()
:Data( NULL ),NextNode( NULL ),PreviousNode( NULL ) {}
//Node的构造函数,缺省值NextNode为NULL,PreviousNode为NULL
/**********************************************************************/
template< class NodeType >
Node< NodeType >::Node( NodeType &Value )
:Data( Value ),NextNode( NULL ),PreviousNode( NULL ) 
{
	cout<<"\n A Node Created Successfully . ";
}
/**********************************************************************/
//Node的析构函数
template< class NodeType >
Node< NodeType >::~Node()
{
	cout<<"\n A Node Destroyed Successfully .";
}
/**********************************************************************/
//构造函数
template< class NodeType >
DoubleLinkList< NodeType >::DoubleLinkList()
:FirstNode( NULL ),RearNode( NULL )
{
	cout<<"\n A Double Linked List Created Successfully .";
}

/**********************************************************************/
//析构函数
template< class NodeType >
DoubleLinkList< NodeType >::~DoubleLinkList()
{
	cout<<"\n Mission : Destroy A Double Linked List . \n";
	Node< NodeType > *CurrentNode = FirstNode, *TempNode ;
	while( CurrentNode != NULL )
		{
		TempNode = CurrentNode;
		CurrentNode = CurrentNode->NextNode;
		cout<<TempNode->Data<<" , ";
		delete TempNode;
		}
	cout<<"\n Status : Mission Successfull . "
		<<"\n A Double Linked List Destroyed Successfully ."
		<<"\n Press Any Key To Continue ";
	getch();
}
/**********************************************************************/
//判断链表是否为空
template< class NodeType >
bool DoubleLinkList< NodeType >::IsEmpty()
{
	if( FirstNode == NULL )
		{
		cout<<"\n No Double Linked List Exists . ";
		return true;
		}
	else
		return false;
}
/**********************************************************************/
//创建一个节点,将Value作为节点的Data
template< class NodeType >
Node< NodeType > *DoubleLinkList< NodeType >::CreateNode( NodeType &Value )
{
	Node< NodeType > *NewNode = new Node< NodeType >( Value );
	assert( NewNode != NULL );
	return NewNode ;
}
/**********************************************************************/
//在链表前插入节点
template< class NodeType >
void DoubleLinkList< NodeType >::InsertAtFront( NodeType &Value )
{
	cout<<"\n Mission : Insertion Of A Node At The Front Of The Linked List .\n";
	Node< NodeType > *NewNode = CreateNode(Value);
	if ( IsEmpty() )
		{
		cout<<"\n Mission : Generating A New Double Link List . ";
		FirstNode = RearNode = NewNode ;
		}
	else
		{
		FirstNode->PreviousNode = NewNode;
		NewNode->NextNode = FirstNode;
		FirstNode = NewNode;
		FirstNode->PreviousNode = NULL;
		}
	cout<<FirstNode->Data;
	cout<<"\n Status : Mission SuccessFull ."
		<<"\n Node Inserted At Front SuccessFully . "
		<<"\n Press Any Key To Continue . ";
	getch();
}
/**********************************************************************/
//在链表中插入节点
template< class NodeType >
void DoubleLinkList< NodeType >::InsertAtMiddle( NodeType &Value )
{
	cout<<"\n Operation : Insertion Of A Node In The Middle Of The Linked List .\n";
	Node< NodeType > *NewNode = CreateNode(Value);
	if ( IsEmpty() )
		{
		cout<<"\n Mission : Generating A New Double Link List . ";
		FirstNode = RearNode = NewNode ;
		}
	else
		{
		Node< NodeType > *CurrentNode = FirstNode,*TempNode;
		int Middle = (LenghtOfDoubleLinkList()/2),Position = 0;
		while( Position != Middle )
			{
			CurrentNode = CurrentNode->NextNode;
			Position++;
			}
		TempNode = CurrentNode;
		NewNode->PreviousNode = CurrentNode->PreviousNode;
		NewNode->NextNode = CurrentNode;
		CurrentNode = CurrentNode->PreviousNode;
		CurrentNode->NextNode = NewNode ;
		TempNode->PreviousNode = NewNode;
		}
	cout<<NewNode->Data;
	cout<<"\n Status : Mission SuccessFull ."
		<<"\n Node Inserted In Middle SuccessFully . "
		<<"\n Press Any Key To Continue . ";
	getch();
}
/**********************************************************************/
//在链表尾插入节点
template< class NodeType >
void DoubleLinkList< NodeType >::InsertAtRear( NodeType &Value )
{
	cout<<"\n Operation : Insertion Of A Node At The End Of The Linked List .\n";
	Node< NodeType > *NewNode = CreateNode(Value);
	if ( IsEmpty() )
		{
		cout<<"\n Mission : Generating A New Double Link List . ";
		FirstNode = RearNode = NewNode ;
		}
	else
		{
		NewNode->PreviousNode = RearNode;
		RearNode->NextNode = NewNode;
		RearNode = NewNode;
		RearNode->NextNode = NULL;
		}
	cout<<RearNode->Data;
	cout<<"\n Status : Mission SuccessFull ."
		<<"\n Node Inserted At Rear SuccessFully . "
		<<"\n Press Any Key To Continue . ";
	getch();
}
/**********************************************************************/
//删除链表头节点
template< class NodeType >
bool DoubleLinkList< NodeType >::RemoveFromFront()
{
	cout<<"\n Operation : Removal Of A Node From Front Of A Double Linked List .\n";
	if( IsEmpty() )
		{
		cout<<"\n Invalid Operation ."
			<<"\n Status : Mission Failed . "
			<<"\n Press Any Key To Continue . ";
		getch();
		return false;
		}
	else
		{
		Node< NodeType > *CurrentNode = FirstNode;
		if( FirstNode == RearNode )
			FirstNode = RearNode = NULL;
		else
			{
			cout<<"\n Data In The First Node Is : ";
			cout<<CurrentNode->Data;
			FirstNode = FirstNode->NextNode;
			FirstNode->PreviousNode = NULL ;
			}
		delete CurrentNode;
		cout<<"\n Status : Mission SuccessFull ."
			<<"\n Node Removed From Front Of The Doubled Linked List Successfully ";
		return true;
		}
		cout<<"\n Press Any Key To Continue . ";
		getch();
}
/**********************************************************************/
//删除链表中节点
template< class NodeType >
bool DoubleLinkList< NodeType >::RemoveFromMiddle()
{
	cout<<"\n Operation : Insertion Of A Node In The Middle Of The Linked List .\n";
	if( IsEmpty() )
		{
		cout<<"\n Invalid Operation ."
			<<"\n Status : Mission Failed . "
			<<"\n Press Any Key To Continue . ";
		getch();

		return false;
		}
	else
		{
		Node< NodeType > *CurrentNode = FirstNode,*TempNode,*TEmpNode;
		int Middle = (LenghtOfDoubleLinkList()/2),Position = 0;
		while( Position != Middle )
			{
			CurrentNode = CurrentNode->NextNode;
			Position++;
			}
		TempNode = CurrentNode ;
		CurrentNode = CurrentNode->NextNode ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -