【数据结构】实现循环链表模板类

节点类的定义

template <class T>
class Node{
public:
    T data;       				 //存放数据
    Node<T> * next;  			 //指向下个节点
    Node(const T& item){      //用item元素初始化节点
        data=item;
        next=NULL;
    }
    ~Node(){};
};

循环链表类的定义

template <class T>
class RoundList{
private:
    Node<T> * head; 		//首节点
    Node<T> * cur;			//当前节点
    int size;				//链表元素个数

public:
RoundList(){
    head=NULL;
    cur=NULL;
    size=0;
}
~RoundList(){}
void insert(const T& item);
//插入节点
void pop();
//弹出当前节点
void display();
//打印链表元素
T top();
//获得当前节点的值
void del();
//删除指定节点
int count();
//链表元素个数
bool empty();
//是否为空链表
Node<T> * GetPre(Node<T> * N);
//获得节点N的前驱节点
Node<T> * GetCur();
//获得当前节点
void MovCur(int n);
//把当前节点向后移动n个节点
void SetCur(int n);
//设置当前节点初始位置
};

链表类的实现

通过循环遍历的方式来像数组一样通过位序访问元素

template <class T>
//类似于尾插法,没有头节点
void RoundList<T>::insert(const T& item){
    Node<T> * tmp=new Node<T>(item);
    tmp->next=head;
    if(cur==NULL){
        head=tmp;
        cur=tmp;
        size++;
    }
    else{
        cur->next=tmp;
        cur=tmp;
        size++;
    }
}

template <class T>
bool RoundList<T>::empty(){
    return size==0;
}

template <class T>
T RoundList<T>::top(){
    return cur->data;
}

template <class T>
void RoundList<T>::display()
{
    Node<T> * ptr=head;
    for (int i=0;i<size;i++){
        cout<<ptr->data<<"->";
        ptr=ptr->next;
    }
    cout<<endl;
}

template <class T>
int RoundList<T>::count(){
    return size;
}

//获得n的前驱节点
template <class T>
Node<T> * RoundList<T>::GetPre(Node<T> * N){
    Node<T> * ptr=head;
    while (ptr->next!=N)
    {
        ptr=ptr->next;
    }
return ptr;
}

template <class T>
Node<T> * RoundList<T>::GetCur(){
    return cur;  
}

template <class T>
void RoundList<T>::pop(){
    Node<T> * pre= GetPre(cur);
    pre->next=cur->next;
    size--;
}

template <class T>
void RoundList<T>::SetCur(int n){
    cur=head;
    for (int i = 0; i < n-1; i++)
    {
        cur=cur->next;
    }
}

template <class T>
void RoundList<T>::MovCur(int n){
    for (int i = 0; i < n; i++)
    {
        cur=cur->next;
    }
}

测试循环链表类

int main(){
    RoundList<int> L;
    // L.insert(33);
    // L.insert(22);
    // L.insert(11);
    // L.display();
    // cout<<endl;
    // cout<<L.top()<<endl;
    // RoundList<int> L;
    L.insert(1);
    L.insert(2);
    L.insert(3);
    L.insert(4);
    L.insert(5);
    L.insert(6);

    
    // 测试SetCur和MovCur
    // L.SetCur(0);
    // cout<<L.GetCur()->data<<endl;
    // L.MovCur(4);
    // cout<<L.GetCur()->data<<endl;


    //测试插入删除
    // L.pop();
    // L.display();
    
    //测试GetCur 和 GetPre
    // Node<int> * p= L.GetCur();
    // cout<<p->data<<endl;
    // Node<int> * ptr = L.GetPre(p);
    // cout<<ptr->data<<endl;
}

用自己的循环链表类解决约瑟夫问题

约瑟夫问题

约瑟夫问题是个有名的问题:N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉。例如N=6,M=5,被杀掉的顺序是:5,4,6,2,3。

int main()
{
	L.insert(1);
    L.insert(2);
    L.insert(3);
    L.insert(4);
    L.insert(5);
    L.insert(6);
    //初始化六个人

	cout<<"人员如下:"<<endl;
    L.display();

    cout<<"输入初始位置n"<<endl;  
    int n;
    cin>>n;
    
    //这里从第一个人开始数的话要把初始位置n置为最后一个元素
    //从第n个人开始数要把初始位置n置为第n-1个元素
    if (n==0)
    {
        L.SetCur(L.count());
    }
    else
    {
        L.SetCur(n-1);
    }
    cout<<"每传递x个位置"<<endl;
    int x;
    cin>>x;
    std::cout << "顺序如下" << std::endl;
    while (!L.empty())
    {
        L.MovCur(x);
        cout<<L.top()<<" ";
        L.pop();
        /* code */
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
循环链表是一种特殊的链表,它的最后一个节点指向第一个节点,形成一个环。使用循环链表可以很方便地实现约瑟夫环问题。 具体实现步骤如下: 1. 定义一个节点结构体,包含一个数据域和一个指向下一个节点的指针域。 2. 构建循环链表,将每个节点连接起来形成一个环。 3. 定义一个计数器,从指定位置开始遍历链表,每遍历到一个节点就将计数器加1,当计数器的值等于指定的数n时,将该节点从链表中删除。 4. 重复步骤3,直到链表中只剩下一个节点为止。 下面是一个C语言实现的例子: ```c #include <stdio.h> #include <stdlib.h> // 定义节点结构体 typedef struct node { int data; struct node *next; } Node; // 构建循环链表 Node *buildList(int n) { Node *head = NULL, *tail = NULL; for (int i = 1; i <= n; i++) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = i; newNode->next = NULL; if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } tail->next = head; // 将最后一个节点指向头节点,形成循环链表 return head; } // 删除节点 Node *deleteNode(Node *head, int n, int m) { Node *p = head, *pre = NULL; int count = 0; while (p->next != p) { // 当链表中只剩下一个节点时结束循环 count++; if (count == m) { // 找到要删除的节点 printf("%d ", p->data); // 输出该节点的值 pre->next = p->next; // 将该节点从链表中删除 Node *temp = p; p = p->next; free(temp); count = 0; } else { pre = p; p = p->next; } } printf("%d\n", p->data); // 输出最后剩下的节点的值 free(p); return NULL; } int main() { int n, m; printf("请输入总人数n和报数m:"); scanf("%d %d", &n, &m); Node *head = buildList(n); printf("出列顺序为:"); head = deleteNode(head, n, m); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值