用C++實現(xiàn)單向循環(huán)鏈表的解決方法
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次
用C++實現(xiàn)一個單向循環(huán)鏈表,從控制臺輸入整型數(shù)字,存儲在單項循環(huán)鏈表中,實現(xiàn)了求鏈表大小。
不足之處,還望指正!
復(fù)制代碼 代碼如下:
// TestSound.cpp : 定義控制臺應(yīng)用程序的入口點。
//實現(xiàn)單向循環(huán)鏈表
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//定義鏈表一個節(jié)點的結(jié)構(gòu)體
template <class T>
struct NODE
{
T data;//節(jié)點的數(shù)據(jù)域
NODE* next;//節(jié)點的指針域
};
//自定義鏈表容器(含有的方法與C++不盡相同)
template <class T>
class MyList
{
public:
//構(gòu)造函數(shù),初始化一個頭結(jié)點,data為空,next指向第一個節(jié)點
MyList()
{
phead = new NODE<T>;
phead->data = NULL;
phead->next = phead;
}
//析構(gòu)函數(shù),將整個鏈表刪除,這里采用的是正序撤銷
~MyList()
{
NODE<T>* p = phead->next;
while (p != phead)
{
NODE<T>* q = p;
p = p->next;
delete q;
}
delete phead;
}
//復(fù)制構(gòu)造函數(shù)
MyList(MyList& mylist)
{
NODE<T>* q = mylist.phead->next;
NODE<T>* pb = new NODE<T>;
this->phead = pb;
while (q != mylist.phead)
{
NODE<T>* p = new NODE<T>;
p->data = q->data;
p->next = phead;
pb->next = p;
pb = p;
q = q->next;
}
}
//返回list表的大小
int get_size();
//將用戶輸入的integer數(shù)據(jù),插入list表中
void push_back();
//將list表中的元素輸出
void get_elements();
private:
NODE<T>* phead;
};
//返回list表的大小
template <class T>
int MyList<T>::get_size()
{
int count(0);
NODE<T>* p = phead->next;
while (p != phead)
{
count ++;
p = p->next;
}
return count;
}
//將用戶輸入的integer數(shù)據(jù),插入list表中
template <class T>
void MyList<T>::push_back()
{
int i;
cout << "Enter several integer number, enter ctrl+z for the end: "<< endl;
NODE<T>* p = phead;
while (cin >> i)
{
NODE<T>* q = new NODE<T>;
p->next = q;
q->data = i;
q->next = phead;
p = q;
}
}
//將list表中的元素輸出
template<class T>
void MyList<T>::get_elements()
{
NODE<T>* q = phead->next;
while (q != phead)
{
cout << q->data << " ";
q = q->next;
}
cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
MyList<int> mylist;
mylist.push_back();
MyList<int> mylist2(mylist);
mylist.get_elements();
mylist2.get_elements();
cout << endl << mylist.get_size() << endl;
return 0;
}
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計-用棧實現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10HDOJ 1443 約瑟夫環(huán)的最新應(yīng)用分析詳解
- 01-10深入理解C++中常見的關(guān)鍵字含義


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法