首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求帮助啊程序能运行但出现了错误了

2012-04-07 
求帮助啊,程序能运行但出现了异常了#includeiostreamusing namespace stdtemplate class T class dou

求帮助啊,程序能运行但出现了异常了
#include<iostream>
using namespace std;

template <class T> class doublelist;
template <class T>
class Node
{
protected:
T element;
Node<T> *last,*next;
public:
Node<T>() {}
friend class doublelist<T>;
};
template <class T>
class doublelist
{
private:
Node<T> *frist;
public:
doublelist();
~doublelist();
bool insert(int i,T x);
bool Delete(int i);
void disp(ostream &out);
int n;
};
template <class T>
doublelist<T>::~doublelist()
{
Node<T> *p=frist->next;
while(p!=frist)
{
p=frist->next;
delete frist;
frist=frist->next;
}
}
template <class T>
doublelist<T>::doublelist()
{
frist=new Node<T>;
frist->last=frist;
frist->next=frist;
frist->element=0;
n=0;
}
template <class T>
bool doublelist<T>::insert(int i,T x)
{
if(i<0||i>n)
{
cout<<"插入失败:"<<endl;
return false;
}
Node<T> *q=new Node<T>;q->element=x; //创建新节点
Node<T> *p=frist->next; //P指向第一个元素
for(int j=0;p->next!=frist&&j<i;j++) //循环是p指向第i个元素
p=p->next;
if(i<n) //如果i<n将新元素插入i之前
{
q->last=p->last;
p->last->next=q;
q->next=p;
p->last=q;
}
if(i==n)  
{
p->next=q;
q->last=p;
q->next=frist;
frist->last=q;
}
n++;
return true;
}
template <class T>
bool doublelist<T>::Delete(int i) //删除元素
{
if(i==0)
{
cout<<"元素空无可删:"<<endl;
return false;
}
Node<T> *p=frist->next;
for(int j=0;j<i;j++)
p=p->next;
p->last->next=p->next;
p->next=p->last;
delete p;
return true;
}
template<class T>
void doublelist<T>::disp(ostream &out)
{
int j=0;
Node<T> *p=frist->next;
while(j<n&&p->next!=frist)
{
out<<p->element<<" ";
p=p->next;
}
out<<endl;
}
int main()
{
doublelist<int> s;
for(int i=2;i<=5;i++)
s.insert(i-2,i);
s.insert(1,1);
s.Delete(5);
s.disp(cout);
return 0;
}

 

[解决办法]
不懂C++,我用C语言是这么写...你应该看的懂吧...-_-!
分成若干函数写容易找出错误!!!

C/C++ code
#include <stdio.h>#include <conio.h>#include <malloc.h>#define LEA sizeof(struct student)struct student{    int num;    float score;    struct student *next;};struct student *exporo();            //创建链表void print(struct student *);        //打印链表struct student *del(struct student *head, int num);        //删除一个结点struct student *insert(struct student *head, struct student *p);void main(){    struct student *stu, *stu_1, *Max;    int n = 0;        stu = exporo();    print(stu);        printf("Please input Number delete: ");    scanf("%d", &n);    print(del(stu, n));        stu_1 = (struct student *)malloc(LEA);    printf("Please input insert Number: ");    scanf("%d", &stu_1->num);    printf("Please input insert Score: ");    scanf("%f", &stu_1->score);    Max = insert(stu, stu_1);    print(Max);}struct student *exporo(){    struct student *p1, *p2, *head;    int n = 0;    head = NULL;        p1 = p2 = (struct student *)malloc(LEA);    printf("Please input Number: ");    scanf("%d", &p1->num);    printf("Please input Score: ");    scanf("%f", &p1->score);        while(p1->num)    {        n++;        if(n == 1)        {            head = p1;        }        else        {            p2->next = p1;        }        p2 = p1;        p1 = (struct student *)malloc(LEA);        printf("Please input Number: ");        scanf("%d", &p1->num);        printf("Please input Score: ");        scanf("%f", &p1->score);    }    p2->next = NULL;    return head;}void print(struct student *head){    while(head != NULL)    {        printf("Number = %d\t\tScore = %f\n", head->num, head->score);        head = head->next;    }}struct student *del(struct student *head, int num){    struct student *p1, *p2;    p1 = head;    if(head == NULL)    {        printf("Fuck you!!");    }    else    {        while(p1->num !=num && p1->next != NULL)        {            p2 = p1;            p1 = p1->next;        }        if(p1->num == num)        {            if(head == p1)            {                p1 = p1->next;                head = p1;            }            else            {                p2->next = p1->next;            }        }    }    return head;}struct student *insert(struct student *head, struct student *p){    struct student *p0, *p1, *p2;    p1 = head;    p0 = p;    if(head == NULL)    {        head = p0;        p0->next = NULL;    }    while((p1->num < p0->num) && p1->next != NULL)    {        p2 = p1;        p1 = p1->next;    }    if(p1->num >= p0->num)    {        if(p1 == head)        {            head = p0;            p0->next = p1;        }        else        {            p2->next = p0;            p0->next = p1;        }    }    else    {        p1->next = p0;        p0->next = NULL;    }    return head;} 

热点排行