C语言精典算法

本文介绍了C语言实现的一些经典算法,包括链表操作、队列创建与操作、图的广度优先搜索(BFS)、N皇后问题的回溯法、高精度加减乘法、约瑟夫问题以及稀疏矩阵的加减乘法。通过这些实例,读者可以深入理解C语言在算法实现中的应用。
摘要由CSDN通过智能技术生成
 

迷宫:

#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", 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值