数据结构-北大-2051

0 views
Skip to first unread message

china_sjc

unread,
Aug 6, 2008, 4:12:44 AM8/6/08
to 中国矿业大学徐海学院算法课程
源代码:
#include <iostream.h>
//using namespace std;

class link
{
public:
int Q_num;
int Period;
long time; //Total time
int coefficient; //系数
link* next;

link(int Q,int P)
{
Q_num=Q;
Period=P;
coefficient = 1;
time = Period * coefficient;
}
link (link* p)
{
Q_num = p-> Q_num;
Period = p-> Period;
time = p-> time;
coefficient = p-> coefficient;
next = p-> next;
}
void coef1()
{
time = Period * (++coefficient);
}
};

class links
{
private:
link* head;

public:
links()
{
head = NULL;
}

void input (int Q_num,int Period);
//把值输入到链表当中。

void insert (link* k); //按升序进行插入

link* GetHead(); //返回第一个节点,并把它删掉。再把它加系数放回去。

void print(int k);

};

void links::print(int k)
{
link* s=NULL;
for(int i=0;i <k;i++)
{
s=GetHead();
cout < <s-> Q_num < <endl;
}

}
void links::insert(link* k)
{
link* cur=head;
link* pre=NULL;
//while(cur!=NULL&& k-> time> cur-> time)
while(cur!=NULL&& (k-> time> cur-> time||((k-> time==cur-> time)&&(k-
> Q_num> cur-> Q_num))))
{
pre=cur;
cur=cur-> next;
}
if(cur==head)
head=k;
else{
pre-> next = k;
}
k-> next = cur;
}

void links::input (int Q_num,int Period)
{
link* s = new link(Q_num,Period);
insert(s);
}

link* links::GetHead()
{
link* s;
link* p;
p=head;
head = head-> next;
s = new link (p);
s-> coef1();
insert(s);
return p;
}

int main()
{

char str[10];
int Q_nums;
int Periods;
int k;
links* c = new links();
cin> > str; //cin> > str> > Q_nums> > Periods;
//把第一个值也输进去。
while (*str!= '# ')
{
cin> > Q_nums> > Periods;
c-> input(Q_nums,Periods);
cin> > str;
//输入Instruction
}
cin> > k;

c-> print(k);

return 0;
}
Reply all
Reply to author
Forward
0 new messages