博文

数据结构与算法(2)(2006-01-28 17:21:00)

摘要:16,链栈(模板编写,无主函数) template<class T>
struct Node
{ T data;
  Node<T> *next;
}; template<class T>
class LinkStack
{
  private:
    Node<T> *top; 
  public:
    LinkStack() { top=NULL; }
    ~LinkStack();
    bool IsEmpty() const { return top==NULL; }
    void Push(const T& x);
    T Pop();
    T GetTop() const;
}; template<class T>
LinkStack<T>::~LinkStack()
{
  Node<T> *p;
  while(top)
  { p=top;
    top=top->next;
    delete p;
  }
}    template<class T>
void LinkStack<T>::Push(const T& x)
{
  Node<T> *s=new Node<T>;
  s->data=x;
  s->next=top;
  top=s;
} template&......

阅读全文(3780) | 评论:0

数据结构与算法(1)(2006-01-28 17:18:00)

摘要:这些天,本人把以前所学的数据结构做了下总结,现在贴出来,希望能对大家的学习有所帮助,这些都是最基本的算法和结构,本人采用C++面向对象的写法,对每个结构都用类封装起来,要修改或者运用直接调用成员函数即可,同时,为了更广泛应用,本人已经全部修改成了模板,为了能让大家编译通过,本人尽量写成完整的程序,应该在大部分C++编译器上都能通过!以下非注明均能在大部分C++编译器上通过,其排序算法跟C语言写法一样...   -------------------------------------------------------------------------------- 首先先介绍排序: 1,大家都知道的冒泡排序: #include
#include
using namespace std;
template
void swap(type x[],int,int);
template
void BubbleSort(type x[],int);
int main()
{
 srand(time(0));
 const int n=10;
 int x[n];
 for(int i=0;i  x[  i]=rand()%99;
 for(int i=0;i  cout<<"  "< BubbleSort(x,n);
 cout<<"\nAfter Sort:"< for(int i=0;i  cout<<"  "<;
 system("pause");
 return 0;
}
template
void BubbleSort(type x[],int n)

 for(int i=n-1;i>=0;i--)
 {   int flag=0;
  for(int j=0;j   if(x[  j]>x[  j......

阅读全文(4803) | 评论:0

排序方法(2006-01-13 15:33:00)

摘要:    对于n个元素的数组的排序,最佳算法的运行时间是O(nlog2n)≈p*nlog2n。方法有堆排序与快速排序。   (1)堆排序     方法SORT重排一个数组,而方法SORT2用于同时对另一组作相应重排。(用java语言)       1,方法SORT。  public class love        {            void sort(int n,double ra[])                 {                         int i,j,l,ir;                        double rra;                       l=(int)(n/2)+1;                  &n......

阅读全文(3285) | 评论:0