请问VC里的链表类怎么用?里面的数据是空的吗?只要加入自己的数据就可以了? 请问VC里的链表类怎么用?里面的数据是空的吗?只要加入自己的数据就可以了?小弟想找一个空的链表类用,里面没有数据,只要用成员函数加上自己的数据就行了,给小弟推荐一个 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 哈哈,今天我自己写了一个空链表类,如果加入自己的数据的话,稍加改动就行typedef struct LNode {int data;struct LNode *next;}LNode, *pLinkList;class LinkList {private:pLinkList m_pList;int m_listLength;public:LinkList();~LinkList();bool InitList ();bool DestroyList ();bool ClearList();bool IsEmpty ();int GetLength ();bool GetNode(int position, LNode** node);int LocateElem(int elem);bool SetNodeData(int position, int newData);bool GetNodeData(int position, int &data);bool InsertNode(int beforeWhich, int data);bool DeleteNode(int position);};#include <iostream.h>#include "1.h"LinkList::LinkList() {m_pList = NULL;m_listLength = 0;InitList();}LinkList::~LinkList() {if (!DestroyList()) { DestroyList();}}//初始化,分配一个头节点。bool LinkList::InitList() {if (!(m_pList = new LNode)) { return false;}m_pList->next = NULL;return true;}//销毁链表。bool LinkList::DestroyList() {if (!ClearList()) { return false;}delete m_pList;return true;}//判断链表是否为空。若为空,返回true,否则返回false。bool LinkList::IsEmpty() {if (m_pList->next == NULL) { return true;}return false;}//返回链表的中当前节点数。int LinkList::GetLength() {return m_listLength;}//将链表清空,释放当前所有节点。bool LinkList::ClearList() {if (m_pList == NULL) { return false;}LNode *pTemp = NULL;while (m_pList->next != NULL) { pTemp = m_pList->next; m_pList->next = pTemp->next; delete pTemp;}m_listLength = 0;return true;}//将position指定的节点内的数据设置为newData。//第一个有效节点的position为1。bool LinkList::SetNodeData(int position, int newData) {LNode *pTemp = NULL;if (!(GetNode(position, &pTemp))) { return false;}pTemp->data = newData;return true;}//得到指定位置节点的数据。//节点索引从1到listLength。bool LinkList::GetNodeData(int position, int &data) {LNode *pTemp = NULL;if (!(GetNode(position, &pTemp))) { return false;}data = pTemp->data;return true;}//在链表中插入一个节点。//插入的位置由beforeWhich指定,新节点插入在beforeWhich之前。//beforeWhich的取值在1到ListLength+1之间。bool LinkList::InsertNode(int beforeWhich, int data) {LNode *pTemp = NULL;if (beforeWhich < 1 || beforeWhich > (m_listLength + 1)) { return false;}if (!(GetNode(beforeWhich - 1, &pTemp))) { return false;}LNode *newNode = new LNode;newNode->data = data;newNode->next = pTemp->next;pTemp->next = newNode;m_listLength++;return true;}//删除一个指定的节点。//节点位置由position指定。//positon的值从1到listLength。//若链表为空或指定的节点不存在则返回false。bool LinkList::DeleteNode(int position) {if (position < 1 || position > m_listLength) { return false;}LNode *pTemp = NULL;if (!(GetNode(position - 1, &pTemp))) { return false;}LNode *pDel = NULL;pDel = pTemp->next;pTemp->next = pDel->next;delete pDel;m_listLength--;return true;}//得到指定位置节点的指针。bool LinkList::GetNode(int position, LNode **node) {LNode *pTemp = NULL;int curPos = -1;pTemp = m_pList;while (pTemp != NULL) { curPos++; if (curPos == position) break; pTemp = pTemp->next;}if (curPos != position) { return false;}*node = pTemp;return true;}//定位与指定数据相等的数据节点。//如果在当前链表中已经存在该数据则返回该数据节点的索引号。//若不存在这样的节点则返回0。//节点索引从0开始到listLength。int LinkList::LocateElem(int elem) {LNode *pTemp = NULL;int curIndex = 1;pTemp = m_pList->next;while ((pTemp != NULL) && (pTemp->data != elem)) { pTemp = pTemp->next; curIndex++;}if (pTemp == NULL) { return 0;}return curIndex;}void main(){LinkList l;l.InsertNode(1, 10);l.InsertNode(2, 20);l.InsertNode(3, 30);l.InsertNode(4, 40);cout << l.GetLength() << endl;int dataTemp = 0;for (int i = 1; i <= l.GetLength(); i++) { l.GetNodeData(i, dataTemp); cout << dataTemp << endl;} 问题来了,我想在节点里加入成员变量,比如下面结构可以变为typedef struct LNode {int data;struct LNode *next;}LNode, *pLinkList;typedef struct LNode {float a;int b;double c;int data;struct LNode *next;}LNode, *pLinkList;加了三个成员变量,a,b,c现在我想用Clist类,在他节点里也都加入三个成员变量,怎么做?能不能介绍一下在空类Clist里的节点里面,也就是它的数据域里面加成员变量的方法? 比如说,我想加入三个成员int a,int b,int c,应该怎么做?下面的用法对不对?Clist<int a,int b,int c>不对的话,应该怎么写? 你要先定义一个结构体,比如楼上的LNode ,把结构体作为链表的一个元素。然后再定义CList<LNode, LNode&> myList;MSDN上有详细说明。看一下就明白了 CList,Vector是最常用的两个链表 stl库 mfc 中有合适的 容器 或者 类 可以利用就直接用 自己写的类或者算法,不一定有他们的好,经不经得起考验是一个问题 window程序设计之“Hello World"编译链接错误 菜鸟求教:对话框编程的两个问题 散分 [求助]请问如何使我的窗口最大化后,可以根据系统任务栏大小进行调整? 如何在MFC扩展DLL中导出CFormView派生类? 在mfc里看到很类都没有实例化,就可以用了,谁给我解释解释?我原是做java和c#的,临时做vc了。 通过CLSID或其他GUID 生成对象 ===帮我看看这个程序,内存泄漏??==== 如何获得view的指针?程序里没有doc对象。 如何在属性表(CPropertySheet)中屏蔽回车键? 用什么可以在VC里面编写播放列表? 在CSDN里下载的书怎么才能打开
typedef struct LNode {
int data;
struct LNode *next;
}LNode, *pLinkList;class LinkList {
private:
pLinkList m_pList;
int m_listLength;
public:
LinkList();
~LinkList();
bool InitList ();
bool DestroyList ();
bool ClearList();
bool IsEmpty ();
int GetLength ();
bool GetNode(int position, LNode** node);
int LocateElem(int elem);
bool SetNodeData(int position, int newData);
bool GetNodeData(int position, int &data);
bool InsertNode(int beforeWhich, int data);
bool DeleteNode(int position);
};#include <iostream.h>
#include "1.h"LinkList::LinkList() {
m_pList = NULL;
m_listLength = 0;InitList();
}LinkList::~LinkList() {
if (!DestroyList()) {
DestroyList();
}
}//初始化,分配一个头节点。
bool LinkList::InitList() {
if (!(m_pList = new LNode)) {
return false;
}
m_pList->next = NULL;return true;
}//销毁链表。
bool LinkList::DestroyList() {
if (!ClearList()) {
return false;
}delete m_pList;return true;
}//判断链表是否为空。若为空,返回true,否则返回false。
bool LinkList::IsEmpty() {
if (m_pList->next == NULL) {
return true;
}
return false;
}//返回链表的中当前节点数。
int LinkList::GetLength() {
return m_listLength;
}//将链表清空,释放当前所有节点。
bool LinkList::ClearList() {
if (m_pList == NULL) {
return false;
}LNode *pTemp = NULL;
while (m_pList->next != NULL) {
pTemp = m_pList->next;
m_pList->next = pTemp->next;
delete pTemp;
}
m_listLength = 0;return true;
}//将position指定的节点内的数据设置为newData。
//第一个有效节点的position为1。
bool LinkList::SetNodeData(int position, int newData) {
LNode *pTemp = NULL;if (!(GetNode(position, &pTemp))) {
return false;
}pTemp->data = newData;return true;
}//得到指定位置节点的数据。
//节点索引从1到listLength。
bool LinkList::GetNodeData(int position, int &data) {
LNode *pTemp = NULL;if (!(GetNode(position, &pTemp))) {
return false;
}data = pTemp->data;return true;
}//在链表中插入一个节点。
//插入的位置由beforeWhich指定,新节点插入在beforeWhich之前。
//beforeWhich的取值在1到ListLength+1之间。
bool LinkList::InsertNode(int beforeWhich, int data) {
LNode *pTemp = NULL;if (beforeWhich < 1 || beforeWhich > (m_listLength + 1)) {
return false;
}if (!(GetNode(beforeWhich - 1, &pTemp))) {
return false;
}LNode *newNode = new LNode;
newNode->data = data;
newNode->next = pTemp->next;
pTemp->next = newNode;m_listLength++;return true;
}//删除一个指定的节点。
//节点位置由position指定。
//positon的值从1到listLength。
//若链表为空或指定的节点不存在则返回false。
bool LinkList::DeleteNode(int position) {
if (position < 1 || position > m_listLength) {
return false;
}LNode *pTemp = NULL;
if (!(GetNode(position - 1, &pTemp))) {
return false;
}LNode *pDel = NULL;
pDel = pTemp->next;
pTemp->next = pDel->next;
delete pDel;m_listLength--;return true;
}//得到指定位置节点的指针。
bool LinkList::GetNode(int position, LNode **node) {
LNode *pTemp = NULL;
int curPos = -1;pTemp = m_pList;
while (pTemp != NULL) {
curPos++;
if (curPos == position)
break;
pTemp = pTemp->next;
}if (curPos != position) {
return false;
}*node = pTemp;return true;
}//定位与指定数据相等的数据节点。
//如果在当前链表中已经存在该数据则返回该数据节点的索引号。
//若不存在这样的节点则返回0。
//节点索引从0开始到listLength。
int LinkList::LocateElem(int elem) {
LNode *pTemp = NULL;
int curIndex = 1;pTemp = m_pList->next;
while ((pTemp != NULL) && (pTemp->data != elem)) {
pTemp = pTemp->next;
curIndex++;
}if (pTemp == NULL) {
return 0;
}return curIndex;
}
void main()
{
LinkList l;l.InsertNode(1, 10);
l.InsertNode(2, 20);
l.InsertNode(3, 30);
l.InsertNode(4, 40);
cout << l.GetLength() << endl;int dataTemp = 0;
for (int i = 1; i <= l.GetLength(); i++)
{
l.GetNodeData(i, dataTemp);
cout << dataTemp << endl;
}
typedef struct LNode {
int data;
struct LNode *next;
}LNode, *pLinkList;typedef struct LNode {
float a;
int b;
double c;
int data;
struct LNode *next;
}LNode, *pLinkList;加了三个成员变量,a,b,c现在我想用Clist类,在他节点里也都加入三个成员变量,怎么做?能不能介绍一下在空类Clist里的节点里面,也就是它的数据域里面加成员变量的方法?
Clist<int a,int b,int c>
不对的话,应该怎么写?
MSDN上有详细说明。看一下就明白了
自己写的类或者算法,不一定有他们的好,经不经得起考验是一个问题