【数据结构-双链表】

DoubleLinkList.h

#pragma once
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>

#define DATASIZE 64

class Node {
public:
	char m_data1[DATASIZE];
	char m_data2[DATASIZE];
	Node* next;	/* 指向下一个节点的指针 */
	Node* prev; /* 指向上一个节点的指针 */
	Node(const char* data1, const char* data2) {
		strcpy(this->m_data1, data1);
		strcpy(this->m_data2, data2);
	}
};

class LinkList {
private:
	int m_len;			/* 链表长度 */
	Node* m_head;		/* 头节点 */
	Node* m_tail;		/* 尾节点 */
public:
	LinkList();
	~LinkList();

	/* 增 */
	void push_back(const char *data1, const char *data2);
	void push_front(const char *data1, const char *data2);
	void push_insert(const char *data1, const char *data2, int index);

	/* 删 */
	void pop_back();
	void pop_front();
	void pop_insert(int index);

	/* 改 */
	void change(const char *data1, const char *data2, int index);

	/* 查 */
	void show();

private:
	/**
	* @brief 获取指定的节点
	*
	* @param index	输入的序号
	* @return Node* 返回第index+1的节点
	*/
	Node *get(int index);
};

DoubleLinkList.cpp

#include "Doublelinklist.h"
#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

LinkList::LinkList() : m_len(0), m_head(NULL), m_tail(NULL) {
}

LinkList::~LinkList() {
	static unsigned int count = 0;
	Node* np = m_head;
	Node* p;
	while (np) {
		count++;
		p = np;
		np = p->next;
		delete p;
		m_len--;
	}
	cout << "delete count num : " << count << endl;
}

void LinkList::push_back(const char * data1, const char * data2) {
	if (sizeof(data1) > DATASIZE || sizeof(data2) > DATASIZE) {
		cout << "error, The string you entered is too long" << endl;
		return;
	}
	Node* np = new Node(data1, data2);
	if (NULL == np) {
		cout << "error, You are running out of memory space" << endl;
		return;
	}
	if (0 == m_len) {
		m_head = np;
	}
	else {
		m_tail->next = np;
		np->prev = m_tail;
	}
	m_tail = np;
	m_len++;
}

void LinkList::push_front(const char * data1, const char * data2) {
	if (sizeof(data1) > DATASIZE || sizeof(data2) > DATASIZE) {
		cout << "error, The string you entered is too long" << endl;
		return;
	}
	Node* np = new Node(data1, data2);
	if (NULL == np) {
		cout << "error, You are running out of memory space" << endl;
		return;
	}
	if (0 == m_len) {
		m_tail = np;
	}
	else {
		m_head->prev = np;
		np->next = m_head;
	}
	m_head = np;
	m_len++;
}

void LinkList::push_insert(const char * data1, const char * data2, int index) {
	if (sizeof(data1) > DATASIZE || sizeof(data2) > DATASIZE) {
		cout << "error, The string you entered is too long" << endl;
		return;
	}
	Node* np = new Node(data1, data2);
	if (NULL == np) {
		cout << "error, You are running out of memory space" << endl;
		return;
	}
	if (index == 0) {
		push_front(data1, data2);
	}
	else if (index == m_len) {
		push_back(data1, data2);
	}
	else {
		Node* np = new Node(data1, data2);
		if (np == NULL) {
			cout << "error, You are running out of memory space" << endl;
			return;
		}
		Node *next = get(index - 1);
		Node *prev = next->prev;
		prev->next = np;
		np->prev = prev;
		np->next = next;
		next->prev = np;
		m_len++;
	}
}

void LinkList::pop_back() {
	if (m_len < 0) {
		cout << "error, This linked list is empty" << endl;
		return;
	}
	Node* tp = get(m_len - 1);
	m_tail = m_tail->prev;
	m_tail->next = NULL;
	delete tp;
	m_len--;
}

void LinkList::pop_front() {
	if (m_len < 0) {
		cout << "error, This linked list is empty" << endl;
		return;
	}
	Node* tp = get(0);
	m_head = m_head->next;
	m_head->prev = NULL;
	delete tp;
	m_len--;
}

void LinkList::pop_insert(int index) {
	if (index < 0 || index > m_len) {
		cout << "error, You entered the wrong ordinal number" << endl;
		return;
	}
	if (0 == index) {
		pop_front();
	}
	else if (m_len == index) {
		pop_back();
	}
	else {
		Node* tp = get(index - 1);
		Node* prev = tp->prev;//关系是相互的,应该两个都建立
		Node* next = tp->next;
		prev->next = next;
		next->prev = prev;
		delete tp;
		m_len--;
	}
}

void LinkList::change(const char * data1, const char * data2, int index) {
	if (sizeof(data1) > DATASIZE || sizeof(data2) > DATASIZE) {
		cout << "error, The string you entered is too long" << endl;
		return;
	}
	if (index < 0 || index > m_len) {
		cout << "error, You entered the wrong ordinal number" << endl;
		return;
	}
	Node* tp = get(index - 1);
	memset(tp->m_data1, 0, DATASIZE);
	memset(tp->m_data2, 0, DATASIZE);
	strcpy(tp->m_data1, data1);
	strcpy(tp->m_data2, data2);
}

void LinkList::show() {
	printf("this linklist length = %d\n", this->m_len);
	Node* np = m_head;
	for (int i = 1; i <= m_len; i++) {
		printf("%s \t", np->m_data1);
		printf("%s \n", np->m_data2);
		np = np->next;//把下一个的地址再赋给np
	}
	printf("\n");
}

Node * LinkList::get(int index) {
	if (index < 0 || index > m_len) {
		printf("ERROR: index idex is out bounds \n");
		exit(0);
	}
	Node* np = m_head;
	while (index--) {
		np = np->next;
	}
	return np;//将这个节点返回
}

main.cpp

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

using namespace std;

int main() {
	LinkList A;
	A.push_back("cacd", "dha");
	A.push_back("abcas", "cdnac");
	A.push_back("cachdac", "11516ca");
	A.push_back("c67qfgcqe", "chr92hc");
	A.show();
	A.pop_back();
	A.pop_back();
	A.pop_front();
	A.show();
	A.push_front("zhang", "cnah");
	A.push_front("hui", "cnah");
	A.push_insert("hi", "hello", 2);
	A.show();
	A.change("jjj", "hhh", 2);
	A.show();
	A.pop_insert(2);
	A.show();
	system("pause");
	return 0;
}

运行结果


在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值