不多说,直接上代码。
//circle_list.h
#ifndef CIRCLE_H_
#define CIRCLE_H_
#include <iostream>
template <typename T>
struct Node
{
T data;
Node<T> *next;
};
template <typename T>
class Circle_list
{
public:
Circle_list();
~Circle_list();
void Input();
void Insert();
void Find_loc();
void Find_val();
void Delete();
void Show();
private:
Node<T> *head;
int m_length;
};
#endif
template <typename T>
Circle_list<T>::Circle_list()
{
head = new Node<T>;
head->next = NULL;
m_length = 0;
}
template <typename T>
Circle_list<T>::~Circle_list()
{
Node<T> *p, *q;
p = head;
q = p;
if (p != NULL)
{
delete q;
p = p->next;
q = p;
m_length--;
}
}
template <typename T>
void Circle_list<T>::Input()
{
using namespace std;
int n; //设置学生成绩的数目
Node<T> *p, *q;
T x; //输入的学生成绩
q = head;
cout << "\n请输入你要输入成绩的数目:";
cin >> n;
while (n <= 0)
{
cout << "\n你输入的数目有问题,请确认.\n" << "请重新输入:";
cin >> n;
}
//输入操作
for (int i = 0; i < n; i++)
{
cout << "\n请输入第" << i + 1 << "位同学的成绩:";
cin >> x;
p = new Node<T>;
p->data = x;
q->next = p;
p->next = head->next;
q = q->next;
m_length++;
}
}
template <typename T>
void Circle_list<T>::Insert()
{
using namespace std;
T x; //插入的值
int s; //插入的位置序号
Node<T> *p, *q;
q = head;
cout << "\n请输入成绩想要插入的位置序号:";
cin >> s;
while (s <= 0 || s > m_length + 1)
{
cout << "\n你输入的位置序号有误,请确认.\n" << "请重新输入:";
cin >> s;
}
for (int i = 0; i < s; i++)
{
q = q->next; //指向想要插入位置序号前一个
}
//插入操作
cout << "\n请输入你想要插入的成绩:";
cin >> x;
p = new Node<T>;
p->data = x;
p->next = q->next;
q->next = p;
m_length++;
}
template <typename T>
void Circle_list<T>::Find_loc()
{
using namespace std;
Node<T> *p;
p = head;
int s;
cout << "\n请输入你想要查找的成绩的位置序号:";
cin >> s;
while (s <= 0 || s > m_length)
{
cout << "\n你输入的位置序号有误,请确认.\n" << "请重新输入:";
cin >> s;
}
//按位查找操作
for (int i = 0; i < s; i++)
{
p = p->next;
}
cout << "\n你要查找的成绩为:" << p->data << endl;
}
template <typename T>
void Circle_list<T>::Find_val()
{
using namespace std;
int s = 0;
T x;
Node<T> *p, *q;
p = head->next;
q = p;
cout << "请输入你要查找的学生成绩:";
cin >> x;
//按值查找操作
for (int i = 0; i < m_length; i++)
{
if (p->data == x)
{
s++;
cout << "\n你要查找的学生成绩的位置序号为:" << s + 1 << endl;
break;
}
else
{
s++;
p = p->next;
}
}
if (s == m_length)
cout << "没有该学生成绩." << endl;
}
template <typename T>
void Circle_list<T>::Delete()
{
using namespace std;
int s;
Node<T> *p, *q;
p = head;
q = p;
cout << "\n请输入你想要删除的学生成绩的位置序号:";
cin >> s;
while (s <= 0 || s > m_length)
{
cout << "\n你输入的位置序号有误,请确认.\n" << "请重新输入:";
cin >> s;
}
//删除操作
if (s == 1) //删除第一个情况特殊
{
for (int i = 0; i < m_length; i++)
p = p->next;
q = p->next;
cout << "\n你要删除的学生成绩为:" << q->data << endl;;
p->next = q->next;
head->next = q->next;
delete q;
}
else //删除除第一个
{
for (int i = 0; i < s - 1; i++)
{
p = p->next; //指向想要删除位置序号前一个
}
q = p->next;
cout << "\n你要删除的学生成绩为:" << q->data << endl;;
p->next = q->next;
delete q;
}
}
template <typename T>
void Circle_list<T>::Show()
{
using namespace std;
Node<T> *p;
p = head->next;
//打印操作
for (int i = 0; i < m_length; i++)
{
cout << "\n第" << i + 1 << "位学生成绩为:" << p->data << endl;
p = p->next;
}
}
//circle_list.cpp
#include <iostream>
#include "circle_list..h"
int main()
{
Circle_list<int> cir;
cir.Input();
cir.Insert();
cir.Find_loc();
cir.Find_val();
cir.Delete();
cir.Show();
return 0;
}
基本实现,有待完善。