精华内容
下载资源
问答
  • C++循环链表

    2019-11-24 15:05:15
    C++循环链表 单向循环链表和单向链表的区别: (1)单向链表为头指针,循环链表为尾指针,头指针指向头结点,尾指针指向终端结点; (2)为统一方便操作,单向链表设置头结点,单向循环链表设置头结点和尾结点; ...

    C++循环链表

    单向循环链表和单向链表的区别

    (1)单向链表为头指针,循环链表为尾指针,头指针指向头结点,尾指针指向终端结点;

    (2)为统一方便操作,单向链表设置头结点,单向循环链表设置头结点和尾结点;

    (3)设置尾结点后,尾指针指向尾结点,插入,删除等操作不用移动尾指针。

    在单向链表中,链表最后一个结点的next指针指向NULL,而所谓的循环链表就是将最后一个结点本来指向空的改变其指针指向从而指向首结点(注意不是头结点,头结点是方便链表操作而存在的结构体,循环链表是一种特殊的单向链表)循环链表可以继承自单向链表。

    循环链表主要实现难点:

    (1)插入数据的位置为0时,头结点和尾结点的next指针均指向新结点。

    (2)删除位置为0的结点时,头结点和尾结点的next指针都指向为位置为1的结点,然后安全删除位置为0的结点。

    //cyclelist.h  定义链表
    #include <iostream>
    using namespace std;
    class ListNode{
    	public:
    		ListNode(){
    		};
    		ListNode(int e,ListNode* p=NULL):data(e),next(p){
    		}
    		~ListNode(){
    		}
    	public:
    		int data;
    		ListNode* next;
    }; 
    class CycleList{
    	public:
    		CycleList();
    		~CycleList();
    		//判断列表是否为空
    		bool isEmpty();
    		//头部添加元素
    		void appendHead(int element);
    		//尾部添加元素
    		void appendTail(int element);
    		//获取元素位置
    		int getElemPos(int element);
    		//获取指定位置的ListNode指针
    		ListNode* getElement(int pos);
    		//向指定位置插入元素
    		void InsertElem(int pos,int element);
    		//s删除首个与element相同的元素
    		void deleteElem(int element);
    		//移除pos位置上的元素
    		void removeElem(int pos);
    		//清空列表
    		void clear();
    		//打印列表
    		void printList();
    		int getLength(){
    			return m_length;
    		} 
    	private:
    		ListNode* head;
    		ListNode* rear;
    		int m_length;
    };
    
    //cyclelist.cpp  实现链表的操作
    #include "cyclelist.h"
    //循环链表的构造函数,构造表头 
    CycleList::CycleList():m_length(0){
    	head=new ListNode;
    	head->next=head;
    	head->data=0;
    	rear=NULL;
    }
    //循环链表析构函数,将链表清空 
    CycleList::~CycleList(){
    	if(head){
    		ListNode* p=head->next;
    		while(p!=rear){
    			head->next=p->next;
    			delete p;
    			p=head->next;
    		}
    		delete head;
    		head=NULL;
    		delete rear;
    		rear=NULL;
    	}
    }
    //判断链表是否为空 
    bool CycleList::isEmpty(){
    	if(m_length>0)
    		return false;
    	return true;
    }
    //头部添加元素
    void CycleList::appendHead(int element){
    	if(head==head->next){
    		//空表
    		ListNode* node=new ListNode(element); 
    		head->next=node;
    		node->next=head;
    		rear=node;
    	}else{
    		ListNode* node=new ListNode(element,head->next);
    		rear->next=node;
    		head->next=node;
    	}
    	++m_length;
    }
    //在链表位插入元素 
    void CycleList::appendTail(int element){
    	if(head==head->next){
    		//判断空表
    		ListNode* node=new ListNode(element);
    		head->next=node;
    		rear=node; 
    	}else{
    		ListNode* node=new ListNode(element,head->next);
    		rear->next=node;
    		rear=node;
    		node->next=head->next;
    	}
    	++m_length;
    }
    //获取元素位置
    int CycleList::getElemPos(int element){
    	if(head==head->next){
    		return -1;
    	}
    	ListNode* node=head->next;
    	for(int i=0;node!=rear&&i<m_length;++i,node=node->next){ //寻找相同元素的位置 
    		if(element==node->data)
    			return i;
    	} 
    	if(rear->data==element){
    		return m_length-1;
    	}
    	return -1;
    }
    //获取指定位置的ListNode指针
    ListNode* CycleList::getElement(int pos){
    	if(pos<0||pos>=m_length||head==head->next)
    		return NULL;
    	ListNode* node=head->next;
    	for(int i=0;i<pos;++i,node=node->next){  //寻找当前结点的位置 
    		;
    	}
    	return node;
    }
    //输入插入位置并在该位置后插入元素 
    void CycleList::InsertElem(int pos,int element){
    	if(pos<0||pos>m_length)
    		return;
    	if(pos==0){  //在头结点插入 
    		head->next=new ListNode(element,head->next);  //新建一个结点 
    		if(m_length==0){
    			rear=head->next;
    		}
    		rear->next=head->next;  //并将尾结点指向头结点 
    	}else if(pos==m_length){  //在尾部插入 
    		ListNode* node=new ListNode(element);
    		if(m_length==0){
    			head->next=node;
    			node->next=node;
    			rear=node;
    		}else{
    			rear->next=node;
    			rear=node;
    			node->next=head->next;
    		}
    	}else{   //在中间插入元素 
    		ListNode* node=head->next;
    		for(int i=0;i<pos-1;++i,node=node->next){  //寻找插入位置的前一个结点 
    			;
    		}
    		ListNode* newNode=new ListNode(element,node->next);  //将新结点指向下一个位置 
    		node->next=newNode; //前一个结点指向新结点 
    	}
    	++m_length;
    }
    //删除第一个与element相等的元素 
    void CycleList::deleteElem(int element){  
    	if(head==head->next)
    		return;
    		ListNode* node=head->next;
    		ListNode* helpNode=head;
    		int pos=-1;
    		//遍历整个链表 
    		while(node!=rear){  //寻找pos所在的位置 
    			++pos;
    			if(node->data==element){
    				break;
    			}
    			node=node->next;
    			helpNode=helpNode->next;
    		}
    		if(m_length==1){  //只有一个结点时候的删除 
    			if(rear->data==element){
    				head->next=head;  
    				rear=NULL;
    				delete node;
    				node=NULL;
    			}
    		}
    		else{
    			if(node!=rear){
    				helpNode->next=node->next;  //将需要删除的结点的指针赋给help结点 
    				if(pos==0){
    					rear->next=node->next;  //删除头结点 
    				}
    				delete node;
    				node=NULL;
    			}else{
    				if(rear->data==element){  //删除尾结点 
    					helpNode->next=node->next;
    					delete node;
    					node=NULL;
    					rear=helpNode;
    				}
    			}
    		}
    		--m_length;
    }
    //移除pos的元素 
    void CycleList::removeElem(int pos){
    	if(pos<0||pos>=m_length||head==head->next)  //判断是否超出链表的长度 
    		return;
    	ListNode* node=head->next;
    	if(m_length==1){  //判断是非只有一个结点并删除当前结点 
    		delete node;
    		node=NULL;
    		head->next=head;
    		rear=NULL;
    	}else{
    		if(pos==0){ //判断输入的位置 
    			ListNode* p=head->next->next;  //将p指向需要移除的下一个位置 
    			head->next=p;
    			rear->next=p;
    			delete node;
    			node=NULL;
    		}else if(pos==m_length-1){  //移除最后一个元素 
    			for(int i=0;i<pos-1;i++){ //寻找最后一个元素 
    				node=node->next;
    			}
    			delete rear; //删除尾元素,并将 
    			rear=node;
    			node->next=head->next;
    		}else{
    			for(int i=0;i<pos-1;i++){
    				node=node->next;
    			}
    			ListNode* temp=node->next;
    			node->next=temp->next;
    			delete temp;
    			temp=NULL;
    		}
    	}
    	--m_length;
    }
    //清空链表 
    void CycleList::clear(){  
    	ListNode* node=head->next;  //定义表头 
    	while(node!=rear){  
    		head->next=node->next;  //改变指针指向位置,使得指针指向下一个结点 
    		delete node;
    		node=head->next;  //将第一个结点定义为node 
    	}
    	head->next=NULL;  //将表头置为NULL 
    	delete rear;
    	rear=NULL;
    	m_length=0;
    }
    //遍历链表 
    void CycleList::printList(){
    	cout<<"PrintList: "<<endl;
    	if(head!=head->next){    //链表是否为空 
    		ListNode* p=head->next;
    		while(p){
    			cout<<p->data<<" ";  //输入当前值,同时p向后移动一位 
    			p=p->next;
    			if(p==head->next){  //查看p是否已经遍历完 
    				break;
    			}
    		}
    	}
    	cout<<endl;
    }
    //测试链表
    #include <iostream>
    #include "cyclelist.h"
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int main(int argc, char** argv) {
    	CycleList testList;
    	for (int i = 0; i < 7; ++i)
    	{
    		testList.appendHead(i);
    		testList.appendTail(i);
    	}
    	testList.printList();
     
    	cout << "GetElemPos(6): " << testList.getElemPos(6) << endl;
    	cout << "GetElemPos(5): " << testList.getElemPos(5) << endl;
    	cout << "GetElemPos(0): " << testList.getElemPos(0) << endl;
     
    	cout << "GetElement(0): " << testList.getElement(0)->data << endl;
    	cout << "GetElement(13): " << testList.getElement(13)->data << endl;
    	cout << "GetElement(7): " << testList.getElement(7)->data << endl;
     
    	testList.removeElem(0);
    	testList.printList();
    	testList.removeElem(12);
    	testList.printList();
     
    	testList.removeElem(7);
    	testList.printList();
     
    	testList.deleteElem(5);
    	testList.printList();
    	testList.deleteElem(5);
    	testList.printList();
    	testList.deleteElem(0);
    	testList.printList();
     
    	CycleList testList2;
    	testList2.appendHead(2);
    	testList2.appendHead(1);
    	testList2.removeElem(1);
    	testList2.printList();
     
    	CycleList testList3;
    	testList3.appendHead(1);
    	testList3.deleteElem(1);
    	testList3.printList();
     
    	testList3.InsertElem(0, 3);
    	testList3.InsertElem(0, 2); 
    	testList3.InsertElem(0, 1);
    	testList3.InsertElem(3, 3);
    	testList3.InsertElem(4, 2);
    	testList3.InsertElem(5, 1);
    	testList3.printList();
     
    	CycleList testList4;
    	testList4.InsertElem(0,1);
    	testList4.InsertElem(1,2);
    	testList4.InsertElem(1, 3);
    	testList4.InsertElem(3, 4);
    	testList4.printList();
     
    	testList.clear();
    	testList.printList();
    	return 0;
    }

    结果如下:

    注意:(1)单向循环链表销毁时,需要将头结点和尾结点删除;(2)单向循环链表插入,删除,遍历,清空链表时,条件从头结点或第一节点始,判断指针是否达到尾结点;(3)清空链表时,最后将头结点指向尾结点;(4)销毁链表时,条件从头结点始,判断条件为指针是否到达头结点,最后将指针置空。

     

     

    展开全文
  • C++ 循环链表

    2020-05-15 17:31:40
    //模板递归推导数列 template<int ...args> struct idx_holder { }; template<int N, class Holder> struct idx_seq { typedef idx_seq<N, Holder> seq; }; ... typedef ty..
    
    //模板递归推导数列
    template<int ...args>
    struct idx_holder {
    
    };
    
    template<int N, class Holder>
    struct idx_seq {
    	typedef idx_seq<N, Holder> seq;
    };
    
    template<int N, int...Ns>
    struct idx_seq<N, idx_holder<Ns...>> {
    	typedef typename idx_seq<N - 1, idx_holder<N, Ns...>>::seq seq;
    };
    
    template<int... Ns>
    struct idx_seq<1, idx_holder<Ns...>> {
    	typedef idx_holder<1, Ns...> seq;
    };
    
    template<int N>
    constexpr typename idx_seq<N, idx_holder<>>::seq make_idx_seq()
    {
    	return {};
    }
    
    void pf(int a, int b, int c)
    {
    	std::cout << a << b << c << std::endl;
    
    }
    
    template<int... idSqs>
    void use_seq(idx_holder<idSqs...>)
    {
    	pf(idSqs...);
    }
    
    
    template<class T>
    struct DbNode {
    	T data;
    	DbNode<T> *pred, *next;
    	DbNode(const T &value, DbNode<T> *a = nullptr, DbNode<T> *b = nullptr) :\
    		data(value), pred(a), next(b) {}
    	DbNode(DbNode<T> *a = nullptr, DbNode<T> *b = nullptr) :pred(a), next(b) {}
    };
    
    template <typename T>
    class CircDbList {
    public:
    	CircDbList() :m_pHead(nullptr) {};
    	~CircDbList() { Release(); };
    
    	void Release();
    	DbNode<T> *CreateHead(const T& data);
    	DbNode<T> *Add(DbNode<T> *node, BOOL bIsEnd = TRUE);
    	DbNode<T> *Add(const T& data, BOOL bInEnd = TRUE);
    	BOOL Remove(DbNode<T> *node, BOOL bIsDel = TRUE);
    	BOOL PopNode(DbNode<T> *node);
    	void Print(int n);
    
    private:
    	DbNode<T> *m_pHead;
    };
    
    template <typename T>
    void CircDbList<T>::Print(int n) {
    	if (n)           //正序打印
    	{
    		std::cout << "Positive order: ";
    		DbNode<T> *tmp = m_pHead;
    		while (tmp->next != m_pHead) {
    			std::cout << tmp->data << "->";
    			tmp = tmp->next;
    		}
    		std::cout << tmp->data << "->over." << std::endl;
    
    	}
    	else            //逆序打印
    	{
    		DbNode<T> *tmp = m_pHead;
    		std::cout << "Reverse sequence: ";
    		while (tmp->pred != m_pHead) {
    			std::cout << tmp->data << "->";
    			tmp = tmp->pred;
    		}
    		std::cout << tmp->data << "->over." << std::endl;
    	}
    }
    
    template <typename T>
    DbNode<T> *CircDbList<T>::CreateHead(const T& data) {
    	if (nullptr == m_pHead) {
    		auto head = new DbNode<T>(data);
    		head->pred = head;
    		head->next = head;
    		m_pHead = head;
    	}
    
    	return  m_pHead;
    }
    
    template<typename T>
    void CircDbList<T>::Release() {
    	if (m_pHead != nullptr) {
    		auto end = m_pHead->pred;
    		DbNode<T> *delnode = nullptr;
    		while (end != m_pHead) {
    			delnode = end;
    			end = end->pred;
    			delete delnode;
    		}
    
    		delete m_pHead;
    		m_pHead = nullptr;
    	}
    }
    
    template <typename T>
    DbNode<T> *CircDbList<T>::Add(DbNode<T> *newnode, BOOL bIsEnd) {
    
    	if (nullptr == newnode)
    		return m_pHead;
    
    	if (nullptr == m_pHead) {
    		m_pHead = newnode;
    		m_pHead->next = m_pHead;
    		m_pHead->pred = m_pHead;
    		return m_pHead;
    	}
    
    	//是否是在尾部加入节点的
    	if (bIsEnd) {
    		auto end = m_pHead->pred;
    		end->next->pred = newnode;
    		newnode->pred = end;
    		newnode->next = end->next;
    		end->next = newnode;
    	}
    	else {
    		m_pHead->pred->next = newnode;
    		newnode->next = m_pHead;
    		newnode->pred = m_pHead->pred;
    		m_pHead->pred = newnode;
    		m_pHead = newnode;
    	}
    
    	return m_pHead;
    }
    
    
    template <typename T>
    DbNode<T> *CircDbList<T>::Add(const T& data, BOOL bIsEnd) {
    	if (nullptr == m_pHead) {
    		return CreateHead(data);
    	}
    
    	auto newnode = new DbNode<T>(data);
    	return Add(newnode, bIsEnd);
    }
    
    template <typename T>
    BOOL CircDbList<T>::Remove(DbNode<T> *node, BOOL bIsDel) {
    	if (node == nullptr)
    		return FALSE;
    
    	auto end = m_pHead->pred;
    	if (node == m_pHead && end != m_pHead) {
    		m_pHead->next->pred = end;
    		m_pHead = m_pHead->next;
    		end->next = m_pHead;
    		if (bIsDel)
    			delete node;
    		return TRUE;
    	}
    	else if(node == m_pHead && end == m_pHead) {
    		if (bIsDel)
    			delete node;
    		m_pHead = nullptr;
    		return TRUE;
    	}
    
    	DbNode<T> *find = nullptr;
    	while (end != m_pHead) {
    		if (end == node) {
    			node->pred->next = node->next;
    			node->next->pred = node->pred;
    			if (bIsDel)
    				delete node;
    			return TRUE;
    		}
    		end = end->pred;
    	}
    
    	return FALSE;
    }
    
    template<typename T>
    BOOL CircDbList<T>::PopNode(DbNode<T> *node) {
    	if (Remove(node, FALSE)) {
    		return TRUE;
    	}
    
    	return FALSE;
    }
    
    
    
    template<typename T>
    struct Node {
    	Node<T> *next;
    	T data;
    
    	Node(const T& d) :next(nullptr), data(d) {};
    };
    
    template<typename T>
    class CircList {
    public:
    	CircList() :m_pHead(nullptr), m_pEnd(nullptr) {};
    	~CircList() { Release(); };
    
    	Node<T>* CreateHead(const T &data);
    	void Release();
    	Node<T>* Add(Node<T>* node, bool bIsEnd = true);
    	Node<T>* Add(const T& data, bool bIsEnd = true);
    	bool Remove(Node<T>* node, bool bIsDel = true);
    	bool Remove(const T& data, bool bIsDel = true);
    	bool PopNode(Node<T>* node);
    	void Print();
    
    private:
    	Node<T>* m_pHead;
    	Node<T>* m_pEnd;
    };
    
    template<typename T>
    void CircList<T>::Print() {
    	if (m_pHead == nullptr) {
    		std::cout << "list is nullptr" << std::endl;
    		return;
    	}
    
    	auto cur = m_pHead;
    	do {
    		std::cout << cur->data << "->";
    		cur = cur->next;
    	} while (cur != m_pEnd);
    
    	if (m_pHead != m_pEnd)
    		std::cout << m_pEnd->data << "->";
    
    	std::cout << "over" << std::endl;
    }
    
    template<typename T>
    Node<T>* CircList<T>::CreateHead(const T &data) {
    	if (m_pHead == nullptr) {
    		m_pHead = new Node<T>(data);
    		m_pHead->next = m_pHead;
    		m_pEnd = m_pHead;
    		return m_pHead;
    	}
    
    	return m_pHead;
    }
    
    template<typename T>
    void CircList<T>::Release() {
    	if (m_pHead == nullptr) {
    		return;
    	}
    
    	if (m_pHead == m_pEnd) {
    		delete m_pHead;
    		m_pHead = nullptr;
    		m_pEnd = nullptr;
    		return;
    	}
    
    	auto cur = m_pHead;
    	while (cur != m_pEnd) {
    		auto temp = cur->next;
    		delete cur;
    		cur = temp;
    	}
    
    	delete m_pEnd;
    	m_pHead = nullptr;
    	m_pEnd = nullptr;
    	return;
    }
    
    template<typename T>
    Node<T>* CircList<T>::Add(Node<T> *node, bool bIsEnd) {
    	if (m_pHead == nullptr) {
    		m_pHead = node;
    		m_pHead->next = node;
    		m_pEnd = node;
    		return m_pHead;
    	}
    
    	if (bIsEnd) {
    		node->next = m_pEnd->next;
    		m_pEnd->next = node;
    		m_pEnd = node;
    	}
    	else {
    		node->next = m_pHead;
    		m_pEnd->next = node;
    		m_pHead = node;
    	}
    
    	return m_pHead;
    }
    
    template<typename T>
    Node<T>* CircList<T>::Add(const T &data, bool bIsEnd) {
    	if (m_pHead == nullptr) {
    		return CreateHead(data);
    	}
    
    	auto node = new Node<T>(data);
    	return Add(node, bIsEnd);
    }
    
    template<typename T>
    bool CircList<T>::Remove(const T& data, bool bIsDel) {
    	if (m_pHead == nullptr) {
    		return false;
    	}
    
    	if (m_pHead->data == data && m_pEnd == m_pHead) {
    		if (bIsDel)
    			delete m_pHead;
    		m_pHead = nullptr;
    		m_pEnd = nullptr;
    		return true;
    	}
    
    	if (m_pHead->data == data) {
    		m_pEnd->next = m_pHead->next;
    		if (bIsDel)
    			delete m_pHead;
    		m_pHead = m_pEnd->next;
    		return true;
    	}
    
    	auto cur = m_pHead;
    	auto pre = m_pEnd;
    	auto next = m_pHead->next;
    	while (cur != m_pEnd) {
    		if (cur->data == data) {
    			pre->next = next;
    			if (cur == m_pHead) {
    				m_pHead = next;
    			}
    
    			if (cur == m_pEnd) {
    				m_pEnd = pre;
    			}
    
    			if (bIsDel)
    				delete cur;
    			return true;
    		}
    
    		pre = cur;
    		cur = cur->next;
    		next = cur->next;
    	}
    
    	return false;
    }
    
    template<typename T>
    bool CircList<T>::Remove(Node<T>* node, bool bIsDel) {
    	if (m_pHead == nullptr) {
    		return false;
    	}
    
    	if (m_pHead == node && m_pEnd == m_pHead) {
    		if (bIsDel)
    			delete m_pHead;
    		m_pHead = nullptr;
    		m_pEnd = nullptr;
    		return true;
    	}
    
    	if (m_pHead == node) {
    		m_pEnd->next = m_pHead->next;
    		if (bIsDel)
    			delete m_pHead;
    		m_pHead = m_pEnd->next;
    		return true;
    	}
    
    	auto cur = m_pHead;
    	auto pre = m_pEnd;
    	auto next = m_pHead->next;
    	while (cur != m_pEnd) {
    		if (cur == node) {
    			pre->next = next;
    			if (cur == m_pHead) {
    				m_pHead = next;
    			}
    
    			if (cur == m_pEnd) {
    				m_pEnd = pre;
    			}
    
    			if (bIsDel)
    				delete cur;
    			return true;
    		}
    
    		pre = cur;
    		cur = cur->next;
    		next = cur->next;
    	}
    
    	return false;
    }
    
    template<typename T>
    bool CircList<T>::PopNode(Node<T>*node) {
    	if (Remove(node, false)) {
    		return true;
    	}
    	
    	return false;
    }

     

    展开全文
  • 主要介绍了C++循环链表之约瑟夫环的实现方法,对于学习数据结构与算法有一定的借鉴价值,需要的朋友可以参考下
  • C++循环链表的实现

    2013-05-23 18:26:40
    C++循环链表的实现,内含.h和.cpp文件各一个,导入IDE自动生成解决方案即可
  • C++循环链表解决约瑟夫环问题

    C++循环链表解决约瑟夫环问题

    参考文章:

    (1)C++循环链表解决约瑟夫环问题

    (2)https://www.cnblogs.com/hubavyn/p/4438088.html


    备忘一下。


    展开全文
  • C++循环链表circular linked list实现算法C++循环链表circular linked list实现算法完整源码(定义,实现,main函数测试) C++循环链表circular linked list实现算法完整源码(定义,实现,main函数测试) #include &...

    C++循环链表circular linked list实现算法完整源码(定义,实现,main函数测试)

    #include <iostream>
    using namespace std;
    
    struct node {
        int val;
        node *next;
    };
    
    node *start;
    
    void insert(int x) {
        node *t = start;
    
        if (start != NULL) {
            while (t->next != start) {
                t = t->next;
            }
            node *n = new node;
            t->next = n;
            n->val = x;
            n->next = start;
        } else {
            node *n = new node;
            n->val = x;
            start = n;
            n->next = start;
        }
    }
    
    void remove(int x) {
        node *t = start;
        node *p;
        while (t->val != x) {
            p = t;
            t = t->next;
        }
        p->next = t->next;
        delete t;
    }
    
    void search(int x) {
        node *t = start;
        int found = 0;
        while (t->next != start) {
            if (t->val == x) {
                cout << "\nFound";
                found = 1;
                break;
            }
            t = t->next;
        }
        if (found == 0) {
            cout << "\nNot Found";
        }
    }
    
    void show() {
        node *t = start;
        do {
            cout << t->val << "\t";
            t = t->next;
        } while (t != start);
    }
    
    int main() {
        int choice, x;
        do {
            cout << "\n1. Insert";
            cout << "\n2. Delete";
            cout << "\n3. Search";
            cout << "\n4. Print";
            cout << "\n\nEnter you choice : ";
            cin >> choice;
            switch (choice) {
            case 1:
                cout << "\nEnter the element to be inserted : ";
                cin >> x;
                insert(x);
                break;
            case 2:
                cout << "\nEnter the element to be removed : ";
                cin >> x;
                remove(x);
                break;
            case 3:
                cout << "\nEnter the element to be searched : ";
                cin >> x;
                search(x);
                break;
            case 4:
                show();
                break;
            }
        } while (choice != 0);
    
        return 0;
    }
    
    
    展开全文
  • C++ 循环链表基本算法

    2018-11-24 19:41:00
    C++ 循环链表基本算法 #ifndef CLinkList_h #define CLinkList_h #include <iostream> using namespace std; template <class T> struct Node{ T data; struct Node<T> * next; }; ...
  • C和C++循环链表

    2020-11-02 09:20:09
    循环链表的定义:将单链表中最后一个数据元素的next指针指向第一个元素 循环链表拥有单链表的所有操作 创建链表 销毁链表 获取链表长度 清空链表 获取第pos个元素操作 插入元素到位置pos 删除位置pos处的元素 新增...
  • 前天学了循环链表,一直再没管,今天来应用一下,感觉自己写麻烦了… 循环链表与单链表的区别就在于表尾指针不是NULL,而是等于first。这就产生了一个好处就是可以从任何节点开始访问到所有节点。 代码 #include<...
  • 1.构建循环链表 2.设置计数器count模拟报数 3.如果报数为k则删除结点且重置count 4.重复操作3直到只剩下一个结点为止 代码 void Joseph(int n,int k) { //1.构建循环链表 Node<int>* head = new Node<int...
  • [问题描述] 约瑟夫(Joseph)问题的一种描述是:编号为1,2,…,n的n个人按顺时针方向围坐一圈,...分析:这道题使用链表做的话可以很容易想到循环链表,接下来构造循环链表并解决问题。 1、首先我们应该先创建一个链
  • C/C++ 循环链表

    2019-01-06 21:17:11
    创建 # include # include typedef int Item ; typedef struct node { Item data ...//在链表开始处插入结点 ...//添加尾指针简化链表操作 ...//移动尾指针,使其保持在链表尾端 }
  • //循环链表的翻转函数 template void circularchain::reverse() { chainNode*pr,*r; chainNode*p = firstNode->next; //把链表分成两段 firstNode->next = firstNode; //从p里面拿出节点插入只有一个节点的...
  • #include <cstdio> #include <iostream> #include <cstdlib> using namespace std; struct node { int index; node * next; }; typedef node * LNode; class CLL { public: ...
  • 2.4.3.1循环链表插入元素的分析 1)普通插入元素(和单链表是一样的) 2)尾插法(和单链表是一样的,单链表的写法支持尾插法; 因:辅助指针向后跳length次,指向最后面那个元素) CircleList_Insert(list, ...
  • 题目:设计以不带头结点的循环链表表示队列,并且只设置一个指针指向队尾结点,但不设头指针。设计相应的入队和出队操作的算法。 C++的实现: #include<iostream> using namespace st...
  • 利用单向循环链表存储结构模拟此过程,按照出列的顺序印出各人的编号。 测试数据 M的初值为20;n=7,7各人的密码依次为3,1,7,2,4,8,4,首先m值为6(正确出栈顺序为6,1,4,7,2,3,5)

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,000
精华内容 800
关键字:

c++循环链表

c++ 订阅