資源簡介
用VC 6.0運行,完美編譯運行,反正我們老師檢查是完美的過
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
/////////////////////////////////////////////////
#define?NULL?0
#define?LStack?linkedStack
/////////////////////////////////////////////////
//?鏈式棧類的前視定義
template?
class?linkedStack;
/////////////////////////////////////////////////
//?定義鏈式棧結點類
template?
class?StackNode
{
friend?class?linkedStack;
private:
T?data;
????????StackNode?*next;
StackNode(T?item?=?0?StackNode?*p?=?NULL)
{
data?=?item;
next?=?p;
}
};
/////////////////////////////////////////////////
//?定義鏈式棧類
template?
class?linkedStack
{
private:
StackNode?*top;
public:
linkedStack();
~linkedStack();
bool?IsEmpty(void)?const;
int?Length(void)?const;
void?Push(const?T?&item);
T?Pop(void);
T?GetTop(void);
void?Clear(void);
};
//?構造函數
template?
linkedStack::linkedStack()
{
top?=?NULL;
}
//?析構函數
template?
linkedStack::~linkedStack()
{
Clear();
}
//?判斷棧是否為空
template?
bool?linkedStack::IsEmpty(void)?const
{
return?(!?top);
}
//?獲取隊列的長度
template?
int?linkedStack::Length(void)?const
{
StackNode?*temp?=?new?StackNode();
temp?=?top;
int?length?=?0;
while?(temp)
{
temp?=?temp->next;
length++;
}
return?length;
}
//?壓入數據(入棧)
template?
void?linkedStack::Push(const?T?&item)
{
top?=?new?StackNode(item?top);
}
//?抽出數據(出棧)
template?
T?linkedStack::Pop(void)
{
if?(!?IsEmpty())
{
????StackNode?*temp?=?top;
top?=?top->next;
T?value?=?temp->data;
delete?temp;
return?value;
}
else
{
cout?<“棧已經為空!“?< getch();
exit(1);
}
}
//?獲取棧頭數據
template?
T?linkedStack::GetTop(void)
{
if?(!?IsEmpty())
{
return?top->data;
}
else
{
cout?<“棧已經為空!“?< getch();
exit(1);
}
}
//?設置棧為空棧
template?
void?linkedStack::Clear(void)
{
StackNode?*temp?=?new?StackNode();
while?(top)
{
temp?=?top;
top?=?top->next;
delete?temp;
}
}
/////////////////////////////////////////////////
//?定義鄰接表的邊表類
class?Edge
{
public:
int?number;
int?position;
char?weight;
Edge?*link;
Edge();
Edge(int?num?int?pos?char?ch);
};
Edge::Edge()
{
number?=?-1;
position?=?-1;
link?=?NULL;
}
Edge::Edge(int?num?int?pos?char?ch)
{
number?=?num;
position?=?pos;
weight?=?ch;
????link?=?NULL;
}
/////////////////////////////////////////////////
//?定義鄰接表的頂點類
class?Vertex
{
public:
int?number;
Vertex?*next;
Edge?*out;
Vertex();
Vertex(int?num);
};
Vertex::Vertex()
{
number?=?-1;
next?=?NULL;
out?=?NULL;
}
Vertex::Vertex(int?num)
{
number?=?num;
next?=?NULL;
out?=?NULL;
}
/////////////////////////////////////////////////
//?用鄰接表定義的圖類
class?AdjacentTable
{
評論
共有 條評論