双链表的实现,插入,删除,遍历,析构

定义有序的双链表类,链表中存储整型数据,创建带头结点的有序双链表,要求包含以下成员函数:

双链表的构造函数(非空的链表,输入数据为0,表示输入结束)

插入操作(将一个数据元素插入到有序的双链表中,插入之后链表仍然有序,输入数据为0表示插入操作结束)

按值删除节点(考虑有重复值的情况)

双链表的遍历操作

双链表的析构

#include <iostream>
#include<algorithm>
using namespace std;
struct node
{
	int data;
	node* prior, * next;
};

class dlist
{
public:

	dlist(int a[], int n);
	void inset(int n);
	void Delete(int n);
	void print();
	~dlist();
private:
	node* first;
	int sum;
};

dlist::dlist(int a[], int n)
{
	sum = n;
	node* p;
	first = new node;
	first->next = NULL;
	first->prior = NULL;
	p = first;
	for (int i = 0; i <n; i++)
	{
		node* s = NULL;
		s = new node;
		s->data = a[i];
		s->prior = p;
		s->next = NULL;
		p->next = s;
		p = s;
	}
	p->next = NULL;
}

void dlist::inset(int n)//插入
{
	node* p,* q;
	int flag = 0;
	q = first;
	p = first->next;
	while (p != NULL)
	{
		if (n <= p->data)
		{
			node* s = new node;
			s->data = n;
			s->next = p;
			s->prior = p->prior;
			p->prior->next = s;
			p->prior = s;
			sum++;
			flag = 1;
			break;
		}
		q = q->next;
		p = p->next;
	}
	if (flag == 0)
	{
		node* s = new node;
		s->next = q->next;
		s->data = n;
		s->prior = q;
		q->next = s;
		sum++;
	}
}

void dlist::Delete(int n)
{
	int h1 = sum;
	node* p = first->next;
	node* q;

	while (p != NULL)
	{
		if (p->data == n)
		{
			q = p;
			p->prior->next = p->next;
			if (p->next != NULL)
				p->next->prior = p->prior;
			delete q;

			sum--;
		}
		p = p->next;
	}
}

void dlist::print()
{
	node* p = first->next;
	while (p!= NULL)
	{
		cout << p->data << " ";
		p = p->next;
	}
	cout <<endl;
}

dlist::~dlist()
{
	node* p = first;
	while (p != NULL)
	{
		first = first->next;
		delete p;
		p = first;
	}
}
int main()
{
	int a[10000], i = 0;
	int x, xx;
	while (cin >> a[i]&&a[i]!=0)
	{
		i++;
	}
	sort(a, a + i);
	dlist l(a, i);
	l.print();
	while (cin >> x && x != 0)
	{
		l.inset(x);
	}
	l.print();
	while (cin >> xx && xx != 0)
	{
		l.Delete(xx);
	}
	l.print();
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表是一种常见的数据结构,它由若干个节点组成,每个节点中包含了一个数据元素和一个指向下一个节点的指针。链表可以用来实现栈、队列等数据结构。 以下是链表插入遍历、查找和删除操作的具体实现插入操作: 在链表插入一个新的节点通常需要以下步骤: 1. 创建一个新的节点,赋值为要插入的数据。 2. 找到要插入的位置,即在哪两个节点之间插入节点。 3. 将新节点的指针指向下一个节点,并将前一个节点的指针指向新节点。 代码实现: ```python class Node: def __init__(self, data=None): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert(self, data): new_node = Node(data) if self.head is None: self.head = new_node return current_node = self.head while current_node.next is not None: current_node = current_node.next current_node.next = new_node ``` 遍历操作: 遍历链表通常需要以下步骤: 1. 从头节点开始,依次遍历每个节点。 2. 对于每个节点,执行相应的操作,例如打印节点中的数据。 代码实现: ```python class LinkedList: def __init__(self): self.head = None ... def traverse(self): if self.head is None: print("Linked list is empty") return current_node = self.head while current_node is not None: print(current_node.data) current_node = current_node.next ``` 查找操作: 在链表中查找一个节点通常需要以下步骤: 1. 从头节点开始,依次遍历每个节点。 2. 对于每个节点,判断其包含的数据是否与要查找的数据相等。 3. 如果找到了相等的节点,返回该节点;否则,返回 None。 代码实现: ```python class LinkedList: def __init__(self): self.head = None ... def search(self, data): if self.head is None: return None current_node = self.head while current_node is not None: if current_node.data == data: return current_node current_node = current_node.next return None ``` 删除操作: 在链表删除一个节点通常需要以下步骤: 1. 找到要删除节点,即在哪两个节点之间删除节点。 2. 将前一个节点的指针指向后一个节点。 代码实现: ```python class LinkedList: def __init__(self): self.head = None ... def delete(self, data): if self.head is None: return if self.head.data == data: self.head = self.head.next return current_node = self.head while current_node.next is not None: if current_node.next.data == data: current_node.next = current_node.next.next return current_node = current_node.next ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值