//#pragma once
//头文件中放的是节点类
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
template<class Type>
class LinkNode {
public:
Type* next;
Type data;
};
#endif // !_LINKLIST_H_
//下面的是函数体
#include<iostream>
#include "LinkList.h"
using namespace std;
template<class Type>
class LinkList_tail {
public:
LinkList_tail() {}
void InitList_tail(LinkNode<Type>* L); //建立一个头节点
void CreateList_tail(LinkNode<Type>* L, const Type a[], const int n);
int ListLength_tail(LinkNode<Type>* L) const;
bool ListEmpty_tail(LinkNode<Type>* L) const;
bool GetElem_tail(LinkNode<Type>* L, const int i, const Type& e); //按照输入的位置查询元素值
int LocateList_tail(LinkNode<Type>* L, const Type& e); //按照元素值查询链表并返回其在链表中的逻辑位置
bool InsertList_tail(LinkNode<Type>* L, const int i, const Type& e); //将元素插入到链表的第i个位置
bool DeleteList_tail(LinkNode<Type>* L, const int i,const Type &e); //删除链表中第i个位置的元素并使用e接收
void DispList_tail(LinkNode<Type>* L) const;
void DestoryList_tail(LinkNode<Type>* L);
~LinkList_tail() {
cout << "调用析构函数释放LinkList对象!" << endl;
}
};
template<class Type>
void LinkList_tail<Type>::InitList_tail(LinkNode<Type>* L){
L = (LinkNode*)malloc(sizeof(LinkNode));
L->next = NULL;
}
template<class Type>
void LinkList_tail<Type>::CreateList_tail(LinkNode<Type>* L, const Type a[], const int n){
LinkNode<Type>* pre = L;
LinkNode<Type>* s;
int i;
for (i = 0;i < n;i++) {
s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = a[i];
pre->next = s;
pre = s;
}
pre->next = NULL;
}
template<class Type>
int LinkList_tail<Type>::ListLength_tail(LinkNode<Type>* L) const{
int length = 0;
LinkNode* pre = L->next;
while (pre != NULL) {
length++;
pre = pre->next;
}
return length;
}
template<class Type>
bool LinkList_tail<Type>::ListEmpty_tail(LinkNode<Type>* L) const
{
return (L->next == NULL);
}
template<class Type>
bool LinkList_tail<Type>::GetElem_tail(LinkNode<Type>* L, const int i, const Type& e)
{
LinkNode<Type>* pre = L->next;
int j = 0;
if (i < 0) return false;
while (j < i - 1 && pre != NULL) {
j++;
pre = pre->next;
}
if (pre == NULL) {
cout << "查询失败!" << endl;
return false;
}
else
{
e = pre->data;
cout << "查询成功!" << endl;
return true;
}
}
template<class Type>
int LinkList_tail<Type>::LocateList_tail(LinkNode<Type>* L, const Type& e)
{
int number = 0;
LinkNode<Type>* pre = L->next;
while (pre != NULL && pre->data != e) {
number++;
pre = pre->next;
}
if (pre == NULL) {
cout << "按元素值查找失败!" << endl;
return 0;
}
else
{
cout << "查找成功!" << endl;
return number;
}
}
template<class Type>
bool LinkList_tail<Type>::InsertList_tail(LinkNode<Type>* L, const int i, const Type& e)
{
LinkNode<Type>* pre = L->next, * s;
int j = 0;
if (i < 0)
return false;
while (j < i - 1 && pre != NULL)
{ //得到链表的物理序号
j++;
pre = pre->next;
}
if (pre == NULL)
return false;
/*else
{
s = (LinkNode*)malloc(sizeof(LinkNode);
s->data = e;
pre->next = s;
pre = s;
cout << "插入成功!" << endl;
return true;
}*/
}
template<class Type>
bool LinkList_tail<Type>::DeleteList_tail(LinkNode<Type>* L, const int i, const Type& e)
{
LinkNode<Type>* pre = L;
LinkNode<Type>* q;
int j = 0;
if (i < 0) {
cout << "删除失败!" << endl;
return false;
}
while (j < i - 1 && pre != NULL) { //得到物理下标
j++;
pre = pre->next;
}
if (pre == NULL) {
cout << "删除失败!" << endl;
return false;
}
else
{
q = pre->next;
if (q == NULL) {
cout << "删除失败!" << endl;
return false;
}
e = q->data;
pre->next = q->next;
free(pre);
}
return false;
}
template<class Type>
void LinkList_tail<Type>::DispList_tail(LinkNode<Type>* L) const
{
LinkNode<Type>* pre = L->next;
while (pre != NULL) {
cout << pre->data << " " << endl;
}
cout << endl;
}
template<class Type>
void LinkList_tail<Type>::DestoryList_tail(LinkNode<Type>* L)
{
LinkNode<Type>* pre = L->next;
while (pre != NULL) {
free(pre);
pre = pre->next;
}
free(pre);
}
int main() {
LinkNode<int>* L;
int arr[9] = { 1,2,3,4,6,7,8,9,10 };
LinkList_tail<int> object;
object.InitList_tail(L);
object.CreateList_tail(L, arr, 9);
object.DestoryList_tail(L);
return 0;
}
哪位大神可以帮我看看这个程序,程序是实现单链表的基本操作,但是在VS上运行的时候碰到了很多问题,这歌程序运行不出结果,哪位大神能帮我找找错误吗,拜托了,救救孩子吧。