用C++實現(xiàn)單向循環(huán)鏈表的解決方法
更新時間:2013年05月29日 15:28:59 作者:
本篇文章是對用C++實現(xiàn)單向循環(huán)鏈表的解決方法進行了詳細的分析介紹,需要的朋友參考下
用C++實現(xiàn)一個單向循環(huán)鏈表,從控制臺輸入整型數(shù)字,存儲在單項循環(huán)鏈表中,實現(xiàn)了求鏈表大小。
不足之處,還望指正!
// TestSound.cpp : 定義控制臺應用程序的入口點。
//實現(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;
}
//復制構(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;
}
不足之處,還望指正!
復制代碼 代碼如下:
// TestSound.cpp : 定義控制臺應用程序的入口點。
//實現(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;
}
//復制構(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)文章
C++靜態(tài)成員變量和靜態(tài)成員函數(shù)的使用方法總結(jié)
下面小編就為大家?guī)硪黄狢++靜態(tài)成員變量和靜態(tài)成員函數(shù)的使用方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01STL priority_queue(優(yōu)先隊列)詳解
這篇文章主要介紹了 STL priority_queue(優(yōu)先隊列)詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10深入學習C++智能指針之shared_ptr與右值引用的方法
智能指針的核心實現(xiàn)技術(shù)是引用計數(shù),每使用它一次,內(nèi)部引用計數(shù)加1,每析構(gòu)一次內(nèi)部的引用計數(shù)減1,減為0時,刪除所指向的堆內(nèi)存,今天通過本文給大家分享C++智能指針之shared_ptr與右值引用的方法,需要的朋友跟隨小編一起看看吧2021-07-07c++ 數(shù)字類型和字符串類型互轉(zhuǎn)詳解
今天小編就為大家分享一篇講解c++ 數(shù)字類型和字符串類型互轉(zhuǎn)的文章,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-09-09C語言中帶頭雙向循環(huán)鏈表基本操作的實現(xiàn)詳解
無頭單向非循環(huán)鏈表結(jié)構(gòu)簡單,一般不會單獨用來存數(shù)據(jù)。而帶頭雙向循環(huán)鏈表的結(jié)構(gòu)較為復雜,一般用在單獨存儲數(shù)據(jù)。本文將介紹帶頭雙向循環(huán)鏈表的基本操作,需要的可以參考一下2022-11-11C++11/14如何使用typedef和using定義類型別名和別名模版
這篇文章主要介紹了C++11/14如何使用typedef和using定義類型別名和別名模版2023-04-04