C++ 模板循环链表练习

abacabcc 2017-11-24 09:58:24
初学C++ 刚看了模板的视频,自己把以前写的一个循环链表改成模板,添加的节点的那个函数死活编不过去,求大神帮看看

#include <iostream>
#include <cstring>//memset
using namespace std;

template<typename T>
struct Node
{
T List_data;
Node *List_pPre;
Node *List_pNext;
};

template<typename T>
Node<T>* NewHead(const T data)
{
Node<T>* listHead = new Node<T>;
memset(listHead,0,sizeof(Node<T>));

listHead->List_data = data;
listHead->List_pNext = listHead;
listHead->List_pPre = listHead;

return listHead;
}

template<typename T>
void AddNod(Node<T>* listHead,const T data)
{
Node<T>* ptemp = listHead;

while(ptemp->List_pNext != listHead)
{
ptemp = ptemp->List_pNext;
}

Node<T>* m_Node = new Node<T>;
memset(m_Node,0,sizeof(Node<T>));

m_Node->List_data = data;
m_Node->List_pNext = listHead;
m_Node->List_pPre = ptemp;
ptemp->List_pNext = m_Node;
listHead->List_pPre = m_Node;

cout << "add data = " << data << " success!" << endl;
}

template<typename T>
void ListDis(const Node<T>* listHead)
{
Node<T>* ptemp = listHead->List_pNext;
int i = 1;

cout << "head" << " = " << listHead->List_data << endl;
while(ptemp!= listHead)
{
cout << "Node" << i << " = " << ptemp->List_data << endl;
ptemp = ptemp->List_pNext;
i++;
}
}

//加入a个节点后最多只能删a个节点,不能删除头节点
template<typename T>
void List_Delete_Node(Node<T>* listHead,T nNode)
{
int m_count = 0;

Node<T>* ptemp = listHead;
while(ptemp->List_pNext != listHead)
{
m_count++;
ptemp = ptemp->List_pNext;
}
if(0 >= nNode || m_count < nNode)
{
cout << "delete error!" << endl;
return;
}

ptemp = listHead;
m_count = nNode;
while(m_count != 0)
{
m_count--;
ptemp = ptemp->List_pNext;
}
ptemp->List_pNext->List_pPre = ptemp->List_pPre;
ptemp->List_pPre->List_pNext = ptemp->List_pNext;
delete ptemp;
cout << "delete Node " << nNode << "success!" << endl;
}
template<typename T>
void List_Delete_All(Node<T>* listHead)
{
Node<T>* ptemp = listHead;

while(ptemp->List_pNext != listHead)
{
ptemp = ptemp->List_pNext;
delete ptemp->List_pPre;
}
delete ptemp;
cout << "delete all list success!" << endl;
}
int main()
{
Node<int>* listHead;

listHead = NewHead(0);
AddNode(listHead,6); //E:\main.cpp:111: error: 'AddNode' was not declared in this scope
AddNode(listHead,a);
AddNode(listHead,9);
AddNode(listHead,10);
AddNode(listHead,11);
AddNode(listHead,12);
cout << "---------------------" << endl;

ListDis(listHead);
cout << "---------------------" << endl;

List_Delete_Node(listHead,2);
ListDis(listHead);
cout << "---------------------" << endl;

List_Delete_Node(listHead,2);
ListDis(listHead);
cout << "---------------------" << endl;
List_Delete_All(listHead);
return 0;
}

...全文
116 4 点赞 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
paschen 版主 2017-11-24
其次,AddNode(listHead, a);这句里的a你并没有定义,不知是什么
  • 打赏
  • 举报
回复
paschen 版主 2017-11-24
template<typename T> void AddNod(Node<T>* listHead, const T data) 改成: template<typename T> void AddNode(Node<T>* listHead, const T data) 注意函数名的拼写
  • 打赏
  • 举报
回复
heronism 2017-11-24
引用 1 楼 hdt 的回复:
你申明的是 AddNod
高手,正解
  • 打赏
  • 举报
回复
你申明的是 AddNod
  • 打赏
  • 举报
回复
相关推荐
发帖
C++ 语言
加入

6.0w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2017-11-24 09:58
社区公告
暂无公告