單鏈表的基本操作(基於模板類)
1.list.**件
#pragma once
#include
#include
using namespace std;
//template
template
struct listnode
;listnode(const t &d, listnode *p = null)
};template
class list ;
void print()
2.make.**件(實現方法)
#pragma once
#include"list.h"
template
list::list()
};template
list::list(const t &l)
catch (const std::exception&)
}template
inline list::list(list& l)
;template
void list::creatlist(int n)
for (int i = 0; i < n; i++)
catch (const std::exception&)
}delete pre;
};template
void list::output()
cout << endl;
};template
int list::len()const
return count;
};template
listnode *list::search(t &x)const
return p;
};template
listnode *list::locate(int locate)
else
p = p->next;
k++;}}
}catch (const std::exception&)
return p;
};template
bool list::pipeidata(int locate, t &x)
return f;
};template
void list::changedata(int locate)
listnode *pre = locate(locate);
cout << pre->next->data << endl;
if (pre != null)
else
};template
bool list::insert(int locate, t &x)
else
listnode *newnode = new listnode(x);
if (newnode == null)
newnode->next = pre->next;
pre->next = newnode;}}
catch (const std::exception&)
return true;
}template
inline bool list::isfull() const
template
inline bool list::isempty() const
};template
inline void list::bubblbsort(listnode head)
}}template
inline void list::delete(listnode* del, int i)
listnode q = p->next;
p->next = p->next->next;
free(q);
}template
int list::findjie(listnode f, t & x)
p = p->next;
}return wei;
}template
void list::reserve()
first->next = q;
listnode*pre = q;
while (pre!=null)
cout << endl;};
3.主函式main()
#include"make.h"
#include"list.h"
#include
using namespace std;
int main()
else
break;
case 4:int cshu,shu;
cout << "輸入你想插入的前節點和數字:\n";
cin >> cshu >> shu;
a.insert(cshu, shu);
break;
case 5:int jie;
cout << "輸入你要修改的節點為:\n";
cin >> jie ;
a.changedata(jie);
break;
case 6://listnode*cur = a.locate(0);
a.bubblbsort(a.locate(0)); break;
case 7:
cout << "輸入刪除的節點:\n"; cin >> jie;
a.delete(l, jie);
break;
case 8:a.output(); break;
case 9:a.reserve(); break;
case 0:exit(1);
default:cout << "輸入不匹配\n";
break;
}}/*
print();
a.creatlist(5);
a.output();
cout << "長度為" << a.len() << endl;
//搜尋:
int x = 3;
pre = a.search(x);
if (pre != null)
cout << "找到了!\n" << pre->data << endl;
else
cout << "沒找到了!\n" << endl;
//定位查詢
pre = a.locate(x);
if (pre != null)
cout << "找到了!\n" << pre->data << endl;
else
cout << "沒找到了!\n" << endl;
//對比值
x = 10;
cout << a.pipeidata(1, x) << endl;
//修改值
a.changedata(1, x);
a.output();
//插入:
x = 99;
a.insert(5, x);
a.output();
//排序:
listnode*cur = a.locate(0);
a.bubblbsort(cur);
a.output();
//刪除
listnode*l=new listnode();
x = 2;
a.delete(l, x);
a.output();
print();
*/system("pause");
return 0;
單鏈表 模板類
include using namespace std 宣告單鏈錶類模板 為了說明友元類 template class list 定義鍊錶結點類模板 template class listnode listnode type item data item next null public 成員函式 ...
單鏈表模板類
鍊錶是最基本的資料結構,是一組不連續的資料的集合,鍊錶中每個結點除包含結點元素外,還包含下一結點的位址。對鍊錶可以實現插入 刪除 查詢以及顯示等操作。單鏈表模板類list.h ifndef list h define list h include using namespace std templa...
單鏈表模板類
在單鏈表中必然需要定義乙個頭節點來指向鍊錶的第乙個元素,struct node public object mutable node m header 這樣直接定義會有乙個問題,頭節點的構造會呼叫t類的建構函式,這顯然時不需要的,解決方案如下 mutable struct public object...