创建双向链表类,实现添加、修改排序等操作

题目描述:
创建双向链表类,该类有默认构造函数、类的拷贝函数、类的析构函数、实现链表添加数据、升序排序、查找链表中某个节点及删除链表中某个节点的操作(禁用STL及String类))。

“dLinkList.h”

#pragma once
#ifndef _DLINKLIST_H
#define _DLINKLIST_H

//节点
typedef struct node {
	int data;
	node *next = nullptr;              //后驱指针
	node *prev = nullptr;              //前驱指针
}node;

//双向链表
class DuLinkList {
public:
	DuLinkList();                           //默认构造函数
	DuLinkList(const DuLinkList & obj);     //拷贝构造函数
	~DuLinkList();                          //析构函数
	void headAdd(int temp);                 //头插法
	void ptrAdd(int temp);			        //尾插法
	void arbAdd(int tem, int temp);			//在任意位置插入数据
	void LinkSort();				        //升序排序
	void LinkSearch(int tem);               //查找节点
	void LinkDelete(int tem);               //删除节点
	void ShowLinkList();			        //输出链表
private:
	int len;//链表长度
	node *head;//指向头节点的指针
	node *ptr;//指向尾节点的指针
};
#endif // !_DLINKLIST_H

"dLinkList.cpp"

#include"pch.h"
#include"dLinkList.h"
#include<iostream>

//默认构造函数
DuLinkList::DuLinkList() {
	head = new node();        //创建头节点
	ptr = new node();		  //创建尾节点
	head->prev = NULL;        //头节点的前驱为空
	ptr->next = NULL;         //为节点的后驱为空
	head->next = ptr;         //头节点指向尾节点
	ptr->prev = head;         //尾节点指向头结点
	len = 0;				  //链表初始化长度为0
}

//拷贝构造函数
DuLinkList::DuLinkList(const DuLinkList & obj):head(NULL),ptr(NULL),len(0) {

		head = new node(); //创建节点
		while (obj.head->next != NULL) {
			head->next = new node();
			head->next->data = obj.head->data;
			obj.head->next = obj.head->next;
			len++;
		}
}

//析构函数
DuLinkList::~DuLinkList() {
	node *phead1 = head;
	node *phead2 = head;
	while (phead1) {
		phead1 = phead1->next;
		delete phead1;
		phead2 = phead1;
	}
	head = NULL;
	ptr = NULL;
	len = 0;
}

//头插法
void DuLinkList::headAdd(int temp) {
	node *phead = new node();		//新节点
	phead->data = temp;
	phead->prev = head;					//新节点前驱指向头节点
	phead->next = head->next;			//后驱指向原头节点后的节点
	head->next = phead;					//头节点指向新节点
	len++;
}

//尾插法
void DuLinkList::ptrAdd(int temp) {
	node *phead = new node();//新节点
	phead->data = temp;
	phead->prev = ptr->prev;
	phead->next = ptr;
	ptr->prev->next = phead;
	ptr->prev = phead;
	len++;
}

//在中间任意位置插入
void DuLinkList::arbAdd(int tem, int temp) {
	node *phead1 = head;
	for (int i = 0;i < tem-1;i++)
		phead1 = phead1->next;         //找到第tem-1个节点
	
	node *phead2 = head;
	for (int i = 0;i < tem;i++)
		phead2 = phead2->next;         //找到第tem个节点
	
	node *phead = new node();          //创建新节点
	phead->data = temp;					//节点数据赋值
	phead->prev=phead1;					
	phead->next=phead2;
	phead1->next=phead;
	phead2->prev=phead;
	len++;
}


//升序排序
void DuLinkList::LinkSort() {
	node *phead1 = new node();
	node *phead2 = new node();
	int t;
	for (phead1 = head->next;phead1->next!= ptr->next;phead1 = phead1->next) {
		for (phead2 = phead1->next;phead2->next!= ptr->next;phead2 = phead2->next) {
			if (phead1->data > phead2->data) {
				t = phead1->data;
				phead1->data = phead2->data;
				phead2->data = t;
			}
		}
	}
}

//查找节点
void DuLinkList::LinkSearch(int tem) {
	node *phead = head;
	for (int i = 0;i < tem;i++)
		phead = phead->next;
	std::cout << "已找到第" << tem << "个节点,该节点中的数据是:" << phead->data << std::endl;
}

//删除节点
void DuLinkList::LinkDelete(int temp) {
	if (temp > len) {
		std::cout << "temp大于链表的长度" << std::endl;
	}
	else {
		node *phead1 = head;
		node *phead2 = head->next;
		for (int i = 1;i < temp;i++) {
			phead1 = phead1->next;
			phead2 = phead2->next;
		}
		phead1->next = phead2->next;
		phead2->next->prev = phead1;
		delete phead2;
	}
}

//输出节点
void DuLinkList::ShowLinkList() {
	node *phead;
	phead = head->next;
	if (phead == NULL) {
	std::cout << "链表为空" << std::endl;
	}
	else {
		while (phead->next != ptr->next) {
			std::cout << phead->data << " ";
			phead=phead->next;
		}
		std::cout<<std::endl;
	}
}

"main.cpp"


#include "pch.h"
#include <iostream>
#include"dLinkList.h"
#include<time.h>
#include<stdlib.h>

int main()
{
	DuLinkList link;

	//头插法
	link.headAdd(5);
	link.headAdd(6);
	link.headAdd(10);
	link.headAdd(18);
	link.headAdd(9);
	link.headAdd(11);
	link.headAdd(7);
	link.headAdd(3);
	std::cout << "头插法后的链表:" << std::endl;
	link.ShowLinkList();
	std::cout << std::endl;

	std::cout << "升序排序后的链表:" << std::endl;
	link.LinkSort();
	link.ShowLinkList();
	std::cout << std::endl;

	std::cout << "查找链表第二个节点:" << std::endl;
	link.LinkSearch(2);
	std::cout << std::endl;

	link.LinkDelete(2);
	std::cout << "删除第二个节点后的链表:" << std::endl;
	link.ShowLinkList();
	std::cout << std::endl;

	std::cout << "将新数据5添加到链表的第二个节点:"<<std::endl;
	link.arbAdd(2, 5);
	link.ShowLinkList();
}

运行结果:

posted @ 2019-04-14 21:42  天真王二小  阅读(539)  评论(0编辑  收藏  举报