永发信息网

利用c++设计一个算法解决对单链表的操作

答案:1  悬赏:20  手机版
解决时间 2021-05-09 01:46
利用c++设计一个算法解决对单链表的操作
最佳答案

// file chain.h


#ifndef Chain_
#define Chain_


#include <iostream.h>
template <class T> class Chain;


template <class T>
class ChainNode {
friend Chain<T>;
private:
T data;
ChainNode<T> *link;
};


template<class T>
class Chain {
public:
Chain() {first = 0;}
~Chain();
bool IsEmpty() const {return first == 0;}
int Length() const;
bool Find(int k, T& x) const;
int Search(const T& x) const;
Chain<T>& Delete(int k, T& x);
Chain<T>& Insert(int k, const T& x);
void Output(ostream& out) const;
private:
ChainNode<T> *first; // pointer to first node
};


template<class T>
Chain<T>::~Chain()
{// Chain destructor. Delete all nodes in chain.
ChainNode<T> *next; // next node
while (first) {
next = first->link;
delete first;
first = next;
}
}


template<class T>
int Chain<T>::Length() const
{// Return the number of elements in the chain.
ChainNode<T> *current = first;
int len = 0;
while (current) {
len++;
current = current->link;
}
return len;
}


template<class T>
bool Chain<T>::Find(int k, T& x) const
{// Set x to the k'th element in the chain.
// Return false if no k'th; return true otherwise.
if (k < 1) return false;
ChainNode<T> *current = first;
int index = 1; // index of current
while (index < k && current) {
current = current->link;
index++;
}
if (current) {x = current->data;
return true;}
return false; // no k'th element
}


template<class T>
int Chain<T>::Search(const T& x) const
{// Locate x. Return position of x if found.
// Return 0 if x not in the chain.
ChainNode<T> *current = first;
int index = 1; // index of current
while (current && current->data != x) {
current = current->link;
index++;
}
if (current) return index;
return 0;
}


template<class T>
Chain<T>& Chain<T>::Delete(int k, T& x)
{// Set x to the k'th element and delete it.
// Throw OutOfBounds exception if no k'th element.
if (k < 1 || !first)
throw OutOfBounds(); // no k'th

// p will eventually point to k'th node
ChainNode<T> *p = first;


// move p to k'th & remove from chain
if (k == 1) // p already at k'th
first = first->link; // remove
else { // use q to get to k-1'st
ChainNode<T> *q = first;
for (int index = 1; index < k - 1 && q;
index++)
q = q->link;
if (!q || !q->link)
throw OutOfBounds(); // no k'th
p = q->link; // k'th
q->link = p->link;} // remove from chain


// save k'th element and free node p
x = p->data;
delete p;
return *this;
}


template<class T>
Chain<T>& Chain<T>::Insert(int k, const T& x)
{// Insert x after the k'th element.
// Throw OutOfBounds exception if no k'th element.
// Pass NoMem exception if inadequate space.
if (k < 0) throw OutOfBounds();


// p will eventually point to k'th node
ChainNode<T> *p = first;
for (int index = 1; index < k && p;
index++) // move p to k'th
p = p->link;
if (k > 0 && !p) throw OutOfBounds(); // no k'th


// insert
ChainNode<T> *y = new ChainNode<T>;
y->data = x;
if (k) {// insert after p
y->link = p->link;
p->link = y;}
else {// insert as first element
y->link = first;
first = y;}
return *this;
}


template<class T>
void Chain<T>::Output(ostream& out) const
{// Insert the chain elements into the stream out.
ChainNode<T> *current;
for (current = first; current;
current = current->link)
out << current->data << " ";
}


// overload <<
template <class T>
ostream& operator<<(ostream& out, const Chain<T>& x)
{x.Output(out); return out;}


#endif

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
谁有变形金刚1的网址
丝路英雄游戏在哪里?
星级要多少分啊才可以完成
协议离婚需要带些什么,协议离婚需要带什么手
这歌叫啥名啊 谁知道
没有漏点的热吻哪来的床上翻滚,没有肉体的摩
谁给我一个“游戏人生”内测资格,大大有赏!
99827是一个什么成语?
江西蓝天科技发展有限公司我想知道这个在什么
高邑一中最浪漫的地方是哪里??
CF浙江一区卡么?
谢娜张杰有没有分手啊
我现在是QQ会员了,为什么进QQ寻仙进不了呢>>>
QQ帐号被盗
八宝粥罐头哪里得?
推荐资讯
西青道属于西青区啊?
重庆谢记麻辣烫大华店地址在哪,我要去那里办
梦幻连个大唐的帮忙想个人名谢谢咋加点厉害
为什么会对手机有恐惧症?
如何可以快速减肥 ?
洪腾这个地址在什么地方,我要处理点事
天使汽车美容店这个地址在什么地方,我要处理
茗俯香都休闲会所我想知道这个在什么地方
游戏人生资格给我一个谢谢了先
为什么现在我进炫舞,舞团贡献度没增加?
谁有山东李氏族谱
找一首歌的名字!!!!!
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?