循环单链表C++实现

#pragma once
#ifndef Node_hpp
#define Node_hpp


typedef double value_data;
class Node
{

public:
	value_data Data;
	Node *Next;
};

#endif /* Node_hpp */

#pragma once
#include "node.hpp"
#include <iostream>


using namespace std;

class Circular_Linklist
{
public:
	typedef double value_data;

	Circular_Linklist();

	//运算符重载
	Circular_Linklist &operator=(Circular_Linklist& s)
	{
		swap(Head_ptr, s.Head_ptr);
		swap(Tail_ptr, s.Tail_ptr);
		return *this;
	}

	void Clear_List();				//清空链表
	void Print_List();				//打印链表

	void Create_List(int n);			//头插法
	void Delete_ListHead();				//头删法

/*
	Node *Find_List(value_data x);				//指定数值查找
	void Insert_List(Node* pos, value_data x);			//指定位置插入
	void Erase_List(Node* pos);							//指定位置删除
	int List_Length();									//链表长度*/
	~Circular_Linklist();

private:
	Node *Head_ptr;
	Node *Tail_ptr;
};



#include "Circular_Linklist.h"
#include <iostream>



Circular_Linklist::Circular_Linklist()
{
	Head_ptr = new Node();
	//Head_ptr->Data = 0;
	//Head_ptr->Next = NULL;
	Head_ptr->Next = Head_ptr;
};

void Circular_Linklist::Clear_List()
{
	Node *temp = Head_ptr;
	while (temp!=Head_ptr)
	{
		Node *Delete = temp;
		temp = temp->Next;
		delete Delete;
		Delete = NULL;
	}
};

void Circular_Linklist::Print_List()
{
	Node *temp = Head_ptr->Next;
	while (temp != Head_ptr)
	{
		cout << temp->Data << "->";
		temp = temp->Next;
	}
	cout << " NULL"<<endl;
};

void Circular_Linklist::Create_List(int n)
{
	
	Node *pnew, *ptemp = Head_ptr;
	int i = n;
	while (n-- > 0) {
		cout << "输入第" << i - n << "个结点值:";
		pnew = new Node;
		cin >> pnew->Data;
		pnew->Next = Head_ptr;
		ptemp->Next = pnew;
		ptemp = pnew;
	}
};

void Circular_Linklist::Delete_ListHead()
{
	if (Head_ptr->Next == Head_ptr)
	{
		cout << "空链表" << endl;
	}
	else
	{
		Node *temp = Head_ptr->Next;
		Node *pnew = temp;
		if (temp != Head_ptr)
		{
			pnew = temp->Next;
			Head_ptr->Next = pnew;
			delete temp;
			temp = pnew;

		}		
	}
}

Circular_Linklist::~Circular_Linklist()
{
	Clear_List();
};
#include "Circular_Linklist.h"
#include <iostream>

using namespace std;

int main()

{

	Circular_Linklist c;
	cout << " n为大于0的数" << endl;
	c.Create_List(4);
	
	//c.Create_ListHead(54.5);
	c.Print_List();
	c.Delete_ListHead();
	c.Delete_ListHead();
	c.Delete_ListHead();
	c.Print_List();
	system("pause");
	return 0;
};

在这里插入图片描述

posted @ 2021-01-19 13:31  code_witness  阅读(63)  评论(0)    收藏  举报