日 历

2008 8.28 Thu
     12
3456789
10111213141516
17181920212223
24252627282930
31      
«» 2008 - 8 «»

文章搜索

日志文章列表

2007年12月11日 22:26:40

冒泡排序

#include<stdio.h>
#define max 40
typedef struct
{int key;
char name;
}datatype;
datatype x[max];
void getsort(datatype x[],int n)
{int i;
printf("recorder;");
for(i=0;i<n;i++)
scanf("%d",&x.key);
}
void hubblesort(datatype x[],int n)
{int i,j,k,flag;
datatype swap;
flag=1;
for(i=1;i<n&&flag==1;i++)
{flag=0;
for(j=0;j<n-i;j++)
  if(x[j].key>x[j+1].key)
  {flag=1;
  swap=x[j];
  x[j]=x[j+1];
  x[j+1]=swap;
&n..

阅读全文>>

Tags: 冒泡排序  

类别: 数据结构 |  评论(6) |  浏览(1847) |  收藏
2007年12月11日 14:41:26

折半查找

#include<stdio.h>
#define max 20
int binary(int x,int list[],int n)
{int low,high,mid;
low=0;
high=n-1;
while(low<=high)
{mid=(low+high)/2;
if(x<list[mid])
  high=mid-1;
else
  if(x>list[mid])
  low=mid=1;
  else
  return(mid);
}
return(-1);
}
int getdata(int list[])
{int num,i;
printf("total=");
scanf("%d",&num);
for(i=0;i<num;i++)
  {printf("data[%d];",i);
  scanf("%d",&li..

阅读全文>>

Tags: 折半查找  

类别: 数据结构 |  评论(2) |  浏览(1619) |  收藏
2007年11月27日 20:22:36

串的一系列运算

串可以用如下的结构描述:
typedef struct
{
char s[MAXSIZE];
int len;
}seqstring;
下面讨论一下定长顺序串的串连接,求子串,串插入和串删除等算法。
串的定义采用seqstring类型的定义。
一 串连接——把两个串s1和s2首尾连接成一个新的串s1。其算法如下:
int strcat(seqstring *s1,seqstring *s2)
{
int i;
if(s1->len+s2->len<maxsize)
{
  for(i=0;i<s2->len;i++)
  s1->s[s1->len+i]=s2->;
  s1->len=s1->len+s2->len;
  return 1;
}
..

阅读全文>>

Tags: 字符串  

类别: 数据结构 |  评论(0) |  浏览(1613) |  收藏
2007年11月24日 16:24:42

用非递归算法遍历二叉树

#include<stdio.h>
#define NULL 0
#define max 40
int counter=0;
typedef struct btreenode
{int data;
struct btreenode *lchild;
struct btreenode *rchild;
}bnode;
int stack[max],top=0;
bnode *creat(int x,bnode *lbt,bnode *rbt)
{bnode *p;
p=(bnode*)malloc(sizeof(bnode));
p->data=x;
p->lchild=lbt;
p->rchild=rbt;
return(p);
}
bnode *ins_lchild(bnode *p,int x)
{bnode *q;
if(p==NULL)
  printf("Illegal insert.");
else
{q=(bnode*)malloc(sizeof(bnode));
  q-..

阅读全文>>

类别: 数据结构 |  评论(0) |  浏览(3152) |  收藏
2007年11月24日 13:33:18

用链式存储结构建立二叉排序树

程序如下:
#include<stdio.h>
#define NULL 0
int counter=0;
typedef struct btreenode
{int data;
struct btreenode *lchild;
struct btreenode *rchild;
}bnode;
bnode *creat(int x,bnode *lbt,bnode *rbt)
{bnode *p;
p=(bnode*)malloc(sizeof(bnode));
p->data=x;
p->lchild=lbt;
p->rchild=rbt;
return(p);
}
bnode *ins_lchild(bnode *p,int x)
{bnode *q;
if(p==NULL)
printf("Illegal insert.");
else
{q=(bnode*)malloc(sizeof(bnode));
  q->data=x;
  q->lchild..

阅读全文>>

类别: 数据结构 |  评论(1) |  浏览(2147) |  收藏
2007年11月21日 21:56:33

遍历算法的应用

以二叉链表作为存储结构,求其最大宽度即二叉树的宽度。算法如下:
void levelnum(btnode *bt,int a[],h)
{ if(bt=NULL)
    h=0;
  else
  {a[h]+=1;
    levelnum(bt->lchild,a,h+1);
    levelnum(bt->rchild,a,h+1);
  }
}

int btnodewidth(btnode *bt)
{int a[maxsize],h=1,wid,i;
for(i=0;i<maxsize;i++)
a=0;
levelnum(bt,a,h);
wid=0;
for(i=1;i<maxsize;i++)
{
  if(a>0
    printf("%d:%d",i,a);
&nb..

阅读全文>>

类别: 数据结构 |  评论(0) |  浏览(1766) |  收藏
2007年11月17日 20:49:19

基本二叉树知识讲解

有关二叉树的学习
性质1:二叉树上叶子结点数等于度为2的结点数加1。
性质2:二叉树的第i层上至多有2的i次方减1个结点(i>=1)。
性质3:深度为h的二叉树至多有2的h次方减1个结点。
满二叉树:在一棵二叉树中,当第i层的结点树为2的i次方减1个时,称此层的结点数是满的。当一棵二叉树中的每一层都满时,称此树为满二叉树。特性:除叶子结点以外的其他的结点的度皆为2,且叶子结点在同一层上。深度为h的满二叉树中的结点数为2的h次方减1。
性质4:设含有n个结点的完全二叉树的深度为k,则k=(int)(log2n)+1,即深度k等于log2n的整数部分..

阅读全文>>

Tags: 二叉树  

类别: 数据结构 |  评论(0) |  浏览(1281) |  收藏
2007年11月13日 22:18:28

二叉树遍历

#include <stdio.h>
#include <malloc.h>
typedef struct node
{
int data;
struct node *lchild,*rchild;
}*treetp,tree;
treetp create (treetp t,int c);
void print1(treetp);
void print2(treetp);
void print3(treetp);
int number=0;
void main()
{
treetp t=0,r;
r=create (t,0);
printf("\nqian xu pai lie: ");
print1 (r);
printf("\nzhong xu pai lie: ");
print2 (r);
printf("\nhou xu pai lie: ");
print3 (r);
getch();
}
treetp create(treetp t,int..

阅读全文>>

Tags: 二叉树遍历  

类别: 数据结构 |  评论(1) |  浏览(1379) |  收藏
2007年11月12日 22:55:19

数据结构与算法基本程序合集5

void describe_tree()   //交互式显示哈夫曼树
{
  bt t;
  stack s,top,temp;
  int choice;
  s=(stack)malloc(sizeof(snode));
  s->amount=0;
  s->ch='\0';
  s->next=NULL;
  s->son=NULL;
  top=s;
 
  t=root;//t指向哈夫曼树的根结点
  temp=(stack)malloc(sizeof(snode));   //分配新栈结点
  temp->amount=t->amount;      
  temp->ch=t-&..

阅读全文>>

Tags: 数据结构  

类别: 数据结构 |  评论(1) |  浏览(1617) |  收藏
2007年11月12日 22:54:35

数据结构与算法基本程序合集4

//建立二*排序树完毕
  //对其进行中序遍历
 
  printf("\n哈夫曼树构造完成,是否查看哈夫曼树,输入1查看,其它输入跳过");
  scanf("%d",&choice);
  getchar();
  if(choice==1)
    describe_tree();
 
  inorder(root);
  printf("\n");
}
//哈夫曼编码
#include<stdio.h>
#include<malloc.h>
#include<math.h>
#define NULL 0
typedef struct huff_code_node   //存储..

阅读全文>>

Tags: 数据结构  

类别: 数据结构 |  评论(0) |  浏览(1604) |  收藏
2007年11月12日 22:52:50

数据结构与算法基本程序合集3

三、队及其操作
//All copyright are preserved bycobby
//链队列的建立
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
typedef struct node         //队列结点的基本数据结构,即队列中每个结点的类型
{
  char c;
  struct node *next;
}qnode,*basic_node;
typedef struct         //队列实际上由头、尾两个结点指针构成,即头指针和尾指针唯一确定时,队列也被唯一确定
{
  qnode *head;
 ..

阅读全文>>

Tags: 数据结构  

类别: 数据结构 |  评论(0) |  浏览(2696) |  收藏
2007年11月12日 22:51:50

数据结构与算法基本程序合集2

//约瑟夫环问题
#include<stdio.h>
#include<malloc.h>
typedef struct lnode
{
  int num;
  struct lnode *next;
}node,*L;
main()
{
  int amount,start,circle,n,c;
  L p,l,q;
  printf("一共有几个人围成一圈?\n");
  scanf("%d",&amount);
  getchar();
  printf("从第几个开始计数?\n");
  scanf("%d",&start);
  getchar();
  printf("每几人一次循环?\n"..

阅读全文>>

Tags: 数据结构  

类别: 数据结构 |  评论(0) |  浏览(1510) |  收藏
2007年11月12日 22:50:03

数据结构与算法基本程序合集1

数据结构与算法基本程序合集
数据结构与算法基本程序目录
一、   线性表及其操作
1、   尾插法建立一个单链表,并按顺序输出
2、   单链表的元素查找,按内容查找
3、   元素插入操作
4、   按内容元素删除操作
5、   按位置删除元素
6、   建立双向链表
7、   单链表就地逆置
8、   约瑟夫环问题
二、   栈及其操作
1、   建立堆栈
2、   进栈与出栈
3、   栈的应用,括号匹配
三、   队及其操作
1、   链队列的建立
2、..

阅读全文>>

Tags: 数据结构  

类别: 数据结构 |  评论(1) |  浏览(1456) |  收藏
2007年11月11日 18:53:39

用队列输出杨辉三角

#define MAXSIZE 10
#include<stdio.h>
typedef int datatype;
typedef struct
{
int data[MAXSIZE];
int front;
int rear;
}SeqQueue;
SeqQueue *InitQueue()
{
SeqQueue *q;
q=(SeqQueue*)malloc(sizeof(SeqQueue));
q->front=q->rear=0;
return q;
}
void EnQueue (SeqQueue *q,datatype x)
{
if((q->rear+1)%MAXSIZE==q->front)
{printf("\nSeqQueue is full!");exit(1);}
q->data[q->rear]=x;
q->rear=(q->rear+1)%MAXSIZE;
}
datatype DeQueue(SeqQueue *q)
{
datatype x;
..

阅读全文>>

Tags: 杨辉三角  

类别: 数据结构 |  评论(1) |  浏览(1216) |  收藏