时间:2021-05-20
构造双向链表,不足之处,还望指正!
复制代码 代码如下:
// DoubleLinkedList.cpp : 定义控制台应用程序的入口点。
//构造双向链表,实现从控制台输入,插入,删除,求大小size等操作
#include "stdafx.h"
#include <iostream>
using namespace std;
//定义双向链表的节点
template<class T>
struct NODE
{
NODE<T>* pre;
T data;
NODE<T>* next;
};
//定义双向链表容器
template<class T>
class DoubleLinkedList
{
public:
DoubleLinkedList()
{
NODE<T>* q = new NODE<T>;
if (q == NULL)
{
cout << "Fail to malloc the head node." << endl;
return;
}
phead = q;
phead->pre = NULL;
phead->data = NULL;
phead->next = NULL;
T i;
cout << "Please input several integer number, input ctrl+z to the end: " << endl;
while (cin >> i)
{
NODE<T>* p = new NODE<T>;
if (p == NULL)
{
cout << "Fail to malloc a new node." << endl;
return;
}
p->data = i;
q->next = p;
p->pre = q;
p->next = NULL;
q = q->next;
}
}
//容器大小方法
int size()
{
NODE<T>* p = phead->next;
int count(0);
while (p != NULL)
{
count++;
p = p->next;
}
return count;
}
//输出DoubleLinkedLIst中的元素
void print_elements()
{
NODE<T>* p = phead->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
//插入一个元素到DoubleLinkedList中,任意合法位置插入
void insert_element(int i, T e)
{
if (i <= this->size())
{
NODE<T>* m = phead;
for (int j = 1; j < i; j ++)
{
m = m->next;
}
NODE<T>* n = m->next;
NODE<T>* p = new NODE<T>;
if (p == NULL)
{
cout << "Failed to malloc the node." << endl;
}
m->next = p;
p->pre = m;
p->data = e;
p->next = n;
n->pre = p;
}
else if (i == (this->size()+1))
{
NODE<T>* m = phead;
for (int j = 1; j < i; j++)
{
m = m->next;
}
NODE<T>* p = new NODE<T>;
if (p == NULL)
{
cout << "Failed to malloc the node." << endl;
}
m->next = p;
p->pre = m;
p->data = e;
p->next = NULL;
}
else
{
cout << "Please input the position number equals or smaller than " << size()+1 << endl;
}
}
//插入一个元素到DoubleLinkedList中,只在末尾插入
void insert_element(T e)
{
NODE<T>* m = phead;
for (int j = 1; j <= size(); j++)
{
m = m->next;
}
NODE<T>* p = new NODE<T>;
if (p == NULL)
{
cout << "Failed to malloc the node." << endl;
}
m->next = p;
p->pre = m;
p->data = e;
p->next = NULL;
}
//删除DoubleLinkedList中的一个元素
void delete_element(int i)
{
NODE<T>* p = phead;
for (int j = 0; j < i; j ++)
{
p = p->next;
if (p == NULL)
{//所要删除的元素超过list的范围
cout << "The size of the list is " << size() << " ,Please input the right number." << endl;
return;
}
}
if(p->next != NULL)
{//删除除最后一个以外的元素
NODE<T>* m = p->pre;
NODE<T>* n = p->next;
m->next = n;
n->pre = m;
delete p;
}
else
{//删除元素为最后一个
NODE<T>* m = p->pre;
m->next = NULL;
delete p;
}
}
private:
NODE<T>* phead;
};
int _tmain(int argc, _TCHAR* argv[])
{
//测试代码
DoubleLinkedList<int> mylist;
mylist.print_elements();
cout << "The size of the double linked list is : " << mylist.size() << endl;
mylist.insert_element(1, 50);
mylist.print_elements();
mylist.insert_element(6, 80);
mylist.print_elements();
mylist.insert_element(250);
mylist.print_elements();
mylist.delete_element(7);
mylist.print_elements();
return 0;
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
双向链表C++的实现本文是通过C++的知识实现数据结构中的双向链表,这里不多说了,代码注释很清楚,实现代码://doubleLinkListimplementw
本文实例讲述了C++双向循环列表用法。分享给大家供大家参考。具体如下:#includeusingnamespacestd;//结构体构造链表
本文实例为大家分享了C++实现单链表的构造代码,供大家参考,具体内容如下单链表的构造,包括最常用函数,setData(),Insert(),Remove(),g
示意图:python双向链表实现代码:复制代码代码如下:#!/usr/bin/python#-*-coding:utf-8-*-classNode(object
C/C++双链表之逆序的实例详解一、结点结构双向链表的数据结构定义如下:typedefstructnode{ElemTypedata;structnode*pr