template <class Type>
struct nodeType {
int info;
nodeType *next;
};
template <class Type>
class linkedListType {
friend ostream& operator<< (ostream&, const linkedListType<Type>&);
public:
const linkedListType<Type>& operator= (const linkedListType<Type>&);
void initializedList();
bool isEmptyList();
int length();
void destroyList();
Type front();
Type back();
bool search(const Type& searchItem);
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();
protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedListType<Type>& otherList);
};
主要函数实现
template <class Type>
bool linkedListType<Type>::search(const Type& searchItem) {
nodeType<Type> *current;
current = first;
while(current!=NULL) {
if(current->info == searchItem)
return true;
else
current = current->next;
}
return false;
}
template <class Type>
void linkedListType<Type>::deleteNode(const Type& deleteItem) {
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
bool found;
/*
* if the list is empty
*/
if(first == NULL)
cerr<<"Cannot delete from an empty list.\n";
/*
* if the list is not empty
*/
else {
/*
* if the delete item is equal to the first element of the list
*
*/
if(first->info == deleteItem) {
current = first;
first = first->next;
count--;
/*
* if after delete the first element of list
* the list become empty
* do something(make the last pointer to be NULL)
*/
if(first == NULL) {
last = NULL;
delete current;
}
}
/*
* if not equal to the first element of the list
*/
else {
found = false;
trailCurrent = first;
current = first->next;
/*
*squence search the list
*/
while(current!=NULL&&!found) {
if(current->info != deleteItem) {
trailCurrent = current;
current = current->next;
}else {
found = true;
}
}
/*
* if find the delete item in the list
*/
if(found) {
trailCurrent->next = current->next;
count--;
if(last==current)
last = trailCurrent;
delete current;
}else {
cout<<"Item to be deleted is not in the list."<<endl;
}
}
}
}
分享到:
相关推荐
c++链表队列的实现,c++链表队列的实现,c++链表队列的实现,c++链表队列的实现,c++链表队列的实现,c++链表队列的实现
链表ADT数据结构,符合ADT要求,头文件放函数结构和存储结构定义,cpp文件放实现,在控制台查看输出,C语言实现,轻松易懂,可用来初学或者作业完成
C++链表基于类实现多项式的加法和乘法; C ++ list class-based polynomial addition and multiplication.
用c++实现链表的建立、插入和删除操作。希望能对刚学数据结构的同学有点帮助。
各种形式的链表C++实现方式 ,以及其对应的代码 1.单向链表链表 2.单向循环链表 3.双向链表 内容非常详细
链表类 c++ 实现的 链表类 c++ 实现的 链表类 c++ 实现的
C++链表的具体实现,包含链表的正向和逆向创建,节点的插入与删除,节点的查找
c++链表的实现及链表的合并,自写链表的两个链表的合并函数
Singly Linked List: 1. DestroyList 2. InsertList 3. DeleteList 4. TraverseList 5. SearchList 6. ReverseList 7. IsLoopList 8. ReverseEvenList 9. FindMidNode ...5. TraverseList_DuL
C++实现的链表
实现的操作:有头链表; 实现的功能:增,删,改,查; 关于资源:我的文章中有部分解释,相关内容还可查看我的文章; 关于问题:有问题及时私信我,例如不能运行,运行失败等问题;
C++链表的基本操作,实现带头节点创建,插入,显示等功能
c++链表的反转,创建链表,插入链表,链表反转,可下载直接运行。
C++链表通讯录系统,适合新手学习,可以实现添加,修改,删除,查询,文件保存文件打开功能,拿来学习还不错
本程序 使用C++链表完成了 数据的录入和读取 适合新接触C++链表的人使用 我当初才学链表的时候 就看这个看明白的
c++ 链表应用 链表类源代码,实现了链表的插入、删除、查找、长度、显示、比较两个链表是否相等、翻转、连个链表的链接、迭代、链表的拷贝、赋值等操作。
c++,算法链表实现一元多项式相加,通过链表实现,非常好的一个程序
自己编写的链表类,声明和实现 功能: 创建链表,插入节点,删除节点,输出链表,求出链表长度
C++链表类 模板类 #include #include #include "LinkedList.h" using namespace std; template Node<T> *LinkedList<T>::GetNode(const T& item, Node* ptrNext) //生成新结点 { Node<T> *p; p = new Node...
链表的c++实现链表的c++实现链表的c++实现