数据结构算法集---C++语言实现

2016-02-19 15:54 0 1 收藏

今天图老师小编要跟大家分享数据结构算法集---C++语言实现,精心挑选的过程简单易学,喜欢的朋友一起来学习吧!

【 tulaoshi.com - 编程语言 】

  这是我学数据结构编写的算法,我把他整理出来,都是基本算法,供大家学习。我使用c++面向对象形式编写,各种算法都封装在各自的类里,假如想增加功能,在相应的类里增加函数即可。我对树和图的构造也做了一些人性化设计,输入更加形象化,你可能看不懂,没关系漫漫来。各种类都使用模版设计,可以对各种数据类型操作(整形,字符,浮点)
  
  
  ///////////////////////////
  //    //
  //   堆栈数据结构   stack.h         //
  //    //
  //////////////////////////
  
  
  #includeiostream.h
  
  templateclass Typeclass Stack;
  
  templateclass Type
  class StackNode
  {
   friend class StackType;
   private:
   Type data;
   StackNodeType *link;
     StackNode(Type D=0,StackNodeType *L=NULL):link(L),data(D){}
  };
  
  templateclass Type
  class Stack
  {
   public:
   Stack():top(NULL),NumItem(0){}
   void Push(Type item);
   Type Pop();
   Type GetTop();
   void MakeEmpty();
   bool ISEmpty();
   int GetNum();
   private:
   int NumItem;
   StackNodeType *top;
  };
  
  templateclass Type
  void StackType::Push(Type item)
  {
    top=new StackNodeType(item,top);
   NumItem++;
  }
  
  templateclass Type
  Type StackType::Pop()
  {
   StackNodeType *p;
   Type temp;
   temp=top-data;
   p=top;
   top=top-link;
   delete p;
   NumItem--;
   return temp;
  
  }
  
  templateclass Type
  Type StackType::GetTop()
  {
   return top-data;
  }
  
  templateclass Type
  bool StackType::ISEmpty()
  {
   return top==NULL;
  }
  
  templateclass Type
  void StackType::MakeEmpty()
  {
   delete top;
  }
  
  templateclass Type
  int StackType::GetNum()
  {
   return NumItem;
  }
  
  
  
  ///////////////////////////
  //    //
  //   队列数据结构       Queue.h //
  //    //
  //////////////////////////
  #includeiostream.h
  
  templateclass Type class Queue;
  
  templateclass Type class QueueNode
  {
   friend class QueueType;
  
   private:
   Type data;
   QueueNodeType *link;
   QueueNode(Type d=0,QueueNode *l=NULL):data(d),link(l){}
  };
  
  template class Type class Queue
  {
   public:
   Queue():rear(NULL),front(NULL){}
   ~Queue();
   void EnQueue(Type item);
   Type DelQueue();
   Type GetFront();
   void MakeEmpty();
   bool ISEmpty() { return front==NULL; }
   private:
   QueueNodeType *front,*rear;
  };
  
  
  templateclass Type
  QueueType::~Queue()
  {
   QueueNodeType *p;
   while(front!=NULL)
   {
   p=front;
   front=front-link;
   delete p;
   }
  }
  
  templateclass Type
  void QueueType::EnQueue(Type item)
  {
   if(front==NULL)
   front=rear=new QueueNodeType (item,NULL);
   else
   rear=rear-link=new QueueNodeType (item,NULL);
  }
  
  
  templateclass Type
  Type QueueType::DelQueue()
  {
   QueueNodeType *p=front;
   Type temp=p-data;;
   front=front-link;
   delete p;
   return temp;
  }
  
  
  templateclass Type
  Type QueueType::GetFront()
  {
   return front-data;
  }
  
  
  templateclass Type
  void QueueType::MakeEmpty()
  {
   QueueNodeType *p;
   while(front!=NULL)
   {
   p=front;
   front=front-link;
   delete p;
   }
  }
  
  
  ///////////////////////////
  //    //
  //   链表数据结构  list.h //
  //    //
  //////////////////////////
  
  
  #includeiostream.h
  
  templateclass type
  class list;
  
  templateclass type
  class listnode
  {
   public:
   friend class listtype;
   private:
   type data;
   listnodetype * next;
  };
  
  
  templateclass type
  class list
  {
   public:
   list();
   ~list();
   void insertend(type); //向链表尾部插入元素
   bool insert(type,int); //向链表任意位置插入元素
   void delnode(int i);  //删除元素
   int find(type T);   //查找元素
   void makeempty();   //销毁链表
   bool print();  //打印链表
   int getlen();  //得到链表长度
  
    private:
   listnodetype *first,*last;
   int length;
  };
  
  templateclass type
  void initlist(type &tmp);
  
  templateclass type
  void list_exit(listtype &L,type tmp);
  
  void initation();
  
  templateclass type
  void list_insertend(listtype &L,type tmp);
  
  
  templateclass type int listtype::getlen()
  {
   return length;
  }
  
  templateclass type void listtype::makeempty()
  {
   listnodetype *p1,*p2;
  
   p1=first-next;
   first-next=NULL;
   while(p1!=NULL)
    {
   p2=p1;
   p1=p1-next;
   delete p2;
   }
   length=0; 
  }
  
  templateclass type void listtype::insertend(type t)
  {
  
   listnodetype *p;
   p=new listnodetype;
   p-data=t;
   p-next=NULL;
   last-next=p;
   last=p;
  
   length++;
  }
  
  templateclass type bool listtype::insert(type t,int i)
  {
   listnodetype *p;
   p=first;
  
   int k=1;
   while(p!=NULL&&ki)
   {
   p=p-next;
   k++;
   }
   if(p==NULL&&k!=i)
   return false;
   else
   {
     listnodetype *tp;
    tp=new listnodetype;
    tp-data=t;
    tp-next=p-next;
    p-next=tp;
    length++;
   
    return true;
   }
  }
  
  templateclass type void listtype::delnode(int i)
  {
   int k=1;
   listnodetype *p,*t;
   p=first;
  
   while(p-next!=NULL&&k!=i)
   {
   p=p-next;
     k++;
   }
    t=p-next;
   cout"你已经将数据项 "t-data"删除"endl;
  
   p-next=p

来源:https://www.tulaoshi.com/n/20160219/1610658.html

延伸阅读
3号盘子的目标柱是C,但是已经有了1号盘子,我们最直觉的反映就是——将碍事的盘子搬到另一根柱子上面去。于是,我们要做的是(规律2):保存当前柱的信息(柱子号、应该搬动的最下面一块盘子的号,和它的目标柱),以备当障碍清除后回到现在的柱子继续搬,将当前柱转换为碍事的盘子所在的柱子。假设这样若干步后,我们将7号盘子从A搬到了C,此...
节点类 #ifndef Node_H #define Node_H template class Type class Node //单链节点类 { public: Type data; NodeType *link; Node() : data(Type()), link(NULL) {} Node(const Type &item) : data(item), link(NULL) {} Node(const Type &item, NodeType *p) : data(ite...
栈和队列是操作受限的线性表,似乎每本讲数据结构的数都是这么说的。有些书按照这个思路给出了定义和实现;但是很遗憾,这本书没有这样做,所以,原书中的做法是重复建设,这或许可以用不是一个人写的这样的理由来开脱。 !-- frame contents -- !-- /frame contents -- 顺序表示的栈和队列,必须预先分配空间,并且空...
我看的两本教科书(《数据结构(C语言版)》还有这本黄皮书)都是以这个讲解队列应用的,而且都是银行营业模拟(太没新意了)。细比较,这两本书模拟的银行营业的方式还是不同的。 !-- frame contents -- !-- /frame contents -- 1997版的《数据结构(C语言版)》的银行还是老式的营业模式(究竟是1997年的事了),现在的很多地方还...
汉诺塔的非递归解法 似乎这个问题的最佳解法就是递归,假如你想用栈来消解掉递归达到形式上的消除递归,你还是在使用递归的思想,因此,他本质上还是一个递归的算法。我们这本黄皮书在谈论到“什么情况使用递归”的时候,在“3.问题的解法是递归的”这里面,就这样说了“有些问题只能用递归的方法来解决,一个典型的例子就是汉...

经验教程

527

收藏

64
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部