迷宫:
#include <iostream.h>
enum boolean{FALSE, TRUE};
template <class T>
struct Node{
T val;
Node *next;
};
template <class T>
class List{
Node<T> *head;
int size;
public:
List()
{
head = NULL;
size = 0;
}
virtual boolean Insert(T val);
boolean Empty();
boolean Delete(T val);
boolean Contains(T val);
void Print();
~List();
};
template <class T>
List<T>::~List()
{
Node<T> *temp;
for (Node<T> *p = head; p;){
temp = p;
p = p->next;
delete temp;
}
}
template <class T>
boolean List<T>::Empty()
{
if (head)
return FALSE;
else
return TRUE;
}
template <class T>
boolean List<T>::Insert(T x)
{
Node<T> *node = new Node<T>;
if (node){
node->val = x;
node->next = head;
head = node;
size++;
return TRUE;
}
return FALSE;
}
template <class T>
boolean List<T>::Delete(T x)
{
Node<T> *temp;
if (Empty())
return FALSE;
if (head->val == x){
temp = head->next;
delete head;
size--;
head = temp;
return TRUE;
}
for (Node<T> *p = temp = head; p; temp = p, p = p->next)
if (p->val == x){
temp->next = p->next;
delete p;
size--;
return TRUE;
}
return FALSE;
}
template <class T>
boolean List<T>::Contains(T x)
{
for (Node<T> *p = head; p; p = p->next)
if (p->val == x)
return TRUE;
return FALSE;
}
template <class T>
void List<T>::Print()
{
for (Node<T> *p = head; p ; p = p->next)
cout << p->val << " ";
cout << " n";
}
void main()
{
List<int> intlist;
int i;
for (i = 0; i < 10; i++)
intlist.Insert(i);
intlist.Print();
intlist.Delete(1);
intlist.Delete(2);
intlist.Print();
List<float> floatlist;
floatlist.Insert(3.1);
floatlist.Insert(1.5);
floatlist.Print();
List<char *> charlist;
charlist.Insert("program.");
charlist.Insert("my ds ");
charlist.Insert("is ");
charlist.Insert("This ");
charlist.Print();
}
#include <stdio.h>
#include <malloc.h>
enum boolean {false, true};
const int MaxSize = 20;
struct celltype{
int id;
celltype *next;
};
struct queue{
celltype *front, *rear;
};
struct EdgeNode{
int adjvex;
EdgeNode *next;
};
struct vexNode{
int info;
EdgeNode *next;
};
vexNode Graph[MaxSize];
int N;
void MakeNull(queue *head)
{
head->front->next = NULL;
head->rear = head->front;
}
void CreateQueue(queue *head)
{
head->front = (celltype*) malloc (sizeof(celltype));
head->rear = (celltype*) malloc (sizeof(celltype));
MakeNull(head);
}
boolean IsEmpty(queue *head)
{
if (head->front == head->rear)
return true;
else
return false;
}
void Dequeue(queue *head)
{
celltype *t;
if (!IsEmpty(head)){
t = head->front;
head->front = head->front->next;
free(t);
} else
printf("%s", "DeleteError!");
}
void Enqueue(queue *head, int id)
{
head->rear->next = (celltype*) malloc (sizeof(celltype));
head->rear = head->rear->next;
head->rear->id = id;
}
void CreateGraph()
{
int i,k;
EdgeNode *p;
printf("%s",