数据结构算法集---C++语言实现
这是我学数据结构编写的算法,我把他整理出来,都是基本算法,供大家学习。我使用c++面向对象形式编写,各种算法都封装在各自的类里,假如想增加功能,在相应的类里增加函数即可。我对树和图的构造也做了一些人性化设计,输入更加形象化,你可能看不懂,没关系漫漫来。各种类都使用模版设计,可以对各种数据类型操作(整形,字符,浮点)
///////////////////////////
// //
// 堆栈数据结构 stack.h //
// //
//////////////////////////
#include
template
template
class StackNode
{
friend class Stack
private:
Type data;
StackNode
StackNode(Type D=0,StackNode
};
template
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;
StackNode
};
template
void Stack
{
top=new StackNode
NumItem++;
}
template
Type Stack
{
StackNode
Type temp;
temp=top->data;
p=top;
top=top->link;
delete p;
NumItem--;
return temp;
}
template
Type Stack
{
return top->data;
}
template
bool Stack
{
return top==NULL;
}
template
void Stack
{
delete top;
}
template
int Stack
{
return NumItem;
}
///////////////////////////
// //
// 队列数据结构 Queue.h //
// //
//////////////////////////
#include
template
template
{
friend class Queue
private:
Type data;
QueueNode
QueueNode(Type d=0,QueueNode *l=NULL):data(d),link(l){}
};
template
{
public:
Queue():rear(NULL),front(NULL){}
~Queue();
void EnQueue(Type item);
Type DelQueue();
Type GetFront();
void MakeEmpty();
bool ISEmpty() { return front==NULL; }
private:
QueueNode
};
template
Queue
{
QueueNode
while(front!=NULL)
{
p=front;
front=front->link;
delete p;
}
}
template
void Queue
{
if(front==NULL)
front=rear=new QueueNode
else
rear=rear->link=new QueueNode
}
template
Type Queue
{
QueueNode
Type temp=p->data;;
front=front->link;
delete p;
return temp;
}
template
Type Queue
{
return front->data;
}
template
void Queue
{
QueueNode
while(front!=NULL)
{
p=front;
front=front->link;
delete p;
}
}
///////////////////////////
// //
// 链表数据结构 list.h //
// //
//////////////////////////
#include
template
class list;
template
class listnode
{
public:
friend class list
private:
type data;
listnode
};
template
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:
listnode
int length;
};
template
void initlist(type &tmp);
template
void list_exit(list
void initation();
template
void list_insertend(list
template
{
return length;
}
template
{
listnode
p1=first->next;
first->next=NULL;
while(p1!=NULL)
{
p2=p1;
p1=p1->next;
delete p2;
}
length=0;
}
template
{
listnode
p=new listnode
p->data=t;
p->next=NULL;
last->next=p;
last=p;
length++;
}
template
{
listnode
p=first;
int k=1;
while(p!=NULL&&k
{
p=p->next;
k++;
}
if(p==NULL&&k!=i)
return false;
else
{
listnode
tp=new listnode
tp->data=t;
tp->next=p->next;
p->next=tp;
length++;
return true;
}
}
template
{
int k=1;
listnode
p=first;
while(p->next!=NULL&&k!=i)
{
p=p->next;
k++;
}
t=p->next;
cout<<"你已经将数据项 "< p->next=p
- 上一篇:数据结构辅导---栈和队列(2)
- 下一篇:数据结构题集--数组(二维数组)