用C++實(shí)現(xiàn)單向循環(huán)鏈表的解決方法
更新時(shí)間:2013年05月29日 15:28:59 作者:
本篇文章是對(duì)用C++實(shí)現(xiàn)單向循環(huán)鏈表的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
用C++實(shí)現(xiàn)一個(gè)單向循環(huán)鏈表,從控制臺(tái)輸入整型數(shù)字,存儲(chǔ)在單項(xiàng)循環(huán)鏈表中,實(shí)現(xiàn)了求鏈表大小。
不足之處,還望指正!
// TestSound.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//實(shí)現(xiàn)單向循環(huán)鏈表
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//定義鏈表一個(gè)節(jié)點(diǎn)的結(jié)構(gòu)體
template <class T>
struct NODE
{
T data;//節(jié)點(diǎn)的數(shù)據(jù)域
NODE* next;//節(jié)點(diǎn)的指針域
};
//自定義鏈表容器(含有的方法與C++不盡相同)
template <class T>
class MyList
{
public:
//構(gòu)造函數(shù),初始化一個(gè)頭結(jié)點(diǎn),data為空,next指向第一個(gè)節(jié)點(diǎn)
MyList()
{
phead = new NODE<T>;
phead->data = NULL;
phead->next = phead;
}
//析構(gòu)函數(shù),將整個(gè)鏈表刪除,這里采用的是正序撤銷
~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;
}
不足之處,還望指正!
復(fù)制代碼 代碼如下:
// TestSound.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//實(shí)現(xiàn)單向循環(huán)鏈表
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//定義鏈表一個(gè)節(jié)點(diǎn)的結(jié)構(gòu)體
template <class T>
struct NODE
{
T data;//節(jié)點(diǎn)的數(shù)據(jù)域
NODE* next;//節(jié)點(diǎn)的指針域
};
//自定義鏈表容器(含有的方法與C++不盡相同)
template <class T>
class MyList
{
public:
//構(gòu)造函數(shù),初始化一個(gè)頭結(jié)點(diǎn),data為空,next指向第一個(gè)節(jié)點(diǎn)
MyList()
{
phead = new NODE<T>;
phead->data = NULL;
phead->next = phead;
}
//析構(gòu)函數(shù),將整個(gè)鏈表刪除,這里采用的是正序撤銷
~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;
}
相關(guān)文章
關(guān)于C/C++中的side effect(負(fù)效應(yīng))和sequence point(序列點(diǎn))
不知你在寫code時(shí)是否遇到這樣的問題?int i = 3; int x = (++i) + (++i) + (++i); 問x值為多少?進(jìn)行各種理論分析,并在編譯器上實(shí)踐,然而可能發(fā)現(xiàn)最終的結(jié)果是不正確的,也是不穩(wěn)定的,不同的編譯器可能會(huì)產(chǎn)生不同的結(jié)果。這讓人很頭疼2013-10-10
Qt中QStringList與QString的常用方法總結(jié)
這篇文章主要為大家總結(jié)了Qt中QString 與 (QStringList | QByteArray)之間的轉(zhuǎn)換,以及QString、QStringList的一些常用方法,感興趣的可以收藏一下2022-12-12
詳解C++ 拷貝構(gòu)造函數(shù)和賦值運(yùn)算符
本文主要介紹了拷貝構(gòu)造函數(shù)和賦值運(yùn)算符的區(qū)別,以及在什么時(shí)候調(diào)用拷貝構(gòu)造函數(shù)、什么情況下調(diào)用賦值運(yùn)算符。最后,簡單的分析了下深拷貝和淺拷貝的問題。有需要的朋友可以看下2016-12-12

