C++ 構(gòu)造雙向鏈表的實(shí)現(xiàn)代碼
更新時(shí)間:2013年05月29日 15:22:09 作者:
本篇文章是對(duì)C++中構(gòu)造雙向鏈表的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
構(gòu)造雙向鏈表,不足之處,還望指正!
// DoubleLinkedList.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//構(gòu)造雙向鏈表,實(shí)現(xiàn)從控制臺(tái)輸入,插入,刪除,求大小size等操作
#include "stdafx.h"
#include <iostream>
using namespace std;
//定義雙向鏈表的節(jié)點(diǎn)
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;
}
//插入一個(gè)元素到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;
}
}
//插入一個(gè)元素到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中的一個(gè)元素
void delete_element(int i)
{
NODE<T>* p = phead;
for (int j = 0; j < i; j ++)
{
p = p->next;
if (p == NULL)
{//所要?jiǎng)h除的元素超過list的范圍
cout << "The size of the list is " << size() << " ,Please input the right number." << endl;
return;
}
}
if(p->next != NULL)
{//刪除除最后一個(gè)以外的元素
NODE<T>* m = p->pre;
NODE<T>* n = p->next;
m->next = n;
n->pre = m;
delete p;
}
else
{//刪除元素為最后一個(gè)
NODE<T>* m = p->pre;
m->next = NULL;
delete p;
}
}
private:
NODE<T>* phead;
};
int _tmain(int argc, _TCHAR* argv[])
{
//測(cè)試代碼
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;
}
復(fù)制代碼 代碼如下:
// DoubleLinkedList.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//構(gòu)造雙向鏈表,實(shí)現(xiàn)從控制臺(tái)輸入,插入,刪除,求大小size等操作
#include "stdafx.h"
#include <iostream>
using namespace std;
//定義雙向鏈表的節(jié)點(diǎn)
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;
}
//插入一個(gè)元素到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;
}
}
//插入一個(gè)元素到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中的一個(gè)元素
void delete_element(int i)
{
NODE<T>* p = phead;
for (int j = 0; j < i; j ++)
{
p = p->next;
if (p == NULL)
{//所要?jiǎng)h除的元素超過list的范圍
cout << "The size of the list is " << size() << " ,Please input the right number." << endl;
return;
}
}
if(p->next != NULL)
{//刪除除最后一個(gè)以外的元素
NODE<T>* m = p->pre;
NODE<T>* n = p->next;
m->next = n;
n->pre = m;
delete p;
}
else
{//刪除元素為最后一個(gè)
NODE<T>* m = p->pre;
m->next = NULL;
delete p;
}
}
private:
NODE<T>* phead;
};
int _tmain(int argc, _TCHAR* argv[])
{
//測(cè)試代碼
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;
}
您可能感興趣的文章:
- c++雙向鏈表操作示例(創(chuàng)建雙向鏈、雙向鏈表中查找數(shù)據(jù)、插入數(shù)據(jù)等)
- C++ 模版雙向鏈表的實(shí)現(xiàn)詳解
- 關(guān)于雙向鏈表的增刪改查和排序的C++實(shí)現(xiàn)
- C++ STL入門教程(2) list雙向鏈表使用方法(附程序代碼)
- C++ 實(shí)現(xiàn)雙向鏈表的實(shí)例
- C++將二叉樹轉(zhuǎn)為雙向鏈表及判斷兩個(gè)鏈表是否相交
- 深入解析C++的循環(huán)鏈表與雙向鏈表設(shè)計(jì)的API實(shí)現(xiàn)
- C++雙向鏈表實(shí)現(xiàn)簡(jiǎn)單通訊錄
- C++實(shí)現(xiàn)雙向鏈表
- C++數(shù)據(jù)結(jié)構(gòu)之雙向鏈表
相關(guān)文章
深入了解C語言中的動(dòng)態(tài)內(nèi)存分配
這篇文章主要為大家詳細(xì)介紹了C語言中的動(dòng)態(tài)內(nèi)存分配,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語言有一定的幫助,需要的可以參考一下2022-06-06
Qt?timerEvent實(shí)現(xiàn)簡(jiǎn)單秒表功能
這篇文章主要為大家詳細(xì)介紹了Qt?timerEvent實(shí)現(xiàn)簡(jiǎn)單秒表功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
C++實(shí)現(xiàn)LeetCode(228.總結(jié)區(qū)間)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(228.總結(jié)區(qū)間),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語言詳解熱門考點(diǎn)結(jié)構(gòu)體內(nèi)存對(duì)齊
C?數(shù)組允許定義可存儲(chǔ)相同類型數(shù)據(jù)項(xiàng)的變量,結(jié)構(gòu)是?C?編程中另一種用戶自定義的可用的數(shù)據(jù)類型,它允許你存儲(chǔ)不同類型的數(shù)據(jù)項(xiàng),本篇讓我們來了解C?的結(jié)構(gòu)體內(nèi)存對(duì)齊2022-04-04
C++簡(jiǎn)單實(shí)現(xiàn)與分析二叉搜索樹流程
二叉搜索樹作為一個(gè)經(jīng)典的數(shù)據(jù)結(jié)構(gòu),具有鏈表的快速插入與刪除的特點(diǎn),同時(shí)查詢效率也很優(yōu)秀,所以應(yīng)用十分廣泛。本文將詳細(xì)講講二叉搜索樹的C++實(shí)現(xiàn),需要的可以參考一下2022-08-08
淺談帶緩沖I/O 和不帶緩沖I/O的區(qū)別與聯(lián)系
下面小編就為大家?guī)硪黄獪\談帶緩沖I/O 和不帶緩沖I/O的區(qū)別與聯(lián)系。小編覺得挺不錯(cuò)的現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01

