C++双向链表的实现

用C++实现双向链表代码如下:

//双向链表 
#include<iostream>
#include<cstdlib>
using namespace std;
struct node{
	int data1;
	node *next;
	node *prior; 
}; 
class singlelink{
	private:
		node data;
		int length;
	public:
		singlelink(){
			length=0;
			data.data1=0;
			data.next=NULL;
			data.prior=NULL;
		}
		~singlelink(){};
		bool isempty(){
			if(length==0)
				return true;
			else 
			 	return false;
		}
		int returnlength(){
			return length;
		}
		bool insert(int position, int num);
		bool deletenode(int position);
		int returnnum(int position);
		bool deleteall();
		void headinsert(int num);
		void rearinsert(int num);
		//int returnlast();
		
};
/*	bool int returnlast(){
		return 
	}*/
	bool singlelink::insert(int position, int num){
		if(position>length+1)
			return false;
		else{
			node* probe=&data;
			for(int i=1;i<position;i++){
				probe=probe->next;
			}
			node* newnode=new node;
			newnode->data1=num;
			newnode->next=probe->next;
			probe->next->prior=newnode;
			newnode->prior=probe;
			
			probe->next=newnode;
			length++;
		}
		return true;
	}
	bool  singlelink::deletenode(int position){
		if(position>length)
			return false;
		else{
			node* probe=&data;
			node* temp=NULL;
			for(int i=1;i<position;i++){
				probe=probe->next;
			}
			temp=probe->next;
			probe->next=probe->next->next; 
			temp->next->prior=probe;
			free(temp);
			length--;
			return true;
			
		}	
	}
	int singlelink::returnnum(int position){
		node* probe=&data;
		for(int i=1;i<=position;i++){
			probe=probe->next;
		}
		return probe->data1;
	}
	bool singlelink::deleteall(){
		node* probehead=&data;
		node* proberear=NULL;
		while(probehead->next!=NULL){
			proberear=probehead->next;
			free(probehead);
			probehead=proberear;
			length--;
		}
		free(probehead);
		if(length==0)
			return true;
		else
			return false;
	}
	void singlelink::headinsert(int num){
         if(length==0){
            node* probe=&data;
            node* newnode=new node;
            newnode->data1=num;
           	newnode->next=probe->next;
            probe->next=newnode;
            newnode->prior=probe;                       
         }
         else{
		node* probe=&data;
		node* newnode=new node;
		newnode->data1=num;
		newnode->next=probe->next;
		probe->next->prior=newnode;
		newnode->prior=probe;
		probe->next=newnode;
        }
		length++;
	}
	void singlelink::rearinsert(int num){
		node* probe=&data;
		for(int i=1;i<=length;i++){
			probe=probe->next;
		}
		node* newnode=new node;
		newnode->data1=num;
		newnode->next=NULL;
		probe->next=newnode;
		newnode->prior=probe;
		length++;
	}
	int main(){
		singlelink a1,a2;
		if(a1.isempty())
		cout<<"是空链表"<<endl;
		for(int i=1, j=10;i<21;i++,j++){
			a1.headinsert(j);
			a2.rearinsert(j);
		}
		cout<<"a1,a2的长度分别是"<<a1.returnlength()<<"  "<<a2.returnlength()<<endl;
		cout<<"a1成员为"<<endl; 
		for (int i=1;i<=a1.returnlength();i++){
		cout<<a1.returnnum(i)<<" ";
		}
		cout<<endl;
		cout<<"a2成员为"<<endl; 
		for (int i=1;i<=a2.returnlength();i++){
		cout<<a2.returnnum(i)	<<" ";
		}
		cout<<endl;
		a1.deletenode(9);
		cout<<"删除第9node后a1成员为"<<endl; 
		for (int i=1;i<=a1.returnlength();i++){
		cout<<a1.returnnum(i)	<<" ";
		}
		cout<<endl;
		a1.insert(9,520);
		cout<<"插入第9node后a1成员为"<<endl; 
		for (int i=1;i<=a1.returnlength();i++){
		cout<<a1.returnnum(i)	<<" ";
		}
		cout<<endl;
		if(a1.deleteall())
		cout<<"删除a1成功"<<endl;
		getchar();
		return 0;
		
	}
	

实现的过程中遇到了一点小麻烦:

在实现头插法时因为这里加的有prior指针所以不能直接因为头结点而与其他节点同样处理,要单独处理。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C++ 实现双向链表的通讯录的示例代码: ```c++ #include <iostream> #include <string> using namespace std; // 定义联系人结构体 struct Contact { string name; string phone; }; // 定义双向链表节点结构体 struct ListNode { Contact data; ListNode* prev; ListNode* next; ListNode(Contact value): data(value), prev(nullptr), next(nullptr) {} }; // 定义双向链表类 class DoubleLinkedList { private: ListNode* head; ListNode* tail; int size; public: DoubleLinkedList(): head(nullptr), tail(nullptr), size(0) {} ~DoubleLinkedList() { ListNode* p = head; while (p != nullptr) { ListNode* q = p->next; delete p; p = q; } head = tail = nullptr; size = 0; } void append(Contact value) { ListNode* node = new ListNode(value); if (tail == nullptr) { head = tail = node; } else { tail->next = node; node->prev = tail; tail = node; } size++; } void remove(ListNode* node) { if (node == nullptr) return; if (node == head) { head = head->next; if (head != nullptr) { head->prev = nullptr; } else { tail = nullptr; } } else if (node == tail) { tail = tail->prev; if (tail != nullptr) { tail->next = nullptr; } else { head = nullptr; } } else { node->prev->next = node->next; node->next->prev = node->prev; } delete node; size--; } void print() { ListNode* p = head; while (p != nullptr) { cout << "Name: " << p->data.name << ", Phone: " << p->data.phone << endl; p = p->next; } } int getSize() { return size; } }; int main() { DoubleLinkedList contacts; Contact c1 = {"Alice", "123456789"}; Contact c2 = {"Bob", "234567890"}; Contact c3 = {"Charlie", "345678901"}; contacts.append(c1); contacts.append(c2); contacts.append(c3); contacts.print(); cout << "Size: " << contacts.getSize() << endl; // 删除 Bob 的联系方式 ListNode* p = contacts.head; while (p != nullptr) { if (p->data.name == "Bob") { contacts.remove(p); break; } p = p->next; } contacts.print(); cout << "Size: " << contacts.getSize() << endl; return 0; } ``` 上述代码实现了一个基本的双向链表类,其中节点结构体 `ListNode` 包括了联系人结构体 `Contact` 和前驱节点和后继节点指针。在主函数中,我们创建了三个联系人并添加到双向链表中,然后打印出链表中所有联系人的信息。接着,我们使用循环遍历链表,找到名字为 Bob 的联系人并删除它,最后再次打印出链表中剩余的联系人信息和链表大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值