循环链表

#include<iostream>  
using namespace std;  
struct Node  
{  
    int data;  
    Node *rear,*next;  
};  
class CircleList  
{  
private:  
    Node *first;  
public:  
    CircleList();  
    CircleList(int a[],int n);  
    ~CircleList();  
    int Locate(int x);  
    void Insert(int i,int x);  
    int Delete(int i);  
    void PrintList();  
};  


CircleList::CircleList()  
{  
    first=new Node;  
    first->next=NULL;  
}  
CircleList::CircleList(int a[],int n)  
{  
    Node *r,*s;  
    first=new Node;  
    r=first;  
    for(int i=0;i<n;i++)  
    {  
        s=new Node;  
        s->data=a[i];  
        s->next=NULL;  
        r->next=s;  
        s->rear=r;  
        r=s; 
    }  
    r->next=NULL;  
}  
CircleList::~CircleList()  
{  
    Node *q=NULL;  
    while(first!=NULL)  
    {  
        q=first;  
        first=first->next;  
        delete q;  
    }  
}  
void CircleList::Insert(int i,int x)  
{  
    /*Node *p=first,*s=NULL,*rear;  
    int count=1;  
    while(p!=NULL&&count<i-1)  
    {p=p->next;  
    count++;}  
    if(p==NULL)throw"位置";  
    else{  
        s=new Node;s->data=x;  
        s->rear=p;  
        s->next=p->next;  
        p->next->rear=s;  
        p->next=s; */


	Node *p=first,*s=NULL,*rear; 
    p = first->next;  
    int count = 1;  
    while (p != first && count < i-1)  
    {  
        p = p->next;  
        count++;  
    }  
    if (p == first)throw "插入位置异常";  
    else  
    {  
        s = new Node;  
        s->data = x;  
        s->next = p->next;  
        p->next = s;   
        length++;  
    }  
}  
int CircleList::Delete(int i)  
{  
    Node *p=first;  
    int count=0;  
    while(p!=NULL&&count<i)  
    {  
        p=p->next;  
        count++;  
    }  
    if(p==NULL)  
        throw"位置";  
    else{  
        (p->rear)->next=p->next;  
        (p->next)->rear=p->rear;  
        free(p);  
    }  
    return 0;  
}  
int CircleList::Locate(int x)  
{  
    Node *p=first->next;  
    int count=1;  
    while(p!=NULL)  
    {  
        if(p->data==x) return count;  
        p=p->next;  
        count++;  
    }  
    return 0;  
}  
void CircleList::PrintList()  
{  
    Node *p=first->next;  
    while(p!=NULL)  
    {  
        cout<<p->data<<" ";  
        p=p->next;  
    }  
    cout<<endl;  
}  
int main()  
{  
    int r[5]={88,92,52,68,78};  
    CircleList L(r,5);  
    cout<<"执行插入操作前数据为:"<<endl;  
    L.PrintList();  
    cout<<"在第三个位置插入成绩93"<<endl;  
	try{L.Insert(3,78);}
    catch(char *s)  
 {  cout<<s<<endl;  }  
    cout<<"执行插入操作后数据为:"<<endl;  
    L.PrintList();  
    cout<<"成绩为92的学生位置为:";  
    cout<<L.Locate(92)<<endl;  
    cout<<"执行删除操作前数据为:"<<endl;  
    L.PrintList();  
    cout<<"删除第一个成绩"<<endl;  
    try  
    {  
        L.Delete(1);  
    }  
    catch(char *s)  
    {  
        cout<<s<<endl;  
    }  
    cout<<"执行删除操作后数据为:"<<endl;  
    L.PrintList();  
    return 0;  
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值